Travis build: 1581

This commit is contained in:
30secondsofcode
2018-02-07 09:31:31 +00:00
parent 3719814f25
commit 12238b8e78
4 changed files with 102 additions and 10 deletions

View File

@ -319,6 +319,7 @@ average(1, 2, 3);
* [`equals`](#equals-)
* [`findKey`](#findkey)
* [`findLastKey`](#findlastkey)
* [`flattenObject`](#flattenobject)
* [`forOwn`](#forown)
* [`forOwnRight`](#forownright)
* [`functions`](#functions)
@ -341,6 +342,7 @@ average(1, 2, 3);
* [`size`](#size)
* [`transform`](#transform)
* [`truthCheckCollection`](#truthcheckcollection)
* [`unflattenObject`](#unflattenobject-)
</details>
@ -5541,6 +5543,38 @@ findLastKey(
<br>[⬆ Back to top](#table-of-contents)
### 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;
}, {});
```
<details>
<summary>Examples</summary>
```js
flattenObject({ a: { b: { c: 1 } }, d: 1 }); // { 'a.b.c': 1, d: 1 }
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### forOwn
Iterates over all own properties of an object, running a callback for each one.
@ -6153,6 +6187,45 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
<br>[⬆ Back to top](#table-of-contents)
### unflattenObject ![advanced](/advanced.svg)
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;
}, {});
```
<details>
<summary>Examples</summary>
```js
unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
```
</details>
<br>[⬆ Back to top](#table-of-contents)
---
## 📜 String

File diff suppressed because one or more lines are too long

View File

@ -11,12 +11,8 @@ You should always omit the second argument, `prefix`, unless you want every key
```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)
);
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;
}, {});

View File

@ -15,9 +15,7 @@ const unflattenObject = obj =>
acc,
JSON.parse(
'{' +
keys
.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`))
.join('') +
keys.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`)).join('') +
obj[k] +
'}'.repeat(keys.length)
)