Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miguelcastillo/split-keypath
Splits strings into an array of keys used for reading nested data structures
https://github.com/miguelcastillo/split-keypath
Last synced: 9 days ago
JSON representation
Splits strings into an array of keys used for reading nested data structures
- Host: GitHub
- URL: https://github.com/miguelcastillo/split-keypath
- Owner: MiguelCastillo
- Created: 2016-03-23T01:37:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-03-23T04:13:07.000Z (over 8 years ago)
- Last Synced: 2024-10-19T18:45:57.558Z (18 days ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# split-keypath
> Splits strings into an array of keys used for reading nested data structuressplit-keypath is a method that takes a string as an input and generates an array of keys that can be used for reading values from a deeply nested object. The algorithm supports extracting array keys, which is how you can specify arbitrary keys. Please see below for examples.
## Install
```
$ npm install split-keypath
```> The npm package has a bundle for the browser
## Examples
``` javascript
import splitKeypath from "split-keypath";
var result = splitKeypath("hello.world");
// Result is ["hello", "world"]
`````` javascript
import splitKeypath from "split-keypath";
var result = splitKeypath("hello[0].world[some really long string. with non ascii chars.]");
// Result is ["hello", "0", "world", "some really long string. with non ascii chars."];
```Practical use that reads a value from a somewhat deeply nested object hierarchy
``` javascript
import splitKeypath from "split-keypath";function readDeepValue(input, keypath) {
return splitKeypath(keypath).reduce((nested, key) => nested[key], input);
}var input = {
some: {
deep: [
{
key: 42
}
]
}
};var result = readDeepValue(input, "some.deep[0].key");
// Result is 42
```## License
MIT