main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
941 bytes
|
Line | |
---|
1 | import { pathOr, curry, mergeRight } from 'ramda';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Flattens a property path so that its fields are spread out into the provided object.
|
---|
5 | * It's like {@link RA.spreadPath|spreadPath}, but preserves object under the property path.
|
---|
6 | *
|
---|
7 | * @func flattenPath
|
---|
8 | * @memberOf RA
|
---|
9 | * @since {@link https://char0n.github.io/ramda-adjunct/1.19.0|v1.19.0}
|
---|
10 | * @category Object
|
---|
11 | * @typedef Idx = String | Int
|
---|
12 | * @sig [Idx] -> {k: v} -> {k: v}
|
---|
13 | * @param {!Array.<string|number>} path The property path to flatten
|
---|
14 | * @param {!Object} obj The provided object
|
---|
15 | * @return {!Object} The flattened object
|
---|
16 | * @see {@link RA.flattenProp|flattenProp}, {@link RA.spreadPath|spreadPath}
|
---|
17 | * @example
|
---|
18 | *
|
---|
19 | * RA.flattenPath(
|
---|
20 | * ['b1', 'b2'],
|
---|
21 | * { a: 1, b1: { b2: { c: 3, d: 4 } } }
|
---|
22 | * ); // => { a: 1, c: 3, d: 4, b1: { b2: { c: 3, d: 4 } } };
|
---|
23 | */
|
---|
24 | const flattenPath = curry((path, obj) =>
|
---|
25 | mergeRight(obj, pathOr({}, path, obj))
|
---|
26 | );
|
---|
27 |
|
---|
28 | export default flattenPath;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.