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

js-object-utilities

v2.2.0

Published

JavaScript utilities for nested objects

Downloads

459,935

Readme

js-object-utilities

This package has a bunch of helper methods to work with nested objects in JavaScript.

Install

npm i js-object-utilities

API

objectutils.get(obj, key)

This function takes in an object (obj) and a key (key) and returns the value for the given key.

const objectutils = require("js-object-utilities");
console.log(objectutils.get({
	"data": {
		"hello": "world"
	}
}, "data.hello"));

// Will print "world"

objectutils.set(obj, key, value)

This function takes in an object (obj), key (key), and a value (value) and mutates the object setting the value for the given key.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world"
	}
};
objectutils.set(object, "data.hello", "universe");
console.log(object); // {"data": {"hello": "universe"}}

objectutils.delete(obj, key)

This function takes in an object (obj), and key (key), and deletes the given value for the key you passed in.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world"
	}
};
objectutils.delete(object, "data.hello");
console.log(object); // {"data": {}}

objectutils.pick(obj, keys)

This function takes in an object (obj), and array of keys (keys), and returns an object for the given keys you passed in.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm"
	}
};
objectutils.delete(object, ["data.hello", "data.space"]);
console.log(object); // {"data": {"hello": "world", "space": "travel"}}

objectutils.keys(obj)

This function takes in an object (obj), and returns an array of keys included in that object.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm"
	}
};
console.log(objectutils.keys(object)); // ["data", "data.hello", "data.space", "data.node"]

objectutils.entries(obj)

This function takes in an object (obj), and returns an array of entries included in that object.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm"
	}
};
console.log(objectutils.keys(object)); // [["data", {"hello": "world", "space": "travel", "node": "npm"}], ["data.hello", "world"], ["data.space", "travel"], ["data.node", "npm]]

objectutils.equals(obj)

This function takes in two values, and returns a boolean representing if they are equal. If objects as passed in it will check to ensure the entire object is identical.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm"
	}
};
console.log(objectutils.equals(object, {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm"
	}
})); // true

console.log(objectutils.equals(object, {
	"data": {
		"hello": "universe",
		"space": "travel",
		"node": "npm"
	}
})); // false

objectutils.clearEmpties(obj)

This function takes in an object and mutates it to remove all objects with a length of 0.

const objectutils = require("js-object-utilities");
const object = {
	"data": {
		"hello": "world",
		"space": "travel",
		"node": "npm",
		"otherData": {}
	}
};
objectutils.clearEmpties(object);
console.log(object);
// {
// 	"data": {
// 		"hello": "world",
// 		"space": "travel",
// 		"node": "npm",
// 	}
// }

objectutils.isCircular(obj[, searchKey])

This function checks to see if an object is circular. If a search key is passed in it will only return true if the object is circular and the search key is the key that caused the circularity.

This function will also check all nested objects for circularity.

const objectutils = require("js-object-utilities");

let object = {};
object.array = {"first": 1};
object.array2 = object;

const isCircular = objectutils.isCircular(object);
console.log(isCircular); // true

const isRandomKeyCircular = objectutils.isCircular(object, "random");
console.log(isRandomKeyCircular); // false

const isArray2KeyCircular = objectutils.isCircular(object, "array2");
console.log(isArray2KeyCircular); // true

objectutils.circularKeys(obj[, searchKey])

This function is identical to objectutils.isCircular except it returns an array of keys that are circular.

const objectutils = require("js-object-utilities");

let object = {};
object.array = {"first": 1};
object.array2 = object;

const circularKeys = objectutils.circularKeys(object);
console.log(circularKeys); // ["array2"]

const randomKeyCircular = objectutils.circularKeys(object, "random");
console.log(randomKeyCircular); // []

const array2KeyCircular = objectutils.circularKeys(object, "array2");
console.log(array2KeyCircular); // ["array2"]