diff --git a/README.md b/README.md index 86498dce..45e3ab9b 100644 --- a/README.md +++ b/README.md @@ -514,46 +514,7 @@ const stringify = fastJson(schema, { schema: externalSchema }) #### Long integers -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({ - title: 'Example Schema', - type: 'object', - properties: { - id: { - type: 'integer' - } - } -}) - -const obj = { - id: Long.fromString('18446744073709551615', true) -} - -console.log(stringify(obj)) // '{"id":18446744073709551615}' -``` +By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). #### Integers diff --git a/index.js b/index.js index b4e03114..55ce3981 100644 --- a/index.js +++ b/index.js @@ -10,13 +10,6 @@ const fjsCloned = Symbol('fast-json-stringify.cloned') const validate = require('./schema-validator') let stringSimilarity = null -let isLong -try { - isLong = require('long').isLong -} catch (e) { - isLong = null -} - const addComma = ` if (addComma) { json += ',' @@ -121,8 +114,6 @@ function build (schema, options) { })() - var isLong = ${isLong ? isLong.toString() : false} - function parseInteger(int) { return Math.${intParseFunctionName}(int) } ` @@ -286,9 +277,7 @@ function $asNull () { } function $asInteger (i) { - if (isLong && isLong(i)) { - return i.toString() - } else if (typeof i === 'bigint') { + if (typeof i === 'bigint') { return i.toString() } else if (Number.isInteger(i)) { return $asNumber(i) @@ -547,22 +536,11 @@ function additionalProperty (location) { } else if (type === 'integer') { code += ` var t = Number(obj[keys[i]]) + if (!isNaN(t)) { + ${addComma} + json += $asString(keys[i]) + ':' + t + } ` - if (isLong) { - code += ` - if (isLong(obj[keys[i]]) || !isNaN(t)) { - ${addComma} - json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) - } - ` - } else { - code += ` - if (!isNaN(t)) { - ${addComma} - json += $asString(keys[i]) + ':' + t - } - ` - } } else if (type === 'number') { code += ` var t = Number(obj[keys[i]]) @@ -764,33 +742,12 @@ function buildCode (location, code, laterCode, name) { } else if (type === 'integer') { code += ` var rendered = false - ` - if (isLong) { - code += ` - if (isLong(obj[${sanitized}])) { - ${addComma} - json += ${asString} + ':' + obj[${sanitized}].toString() - rendered = true - } else { - var t = Number(obj[${sanitized}]) - if (!isNaN(t)) { - ${addComma} - json += ${asString} + ':' + t - rendered = true - } - } - ` - } else { - code += ` - var t = Number(obj[${sanitized}]) - if (!isNaN(t)) { - ${addComma} - json += ${asString} + ':' + t - rendered = true - } - ` - } - code += ` + var t = Number(obj[${sanitized}]) + if (!isNaN(t)) { + ${addComma} + json += ${asString} + ':' + t + rendered = true + } if (rendered) { ` } else { diff --git a/package.json b/package.json index 8f0909a7..243f0084 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,6 @@ "benchmark": "^2.1.4", "compile-json-stringify": "^0.1.2", "is-my-json-valid": "^2.20.0", - "long": "^4.0.0", "moment": "^2.24.0", "pre-commit": "^1.2.2", "proxyquire": "^2.1.3", diff --git a/test/bigint.js b/test/bigint.js deleted file mode 100644 index ec166b46..00000000 --- a/test/bigint.js +++ /dev/null @@ -1,74 +0,0 @@ -'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}') - }) -} diff --git a/test/bigint.test.js b/test/bigint.test.js new file mode 100644 index 00000000..02a75d4d --- /dev/null +++ b/test/bigint.test.js @@ -0,0 +1,76 @@ +'use strict' + +const t = require('tap') +const test = t.test +const build = require('..') + +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}') +}) diff --git a/test/integer.test.js b/test/integer.test.js index c2c7d04d..fd1530f9 100644 --- a/test/integer.test.js +++ b/test/integer.test.js @@ -2,10 +2,8 @@ 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 }) +const build = require('..') test('render an integer as JSON', (t) => { t.plan(2) @@ -130,10 +128,3 @@ 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() -} diff --git a/test/long.test.js b/test/long.test.js deleted file mode 100644 index d5bf1138..00000000 --- a/test/long.test.js +++ /dev/null @@ -1,85 +0,0 @@ -'use strict' - -const test = require('tap').test -const validator = require('is-my-json-valid') -const Long = require('long') -const build = require('..') - -test('render a long as JSON', (t) => { - t.plan(2) - - const schema = { - title: 'long', - type: 'integer' - } - - const validate = validator(schema) - const stringify = build(schema) - const output = stringify(Long.fromString('18446744073709551615', true)) - - t.equal(output, '18446744073709551615') - t.ok(validate(JSON.parse(output)), 'valid schema') -}) - -test('render an object with long as JSON', (t) => { - t.plan(2) - - const schema = { - title: 'object with long', - type: 'object', - properties: { - id: { - type: 'integer' - } - } - } - - const validate = validator(schema) - const stringify = build(schema) - const output = stringify({ - id: Long.fromString('18446744073709551615', true) - }) - - t.equal(output, '{"id":18446744073709551615}') - t.ok(validate(JSON.parse(output)), 'valid schema') -}) - -test('render an array with long as JSON', (t) => { - t.plan(2) - - const schema = { - title: 'array with long', - type: 'array', - items: { - type: 'integer' - } - } - - const validate = validator(schema) - const stringify = build(schema) - const output = stringify([Long.fromString('18446744073709551615', true)]) - - t.equal(output, '[18446744073709551615]') - t.ok(validate(JSON.parse(output)), 'valid schema') -}) - -test('render an object with a long additionalProperty as JSON', (t) => { - t.plan(2) - - const schema = { - title: 'object with long', - type: 'object', - additionalProperties: { - type: 'integer' - } - } - - const validate = validator(schema) - const stringify = build(schema) - const output = stringify({ - num: Long.fromString('18446744073709551615', true) - }) - - t.equal(output, '{"num":18446744073709551615}') - t.ok(validate(JSON.parse(output)), 'valid schema') -})