dot-wild
Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON.
Table of Contents
Install
$ npm install dot-wild --save # or $ yarn add dot-wild
Usage
Basic
; /** * Getter */ dot;// => 'baz' dot;// => 'baz' dot;// => 'default' const authorData = authors: username: 'tsuyoshiwada' profile: age: 24 username: 'sampleuser' profile: age: 30 username: 'foobarbaz' profile: age: 33 ; dot;// => ['tsuyoshiwada', 'sampleuser', 'foobarbaz'] dot;// => [24, 30, 33] /** * Setter */dot;// => { foo: { bar: 'newvalue' } } dot;// => [{ foo: { bar: { baz: 'value' } } }] const members = username: 'tsuyoshiwada' profile: age: 24 username: 'sampleuser' profile: age: 30 username: 'foobarbaz' profile: age: 33 ; dot;// => [ { username: 'tsuyoshiwada', profile: { age: 24 }, id: 1 },// { username: 'sampleuser', profile: { age: 30 }, id: 1 },// { username: 'foobarbaz', profile: { age: 33 }, id: 1 } ] /** * Deleter */dot;// => { foo: {} } dot;// => [ { username: 'tsuyoshiwada' },// { username: 'sampleuser' },// { username: 'foobarbaz' } ] /** * Has */dot;dot;// => true dot;dot;// => false
Advanced
; const postData = text: 'ok' code: 200 data: posts: id: 1 title: 'post 1' id: 2 title: 'post 2' tags: id: 1 name: 'tag 1' id: 2 name: 'tag 2' ; /** * Flatten values */dot;// => {// text: 'ok',// code: 200,// 'data.posts.0.id': 1,// 'data.posts.0.title': 'post 1',// 'data.posts.1.id': 2,// 'data.posts.1.title': 'post 2',// 'data.tags.0.id': 1,// 'data.tags.0.name': 'tag 1',// 'data.tags.1.id': 2,// 'data.tags.1.name': 'tag 2'// } /** * Expand values */dot;// => { foo: { bar: 'baz' } } /** * Collection helpers (forEach, map) */dot; dot;// => ['tag 1 === tag 1 (name)', 'tag 2 === tag 2 (name)'] /** * String to Tokens */dot;// => ['foo', 'bar', 'baz'] dot;// => [ 'foo', '1', '2', '*', 'bar.*', 'baz' ] /** * Match path (helper method) */dot;dot;// => true /** * Escape path string */dot;// => 'foo\\.bar' dot;// => 'foo\\.bar\\.baz' /** * Build path from Tokens like array */dot;// => 'foo.bar.baz' dot;// => '1.2.3.foo.bar\\.baz' /** * Check contains wildcard token */dot;dot;// => true dot;dot;// => false
API
See API Documetation.
All methods return a new object or array. (immutable)
get(data, path, [value, options]): Object | any[]
set(data, path, value): Object | any[]
remove(data, path): Object | any[]
has(data, path): boolean
flatten(data): Object
expand(data): Object | any[]
forEach(data, path, iteratee, options): void
map(data, path, iteratee, options): any[]
tokenize(path): string[]
matchPath(pathA, pathB): boolean
escapePath(path): string
buildPath(tokens)[]): string
containWilcardToken(path): boolean
data
type: Object | any[]
Original object or array. Destructive operation is not performed.
path
type: string
Path of the property in JSON object. Use the .
to select properties.
Separator in path syntax can be escaped by using the \\.
.
And, you can match arrays by using *
(wildcard).
value
type: any
Value to set at path or optional default value to return from get.
tokens
type: (string | number)[]
An array of tokens that make up the path.
options
This is an option for Getter method. (get
, forEach
. and map
)
iterateObject: true; // If it is `true`, it will enumerate the values of the object when using wildcards iterateArray: true; // If it is `true`, it will enumerate the values of the array when using wildcards
Contribute
- Fork it!
- Create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request :D
Bugs, feature requests and comments are more than welcome in the issues.