-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
1 parent
380e836
commit 5fff1dc
Showing
13 changed files
with
165 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,49 @@ | ||
import { isObject } from "@harmoniclabs/obj-utils"; | ||
import { toHex } from "@harmoniclabs/uint8array-utils"; | ||
|
||
export function mkReplacer( | ||
replacer?: (key: string, value: any) => any | null, | ||
map: WeakMap<any, string> = new WeakMap() | ||
) | ||
{ | ||
if( typeof replacer !== "function" ) replacer = ( k, v ) => v; | ||
return function( key: string, value: any ) | ||
{ | ||
value = replacer!(key, value); | ||
if( isObject( value ) ) | ||
{ | ||
if ( map.has(value) ) { | ||
return map.get(value); | ||
} | ||
map.set(value, key); | ||
|
||
if( typeof value.buffer === "object" && value.buffer instanceof ArrayBuffer ) | ||
{ | ||
value = new Uint8Array(value.buffer); | ||
} | ||
if( value instanceof Uint8Array) | ||
{ | ||
value = toHex(value); | ||
} | ||
} | ||
if( typeof value === "bigint" ) | ||
{ | ||
value = value.toString(); | ||
} | ||
|
||
return value; | ||
}; | ||
} | ||
|
||
export function stringify( | ||
value: any, | ||
replacer?: (key: string, value: any) => any | null | (number | string)[], | ||
space: string | number = 0 | ||
): string | ||
{ | ||
if( replacer ) | ||
{ | ||
return JSON.stringify(value, replacer, space); | ||
} | ||
return JSON.stringify(value, mkReplacer( replacer, new WeakMap() ), space); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.