Skip to content

Commit 2da2121

Browse files
authored
Added BigInt support (#197)
* Added BigInt support * Updated test * Updated README
1 parent 4b6df2e commit 2da2121

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ And nested ones, too.
106106
| -----------|------------------------------|
107107
| `Date` | `string` via `toISOString()` |
108108
| `RegExp` | `string` |
109+
| `BigInt` | `integer` via `toString` |
109110

110111
<a name="required"></a>
111112
#### Required
@@ -439,9 +440,28 @@ const stringify = fastJson(schema, { schema: externalSchema })
439440
440441
<a name="long"></a>
441442
#### Long integers
442-
Long integers (64-bit) are supported using the [long](https://github.com/dcodeIO/long.js) module.
443+
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) from Node.js v10.3 and above.
444+
If you can't use BigInts in your environment, long integers (64-bit) are also supported using the [long](https://github.com/dcodeIO/long.js) module.
443445
Example:
444446
```javascript
447+
// => using native BigInt
448+
const stringify = fastJson({
449+
title: 'Example Schema',
450+
type: 'object',
451+
properties: {
452+
id: {
453+
type: 'integer'
454+
}
455+
}
456+
})
457+
458+
const obj = {
459+
id: 18446744073709551615n
460+
}
461+
462+
console.log(stringify(obj)) // '{"id":18446744073709551615}'
463+
464+
// => using the long library
445465
const Long = require('long')
446466

447467
const stringify = fastJson({

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ function $asNull () {
214214
function $asInteger (i) {
215215
if (isLong && isLong(i)) {
216216
return i.toString()
217+
} else if (typeof i === 'bigint') {
218+
return i.toString()
217219
} else {
218220
return $asNumber(i)
219221
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"long": "^4.0.0",
4040
"pre-commit": "^1.2.2",
4141
"proxyquire": "^2.1.3",
42+
"semver": "^6.3.0",
4243
"standard": "^14.0.0",
4344
"tap": "^12.6.5",
4445
"tap-mocha-reporter": "^3.0.9",

test/bigint.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict'
2+
3+
module.exports = function (test, build) {
4+
test('render a bigint as JSON', (t) => {
5+
t.plan(1)
6+
7+
const schema = {
8+
title: 'bigint',
9+
type: 'integer'
10+
}
11+
12+
const stringify = build(schema)
13+
const output = stringify(1615n)
14+
15+
t.equal(output, '1615')
16+
})
17+
18+
test('render an object with a bigint as JSON', (t) => {
19+
t.plan(1)
20+
21+
const schema = {
22+
title: 'object with bigint',
23+
type: 'object',
24+
properties: {
25+
id: {
26+
type: 'integer'
27+
}
28+
}
29+
}
30+
31+
const stringify = build(schema)
32+
const output = stringify({
33+
id: 1615n
34+
})
35+
36+
t.equal(output, '{"id":1615}')
37+
})
38+
39+
test('render an array with a bigint as JSON', (t) => {
40+
t.plan(1)
41+
42+
const schema = {
43+
title: 'array with bigint',
44+
type: 'array',
45+
items: {
46+
type: 'integer'
47+
}
48+
}
49+
50+
const stringify = build(schema)
51+
const output = stringify([1615n])
52+
53+
t.equal(output, '[1615]')
54+
})
55+
56+
test('render an object with an additionalProperty of type bigint as JSON', (t) => {
57+
t.plan(1)
58+
59+
const schema = {
60+
title: 'object with bigint',
61+
type: 'object',
62+
additionalProperties: {
63+
type: 'integer'
64+
}
65+
}
66+
67+
const stringify = build(schema)
68+
const output = stringify({
69+
num: 1615n
70+
})
71+
72+
t.equal(output, '{"num":1615}')
73+
})
74+
}

test/integer.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

3-
const test = require('tap').test
3+
const t = require('tap')
4+
const test = t.test
5+
const semver = require('semver')
46
const validator = require('is-my-json-valid')
57
const proxyquire = require('proxyquire')
68
const build = proxyquire('..', { long: null })
@@ -83,3 +85,10 @@ test('render an object with an additionalProperty of type integer as JSON', (t)
8385
t.equal(output, '{"num":1615}')
8486
t.ok(validate(JSON.parse(output)), 'valid schema')
8587
})
88+
89+
if (semver.gt(process.versions.node, '10.3.0')) {
90+
require('./bigint')(t.test, build)
91+
} else {
92+
t.pass('Skip because Node version < 10.4')
93+
t.end()
94+
}

0 commit comments

Comments
 (0)