Skip to content

Commit 331e83e

Browse files
committed
feat: CodeBuilder push and unshift can format strings
- add an example to CodeBuilder - convert objc/nsurlsession to not use util.format anymore
1 parent c5f4706 commit 331e83e

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/helpers/code-builder.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
var util = require('util')
4+
35
/**
46
* Helper object to format and aggragate lines of code.
57
* Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.
@@ -16,14 +18,29 @@ var CodeBuilder = function (indentation, join) {
1618
}
1719

1820
/**
19-
* Add given indentation level to given string
20-
* @param {number} [indentationLevel=0] Desired level of indentation for this line
21-
* @param {string} line Line of code
21+
* Add given indentation level to given string and format the string (variadic)
22+
* @param {number} [indentationLevel=0] - Desired level of indentation for this line
23+
* @param {string} line - Line of code. Can contain formatting placeholders
24+
* @param {...anyobject} - Parameter to bind to `line`'s formatting placeholders
2225
* @return {string}
26+
*
27+
* @example
28+
* var builder = CodeBuilder('\t')
29+
*
30+
* builder.buildLine('console.log("hello world")')
31+
* // returns: 'console.log("hello world")'
32+
*
33+
* builder.buildLine(2, 'console.log("hello world")')
34+
* // returns: 'console.log("\t\thello world")'
35+
*
36+
* builder.buildLine(2, 'console.log("%s %s")', 'hello', 'world')
37+
* // returns: 'console.log("\t\thello world")'
2338
*/
2439
CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
2540
var lineIndentation = ''
41+
var slice = 2
2642
if (Object.prototype.toString.call(indentationLevel) === '[object String]') {
43+
slice = 1
2744
line = indentationLevel
2845
indentationLevel = 0
2946
} else if (indentationLevel === null) {
@@ -35,29 +52,32 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
3552
indentationLevel--
3653
}
3754

38-
return lineIndentation + line
55+
var format = Array.prototype.slice.call(arguments, slice, arguments.length)
56+
format.unshift(lineIndentation + line)
57+
58+
return util.format.apply(this, format)
3959
}
4060

4161
/**
42-
* Add a line at the top of current lines with given indentation level
62+
* Invoke buildLine() and add the line at the top of current lines
4363
* @param {number} [indentationLevel=0] Desired level of indentation for this line
4464
* @param {string} line Line of code
4565
* @return {this}
4666
*/
47-
CodeBuilder.prototype.unshift = function (indentationLevel, str) {
48-
this.code.unshift(this.buildLine(indentationLevel, str))
67+
CodeBuilder.prototype.unshift = function () {
68+
this.code.unshift(this.buildLine.apply(this, arguments))
4969

5070
return this
5171
}
5272

5373
/**
54-
* Add a line at the end of current lines with given indentation level
74+
* Invoke buildLine() and add the line at the bottom of current lines
5575
* @param {number} [indentationLevel=0] Desired level of indentation for this line
5676
* @param {string} line Line of code
5777
* @return {this}
5878
*/
59-
CodeBuilder.prototype.push = function (indentationLevel, str) {
60-
this.code.push(this.buildLine(indentationLevel, str))
79+
CodeBuilder.prototype.push = function () {
80+
this.code.push(this.buildLine.apply(this, arguments))
6181

6282
return this
6383
}

src/targets/objc/nsurlsession.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ module.exports = function (source, options) {
4646
// we make it easier for the user to edit it according to his or her needs after pasting.
4747
// The user can just add/remove lines adding/removing body parameters.
4848
code.blank()
49-
.push(util.format('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
50-
source.postData.params[0].name, source.postData.params[0].value))
49+
.push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
50+
source.postData.params[0].name, source.postData.params[0].value)
5151
for (var i = 1, len = source.postData.params.length; i < len; i++) {
52-
code.push(util.format('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
53-
source.postData.params[i].name, source.postData.params[i].value))
52+
code.push('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
53+
source.postData.params[i].name, source.postData.params[i].value)
5454
}
5555
break
5656

@@ -67,7 +67,7 @@ module.exports = function (source, options) {
6767
// we make it easier for the user to edit it according to his or her needs after pasting.
6868
// The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
6969
code.push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
70-
.push(util.format('NSString *boundary = @"%s";', source.postData.boundary))
70+
.push('NSString *boundary = @"%s";', source.postData.boundary)
7171
.blank()
7272
.push('NSError *error;')
7373
.push('NSMutableString *body = [NSMutableString string];')

0 commit comments

Comments
 (0)