Skip to content

Commit

Permalink
Added BigInt support (#197)
Browse files Browse the repository at this point in the history
* Added BigInt support

* Updated test

* Updated README
  • Loading branch information
delvedor committed Dec 15, 2019
1 parent 4b6df2e commit 2da2121
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -106,6 +106,7 @@ And nested ones, too.
| -----------|------------------------------|
| `Date` | `string` via `toISOString()` |
| `RegExp` | `string` |
| `BigInt` | `integer` via `toString` |

<a name="required"></a>
#### Required
Expand Down Expand Up @@ -439,9 +440,28 @@ const stringify = fastJson(schema, { schema: externalSchema })
<a name="long"></a>
#### Long integers
Long integers (64-bit) are supported using the [long](https://github.com/dcodeIO/long.js) module.
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.
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.
Example:
```javascript
// => using native BigInt
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
})

const obj = {
id: 18446744073709551615n
}

console.log(stringify(obj)) // '{"id":18446744073709551615}'

// => using the long library
const Long = require('long')

const stringify = fastJson({
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -214,6 +214,8 @@ function $asNull () {
function $asInteger (i) {
if (isLong && isLong(i)) {
return i.toString()
} else if (typeof i === 'bigint') {
return i.toString()
} else {
return $asNumber(i)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -39,6 +39,7 @@
"long": "^4.0.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"semver": "^6.3.0",
"standard": "^14.0.0",
"tap": "^12.6.5",
"tap-mocha-reporter": "^3.0.9",
Expand Down
74 changes: 74 additions & 0 deletions test/bigint.js
@@ -0,0 +1,74 @@
'use strict'

module.exports = function (test, build) {
test('render a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'bigint',
type: 'integer'
}

const stringify = build(schema)
const output = stringify(1615n)

t.equal(output, '1615')
})

test('render an object with a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'object with bigint',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
}

const stringify = build(schema)
const output = stringify({
id: 1615n
})

t.equal(output, '{"id":1615}')
})

test('render an array with a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'array with bigint',
type: 'array',
items: {
type: 'integer'
}
}

const stringify = build(schema)
const output = stringify([1615n])

t.equal(output, '[1615]')
})

test('render an object with an additionalProperty of type bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'object with bigint',
type: 'object',
additionalProperties: {
type: 'integer'
}
}

const stringify = build(schema)
const output = stringify({
num: 1615n
})

t.equal(output, '{"num":1615}')
})
}
11 changes: 10 additions & 1 deletion test/integer.test.js
@@ -1,6 +1,8 @@
'use strict'

const test = require('tap').test
const t = require('tap')
const test = t.test
const semver = require('semver')
const validator = require('is-my-json-valid')
const proxyquire = require('proxyquire')
const build = proxyquire('..', { long: null })
Expand Down Expand Up @@ -83,3 +85,10 @@ test('render an object with an additionalProperty of type integer as JSON', (t)
t.equal(output, '{"num":1615}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

if (semver.gt(process.versions.node, '10.3.0')) {
require('./bigint')(t.test, build)
} else {
t.pass('Skip because Node version < 10.4')
t.end()
}

0 comments on commit 2da2121

Please sign in to comment.