Add flattenObject, unflattenObject
Tagged and tested. Quite complex methods btw.
This commit is contained in:
27
snippets/flattenObject.md
Normal file
27
snippets/flattenObject.md
Normal file
@ -0,0 +1,27 @@
|
||||
### flattenObject
|
||||
|
||||
Flatten an object with the paths for keys.
|
||||
|
||||
Use recursion.
|
||||
Use `Object.keys(obj)` combined with `Array.reduce()` to convert every leaf node to a flattened path node.
|
||||
If the value of a key is an object, the function calls itself with the appropriate `prefix` to create the path using `Object.assign()`.
|
||||
Otherwise, it adds the appropriate prefixed key-value pair to the accumulator object.
|
||||
You should always omit the second argument, `prefix`, unless you want every key to have a prefix.
|
||||
|
||||
```js
|
||||
const flattenObject = (obj, prefix = '') =>
|
||||
Object.keys(obj).reduce((acc, k) => {
|
||||
const pre = prefix.length ? (prefix + '.') : '';
|
||||
if (typeof obj[k] === 'object')
|
||||
Object.assign(
|
||||
acc,
|
||||
flattenObject(obj[k], pre + k)
|
||||
);
|
||||
else acc[pre + k] = obj[k];
|
||||
return acc;
|
||||
}, {});
|
||||
```
|
||||
|
||||
```js
|
||||
flattenObject({ a: { b: { c: 1 } }, d: 1 }); // { 'a.b.c': 1, d: 1 }
|
||||
```
|
||||
32
snippets/unflattenObject.md
Normal file
32
snippets/unflattenObject.md
Normal file
@ -0,0 +1,32 @@
|
||||
### unflattenObject
|
||||
|
||||
Unlatten an object with the paths for keys.
|
||||
|
||||
Use `Object.keys(obj)` combined with `Array.reduce()` to convert flattened path node to a leaf node.
|
||||
If the value of a key contains a dot delimiter (`.`), use `Array.split('.')`, string transformations and `JSON.parse()` to create an object, then `Object.assign()` to create the leaf node.
|
||||
Otherwise, add the appropriate key-value pair to the accumulator object.
|
||||
|
||||
```js
|
||||
const unflattenObject = obj =>
|
||||
Object.keys(obj).reduce((acc, k) => {
|
||||
if (k.indexOf('.') !== -1) {
|
||||
const keys = k.split('.');
|
||||
Object.assign(
|
||||
acc,
|
||||
JSON.parse(
|
||||
'{' +
|
||||
keys
|
||||
.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`))
|
||||
.join('') +
|
||||
obj[k] +
|
||||
'}'.repeat(keys.length)
|
||||
)
|
||||
);
|
||||
} else acc[k] = obj[k];
|
||||
return acc;
|
||||
}, {});
|
||||
```
|
||||
|
||||
```js
|
||||
unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
|
||||
```
|
||||
@ -65,6 +65,7 @@ findLast:array
|
||||
findLastIndex:array,function
|
||||
findLastKey:object,function
|
||||
flatten:array
|
||||
flattenObject:object,recursion
|
||||
flip:adapter,function
|
||||
forEachRight:array,function
|
||||
formatDuration:date,math,string,utility
|
||||
@ -251,6 +252,7 @@ truncateString:string
|
||||
truthCheckCollection:object,logic,array
|
||||
unary:adapter,function
|
||||
unescapeHTML:string,browser
|
||||
unflattenObject:object,advanced
|
||||
unfold:function,array
|
||||
union:array,math
|
||||
unionBy:array,function
|
||||
|
||||
12
test/flattenObject/flattenObject.js
Normal file
12
test/flattenObject/flattenObject.js
Normal file
@ -0,0 +1,12 @@
|
||||
const flattenObject = (obj, prefix = '') =>
|
||||
Object.keys(obj).reduce((acc, k) => {
|
||||
const pre = prefix.length ? (prefix + '.') : '';
|
||||
if (typeof obj[k] === 'object')
|
||||
Object.assign(
|
||||
acc,
|
||||
flattenObject(obj[k], pre + k)
|
||||
);
|
||||
else acc[pre + k] = obj[k];
|
||||
return acc;
|
||||
}, {});
|
||||
module.exports = flattenObject;
|
||||
14
test/flattenObject/flattenObject.test.js
Normal file
14
test/flattenObject/flattenObject.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
const test = require('tape');
|
||||
const flattenObject = require('./flattenObject.js');
|
||||
|
||||
test('Testing flattenObject', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof flattenObject === 'function', 'flattenObject is a Function');
|
||||
t.deepEqual(flattenObject({ a: { b: { c: 1 } }, d: 1 }), { 'a.b.c': 1, d: 1 }, 'Flattens an object with the paths for keys');
|
||||
//t.deepEqual(flattenObject(args..), 'Expected');
|
||||
//t.equal(flattenObject(args..), 'Expected');
|
||||
//t.false(flattenObject(args..), 'Expected');
|
||||
//t.throws(flattenObject(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
1796
test/testlog
1796
test/testlog
File diff suppressed because it is too large
Load Diff
19
test/unflattenObject/unflattenObject.js
Normal file
19
test/unflattenObject/unflattenObject.js
Normal file
@ -0,0 +1,19 @@
|
||||
const unflattenObject = obj =>
|
||||
Object.keys(obj).reduce((acc, k) => {
|
||||
if (k.indexOf('.') !== -1) {
|
||||
const keys = k.split('.');
|
||||
Object.assign(
|
||||
acc,
|
||||
JSON.parse(
|
||||
'{' +
|
||||
keys
|
||||
.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`))
|
||||
.join('') +
|
||||
obj[k] +
|
||||
'}'.repeat(keys.length)
|
||||
)
|
||||
);
|
||||
} else acc[k] = obj[k];
|
||||
return acc;
|
||||
}, {});
|
||||
module.exports = unflattenObject;
|
||||
14
test/unflattenObject/unflattenObject.test.js
Normal file
14
test/unflattenObject/unflattenObject.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
const test = require('tape');
|
||||
const unflattenObject = require('./unflattenObject.js');
|
||||
|
||||
test('Testing unflattenObject', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof unflattenObject === 'function', 'unflattenObject is a Function');
|
||||
t.deepEqual(unflattenObject({ 'a.b.c': 1, d: 1 }), { a: { b: { c: 1 } }, d: 1 }, 'Unflattens an object with the paths for keys');
|
||||
//t.deepEqual(unflattenObject(args..), 'Expected');
|
||||
//t.equal(unflattenObject(args..), 'Expected');
|
||||
//t.false(unflattenObject(args..), 'Expected');
|
||||
//t.throws(unflattenObject(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
Reference in New Issue
Block a user