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.
deeks
Advanced tools
Readme
Retrieve all keys and nested keys from objects and arrays of objects.
npm install --save deeks
Example:
const { deepKeys } = require('deeks');
// Alternatively:
// import { deepKeys } from 'deeks';
let generatedKeys = deepKeys({
make: 'Nissan',
model: 'GT-R',
trim: 'NISMO',
specifications: [
{mileage: 10},
{cylinders: 6}
]
}, {
expandArrayObjects: true,
ignoreEmptyArraysWhenExpanding: true
});
// => ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
deepKeys(object, options)
Returns all keys in an object, even if they're nested several layers deep.
The array of keys that is returned can then be used with the
doc-path
module to get and set values
at a specific key path.
Options (optional):
Boolean
(Default false
) - Should array indexes be used as keys in the generated path?
[
{
"list": [
{
"a": 1
},
{
"a": 2
}
]
}
]
false
results in: ['list.a']
true
results in: ['list.0.a', 'list.1.a']
Boolean
(Default: true
) - Should nested objects appearing in the provided object also be expanded, such asthat keys appearing in those objects are extracted and included in the returned key path list?
{
"make": "Nissan",
"model": "Murano",
"year": 2013,
"specifications": {
"mileage": 7106,
"trim": "S AWD"
}
}
false
results in: ['make', 'model', 'year', 'specifications']
true
results in: ['make', 'model', 'year', 'specifications.mileage', 'specifications.trim']
Boolean
(Default: false
) - Should objects appearing in arrays in the provided
object also be expanded, such that keys appearing in those objects are extracted and
included in the returned key path list?
{
"make": "Nissan",
"model": "GT-R",
"trim": "NISMO",
"specifications": [
{"mileage": 10},
{"cylinders": 6}
]
}
false
results in: ['make', 'model', 'trim', 'specifications']
true
results in: ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
Boolean
(Default: false
) - Should empty array keys be ignored when expanding array objects?
expandArrayObjects
.{
"features": [ {"name": "A/C" }],
"rebates": []
}
false
results in: ['features.name', 'rebates']
true
results in: ['features.name']
Boolean
(Default: false
) - Should .
characters that appear in keys be escaped with a preceding \
character?
{
"a.a": "1",
"a.b": {
"c": "2",
"c.d": "3"
}
}
false
results in: ['a.a', 'a.b.c', 'a.b.c.d']
true
results in: ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d']
Boolean
(Default: false
) - Should key paths for empty arrays be ignored in the generated key list?
{
"a": {
"b": [],
"c": {
"f": 4,
"e": []
}
},
"b": []
}
false
results in ['a.b', 'a.c.f', 'a.c.e', 'b']
true
results in ['a.c.f']
Returns: string[]
Example: ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers']
deepKeysFromList(array, options)
Returns all keys in each object in the array, even if the keys are nested
several layers deep in each of the documents. These can also be used with the
doc-path
module.
Options (optional):
Boolean
(Default: true
) - Should nested objects appearing in the provided object also be expanded, such asthat keys appearing in those objects are extracted and included in the returned key path list?
{
"make": "Nissan",
"model": "Murano",
"year": 2013,
"specifications": {
"mileage": 7106,
"trim": "S AWD"
}
}
false
results in: ['make', 'model', 'year', 'specifications']
true
results in: ['make', 'model', 'year', 'specifications.mileage', 'specifications.trim']
Boolean
(Default: false
) - Should objects appearing in arrays in the provided
object also be expanded, such that keys appearing in those objects are extracted and
included in the returned key path list?
{
"make": "Nissan",
"model": "GT-R",
"trim": "NISMO",
"specifications": [
{"mileage": 10},
{"cylinders": 6}
]
}
false
results in: ['make', 'model', 'trim', 'specifications']
true
results in: ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
Boolean
(Default: false
) - Should empty array keys be ignored when expanding array objects?
expandArrayObjects
.[
{ "features": [ {"name": "A/C" }] },
{ "features": [] }
]
false
results in: [ ['features.name', 'features'] ]
true
results in: [ ['features.name'] ]
Boolean
(Default: false
) - Should .
characters that appear in keys be escaped with a preceding \
character.
[
{
"a.a": "1",
"a.b": {
"c": "2",
"c.d": "3"
}
}
]
false
results in: [ ['a.a', 'a.b.c', 'a.b.c.d'] ]
true
results in: [ ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d'] ]
Boolean
(Default: false
) - Should key paths for empty arrays be ignored in the generated key list?
[
{
"a": {
"b": [],
"c": {
"f": 4,
"e": []
}
},
"b": []
}
]
false
results in [ ['a.b', 'a.c.f', 'a.c.e', 'b'] ]
true
results in [ ['a.c.f'] ]
Returns: string[][]
Example: [ ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers'] ]
This module integrates really nicely with the
doc-path
module, which allows
the programmatic getting and setting of key paths produced by this module.
Additionally, doc-path@>=3
works with the keys returned when the escapeNestedDots
option is specified.
Here's an example of how this works:
const path = require('doc-path'),
{ deepKeys } = require('deeks');
let car = {
make: 'Nissan',
model: 'GT-R',
trim: 'NISMO',
specifications: {
mileage: 10,
cylinders: '6'
}
},
carKeys = deepKeys(car);
for(let keyPath of carKeys) {
// Clear all values
path.setPath(car, keyPath, '');
}
$ npm test
Note: This requires mocha
.
To see test coverage, please run:
$ npm run coverage
FAQs
Retrieve all keys and nested keys from objects and arrays of objects.
The npm package deeks receives a total of 242,685 weekly downloads. As such, deeks popularity was classified as popular.
We found that deeks 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.