npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deeks

v3.1.1

Published

Retrieve all keys and nested keys from objects and arrays of objects.

Downloads

1,833,037

Readme

deeks - Deep Object Key Extraction

NPM version Downloads Minzipped Size Build Status Coverage Status Typings

Retrieve all keys and nested keys from objects and arrays of objects.

Installing

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']

API

deepKeys

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):

  • arrayIndexesAsKeys - Boolean (Default false) - Should array indexes be used as keys in the generated path?
    • Example:
    [
    	{
    		"list": [
    			{
    				"a": 1
    			},
    			{
    				"a": 2
    			}
    		]
    	}
    ]
    • arrayIndexesAsKeys = false results in: ['list.a']
    • arrayIndexesAsKeys = true results in: ['list.0.a', 'list.1.a']
  • expandNestedObjects - 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?
    • Example:
    {
    	"make": "Nissan",
    	"model": "Murano",
    	"year": 2013,
    	"specifications": {
    		"mileage": 7106,
    		"trim": "S AWD"
    	}
    }
    • expandNestedObjects = false results in: ['make', 'model', 'year', 'specifications']
    • expandNestedObjects = true results in: ['make', 'model', 'year', 'specifications.mileage', 'specifications.trim']
  • expandArrayObjects - 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?
    • Example:
    {
    	"make": "Nissan",
    	"model": "GT-R",
    	"trim": "NISMO",
    	"specifications": [
    		{"mileage": 10},
    		{"cylinders": 6}
    	]
    }
    • expandArrayObjects = false results in: ['make', 'model', 'trim', 'specifications']
    • expandArrayObjects = true results in: ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
  • ignoreEmptyArraysWhenExpanding - Boolean (Default: false) - Should empty array keys be ignored when expanding array objects?
    • Note: This only has an effect when used with expandArrayObjects.
    • Example:
    { 
    	"features": [ {"name": "A/C" }],
    	"rebates": []
    }
    • ignoreEmptyArraysWhenExpanding = false results in: ['features.name', 'rebates']
    • ignoreEmptyArraysWhenExpanding = true results in: ['features.name']
  • escapeNestedDots - Boolean (Default: false) - Should . characters that appear in keys be escaped with a preceding \ character?
    • Example:
    {
    	"a.a": "1",
    	"a.b": {
    		"c": "2",
    		"c.d": "3"
    	}
    }
    • escapeNestedDots = false results in: ['a.a', 'a.b.c', 'a.b.c.d']
    • escapeNestedDots = true results in: ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d']
  • ignoreEmptyArrays - Boolean (Default: false) - Should key paths for empty arrays be ignored in the generated key list?
    • Example:
    {
    	"a": {
    		"b": [],
    		"c": {
    			"f": 4,
    			"e": []
    		}
    	},
    	"b": []
    }
    • ignoreEmptyArrays = false results in ['a.b', 'a.c.f', 'a.c.e', 'b']
    • ignoreEmptyArrays = true results in ['a.c.f']

Returns: string[]

Example: ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers']

deepKeysFromList

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):

  • expandNestedObjects - 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?
    • Example:
    {
    	"make": "Nissan",
    	"model": "Murano",
    	"year": 2013,
    	"specifications": {
    		"mileage": 7106,
    		"trim": "S AWD"
    	}
    }
    • expandNestedObjects = false results in: ['make', 'model', 'year', 'specifications']
    • expandNestedObjects = true results in: ['make', 'model', 'year', 'specifications.mileage', 'specifications.trim']
  • expandArrayObjects - 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?
    • Example:
    {
    	"make": "Nissan",
    	"model": "GT-R",
    	"trim": "NISMO",
    	"specifications": [
    		{"mileage": 10},
    		{"cylinders": 6}
    	]
    }
    • expandArrayObjects = false results in: ['make', 'model', 'trim', 'specifications']
    • expandArrayObjects = true results in: ['make', 'model', 'trim', 'specifications.mileage', 'specifications.cylinders']
  • ignoreEmptyArraysWhenExpanding - Boolean (Default: false) - Should empty array keys be ignored when expanding array objects?
    • Note: This only has an effect when used with expandArrayObjects.
    • Example:
    [
    	{ "features": [ {"name": "A/C" }] },
    	{ "features": [] }
    ] 
    • ignoreEmptyArraysWhenExpanding = false results in: [ ['features.name', 'features'] ]
    • ignoreEmptyArraysWhenExpanding = true results in: [ ['features.name'] ]
  • escapeNestedDots - Boolean (Default: false) - Should . characters that appear in keys be escaped with a preceding \ character.
    • Example:
    [
    	{
    		"a.a": "1",
    		"a.b": {
    			"c": "2",
    			"c.d": "3"
    		}
    	}
    ]
    • escapeNestedDots = false results in: [ ['a.a', 'a.b.c', 'a.b.c.d'] ]
    • escapeNestedDots = true results in: [ ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d'] ]
  • ignoreEmptyArrays - Boolean (Default: false) - Should key paths for empty arrays be ignored in the generated key list?
    • Example:
    [
    	{
    		"a": {
    			"b": [],
    			"c": {
    				"f": 4,
    				"e": []
    			}
    		},
    		"b": []
    	}
    ]
    • ignoreEmptyArrays = false results in [ ['a.b', 'a.c.f', 'a.c.e', 'b'] ]
    • ignoreEmptyArrays = true results in [ ['a.c.f'] ]

Returns: string[][]

Example: [ ['make', 'model', 'specifications.odometer.miles', 'specifications.odometer.kilometers'] ]

Examples

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, '');
}

Tests

$ npm test

Note: This requires mocha.

To see test coverage, please run:

$ npm run coverage