Skip to content

Commit 65125aa

Browse files
committed
feat: enhance serialization for number types
1 parent adee6b0 commit 65125aa

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

test/utils/serialize-javascript.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,30 @@ function serializeRecursively(
361361
return `${ST}null${ET}`;
362362
} else if (source === undefined) {
363363
return undefined;
364-
} else if (source instanceof RegExp) {
365-
return `${ST}new RegExp('${source.source.replace(/\\\\/g, '\\')}', '${source.flags ?? ''}')${ET}`;
366-
// `value instanceof Date` never works, try testing date format instead
364+
} else if (typeof source === 'number') {
365+
if (Number.isNaN(source)) {
366+
return `${ST}NaN${ET}`;
367+
} else if (source === Number.POSITIVE_INFINITY) {
368+
return `${ST}Infinity${ET}`;
369+
} else if (source === Number.NEGATIVE_INFINITY) {
370+
return `${ST}-Infinity${ET}`;
371+
} else {
372+
return `${ST}${source}${ET}`;
373+
}
367374
} else if (source instanceof Date) {
375+
if (Number.isNaN(source.getTime())) {
376+
return `${ST}new Date(NaN)${ET}`;
377+
}
368378
return `${ST}new Date('${source.toISOString()}')${ET}`;
369379
} else if (typeof source === 'bigint') {
370380
return `${ST}BigInt('${source.toString()}')${ET}`;
381+
} else if (source instanceof RegExp) {
382+
return `${ST}new RegExp('${source.source.replace(/\\\\/g, '\\')}', '${source.flags ?? ''}')${ET}`;
383+
// `value instanceof Date` never works, try testing date format instead
371384
} else if (typeof source === 'symbol') {
372-
if (Symbol.keyFor(source)) {
385+
if (wellKnownSymbols.includes(source)) {
386+
return `${ST}${source.description}${ET}`;
387+
} else if (Symbol.keyFor(source)) {
373388
return `${ST}Symbol.for('${Symbol.keyFor(source)}')${ET}`;
374389
}
375390
return `${ST}Symbol('${source.description}')${ET}`;
@@ -390,6 +405,7 @@ function serializeRecursively(
390405
NaN 和 Infinity 格式的数值及 null 都会被当做 null。
391406
其他类型的对象,包括 Map/Set/WeakMap/WeakSet,仅会序列化可枚举的属性。
392407
408+
todo:
393409
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/structuredClone
394410
*/
395411
// Save the symbol keys to a string property, because symbol keys are not serializable.
@@ -408,31 +424,22 @@ function serializeRecursively(
408424
}
409425
Object.keys(source).forEach((key) => {
410426
const subValue = source[key];
411-
if (
412-
subValue != null &&
413-
typeof subValue !== 'string' &&
414-
typeof subValue !== 'number' &&
415-
typeof subValue !== 'boolean'
416-
) {
417-
const serializedValue = serializeRecursively(subValue, {
418-
...options,
419-
parentPath: [...parentPath, key],
420-
printLabel,
421-
});
422-
if (debug) {
423-
console.log(
424-
`------------------ serializeRecursively${printLabel ? ` (${printLabel})` : ''}`,
425-
printPath ? printPath({ parentPath, key }) : [...parentPath, key],
426-
' ------------------'
427-
);
428-
console.log('value:', subValue);
429-
console.log('result:');
430-
console.log(serializedValue);
431-
}
432-
source[key] = serializedValue;
433-
} else {
434-
source[key] = subValue;
427+
const serializedValue = serializeRecursively(subValue, {
428+
...options,
429+
parentPath: [...parentPath, key],
430+
printLabel,
431+
});
432+
if (debug) {
433+
console.log(
434+
`------------------ serializeRecursively${printLabel ? ` (${printLabel})` : ''}`,
435+
printPath ? printPath({ parentPath, key }) : [...parentPath, key],
436+
' ------------------'
437+
);
438+
console.log('value:', subValue);
439+
console.log('result:');
440+
console.log(serializedValue);
435441
}
442+
source[key] = serializedValue;
436443
});
437444
return source;
438445
} else if (typeof source === 'function') {
@@ -883,17 +890,21 @@ interface RefInfo {
883890
path: PathType[];
884891
from: PathType[];
885892
}
893+
// interface Jsoner<T> {
894+
// fromJson: (value: T) => string;
895+
// deserialize: (value: string) => T;
896+
// }
886897

887898
interface SerializedResult {
888899
version?: string;
889900
startTag?: string;
890901
variablePrefix?: string;
891902
endTag?: string;
892903
source: string | undefined;
893-
patches: PatchInfo[];
894-
descriptors?: DescriptorInfo[];
895904
types: TypeInfo[];
905+
patches: PatchInfo[];
896906
refs: RefInfo[];
907+
descriptors?: DescriptorInfo[];
897908
}
898909

899910
export interface StringifyOptions {

0 commit comments

Comments
 (0)