Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
/** @hidden */ | ||
function stringifyOne<Ts>(value: Ts): string { | ||
if (typeof value === 'string') return JSON.stringify(value); | ||
|
||
const defaultRepr: string = `${value}`; | ||
if (/^\[object (Object|Null|Undefined)\]$/.exec(defaultRepr) === null) return defaultRepr; | ||
try { | ||
return JSON.stringify(value, (k, v) => { | ||
if (typeof v === 'bigint') { | ||
return v.toString() + 'n'; | ||
} else { | ||
return v; | ||
} | ||
}); | ||
} catch (err) { | ||
// ignored: object cannot be stringified using JSON.stringify | ||
function stringifyNumber(numValue: number) { | ||
switch (numValue) { | ||
case 0: | ||
return 1 / numValue === Number.NEGATIVE_INFINITY ? '-0' : '0'; | ||
case Number.NEGATIVE_INFINITY: | ||
return 'Number.NEGATIVE_INFINITY'; | ||
case Number.POSITIVE_INFINITY: | ||
return 'Number.POSITIVE_INFINITY'; | ||
default: | ||
return numValue === numValue ? String(numValue) : 'Number.NaN'; | ||
} | ||
return defaultRepr; | ||
} | ||
|
||
/** @hidden */ | ||
export function stringify<Ts>(value: Ts): string { | ||
if (Array.isArray(value)) return `[${value.map(stringify).join(',')}]`; | ||
return stringifyOne(value); | ||
switch (Object.prototype.toString.call(value)) { | ||
case '[object Array]': | ||
return `[${(value as any).map(stringify).join(',')}]`; | ||
case '[object BigInt]': | ||
return `${value}n`; | ||
case '[object Boolean]': | ||
return typeof value === 'boolean' ? JSON.stringify(value) : `new Boolean(${JSON.stringify(value)})`; | ||
case '[object Map]': | ||
return `new Map(${stringify(Array.from(value as any))})`; | ||
case '[object Null]': | ||
return `null`; | ||
case '[object Number]': | ||
return typeof value === 'number' ? stringifyNumber(value) : `new Number(${stringifyNumber(Number(value))})`; | ||
case '[object Object]': | ||
const defaultRepr: string = `${value}`; | ||
if (defaultRepr !== '[object Object]') return defaultRepr; | ||
try { | ||
return ( | ||
'{' + | ||
Object.keys(value) | ||
.map(k => `${JSON.stringify(k)}:${stringify((value as any)[k])}`) | ||
.join(',') + | ||
'}' | ||
); | ||
} catch (err) { | ||
if (err instanceof RangeError) return '[cyclic]'; | ||
return '[object Object]'; | ||
} | ||
case '[object Set]': | ||
return `new Set(${stringify(Array.from(value as any))})`; | ||
case '[object String]': | ||
return typeof value === 'string' ? JSON.stringify(value) : `new String(${JSON.stringify(value)})`; | ||
case '[object Undefined]': | ||
return `undefined`; | ||
default: | ||
try { | ||
return `${value}`; | ||
} catch { | ||
return Object.prototype.toString.call(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters