Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function sort (array, comparator) {
return array
}

/** @type {() => string | undefined} */
const typedArrayPrototypeGetSymbolToStringTag =
// @ts-expect-error
Object.getOwnPropertyDescriptor(
Expand All @@ -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
}
Expand Down Expand Up @@ -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++) {
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down