Skip to content

Commit 4a2b681

Browse files
authored
Drop support for long as we have BigInt. (#315)
1 parent f1784a5 commit 4a2b681

File tree

7 files changed

+89
-264
lines changed

7 files changed

+89
-264
lines changed

README.md

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -514,46 +514,7 @@ const stringify = fastJson(schema, { schema: externalSchema })
514514

515515
<a name="long"></a>
516516
#### Long integers
517-
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.
518-
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.
519-
Example:
520-
```javascript
521-
// => using native BigInt
522-
const stringify = fastJson({
523-
title: 'Example Schema',
524-
type: 'object',
525-
properties: {
526-
id: {
527-
type: 'integer'
528-
}
529-
}
530-
})
531-
532-
const obj = {
533-
id: 18446744073709551615n
534-
}
535-
536-
console.log(stringify(obj)) // '{"id":18446744073709551615}'
537-
538-
// => using the long library
539-
const Long = require('long')
540-
541-
const stringify = fastJson({
542-
title: 'Example Schema',
543-
type: 'object',
544-
properties: {
545-
id: {
546-
type: 'integer'
547-
}
548-
}
549-
})
550-
551-
const obj = {
552-
id: Long.fromString('18446744073709551615', true)
553-
}
554-
555-
console.log(stringify(obj)) // '{"id":18446744073709551615}'
556-
```
517+
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
557518

558519
<a name="integer"></a>
559520
#### Integers

index.js

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ const fjsCloned = Symbol('fast-json-stringify.cloned')
1010
const validate = require('./schema-validator')
1111
let stringSimilarity = null
1212

13-
let isLong
14-
try {
15-
isLong = require('long').isLong
16-
} catch (e) {
17-
isLong = null
18-
}
19-
2013
const addComma = `
2114
if (addComma) {
2215
json += ','
@@ -121,8 +114,6 @@ function build (schema, options) {
121114
})()
122115
123116
124-
var isLong = ${isLong ? isLong.toString() : false}
125-
126117
function parseInteger(int) { return Math.${intParseFunctionName}(int) }
127118
`
128119

@@ -286,9 +277,7 @@ function $asNull () {
286277
}
287278

288279
function $asInteger (i) {
289-
if (isLong && isLong(i)) {
290-
return i.toString()
291-
} else if (typeof i === 'bigint') {
280+
if (typeof i === 'bigint') {
292281
return i.toString()
293282
} else if (Number.isInteger(i)) {
294283
return $asNumber(i)
@@ -547,22 +536,11 @@ function additionalProperty (location) {
547536
} else if (type === 'integer') {
548537
code += `
549538
var t = Number(obj[keys[i]])
539+
if (!isNaN(t)) {
540+
${addComma}
541+
json += $asString(keys[i]) + ':' + t
542+
}
550543
`
551-
if (isLong) {
552-
code += `
553-
if (isLong(obj[keys[i]]) || !isNaN(t)) {
554-
${addComma}
555-
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
556-
}
557-
`
558-
} else {
559-
code += `
560-
if (!isNaN(t)) {
561-
${addComma}
562-
json += $asString(keys[i]) + ':' + t
563-
}
564-
`
565-
}
566544
} else if (type === 'number') {
567545
code += `
568546
var t = Number(obj[keys[i]])
@@ -764,33 +742,12 @@ function buildCode (location, code, laterCode, name) {
764742
} else if (type === 'integer') {
765743
code += `
766744
var rendered = false
767-
`
768-
if (isLong) {
769-
code += `
770-
if (isLong(obj[${sanitized}])) {
771-
${addComma}
772-
json += ${asString} + ':' + obj[${sanitized}].toString()
773-
rendered = true
774-
} else {
775-
var t = Number(obj[${sanitized}])
776-
if (!isNaN(t)) {
777-
${addComma}
778-
json += ${asString} + ':' + t
779-
rendered = true
780-
}
781-
}
782-
`
783-
} else {
784-
code += `
785-
var t = Number(obj[${sanitized}])
786-
if (!isNaN(t)) {
787-
${addComma}
788-
json += ${asString} + ':' + t
789-
rendered = true
790-
}
791-
`
792-
}
793-
code += `
745+
var t = Number(obj[${sanitized}])
746+
if (!isNaN(t)) {
747+
${addComma}
748+
json += ${asString} + ':' + t
749+
rendered = true
750+
}
794751
if (rendered) {
795752
`
796753
} else {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"benchmark": "^2.1.4",
3434
"compile-json-stringify": "^0.1.2",
3535
"is-my-json-valid": "^2.20.0",
36-
"long": "^4.0.0",
3736
"moment": "^2.24.0",
3837
"pre-commit": "^1.2.2",
3938
"proxyquire": "^2.1.3",

test/bigint.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

test/bigint.test.js

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

test/integer.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
const t = require('tap')
44
const test = t.test
5-
const semver = require('semver')
65
const validator = require('is-my-json-valid')
7-
const proxyquire = require('proxyquire')
8-
const build = proxyquire('..', { long: null })
6+
const build = require('..')
97

108
test('render an integer as JSON', (t) => {
119
t.plan(2)
@@ -130,10 +128,3 @@ test('render an object with an additionalProperty of type integer as JSON', (t)
130128
t.equal(output, '{"num":1615}')
131129
t.ok(validate(JSON.parse(output)), 'valid schema')
132130
})
133-
134-
if (semver.gt(process.versions.node, '10.3.0')) {
135-
require('./bigint')(t.test, build)
136-
} else {
137-
t.pass('Skip because Node version < 10.4')
138-
t.end()
139-
}

0 commit comments

Comments
 (0)