2
2
3
3
const fastSafeStringify = require ( 'fast-safe-stringify' )
4
4
5
+ let isLong
6
+ try {
7
+ isLong = require ( 'long' ) . isLong
8
+ } catch ( e ) {
9
+ isLong = null
10
+ }
11
+
5
12
function build ( schema , options ) {
6
13
options = options || { }
7
14
/* eslint no-new-func: "off" */
@@ -20,6 +27,19 @@ function build (schema, options) {
20
27
${ $asNull . toString ( ) }
21
28
${ $asBoolean . toString ( ) }
22
29
`
30
+
31
+ // only handle longs if the module is used
32
+ if ( isLong ) {
33
+ code += `
34
+ const isLong = ${ isLong . toString ( ) }
35
+ ${ $asInteger . toString ( ) }
36
+ `
37
+ } else {
38
+ code += `
39
+ const $asInteger = $asNumber
40
+ `
41
+ }
42
+
23
43
var main
24
44
25
45
switch ( schema . type ) {
@@ -31,6 +51,8 @@ function build (schema, options) {
31
51
main = $asString . name
32
52
break
33
53
case 'integer' :
54
+ main = $asInteger . name
55
+ break
34
56
case 'number' :
35
57
main = $asNumber . name
36
58
break
@@ -62,6 +84,14 @@ function $asNull () {
62
84
return 'null'
63
85
}
64
86
87
+ function $asInteger ( i ) {
88
+ if ( isLong && isLong ( i ) ) {
89
+ return i . toString ( )
90
+ } else {
91
+ return $asNumber ( i )
92
+ }
93
+ }
94
+
65
95
function $asNumber ( i ) {
66
96
var num = Number ( i )
67
97
if ( isNaN ( num ) ) {
@@ -149,7 +179,11 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
149
179
code += `
150
180
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
151
181
`
152
- } else if ( type === 'number' || type === 'integer' ) {
182
+ } else if ( type === 'integer' ) {
183
+ code += `
184
+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
185
+ `
186
+ } else if ( type === 'number' ) {
153
187
code += `
154
188
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
155
189
`
@@ -209,7 +243,11 @@ function additionalProperty (schema, externalSchema, fullSchema) {
209
243
code += `
210
244
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
211
245
`
212
- } else if ( type === 'number' || type === 'integer' ) {
246
+ } else if ( type === 'integer' ) {
247
+ code += `
248
+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
249
+ `
250
+ } else if ( type === 'number' ) {
213
251
code += `
214
252
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
215
253
`
@@ -365,8 +403,12 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
365
403
json += $asString(obj${ key } )
366
404
`
367
405
break
368
- case 'number' :
369
406
case 'integer' :
407
+ code += `
408
+ json += $asInteger(obj${ key } )
409
+ `
410
+ break
411
+ case 'number' :
370
412
code += `
371
413
json += $asNumber(obj${ key } )
372
414
`
0 commit comments