11import { quoteString } from "./quote" ;
2- import { Next } from "./types" ;
3- import { arrayToString } from "./array" ;
2+ import { Next , ToString } from "./types" ;
43import { objectToString } from "./object" ;
54import { functionToString } from "./function" ;
65
76/**
87 * Stringify primitive values.
98 */
10- const PRIMITIVE_TYPES = {
9+ const PRIMITIVE_TYPES : Record < string , ToString > = {
1110 string : quoteString ,
1211 number : ( value : number ) => ( Object . is ( value , - 0 ) ? "-0" : String ( value ) ) ,
1312 boolean : String ,
@@ -19,82 +18,16 @@ const PRIMITIVE_TYPES = {
1918 // ES2018 `Symbol.description`.
2019 return `Symbol(${ next ( ( value as any ) . description ) } )` ;
2120 } ,
22- undefined : String
23- } ;
24-
25- /**
26- * Stringify global variable access.
27- */
28- function globalToString ( value : any , space : string , next : Next ) {
29- return `Function(${ next ( "return this" ) } )()` ;
30- }
31-
32- /**
33- * Convert JavaScript objects into strings.
34- */
35- const OBJECT_TYPES = {
36- "[object Array]" : arrayToString ,
37- "[object Object]" : objectToString ,
38- "[object Error]" : function ( error : Error , space : string , next : Next ) {
39- return `new Error(${ next ( error . message ) } )` ;
40- } ,
41- "[object Date]" : function ( date : Date ) {
42- return `new Date(${ date . getTime ( ) } )` ;
43- } ,
44- "[object String]" : function ( str : String , space : string , next : Next ) {
45- return `new String(${ next ( str . toString ( ) ) } )` ;
46- } ,
47- "[object Number]" : function ( num : number ) {
48- return `new Number(${ num } )` ;
49- } ,
50- "[object Boolean]" : function ( bool : boolean ) {
51- return `new Boolean(${ bool } )` ;
52- } ,
53- "[object Set]" : function ( set : Set < any > , space : string , next : Next ) {
54- return `new Set(${ next ( Array . from ( set ) ) } )` ;
55- } ,
56- "[object Map]" : function ( map : Map < any , any > , space : string , next : Next ) {
57- return `new Map(${ next ( Array . from ( map ) ) } )` ;
58- } ,
59- "[object RegExp]" : String ,
60- "[object Function]" : functionToString ,
61- "[object GeneratorFunction]" : functionToString ,
62- "[object AsyncFunction]" : functionToString ,
63- "[object AsyncGeneratorFunction]" : functionToString ,
64- "[object global]" : globalToString ,
65- "[object Window]" : globalToString
21+ undefined : String ,
22+ object : objectToString ,
23+ function : functionToString
6624} ;
6725
6826/**
6927 * Stringify a value recursively.
7028 */
71- export function toString ( value : any , space : string , next : Next ) {
29+ export const toString : ToString = ( value , space , next , key ) => {
7230 if ( value === null ) return "null" ;
7331
74- const typeOf = typeof value ;
75-
76- if ( PRIMITIVE_TYPES . hasOwnProperty ( typeOf ) ) {
77- return PRIMITIVE_TYPES [ typeOf as keyof typeof PRIMITIVE_TYPES ] (
78- value ,
79- space ,
80- next
81- ) ;
82- }
83-
84- // Handle buffer objects before object types (node < 6 was an object, node >= 6 is a `Uint8Array`).
85- if ( typeof ( Buffer as unknown ) === "function" && Buffer . isBuffer ( value ) ) {
86- return `new Buffer(${ next ( value . toString ( ) ) } )` ;
87- }
88-
89- // Use the internal object string to select stringify method.
90- const toString = Object . prototype . toString . call ( value ) ;
91-
92- // Convert objects into strings.
93- if ( OBJECT_TYPES . hasOwnProperty ( toString ) ) {
94- return OBJECT_TYPES [ toString as keyof typeof OBJECT_TYPES ] (
95- value ,
96- space ,
97- next
98- ) ;
99- }
100- }
32+ return PRIMITIVE_TYPES [ typeof value ] ( value , space , next , key ) ;
33+ } ;
0 commit comments