Skip to content

Commit 9883fd4

Browse files
committed
chore: CodeBuilder for efficient line concatenation
1 parent 69efbe7 commit 9883fd4

18 files changed

Lines changed: 123 additions & 53 deletions

File tree

src/helpers/code-builder.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
var CodeBuilder = function (indentation, join) {
4+
this.code = []
5+
this.indentation = indentation
6+
this.lineJoin = join ? join : '\n'
7+
}
8+
9+
CodeBuilder.prototype.buildLine = function (indentationLevel, str) {
10+
var lineIndentation = ''
11+
if (Object.prototype.toString.call(indentationLevel) === '[object String]') {
12+
str = indentationLevel
13+
indentationLevel = 0
14+
} else if (indentationLevel === null) {
15+
return null
16+
}
17+
18+
while (indentationLevel) {
19+
lineIndentation += this.indentation
20+
indentationLevel--
21+
}
22+
23+
return lineIndentation + str
24+
}
25+
26+
CodeBuilder.prototype.unshift = function (indentationLevel, str) {
27+
this.code.unshift(this.buildLine(indentationLevel, str))
28+
29+
return this
30+
}
31+
32+
CodeBuilder.prototype.push = function (indentationLevel, str) {
33+
this.code.push(this.buildLine(indentationLevel, str))
34+
35+
return this
36+
}
37+
38+
CodeBuilder.prototype.blank = function () {
39+
this.code.push(null)
40+
41+
return this
42+
}
43+
44+
CodeBuilder.prototype.join = function () {
45+
return this.code.join(this.lineJoin)
46+
}
47+
48+
module.exports = CodeBuilder

src/targets/go/native.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
// Let's Go!
17-
var code = []
18+
var code = new CodeBuilder()
1819

