1- 'use strict' ;
2- const isRegexp = require ( 'is-regexp' ) ;
3- const isObj = require ( 'is-obj' ) ;
4- const getOwnEnumPropSymbols = require ( 'get-own-enumerable-property-symbols' ) . default ;
1+ import isRegexp from 'is-regexp' ;
2+ import isObject from 'is-obj' ;
3+ import getOwnEnumPropSymbols from 'get-own-enumerable-property-symbols' ;
54
6- module . exports = ( input , options , pad ) => {
5+ export default function stringifyObject ( input , options , pad ) {
76 const seen = [ ] ;
87
98 return ( function stringify ( input , options = { } , pad = '' ) {
10- options . indent = options . indent || '\t' ;
9+ const indent = options . indent || '\t' ;
1110
1211 let tokens ;
13-
1412 if ( options . inlineCharacterLimit === undefined ) {
1513 tokens = {
16- newLine : '\n' ,
17- newLineOrSpace : '\n' ,
14+ newline : '\n' ,
15+ newlineOrSpace : '\n' ,
1816 pad,
19- indent : pad + options . indent
17+ indent : pad + indent ,
2018 } ;
2119 } else {
2220 tokens = {
23- newLine : '@@__STRINGIFY_OBJECT_NEW_LINE__@@' ,
24- newLineOrSpace : '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@' ,
21+ newline : '@@__STRINGIFY_OBJECT_NEW_LINE__@@' ,
22+ newlineOrSpace : '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@' ,
2523 pad : '@@__STRINGIFY_OBJECT_PAD__@@' ,
26- indent : '@@__STRINGIFY_OBJECT_INDENT__@@'
24+ indent : '@@__STRINGIFY_OBJECT_INDENT__@@' ,
2725 } ;
2826 }
2927
@@ -33,31 +31,32 @@ module.exports = (input, options, pad) => {
3331 }
3432
3533 const oneLined = string
36- . replace ( new RegExp ( tokens . newLine , 'g' ) , '' )
37- . replace ( new RegExp ( tokens . newLineOrSpace , 'g' ) , ' ' )
34+ . replace ( new RegExp ( tokens . newline , 'g' ) , '' )
35+ . replace ( new RegExp ( tokens . newlineOrSpace , 'g' ) , ' ' )
3836 . replace ( new RegExp ( tokens . pad + '|' + tokens . indent , 'g' ) , '' ) ;
3937
4038 if ( oneLined . length <= options . inlineCharacterLimit ) {
4139 return oneLined ;
4240 }
4341
4442 return string
45- . replace ( new RegExp ( tokens . newLine + '|' + tokens . newLineOrSpace , 'g' ) , '\n' )
43+ . replace ( new RegExp ( tokens . newline + '|' + tokens . newlineOrSpace , 'g' ) , '\n' )
4644 . replace ( new RegExp ( tokens . pad , 'g' ) , pad )
47- . replace ( new RegExp ( tokens . indent , 'g' ) , pad + options . indent ) ;
45+ . replace ( new RegExp ( tokens . indent , 'g' ) , pad + indent ) ;
4846 } ;
4947
50- if ( seen . indexOf ( input ) !== - 1 ) {
48+ if ( seen . includes ( input ) ) {
5149 return '"[Circular]"' ;
5250 }
5351
54- if ( input === null ||
55- input === undefined ||
56- typeof input === 'number' ||
57- typeof input === 'boolean' ||
58- typeof input === 'function' ||
59- typeof input === 'symbol' ||
60- isRegexp ( input )
52+ if (
53+ input === null
54+ || input === undefined
55+ || typeof input === 'number'
56+ || typeof input === 'boolean'
57+ || typeof input === 'function'
58+ || typeof input === 'symbol'
59+ || isRegexp ( input )
6160 ) {
6261 return String ( input ) ;
6362 }
@@ -73,10 +72,10 @@ module.exports = (input, options, pad) => {
7372
7473 seen . push ( input ) ;
7574
76- const ret = '[' + tokens . newLine + input . map ( ( el , i ) => {
77- const eol = input . length - 1 === i ? tokens . newLine : ',' + tokens . newLineOrSpace ;
75+ const returnValue = '[' + tokens . newline + input . map ( ( element , i ) => {
76+ const eol = input . length - 1 === i ? tokens . newline : ',' + tokens . newlineOrSpace ;
7877
79- let value = stringify ( el , options , pad + options . indent ) ;
78+ let value = stringify ( element , options , pad + indent ) ;
8079 if ( options . transform ) {
8180 value = options . transform ( input , i , value ) ;
8281 }
@@ -86,39 +85,43 @@ module.exports = (input, options, pad) => {
8685
8786 seen . pop ( ) ;
8887
89- return expandWhiteSpace ( ret ) ;
88+ return expandWhiteSpace ( returnValue ) ;
9089 }
9190
92- if ( isObj ( input ) ) {
93- let objKeys = Object . keys ( input ) . concat ( getOwnEnumPropSymbols ( input ) ) ;
91+ if ( isObject ( input ) ) {
92+ let objectKeys = [
93+ ...Object . keys ( input ) ,
94+ ...getOwnEnumPropSymbols . default ( input ) ,
95+ ] ;
9496
9597 if ( options . filter ) {
96- objKeys = objKeys . filter ( el => options . filter ( input , el ) ) ;
98+ // eslint-disable-next-line unicorn/no-array-callback-reference, unicorn/no-array-method-this-argument
99+ objectKeys = objectKeys . filter ( element => options . filter ( input , element ) ) ;
97100 }
98101
99- if ( objKeys . length === 0 ) {
102+ if ( objectKeys . length === 0 ) {
100103 return '{}' ;
101104 }
102105
103106 seen . push ( input ) ;
104107
105- const ret = '{' + tokens . newLine + objKeys . map ( ( el , i ) => {
106- const eol = objKeys . length - 1 === i ? tokens . newLine : ',' + tokens . newLineOrSpace ;
107- const isSymbol = typeof el === 'symbol' ;
108- const isClassic = ! isSymbol && / ^ [ a - z $ _ ] [ a - z $ _ 0 - 9 ] * $ / i. test ( el ) ;
109- const key = isSymbol || isClassic ? el : stringify ( el , options ) ;
108+ const returnValue = '{' + tokens . newline + objectKeys . map ( ( element , i ) => {
109+ const eol = objectKeys . length - 1 === i ? tokens . newline : ',' + tokens . newlineOrSpace ;
110+ const isSymbol = typeof element === 'symbol' ;
111+ const isClassic = ! isSymbol && / ^ [ a - z $ _ ] [ $ \w ] * $ / i. test ( element ) ;
112+ const key = isSymbol || isClassic ? element : stringify ( element , options ) ;
110113
111- let value = stringify ( input [ el ] , options , pad + options . indent ) ;
114+ let value = stringify ( input [ element ] , options , pad + indent ) ;
112115 if ( options . transform ) {
113- value = options . transform ( input , el , value ) ;
116+ value = options . transform ( input , element , value ) ;
114117 }
115118
116119 return tokens . indent + String ( key ) + ': ' + value + eol ;
117120 } ) . join ( '' ) + tokens . pad + '}' ;
118121
119122 seen . pop ( ) ;
120123
121- return expandWhiteSpace ( ret ) ;
124+ return expandWhiteSpace ( returnValue ) ;
122125 }
123126
124127 input = String ( input ) . replace ( / [ \r \n ] / g, x => x === '\n' ? '\\n' : '\\r' ) ;
@@ -131,4 +134,4 @@ module.exports = (input, options, pad) => {
131134 input = input . replace ( / \\ ? ' / g, '\\\'' ) ;
132135 return `'${ input } '` ;
133136 } ) ( input , options , pad ) ;
134- } ;
137+ }
0 commit comments