import { binToHex } from './hex' ; const defaultStringifySpacing = 2 ; remarks param value param spacing export const stringify = ( value , spacing = defaultStringifySpacing ) => JSON . stringify ( value , ( _ , item ) => { const type = typeof item ; const name = typeof item === 'object' && item !== null ? item . constructor . name : type ; switch ( name ) { case 'Uint8Array' : return `<Uint8Array: 0x${ binToHex ( item as Uint8Array ) }>` ; case 'bigint' : return `<bigint: ${ ( item as bigint ) . toString ( ) }n>` ; case 'function' : case 'symbol' : return `<${ name }: ${ ( item as symbol | Function ) . toString ( ) }>` ; default : return item ; } } , spacing ) ; param objectOrArray export const sortObjectKeys = ( objectOrArray ) : any => { if ( Array . isArray ( objectOrArray ) ) { return objectOrArray . map ( sortObjectKeys ) ; } if ( typeof objectOrArray !== 'object' || objectOrArray === null || objectOrArray . constructor . name !== 'Object' ) { return objectOrArray ; } const keys = Object . keys ( objectOrArray ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ; return keys . reduce ( ( all , key ) => ( { ... all , [ key ] : sortObjectKeys ( ( objectOrArray as { [ key ] : unknown ; } ) [ key ] ) , } ) , { } ) ; } ; const uint8ArrayRegex = /"<Uint8Array: 0x(?<hex>[0-9a-f]*)>"/gu ; const bigIntRegex = /"<bigint: (?<bigint>[0-9]*)n>"/gu ; param stringified export const stringifyTestVector = ( value , alphabetize = true ) => { const stringified = alphabetize ? stringify ( sortObjectKeys ( value ) ) : stringify ( value ) ; return stringified . replace ( uint8ArrayRegex , "hexToBin('$1')" ) . replace ( bigIntRegex , "BigInt('$1')" ) ; } ;