Skip to content

Commit 915bf7a

Browse files
committed
fix: set maximum spacer length to 10 and fix TypeScript definition
The TypeScript definition was not correct and is now working as expected. The spacer argument was not limited to 10 as it should be to be spec compliant.
1 parent fb05a4a commit 915bf7a

5 files changed

Lines changed: 51 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v2.0.1
4+
5+
- Fixed maximum spacer length (10)
6+
- Fixed TypeScript definition
7+
38
## v2.0.0
49

510
- __[BREAKING]__ Convert BigInt to number by default instead of ignoring these values

index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
declare function stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
2-
declare function stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
1+
export function stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string;
2+
export function stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
33

44
export interface StringifyOptions {
55
bigint?: boolean,
66
circularValue?: string | null,
77
deterministic?: boolean,
88
}
99

10-
declare function configure(StringifyOptions): stringify;
10+
export namespace stringify {
11+
export function configure(options: StringifyOptions): typeof stringify;
12+
}
1113

12-
stringify.configure = configure;
14+
export function configure(options: StringifyOptions): typeof stringify;
1315

1416
export default stringify;

index.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
'use strict'
22

33
const stringify = main()
4+
45
stringify.configure = main
6+
stringify.stringify = stringify
7+
58
stringify.default = stringify
69

10+
exports.stringify = stringify
11+
exports.configure = main
12+
713
module.exports = stringify
814

915
// eslint-disable-next-line
@@ -122,30 +128,20 @@ function getCircularValueOption (options) {
122128
return circularValue === undefined ? '"[Circular]"' : circularValue
123129
}
124130

125-
function getBigIntOption (options) {
126-
if (options && Object.prototype.hasOwnProperty.call(options, 'bigint')) {
127-
var bigint = options.bigint
128-
if (typeof bigint !== 'boolean') {
129-
throw new TypeError('The "bigint" argument must be of type boolean')
130-
}
131-
}
132-
return bigint === undefined ? true : bigint
133-
}
134-
135-
function getDeterministicOption (options) {
136-
if (options && Object.prototype.hasOwnProperty.call(options, 'deterministic')) {
137-
var deterministic = options.deterministic
138-
if (typeof deterministic !== 'boolean') {
139-
throw new TypeError('The "deterministic" argument must be of type boolean')
131+
function getBooleanOption (options, key) {
132+
if (options && Object.prototype.hasOwnProperty.call(options, key)) {
133+
var value = options[key]
134+
if (typeof value !== 'boolean') {
135+
throw new TypeError(`The "${key}" argument must be of type boolean`)
140136
}
141137
}
142-
return deterministic === undefined ? true : deterministic
138+
return value === undefined ? true : value
143139
}
144140

145141
function main (options) {
146142
const circularValue = getCircularValueOption(options)
147-
const bigint = getBigIntOption(options)
148-
const deterministic = getDeterministicOption(options)
143+
const bigint = getBooleanOption(options, 'bigint')
144+
const deterministic = getBooleanOption(options, 'deterministic')
149145

150146
// Full version: supports all options
151147
function stringifyFullFn (key, parent, stack, replacer, spacer, indentation) {
@@ -480,9 +476,9 @@ function main (options) {
480476
if (arguments.length > 1) {
481477
let spacer = ''
482478
if (typeof space === 'number') {
483-
spacer = ' '.repeat(space)
479+
spacer = ' '.repeat(Math.min(space, 10))
484480
} else if (typeof space === 'string') {
485-
spacer = space
481+
spacer = space.slice(0, 10)
486482
}
487483
if (replacer != null) {
488484
if (typeof replacer === 'function') {

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
"test": "standard && tap test.js",
2626
"benchmark": "node benchmark.js",
2727
"compare": "node compare.js",
28-
"lint": "standard --fix"
28+
"lint": "standard --fix",
29+
"tsc": "tsc"
2930
},
3031
"author": "Ruben Bridgewater",
3132
"license": "MIT",
32-
"typings": "index",
33+
"typings": "index.d.ts",
3334
"devDependencies": {
3435
"@types/json-stable-stringify": "^1.0.32",
3536
"benchmark": "^2.1.4",
@@ -42,7 +43,8 @@
4243
"json-stringify-deterministic": "^1.0.1",
4344
"json-stringify-safe": "^5.0.1",
4445
"standard": "^15.0.0",
45-
"tap": "^15.0.9"
46+
"tap": "^15.0.9",
47+
"typescript": "^4.4.3"
4648
},
4749
"repository": {
4850
"type": "git",

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,32 @@ test('check small typed arrays with extra properties', function (assert) {
609609
})
610610

611611
test('trigger sorting fast path for objects with lots of properties', function (assert) {
612+
const keys = []
612613
const obj = {}
613614
for (let i = 0; i < 1e4; i++) {
614615
obj[`a${i}`] = i
616+
keys.push(`a${i}`)
615617
}
618+
616619
const start = Date.now()
620+
617621
stringify(obj)
618622
assert.ok(Date.now() - start < 100)
623+
const now = Date.now()
624+
const actualTime = now - start
625+
keys.sort()
626+
const expectedTime = Date.now() - now
627+
assert.ok(Math.abs(actualTime - expectedTime) < 25)
628+
assert.end()
629+
})
630+
631+
test('maximum spacer length', function (assert) {
632+
const input = { a: 0 }
633+
const expected = `{\n${' '.repeat(10)}"a": 0\n}`
634+
assert.equal(stringify(input, 11), expected)
635+
assert.equal(stringify(input, 1e5), expected)
636+
assert.equal(stringify(input, ' '.repeat(11)), expected)
637+
assert.equal(stringify(input, ' '.repeat(1e3)), expected)
619638
assert.end()
620639
})
621640

0 commit comments

Comments
 (0)