Skip to content

Commit 71471b3

Browse files
authored
Merge pull request #30 from rochdev/long
Support for long integers
2 parents 36035e8 + 14c00d9 commit 71471b3

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fast-json-stringify obj x 4,976,369 ops/sec ±1.00% (89 runs sampled)
2727
- <a href="#patternProperties">`Pattern Properties`</a>
2828
- <a href="#additionalProperties">`Additional Properties`</a>
2929
- <a href="#ref">`Reuse - $ref`</a>
30+
- <a href="#long">`Long integers`</a>
3031
- <a href="#acknowledgements">`Acknowledgements`</a>
3132
- <a href="#license">`License`</a>
3233

@@ -294,6 +295,30 @@ const externalSchema = {
294295
const stringify = fastJson(schema, { schema: externalSchema })
295296
```
296297

298+
<a name="long"></a>
299+
#### Long integers
300+
Long integers (64-bit) are supported using the [long](https://github.com/dcodeIO/long.js) module.
301+
Example:
302+
```javascript
303+
const Long = require('long')
304+
305+
const stringify = fastJson({
306+
title: 'Example Schema',
307+
type: 'object',
308+
properties: {
309+
id: {
310+
type: 'integer'
311+
}
312+
}
313+
})
314+
315+
const obj = {
316+
id: Long.fromString('18446744073709551615', true)
317+
}
318+
319+
console.log(stringify(obj)) // '{"id":18446744073709551615}'
320+
```
321+
297322
<a name="acknowledgements"></a>
298323
## Acknowledgements
299324

index.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
const fastSafeStringify = require('fast-safe-stringify')
44

5+
let isLong
6+
try {
7+
isLong = require('long').isLong
8+
} catch (e) {
9+
isLong = null
10+
}
11+
512
function build (schema, options) {
613
options = options || {}
714
/* eslint no-new-func: "off" */
@@ -20,6 +27,19 @@ function build (schema, options) {
2027
${$asNull.toString()}
2128
${$asBoolean.toString()}
2229
`
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+
2343
var main
2444

2545
switch (schema.type) {
@@ -31,6 +51,8 @@ function build (schema, options) {
3151
main = $asString.name
3252
break
3353
case 'integer':
54+
main = $asInteger.name
55+
break
3456
case 'number':
3557
main = $asNumber.name
3658
break
@@ -62,6 +84,14 @@ function $asNull () {
6284
return 'null'
6385
}
6486

87+
function $asInteger (i) {
88+
if (isLong && isLong(i)) {
89+
return i.toString()
90+
} else {
91+
return $asNumber(i)
92+
}
93+
}
94+
6595
function $asNumber (i) {
6696
var num = Number(i)
6797
if (isNaN(num)) {
@@ -149,7 +179,11 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
149179
code += `
150180
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
151181
`
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') {
153187
code += `
154188
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
155189
`
@@ -209,7 +243,11 @@ function additionalProperty (schema, externalSchema, fullSchema) {
209243
code += `
210244
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
211245
`
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') {
213251
code += `
214252
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
215253
`
@@ -365,8 +403,12 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
365403
json += $asString(obj${key})
366404
`
367405
break
368-
case 'number':
369406
case 'integer':
407+
code += `
408+
json += $asInteger(obj${key})
409+
`
410+
break
411+
case 'number':
370412
code += `
371413
json += $asNumber(obj${key})
372414
`

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"devDependencies": {
2727
"benchmark": "^2.1.1",
2828
"is-my-json-valid": "^2.16.0",
29+
"long": "^3.2.0",
2930
"pre-commit": "^1.1.3",
3031
"standard": "^10.0.0",
3132
"tap": "^10.3.0"

test/long.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const validator = require('is-my-json-valid')
5+
const Long = require('long')
6+
const build = require('..')
7+
8+
test(`render a long as JSON`, (t) => {
9+
t.plan(2)
10+
11+
const schema = {
12+
title: 'long',
13+
type: 'integer'
14+
}
15+
16+
const validate = validator(schema)
17+
const stringify = build(schema)
18+
const output = stringify(Long.fromString('18446744073709551615', true))
19+
20+
t.equal(output, '18446744073709551615')
21+
t.ok(validate(JSON.parse(output)), 'valid schema')
22+
})
23+
24+
test(`render an object with long as JSON`, (t) => {
25+
t.plan(2)
26+
27+
const schema = {
28+
title: 'object with long',
29+
type: 'object',
30+
properties: {
31+
id: {
32+
type: 'integer'
33+
}
34+
}
35+
}
36+
37+
const validate = validator(schema)
38+
const stringify = build(schema)
39+
const output = stringify({
40+
id: Long.fromString('18446744073709551615', true)
41+
})
42+
43+
t.equal(output, '{"id":18446744073709551615}')
44+
t.ok(validate(JSON.parse(output)), 'valid schema')
45+
})
46+
47+
test(`render aan array with long as JSON`, (t) => {
48+
t.plan(2)
49+
50+
const schema = {
51+
title: 'array with long',
52+
type: 'array',
53+
items: {
54+
type: 'integer'
55+
}
56+
}
57+
58+
const validate = validator(schema)
59+
const stringify = build(schema)
60+
const output = stringify([Long.fromString('18446744073709551615', true)])
61+
62+
t.equal(output, '[18446744073709551615]')
63+
t.ok(validate(JSON.parse(output)), 'valid schema')
64+
})

0 commit comments

Comments
 (0)