-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): added to and from object utils
- Loading branch information
1 parent
8796aef
commit 7989b82
Showing
3 changed files
with
19 additions
and
0 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
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,6 @@ | ||
import isArray from '../type/is-array'; | ||
|
||
export default function fromPath<TValue extends object>(value: TValue, path: string | PropertyKey[]): object | undefined { | ||
const nodes = isArray(path) ? path : path.split('/'); | ||
return nodes.reduce((branch, node) => (branch as any)?.[node], value); | ||
} |
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,11 @@ | ||
export default function toPath(nodes: PropertyKey[]): string { | ||
return nodes.reduceRight((path, node) => { | ||
const nodeStr = node.toString(); | ||
|
||
const segment = isNaN(parseInt(nodeStr, 10)) | ||
? `/${node.toString()}` | ||
: `[${node.toString()}]`; | ||
|
||
return segment + path.toString(); | ||
}, '').toString(); | ||
} |