@szydlovski/deep-keys
v0.1.0
Published
retrieve deeply nested keys from objects
Downloads
6
Maintainers
Readme
deep-keys
Retrieve deeply nested keys from objects.
Usage
npm install @szydlovski/deep-keys
const { getDeepKeys, getDeepKeysAndPaths } = require('@szydlovski/deep-keys');
const source = {
firstName: 'John',
lastName: 'Doe',
address: {
streetName: 'Longview Road',
streetNumber: '12A',
country: {
name: 'United States',
code: 'US'
}
}
};
// retrieve final keys
getDeepKeys(source);
[
'firstName',
'lastName',
'address',
'streetName',
'streetNumber',
'country',
'name',
'code'
]
// retrieve full paths
getDeepKeys(source, true)
[
'firstName',
'lastName',
'address',
'address.streetName',
'address.streetNumber',
'address.country',
'address.country.name',
'address.country.code'
]
// returns [finalKey, fullPath]
getDeepKeysAndPaths(source);
[
[ 'firstName', 'firstName' ],
[ 'lastName', 'lastName' ],
[ 'address', 'address' ],
[ 'streetName', 'address.streetName' ],
[ 'streetNumber', 'address.streetNumber' ],
[ 'country', 'address.country' ],
[ 'name', 'address.country.name' ],
[ 'code', 'address.country.code' ]
]
API
getDeepKeys(source[, intermediate = false])
Retrieves deeply nested keys from objects.
If intermediate
is true, returns an array of full key paths (e.g. "address.country.code"
), otherwise an array of the final keys (e.g. "code"
).
getDeepKeysAndPaths(source)
Retrieves deeply nested keys and full key paths from objects.
Returns an array of arrays, each containing the final key of a property at index 0
and the full path at index 1
(e.g. ["code", "address.country.code"]
).
License
MIT License
Copyright (c) 2020 Kamil Szydlowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.