diff --git a/index.js b/index.js index f71bf98..7487e6b 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,7 @@ function sort (array, comparator) { return array } +/** @type {() => string | undefined} */ const typedArrayPrototypeGetSymbolToStringTag = // @ts-expect-error Object.getOwnPropertyDescriptor( @@ -75,10 +76,12 @@ const typedArrayPrototypeGetSymbolToStringTag = ), Symbol.toStringTag ).get +const typedArrayPrototypeGetLength = /** @type {{ get: () => number }} */ ( + Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Int8Array.prototype), 'length') +).get /** @param {object & {length?: unknown}} value */ function getTypedArrayType (value) { - // @ts-expect-error const type = typedArrayPrototypeGetSymbolToStringTag.call(value) return type !== undefined && value.length !== 0 ? type : undefined } @@ -371,8 +374,20 @@ function configure (options) { whitespace = ' ' } const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth) - if (deterministic && getTypedArrayType(value) === undefined) { - keys = sort(keys, comparator) + if (deterministic && keyLength > 1) { + let index = typedArrayPrototypeGetSymbolToStringTag.call(value) === undefined + ? 0 + : typedArrayPrototypeGetLength.call(value) + if (index === 0) { + keys = sort(keys, comparator) + } else if ( + keyLength > index + 1 && + (comparator !== undefined || maximumPropertiesToStringify > index) + ) { + for (const key of sort(keys.slice(index), comparator)) { + keys[index++] = key + } + } } stack.push(value) for (let i = 0; i < maximumPropertiesToStringify; i++) { diff --git a/test.js b/test.js index 4ad065d..c7aaa8e 100644 --- a/test.js +++ b/test.js @@ -203,6 +203,42 @@ test('omitted bigint typed array entries preserve property order and breadth', assert.end() }) +test('replacer functions sort extra typed array properties deterministically', + /** @param {import('tap').Test} assert */ + (assert) => { + const value = Object.assign(new Uint8Array([1, 2]), { z: 1, a: 2, m: 3 }) + let compared = false + const descending = /** @param {string} left @param {string} right */ (left, right) => { + assert.match(left, /^[amz]$/) + assert.match(right, /^[amz]$/) + compared = true + return right.localeCompare(left) + } + const replacer = /** @param {string} key @param {unknown} value */ (key, value) => value + for (const [deterministic, expected] of /** @type {const} */ ([ + [true, { 0: 1, 1: 2, a: 2, m: 3, z: 1 }], + [descending, { 0: 1, 1: 2, z: 1, m: 3, a: 2 }] + ])) { + const serialize = stringify.configure({ deterministic }) + for (const space of [undefined, 2]) { + assert.equal(serialize(value, replacer, space), JSON.stringify(expected, null, space)) + assert.equal(serialize(value, replacer, space), serialize(value, null, space)) + } + } + assert.equal(stringify(new Uint8Array([1]), replacer), '{"0":1}') + assert.equal(stringify(new Uint8Array([1, 2]), replacer), '{"0":1,"1":2}') + for (const deterministic of [true, descending]) { + compared = false + const capped = stringify.configure({ deterministic, maximumBreadth: 2 }) + assert.equal(capped(value, replacer), '{"0":1,"1":2,"...":"3 items not stringified"}') + assert.equal(compared, typeof deterministic === 'function') + } + Object.defineProperty(value, 'length', { value: 1 }) + const limited = stringify.configure({ deterministic: descending, maximumBreadth: 3 }) + assert.equal(limited(value, replacer), '{"0":1,"1":2,"z":1,"...":"2 items not stringified"}') + assert.end() + }) + test('bigint typed array policy uses the intrinsic type', /** @param {import('tap').Test} assert */ (assert) => {