deno.land / x / rambda@v9.1.1 / files / waiting / flattenObject.js

flattenObject.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// It transforms object to object where each value is represented with its path.import { type } from '../../source/type.js'
export function flattenObjectHelper(obj, accumulator = []){ const willReturn = {} Object.keys(obj).forEach(key => { const typeIs = type(obj[ key ]) if (typeIs === 'Object'){ const [ flatResultValue, flatResultPath ] = flattenObjectHelper(obj[ key ], [ ...accumulator, key ]) willReturn[ flatResultPath.join('.') ] = flatResultValue
return } else if (accumulator.length > 0){ const finalKey = [ ...accumulator, key ].join('.') willReturn[ finalKey ] = obj[ key ]
return } willReturn[ key ] = obj[ key ] }) if (accumulator.length > 0) return [ willReturn, accumulator ]
return willReturn}
export function transformFlatObject(obj){ const willReturn = {}
const transformFlatObjectFn = objLocal => { const willReturnLocal = {} Object.keys(objLocal).forEach(key => { const typeIs = type(objLocal[ key ]) if (typeIs === 'Object'){ transformFlatObjectFn(objLocal[ key ])
return } willReturnLocal[ key ] = objLocal[ key ] willReturn[ key ] = objLocal[ key ] })
return willReturnLocal }
Object.keys(obj).forEach(key => { const typeIs = type(obj[ key ]) if (typeIs === 'Object'){ transformFlatObjectFn(obj[ key ], key)
return } willReturn[ key ] = obj[ key ] })
return willReturn}
export function flattenObject(obj){ const willReturn = {}
Object.keys(obj).forEach(key => { const typeIs = type(obj[ key ]) if (typeIs === 'Object'){ const flatObject = flattenObjectHelper(obj[ key ]) const transformed = transformFlatObject(flatObject)
Object.keys(transformed).forEach(keyTransformed => { willReturn[ `${ key }.${ keyTransformed }` ] = transformed[ keyTransformed ] }) } else { willReturn[ key ] = obj[ key ] } })
return willReturn}
rambda

Version Info

Tagged at
a year ago