Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON
Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON.
$ npm install dot-wild
import * as dot from 'dot-wild';
/**
* Getter
*/
dot.get({ foo: { bar: 'baz' } }, 'foo.bar');
// => 'baz'
dot.get({ 'foo.bar': 'baz' }, 'foo\\.bar');
// => 'baz'
dot.get({ 'foo.bar': 'baz' }, 'notfound', 'default');
// => 'default'
const authorData = {
authors: [
{ username: 'tsuyoshiwada', profile: { age: 24 } },
{ username: 'sampleuser', profile: { age: 30 } },
{ username: 'foobarbaz', profile: { age: 33 } }
]
};
dot.get(authorData, 'authors.*.username');
// => ['tsuyoshiwada', 'sampleuser', 'foobarbaz']
dot.get(authorData, 'authors.*.profile.age');
// => [24, 30, 33]
/**
* Setter
*/
dot.set({ foo: { bar: 'baz' } }, 'foo.bar', 'newvalue');
// => { foo: { bar: 'newvalue' } }
dot.set([{ foo: {} }], '0.foo.bar.baz', 'value');
// => [{ foo: { bar: { baz: 'value' } } }]
const members = [
{ username: 'tsuyoshiwada', profile: { age: 24 } },
{ username: 'sampleuser', profile: { age: 30 } },
{ username: 'foobarbaz', profile: { age: 33 } }
];
dot.set(members, '*.id', 1);
// => [ { username: 'tsuyoshiwada', profile: { age: 24 }, id: 1 },
// { username: 'sampleuser', profile: { age: 30 }, id: 1 },
// { username: 'foobarbaz', profile: { age: 33 }, id: 1 } ]
/**
* Deleter
*/
dot.remove({ foo: { bar: 'baz' } }, 'foo.bar');
// => { foo: {} }
dot.remove(members, '*.profile');
// => [ { username: 'tsuyoshiwada' },
// { username: 'sampleuser' },
// { username: 'foobarbaz' } ]
/**
* Has
*/
dot.has({ foo: { bar: 'baz' } }, 'foo.bar');
dot.has(members, '*.profile.age');
// => true
dot.has({ foo: { bar: 'baz' } }, 'foo\\.bar');
dot.has(members, '*.notfound.key');
// => false
import * as dot from 'dot-wild';
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.flatten(postData);
// => {
// 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.expand({ 'foo.bar': 'baz' });
// => { foo: { bar: 'baz' } }
/**
* Collection helpers (forEach, map)
*/
dot.forEach(postData, 'data.posts.*.id', (value, key) => {
// value => 1, 2
// key => 0, 1
});
dot.map(postData, 'data.tags.*.name', (value, key) => {
return `${key}.${value}`;
});
// => ['0.tag 1', '1.tag 2']
/**
* Match path (helper method)
*/
dot.matchPath('foo.bar', 'foo.bar');
dot.matchPath('foo.*.bar.*.baz', 'foo.5.bar.1.baz');
// => true
/**
* Escape path string
*/
dot.escapePath('foo.bar');
// => 'foo\\.bar'
dot.escapePath('foo\\.bar.baz');
// => 'foo\\.bar\\.baz'
See API Documetation.
All methods return a new object or array. (immutable)
get(data, path, [value]): 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): void
map(data, path, iteratee): any[]
matchPath(pathA, pathB): boolean
escapePath(path): string
type: Object | any[]
Original object or array. Destructive operation is not performed.
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).
type: any
Value to set at path or optional default value to return from get.
Bugs, feature requests and comments are more than welcome in the issues.
v1.1.0
2017-04-10
forEach()
map()
FAQs
Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON
The npm package dot-wild receives a total of 4,842 weekly downloads. As such, dot-wild popularity was classified as popular.
We found that dot-wild demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.