Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
doc-path
Advanced tools
Readme
This module will take paths in documents which can include nested paths specified by '.'s and can evaluate the path to a value, or can set the value at that path depending on the function called.
$ npm install doc-path
let path = require('doc-path');
document
- Object
- A JSON document that will be iterated over.key
- String
- A path to the existing key whose value will be returned.
a.b
) then be sure to escape the dot with a blackslash (eg. a\\.b
).If the key does not exist, undefined
is returned.
If the object's structure is extremely deep, then an error may be thrown if the maximum call stack size is exceeded while traversing the object.
const path = require('doc-path');
let document = {
Make: 'Nissan',
Model: 'Murano',
Year: '2013',
Specifications: {
Mileage: '7106',
Trim: 'S AWD'
},
Features: [
{
feature: 'A/C',
packages: [
{name: 'Base'},
{name: 'Premium'}
]
},
{
feature: 'Radio',
packages: [
{name: 'Convenience'},
{name: 'Premium'}
]
}
]
};
console.log(path.evaluatePath(document, 'Make'));
// => 'Nissan'
console.log(path.evaluatePath(document, 'Specifications.Mileage'));
// => '7106'
console.log(path.evaluatePath(document, 'Features.feature'));
// => [ 'A/C', 'Radio' ]
console.log(path.evaluatePath(document, 'Features.packages.name'));
// => [ ['Base', 'Premium'], ['Convenience', 'Premium'] ]
document
- Object
- A JSON document that will be iterated over.key
- String
- A path to the existing key whose value will be set.
a.b
) then be sure to escape the dot with a blackslash (eg. a\\.b
).value
- *
- The value that will be set at the given key.If the key does not exist, then the object will be built up to have that path. If no document is provided, an error will be thrown. If the object's structure is extremely deep, then an error may be thrown if the maximum call stack size is exceeded while traversing the object.
const path = require('doc-path');
let document = {
Make: 'Nissan',
Features: [
{ feature: 'A/C' }
]
};
console.log(path.setPath(document, 'Color.Interior', 'Tan'));
/*
{
Make: 'Nissan',
Features: [
{ feature: 'A/C' }
]
Color: {
Interior: 'Tan'
}
}
*/
console.log(path.setPath(document, 'StockNumber', '34567'));
/*
{
Make: 'Nissan',
Features: [
{ feature: 'A/C' }
]
Color: {
Interior: 'Tan'
},
StockNumber: '34567'
}
*/
console.log(path.setPath(document, 'Features.cost', '$0 (Standard)'));
/*
{
Make: 'Nissan',
Features: [
{
feature: 'A/C',
cost: '$0 (Standard)'
}
]
Color: {
Interior: 'Tan'
},
StockNumber: '34567'
}
*/
$ npm test
Note: This requires mocha
, should
, async
, and underscore
.
To see test coverage, please run:
$ npm run coverage
Current Coverage is:
Statements : 100% ( 33/33 )
Branches : 100% ( 24/24 )
Functions : 100% ( 3/3 )
Lines : 100% ( 29/29 )
.
characters (as of v3.0.0)FAQs
A document path library for Node
The npm package doc-path receives a total of 244,277 weekly downloads. As such, doc-path popularity was classified as popular.
We found that doc-path demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.