This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
4 changed files
with
99 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './stringify.js'; | ||
export * from './parser.js'; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function nativeJSONParse(value) { | ||
return JSON.parse(value, reviver); | ||
} | ||
|
||
function dateReviver(value) { | ||
return new Date(value.iso); | ||
} | ||
|
||
function regExpReviver(value) { | ||
return new RegExp(value.source, value.flags); | ||
} | ||
|
||
function setReviver(value) { | ||
return new Set(value.values); | ||
} | ||
|
||
function reviver(key, value) { | ||
if (typeof value === 'object' && value !== null) { | ||
if ('__type' in value) { | ||
switch (value.__type) { | ||
case 'Date': | ||
return dateReviver(value); | ||
case 'RegExp': | ||
return regExpReviver(value); | ||
case 'Set': | ||
return setReviver(value); | ||
} | ||
} | ||
} | ||
|
||
// For primitives, use native function | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export function nativeJSONStringify(value) { | ||
return JSON.stringify(value, replacer); | ||
} | ||
|
||
function dateReplacer(value) { | ||
return { | ||
__type: 'Date', | ||
iso: value.toISOString(), | ||
}; | ||
} | ||
|
||
function regExpReplacer(value) { | ||
return { | ||
__type: 'RegExp', | ||
flags: value.flags, | ||
source: value.source, | ||
}; | ||
} | ||
|
||
function setReplacer(value) { | ||
return { | ||
__type: 'Set', | ||
values: Array.from(value), | ||
}; | ||
} | ||
|
||
function replacer(key, value) { | ||
if (typeof value === 'bigint') { | ||
return value.toString(); | ||
} else if (typeof value === 'symbol') { | ||
return value.toString(); | ||
} else if (typeof value === 'object') { | ||
if (value instanceof Date) { | ||
return dateReplacer(value); | ||
} else if (value instanceof RegExp) { | ||
return regExpReplacer(value); | ||
} else if (value instanceof Set) { | ||
return setReplacer(value); | ||
} | ||
|
||
return value; | ||
} else if (typeof value === 'string') { | ||
if (this[key] instanceof Date) { | ||
return dateReplacer(this[key]); | ||
} | ||
} | ||
|
||
// For primitives, use native function | ||
return value; | ||
} |