1
1
'use strict'
2
2
3
- const fastSafeStringify = require ( 'fast-safe-stringify' )
3
+ var fastSafeStringify = require ( 'fast-safe-stringify' )
4
4
5
5
var uglify = null
6
- let isLong
6
+ var isLong
7
7
try {
8
8
isLong = require ( 'long' ) . isLong
9
9
} catch ( e ) {
10
10
isLong = null
11
11
}
12
12
13
+ var addComma = `
14
+ if (addComma) {
15
+ json += ','
16
+ }
17
+ addComma = true
18
+ `
19
+
13
20
function build ( schema , options ) {
14
21
options = options || { }
15
22
/* eslint no-new-func: "off" */
@@ -19,7 +26,7 @@ function build (schema, options) {
19
26
// used to support patternProperties and additionalProperties
20
27
// they need to check if a field belongs to the properties in the schema
21
28
code += `
22
- const properties = ${ JSON . stringify ( schema . properties ) } || {}
29
+ var properties = ${ JSON . stringify ( schema . properties ) } || {}
23
30
`
24
31
code += `
25
32
${ $asString . toString ( ) }
@@ -32,12 +39,12 @@ function build (schema, options) {
32
39
// only handle longs if the module is used
33
40
if ( isLong ) {
34
41
code += `
35
- const isLong = ${ isLong . toString ( ) }
42
+ var isLong = ${ isLong . toString ( ) }
36
43
${ $asInteger . toString ( ) }
37
44
`
38
45
} else {
39
46
code += `
40
- const $asInteger = $asNumber
47
+ var $asInteger = $asNumber
41
48
`
42
49
}
43
50
@@ -168,7 +175,7 @@ function $asStringSmall (str) {
168
175
169
176
function addPatternProperties ( schema , externalSchema , fullSchema ) {
170
177
var pp = schema . patternProperties
171
- let code = `
178
+ var code = `
172
179
var keys = Object.keys(obj)
173
180
for (var i = 0; i < keys.length; i++) {
174
181
if (properties[keys[i]]) continue
@@ -184,32 +191,39 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
184
191
if ( type === 'object' ) {
185
192
code += buildObject ( pp [ regex ] , '' , 'buildObjectPP' + index , externalSchema , fullSchema )
186
193
code += `
187
- json += $asString(keys[i]) + ':' + buildObjectPP${ index } (obj[keys[i]]) + ','
194
+ ${ addComma }
195
+ json += $asString(keys[i]) + ':' + buildObjectPP${ index } (obj[keys[i]])
188
196
`
189
197
} else if ( type === 'array' ) {
190
198
code += buildArray ( pp [ regex ] , '' , 'buildArrayPP' + index , externalSchema , fullSchema )
191
199
code += `
192
- json += $asString(keys[i]) + ':' + buildArrayPP${ index } (obj[keys[i]]) + ','
200
+ ${ addComma }
201
+ json += $asString(keys[i]) + ':' + buildArrayPP${ index } (obj[keys[i]])
193
202
`
194
203
} else if ( type === 'null' ) {
195
204
code += `
196
- json += $asString(keys[i]) +':null,'
205
+ ${ addComma }
206
+ json += $asString(keys[i]) +':null'
197
207
`
198
208
} else if ( type === 'string' ) {
199
209
code += `
200
- json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
210
+ ${ addComma }
211
+ json += $asString(keys[i]) + ':' + $asString(obj[keys[i]])
201
212
`
202
213
} else if ( type === 'integer' ) {
203
214
code += `
204
- json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
215
+ ${ addComma }
216
+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
205
217
`
206
218
} else if ( type === 'number' ) {
207
219
code += `
208
- json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
220
+ ${ addComma }
221
+ json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]])
209
222
`
210
223
} else if ( type === 'boolean' ) {
211
224
code += `
212
- json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) + ','
225
+ ${ addComma }
226
+ json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]])
213
227
`
214
228
} else {
215
229
code += `
@@ -234,47 +248,56 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
234
248
235
249
function additionalProperty ( schema , externalSchema , fullSchema ) {
236
250
var ap = schema . additionalProperties
237
- let code = ''
251
+ var code = ''
238
252
if ( ap === true ) {
239
253
return `
240
- if (obj[keys[i]] !== undefined)
241
- json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
254
+ if (obj[keys[i]] !== undefined) {
255
+ ${ addComma }
256
+ json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]])
257
+ }
242
258
`
243
259
}
244
260
if ( ap [ '$ref' ] ) {
245
261
ap = refFinder ( ap [ '$ref' ] , fullSchema , externalSchema )
246
262
}
247
263
248
- let type = ap . type
264
+ var type = ap . type
249
265
if ( type === 'object' ) {
250
266
code += buildObject ( ap , '' , 'buildObjectAP' , externalSchema )
251
267
code += `
252
- json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ','
268
+ ${ addComma }
269
+ json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]])
253
270
`
254
271
} else if ( type === 'array' ) {
255
272
code += buildArray ( ap , '' , 'buildArrayAP' , externalSchema , fullSchema )
256
273
code += `
257
- json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ','
274
+ ${ addComma }
275
+ json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]])
258
276
`
259
277
} else if ( type === 'null' ) {
260
278
code += `
261
- json += $asString(keys[i]) +':null,'
279
+ ${ addComma }
280
+ json += $asString(keys[i]) +':null'
262
281
`
263
282
} else if ( type === 'string' ) {
264
283
code += `
265
- json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
284
+ ${ addComma }
285
+ json += $asString(keys[i]) + ':' + $asString(obj[keys[i]])
266
286
`
267
287
} else if ( type === 'integer' ) {
268
288
code += `
269
- json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
289
+ ${ addComma }
290
+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
270
291
`
271
292
} else if ( type === 'number' ) {
272
293
code += `
273
- json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
294
+ ${ addComma }
295
+ json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]])
274
296
`
275
297
} else if ( type === 'boolean' ) {
276
298
code += `
277
- json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) + ','
299
+ ${ addComma }
300
+ json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]])
278
301
`
279
302
} else {
280
303
code += `
@@ -301,9 +324,9 @@ function refFinder (ref, schema, externalSchema) {
301
324
if ( ref [ 0 ] ) {
302
325
schema = externalSchema [ ref [ 0 ] ]
303
326
}
304
- const walk = ref [ 1 ] . split ( '/' )
305
- let code = 'return schema'
306
- for ( let i = 1 ; i < walk . length ; i ++ ) {
327
+ var walk = ref [ 1 ] . split ( '/' )
328
+ var code = 'return schema'
329
+ for ( var i = 1 ; i < walk . length ; i ++ ) {
307
330
code += `['${ walk [ i ] } ']`
308
331
}
309
332
return ( new Function ( 'schema' , code ) ) ( schema )
@@ -313,6 +336,7 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
313
336
code += `
314
337
function ${ name } (obj) {
315
338
var json = '{'
339
+ var addComma = false
316
340
`
317
341
318
342
if ( schema . patternProperties ) {
@@ -328,24 +352,18 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
328
352
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
329
353
code += `
330
354
if (obj['${ key } '] !== undefined) {
355
+ ${ addComma }
331
356
json += '${ $asString ( key ) } :'
332
357
`
333
358
334
359
if ( schema . properties [ key ] [ '$ref' ] ) {
335
360
schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , fullSchema , externalSchema )
336
361
}
337
362
338
- const result = nested ( laterCode , name , key , schema . properties [ key ] , externalSchema , fullSchema )
363
+ var result = nested ( laterCode , name , key , schema . properties [ key ] , externalSchema , fullSchema )
339
364
340
365
code += result . code
341
366
laterCode = result . laterCode
342
- /* eslint-disable no-useless-escape */
343
- if ( i < a . length - 1 ) {
344
- code += `
345
- json += \',\'
346
- `
347
- }
348
- /* eslint-enable no-useless-escape */
349
367
350
368
if ( schema . required && schema . required . indexOf ( key ) !== - 1 ) {
351
369
code += `
@@ -361,7 +379,6 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
361
379
362
380
// Removes the comma if is the last element of the string (in case there are not properties)
363
381
code += `
364
- if (json[json.length - 1] === ',') json = json.substring(0, json.length - 1)
365
382
json += '}'
366
383
return json
367
384
}
@@ -383,16 +400,16 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
383
400
schema . items = refFinder ( schema . items [ '$ref' ] , fullSchema , externalSchema )
384
401
}
385
402
386
- const result = nested ( laterCode , name , '[i]' , schema . items , externalSchema , fullSchema )
403
+ var result = nested ( laterCode , name , '[i]' , schema . items , externalSchema , fullSchema )
387
404
388
405
code += `
389
- const l = obj.length
390
- const w = l - 1
406
+ var l = obj.length
407
+ var w = l - 1
391
408
for (var i = 0; i < l; i++) {
392
- ${ result . code }
393
- if (i < w) {
409
+ if (i > 0) {
394
410
json += ','
395
411
}
412
+ ${ result . code }
396
413
}
397
414
`
398
415
@@ -412,8 +429,8 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
412
429
function nested ( laterCode , name , key , schema , externalSchema , fullSchema ) {
413
430
var code = ''
414
431
var funcName
415
- const type = schema . type
416
- const accessor = key . indexOf ( '[' ) === 0 ? key : `['${ key } ']`
432
+ var type = schema . type
433
+ var accessor = key . indexOf ( '[' ) === 0 ? key : `['${ key } ']`
417
434
switch ( type ) {
418
435
case 'null' :
419
436
code += `
@@ -469,7 +486,7 @@ function uglifyCode (code) {
469
486
loadUglify ( )
470
487
}
471
488
472
- const uglified = uglify . minify ( code , { parse : { bare_returns : true } } )
489
+ var uglified = uglify . minify ( code , { parse : { bare_returns : true } } )
473
490
474
491
if ( uglified . error ) {
475
492
throw uglified . error
@@ -481,7 +498,7 @@ function uglifyCode (code) {
481
498
function loadUglify ( ) {
482
499
try {
483
500
uglify = require ( 'uglify-es' )
484
- const uglifyVersion = require ( 'uglify-es/package.json' ) . version
501
+ var uglifyVersion = require ( 'uglify-es/package.json' ) . version
485
502
486
503
if ( uglifyVersion [ 0 ] !== '3' ) {
487
504
throw new Error ( 'Only version 3 of uglify-es is supported' )
0 commit comments