-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #961 from notoraptor/db-flatten-params
[dashboard/db page] DIsplay parameters in separated flattened columns after ID column
- Loading branch information
Showing
3 changed files
with
123 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { flattenObject } from '../utils/flattenObject'; | ||
|
||
test('test flatten object', () => { | ||
const input = { | ||
a: 1, | ||
b: { | ||
ba: 'world', | ||
bb: 1.5, | ||
bc: { | ||
bd: { | ||
'a key': 333, | ||
'another key': { | ||
x: -1, | ||
y: true, | ||
}, | ||
}, | ||
}, | ||
bd: { | ||
bff: 'abcdefgh', | ||
orion: 'benchmarks', | ||
}, | ||
be: 100, | ||
bf: 'bf', | ||
}, | ||
c: [10, '2a'], | ||
d: 'hello', | ||
}; | ||
const output = flattenObject(input); | ||
console.log(output); | ||
const keys = Object.keys(output); | ||
expect(keys.length).toBe(12); | ||
expect(output.hasOwnProperty('a')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.ba')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bb')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bc.bd.a key')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bc.bd.another key.x')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bc.bd.another key.y')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bd.bff')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bd.orion')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.be')).toBeTruthy(); | ||
expect(output.hasOwnProperty('b.bf')).toBeTruthy(); | ||
expect(output.hasOwnProperty('c')).toBeTruthy(); | ||
expect(output.hasOwnProperty('d')).toBeTruthy(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function _flattenObject(inObj, outObj, prefix = '') { | ||
if (prefix.length) { | ||
prefix += '.'; | ||
} | ||
for (let key of Object.keys(inObj)) { | ||
const value = inObj[key]; | ||
if (value.constructor === Object) { | ||
_flattenObject(value, outObj, prefix + key); | ||
} else { | ||
outObj[prefix + key] = value; | ||
} | ||
} | ||
} | ||
|
||
export function flattenObject(obj, prefix = '') { | ||
const output = {}; | ||
_flattenObject(obj, output, prefix); | ||
return output; | ||
} |