Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Retrieve all keys and nested keys from objects and arrays of objects.
npm install --save deeks
Example:
let keys = require('deeks'),
docPath = require('doc-path');
let generatedKeys = keys.deepKeys({
make: 'Nissan',
model: 'GT-R',
trim: 'NISMO',
specifications: [
{mileage: 10},
{cylinders: 6}
]
}, {
expandArrayObjects: true,
ignoreEmptyArraysWhenExpanding: true
});
// => ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
generatedKeys.forEach((key) =>
console.log(
docPath.evaluatePath(key)
)
)
// Console Output:
// Nissan
// GT-R
// NISMO
// 10
// 6
keys.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 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']
Returns: Array[String]
Example: ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers']
keys.deepKeysFromList(array)
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: 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']
Returns: Array[Array[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.
Here's an example of how this works:
const path = require('doc-path'),
keys = require('deeks');
let car = {
make: 'Nissan',
model: 'GT-R',
trim: 'NISMO',
specifications: {
mileage: 10,
cylinders: '6'
}
},
carKeys = keys.deepKeys(car);
for(let keyPath of carKeys) {
// Clear all values
path.setPath(car, keyPath, '');
}
$ npm test
Note: This requires mocha
, should
, and underscore
.
To see test coverage, please run:
$ npm run coverage
Current Coverage is:
Statements : 100% ( 46/46 )
Branches : 100% ( 30/30 )
Functions : 100% ( 9/9 )
Lines : 100% ( 45/45 )
FAQs
Retrieve all keys and nested keys from objects and arrays of objects.
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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.