1920
// Define Options
2021
var opts = util._extend({
@@ -108,7 +109,7 @@ module.exports = function (source, options) {
108109
// End main block
109110
code.push('\n}')
110111

111-
return code.join('\n')
112+
return code.join()
112113
}
113114

114115
module.exports.info = {

src/targets/java/unirest.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (options) {
1617
var self = this
1718
var opts = util._extend({
1819
indent: ' '
1920
}, options)
2021

21-
var code = []
22+
var code = new CodeBuilder(opts.indent)
2223

2324
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
2425

@@ -34,16 +35,17 @@ module.exports = function (options) {
3435
// construct headers
3536
if (headers.length) {
3637
headers.map(function (key) {
37-
code.push(opts.indent + util.format('.header("%s", "%s")', key, self.source.allHeaders[key]))
38+
code.push(1, util.format('.header("%s", "%s")', key, self.source.allHeaders[key]))
3839
})
3940
}
4041

4142
if (self.source.postData.text) {
42-
code.push(opts.indent + util.format('.body(%s)', JSON.stringify(self.source.postData.text)))
43+
code.push(1, util.format('.body(%s)', JSON.stringify(self.source.postData.text)))
4344
}
4445

45-
code.push(opts.indent + '.asString();')
46-
return code.join('\n')
46+
code.push(1, '.asString();')
47+
48+
return code.join()
4749
}
4850

4951
module.exports.info = {

src/targets/javascript/jquery.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
var opts = util._extend({
1718
indent: ' '
1819
}, options)
1920

20-
var code = []
21+
var code = new CodeBuilder(opts.indent)
2122

2223
var settings = {
2324
async: true,
@@ -68,7 +69,7 @@ module.exports = function (source, options) {
6869

6970
code.push('$.ajax(settings).done(function (response) {\n\tconsole.log(response);\n});'.replace(/\t/g, opts.indent))
7071

71-
return code.join('\n')
72+
return code.join()
7273
}
7374

7475
module.exports.info = {

src/targets/javascript/xhr.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
var opts = util._extend({
1718
indent: ' ',
1819
cors: true
1920
}, options)
2021

21-
var code = []
22+
var code = new CodeBuilder(opts.indent)
2223

2324
switch (source.postData.mimeType) {
2425
case 'application/json':
@@ -68,7 +69,7 @@ module.exports = function (source, options) {
6869

6970
code.push('xhr.send(data);')
7071

71-
return code.join('\n')
72+
return code.join()
7273
}
7374

7475
module.exports.info = {

src/targets/node/native.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
var opts = util._extend({
1718
indent: ' '
1819
}, options)
1920

20-
var code = []
21+
var code = new CodeBuilder(opts.indent)
2122

2223
var reqOpts = {
2324
method: source.method,
@@ -41,13 +42,13 @@ module.exports = function (source, options) {
4142

4243
code.push('var req = http.request(options, function (res) {')
4344

44-
code.push(opts.indent + 'var chunks = [];')
45+
code.push(1, 'var chunks = [];')
4546

4647
code.push(null)
4748

48-
code.push(opts.indent + 'res.on("data", function (chunk) {')
49-
code.push(opts.indent + opts.indent + 'chunks.push(chunk);')
50-
code.push(opts.indent + '});')
49+
code.push(1, 'res.on("data", function (chunk) {')
50+
code.push(2, 'chunks.push(chunk);')
51+
code.push(1, '});')
5152

5253
code.push(null)
5354

@@ -65,7 +66,7 @@ module.exports = function (source, options) {
6566

6667
code.push('req.end();')
6768

68-
return code.join('\n')
69+
return code.join()
6970
}
7071

7172
module.exports.info = {

src/targets/node/request.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212

1313
var util = require('util')
1414
var path = require('path')
15+
var CodeBuilder = require('../../helpers/code-builder')
1516

1617
module.exports = function (source, options) {
1718
var opts = util._extend({
1819
indent: ' '
1920
}, options)
2021

2122
var includeFS = false
22-
var code = ['var request = require("request");', null]
23+
var code = new CodeBuilder(opts.indent)
24+
25+
code.push('var request = require("request");')
26+
code.push(null)
2327

2428
var reqOpts = {
2529
method: source.method,
@@ -105,7 +109,7 @@ module.exports = function (source, options) {
105109
code.push('});')
106110
code.push(null)
107111

108-
return code.join('\n').replace('"JAR"', 'jar').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")')
112+
return code.join().replace('"JAR"', 'jar').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")')
109113
}
110114

111115
module.exports.info = {

src/targets/node/unirest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
var opts = util._extend({
1718
indent: ' '
1819
}, options)
1920

2021
var includeFS = false
21-
var code = ['var unirest = require("unirest");', null]
22+
var code = new CodeBuilder(opts.indent)
2223

24+
code.push('var unirest = require("unirest");')
25+
code.push(null)
2326
code.push(util.format('var req = unirest("%s", "%s");', source.method, source.url))
2427
code.push(null)
2528

src/targets/objc/native.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
var util = require('util')
1414
var objcHelpers = require('./helpers')
15+
var CodeBuilder = require('../../helpers/code-builder')
1516

1617
module.exports = function (source, options) {
1718
var opts = util._extend({
@@ -21,7 +22,7 @@ module.exports = function (source, options) {
2122
}, options)
2223

2324
var indent = opts.indent
24-
var code = []
25+
var code = new CodeBuilder(opts.indent)
2526
// Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
2627
var req = {
2728
hasHeaders: false,
@@ -125,7 +126,7 @@ module.exports = function (source, options) {
125126
code.push(' }];')
126127
code.push('[dataTask resume];')
127128

128-
return code.join('\n')
129+
return code.join()
129130
}
130131

131132
module.exports.info = {

src/targets/ocaml/cohttp.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
1415

1516
module.exports = function (source, options) {
1617
var opts = util._extend({
1718
indent: ' '
1819
}, options)
1920

2021
var methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options']
21-
var code = []
22+
var code = new CodeBuilder(opts.indent)
2223

2324
code.push('open Cohttp_lwt_unix')
2425
code.push('open Cohttp')
@@ -60,7 +61,7 @@ module.exports = function (source, options) {
6061
code.push('>>= fun (res, body_stream) ->')
6162
code.push(opts.indent + '(* Do stuff with the result *)')
6263

63-
return code.join('\n')
64+
return code.join()
6465
}
6566

6667
module.exports.info = {

0 commit comments

Comments
 (0)