@@ -16,7 +16,7 @@ interface JSONFlattenOptions {
16
16
export type JSONDataType = string | number | boolean | unknown [ ] | Record < string , unknown > | null ;
17
17
18
18
export interface JSONFlattenReturnType extends JSONFlattenOptions {
19
- content : string ;
19
+ content : string | number | null | boolean ;
20
20
level : number ;
21
21
path : string ;
22
22
}
@@ -45,17 +45,16 @@ export function jsonFlatten(
45
45
const dataType = getDataType ( data ) ;
46
46
47
47
if ( dataType === 'array' ) {
48
- const inner = ( data as JSONDataType [ ] )
49
- . map ( ( item , idx , arr ) =>
48
+ const inner = arrFlat (
49
+ ( data as JSONDataType [ ] ) . map ( ( item , idx , arr ) =>
50
50
jsonFlatten ( item , `${ path } [${ idx } ]` , level + 1 , {
51
51
index : idx ,
52
52
showComma : idx !== arr . length - 1 ,
53
53
length,
54
54
type,
55
55
} ) ,
56
- )
57
- // No flat, for compatibility.
58
- . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ;
56
+ ) ,
57
+ ) as JSONFlattenReturnType [ ] ;
59
58
return [
60
59
jsonFlatten ( '[' , path , level , {
61
60
showComma : false ,
@@ -73,8 +72,8 @@ export function jsonFlatten(
73
72
) ;
74
73
} else if ( dataType === 'object' ) {
75
74
const keys = Object . keys ( data as Record < string , JSONDataType > ) ;
76
- const inner = keys
77
- . map ( ( objKey , idx , arr ) =>
75
+ const inner = arrFlat (
76
+ keys . map ( ( objKey , idx , arr ) =>
78
77
jsonFlatten (
79
78
( data as Record < string , JSONDataType > ) [ objKey ] ,
80
79
objKey . includes ( '.' ) ? `${ path } ["${ objKey } "]` : `${ path } .${ objKey } ` ,
@@ -86,9 +85,8 @@ export function jsonFlatten(
86
85
type,
87
86
} ,
88
87
) ,
89
- )
90
- // No flat, for compatibility.
91
- . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ;
88
+ ) ,
89
+ ) as JSONFlattenReturnType [ ] ;
92
90
return [
93
91
jsonFlatten ( '{' , path , level , {
94
92
showComma : false ,
@@ -103,26 +101,35 @@ export function jsonFlatten(
103
101
) ;
104
102
}
105
103
106
- const output = Object . entries ( {
107
- content : data ,
108
- level,
109
- key,
110
- index,
111
- path,
112
- showComma,
113
- length,
114
- type,
115
- } ) . reduce ( ( acc , [ key , value ] ) => {
116
- if ( value !== undefined ) {
117
- return {
118
- ...acc ,
119
- [ key ] : value ,
120
- } ;
121
- }
122
- return acc ;
123
- } , { } ) as JSONFlattenReturnType ;
104
+ return [
105
+ {
106
+ content : data as JSONFlattenReturnType [ 'content' ] ,
107
+ level,
108
+ key,
109
+ index,
110
+ path,
111
+ showComma,
112
+ length,
113
+ type,
114
+ } ,
115
+ ] ;
116
+ }
124
117
125
- return [ output ] ;
118
+ export function arrFlat < T extends unknown [ ] > ( arr : T ) : unknown [ ] {
119
+ if ( typeof Array . prototype . flat === 'function' ) {
120
+ return arr . flat ( ) ;
121
+ }
122
+ const stack = [ ...arr ] ;
123
+ const result = [ ] ;
124
+ while ( stack . length ) {
125
+ const first = stack . shift ( ) ;
126
+ if ( Array . isArray ( first ) ) {
127
+ stack . unshift ( ...first ) ;
128
+ } else {
129
+ result . push ( first ) ;
130
+ }
131
+ }
132
+ return result ;
126
133
}
127
134
128
135
export function cloneDeep < T extends unknown > ( source : T , hash = new WeakMap ( ) ) : T {
0 commit comments