MCPcopy Create free account
hub / github.com/rollup/plugins / serialize

Function serialize

packages/pluginutils/src/dataToEsm.ts:40–60  ·  view source on GitHub ↗
(obj: unknown, indent: Indent, baseIndent: string)

Source from the content-addressed store, hash-verified

38}
39
40function serialize(obj: unknown, indent: Indent, baseIndent: string): string {
41 if (typeof obj === 'object' && obj !== null) {
42 if (Array.isArray(obj)) return serializeArray(obj, indent, baseIndent);
43 if (obj instanceof Date) return `new Date(${obj.getTime()})`;
44 if (obj instanceof RegExp) return obj.toString();
45 return serializeObject(obj, indent, baseIndent);
46 }
47 if (typeof obj === 'number') {
48 if (obj === Infinity) return 'Infinity';
49 if (obj === -Infinity) return '-Infinity';
50 if (obj === 0) return 1 / obj === Infinity ? '0' : '-0';
51 if (obj !== obj) return 'NaN'; // eslint-disable-line no-self-compare
52 }
53 if (typeof obj === 'symbol') {
54 const key = Symbol.keyFor(obj);
55 // eslint-disable-next-line no-undefined
56 if (key !== undefined) return `Symbol.for(${stringify(key)})`;
57 }
58 if (typeof obj === 'bigint') return `${obj}n`;
59 return stringify(obj);
60}
61
62// isWellFormed exists from Node.js 20
63const hasStringIsWellFormed = 'isWellFormed' in String.prototype;

Callers 3

serializeArrayFunction · 0.85
serializeObjectFunction · 0.85
dataToEsmFunction · 0.85

Calls 3

serializeArrayFunction · 0.85
serializeObjectFunction · 0.85
stringifyFunction · 0.85

Tested by

no test coverage detected