( value: unknown, )
| 38 | } |
| 39 | |
| 40 | function toSerializableIndexValue( |
| 41 | value: unknown, |
| 42 | ): CollectionIndexSerializableValue | undefined { |
| 43 | if (value == null) { |
| 44 | return value |
| 45 | } |
| 46 | |
| 47 | switch (typeof value) { |
| 48 | case `string`: |
| 49 | case `boolean`: |
| 50 | return value |
| 51 | case `number`: |
| 52 | return Number.isFinite(value) ? value : null |
| 53 | case `bigint`: |
| 54 | return { __type: `bigint`, value: value.toString() } |
| 55 | case `function`: |
| 56 | case `symbol`: |
| 57 | // Function and symbol identity are process-local and not stable across runtimes. |
| 58 | // Dropping them keeps signatures deterministic; we may skip index reuse, which is acceptable. |
| 59 | return undefined |
| 60 | case `undefined`: |
| 61 | return undefined |
| 62 | } |
| 63 | |
| 64 | if (Array.isArray(value)) { |
| 65 | return value.map((entry) => toSerializableIndexValue(entry) ?? null) |
| 66 | } |
| 67 | |
| 68 | if (value instanceof Date) { |
| 69 | return { |
| 70 | __type: `date`, |
| 71 | value: value.toISOString(), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (value instanceof Set) { |
| 76 | const serializedValues = Array.from(value) |
| 77 | .map((entry) => toSerializableIndexValue(entry) ?? null) |
| 78 | .sort((a, b) => |
| 79 | compareStringsCodePoint( |
| 80 | stableStringifyCollectionIndexValue(a), |
| 81 | stableStringifyCollectionIndexValue(b), |
| 82 | ), |
| 83 | ) |
| 84 | return { |
| 85 | __type: `set`, |
| 86 | values: serializedValues, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (value instanceof Map) { |
| 91 | const serializedEntries = Array.from(value.entries()) |
| 92 | .map(([mapKey, mapValue]) => ({ |
| 93 | key: toSerializableIndexValue(mapKey) ?? null, |
| 94 | value: toSerializableIndexValue(mapValue) ?? null, |
| 95 | })) |
| 96 | .sort((a, b) => |
| 97 | compareStringsCodePoint( |
no test coverage detected