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

object-ninja

v0.0.0

Published

Introducing Object Ninja 🥷: Your ultimate JavaScript toolkit for mastering objects like a pro! With Object Ninja, you can effortlessly manipulate properties, iterate through objects with ease, compare them effortlessly, and serialize/deserialize data has

Downloads

19

Readme

Object Ninja

Object Ninja is a powerful npm package designed to make managing complex nested objects a breeze. Whether you need to update, delete, add, or compare deeply nested objects, Object Ninja has got you covered.

🥷 Why Object Ninja?

Managing nested objects in JavaScript can be tricky and time-consuming. Object Ninja aims to simplify this process, offering a suite of functions to manipulate nested objects effortlessly.

Table of Contents

Features

  • 🛠 Update Nested Objects: Easily modify values within deeply nested objects with just one function call.

  • 🗑 Delete Keys: Effortlessly remove keys from nested objects without breaking a sweat.

  • Add Keys: Add new keys to your nested objects in a breeze, no hassle involved.

  • 🔍 Compare Deeply Nested Objects: Get detailed insights by comparing two nested objects, identifying missing keys and value changes.

  • 📦 Object Serialization and Deserialization: Serialize and deserialize objects seamlessly, making data storage and retrieval a cinch.

  • 🔑 Collect All Keys: Iterate through nested objects to collect all keys, simplifying key management and analysis.

Installation

You can install object-ninja via npm:

npm install object-ninja
import { clone ,compareObjectsWrapper ,getAllKeys ,addPropertyToObject, removePropertyFromObject ,serializeToJSON, deserializeFromJSON  } from "object-ninja";

Function Details

addPropertyToObject

Parameters

  • obj: The target object to which the property will be added.
  • keys: An array of strings representing the keys to traverse in the object hierarchy. The last key in the array will be used to add the property.
  • value: The value to be assigned to the property.
  • mutate (optional): A boolean flag indicating whether to mutate the original object (true) or return a new mutated object (false). Default is false.

Return Value

  • If mutate is true, the function returns the mutated original object with the property added.
  • If mutate is false or not provided, the function returns a new object with the property added.

Example Usage

const originalObj = { a: { b: { c: 5 } } };
const keys = ['a', 'b', 'd']; // Adding 'd' to the object hierarchy
const value = 10;

// Example 1: Mutate the original object
const mutatedObj = addPropertyToObject(originalObj, keys, value, true);
console.log(mutatedObj); // Output: { a: { b: { c: 5, d: 10 } } }
console.log(originalObj === mutatedObj); // Output: true (original object mutated)

// Example 2: Create a new mutated object
const newMutatedObj = addPropertyToObject(originalObj, keys, value);
console.log(newMutatedObj); // Output: { a: { b: { c: 5, d: 10 } } }
console.log(originalObj === newMutatedObj); // Output: false (new object created)

removePropertyFromObject

Parameters

  • obj: The target object from which the property will be removed.
  • keys: An array of strings representing the keys to traverse in the object hierarchy. The last key in the array will be used to remove the property.
  • mutate (optional): A boolean flag indicating whether to mutate the original object (true) or return a new object with the property removed (false). Default is false.

Return Value

  • If mutate is true, the function returns the original object with the specified property removed.
  • If mutate is false or not provided, the function returns a new object with the specified property removed.

Example Usage

const originalObj = { a: { b: { c: 5, d: 10 } } };
const keys = ['a', 'b', 'd']; // Removing 'd' from the object hierarchy

// Example 1: Mutate the original object
const mutatedObj = removePropertyFromObject(originalObj, keys, true);
console.log(mutatedObj); // Output: { a: { b: { c: 5 } } }
console.log(originalObj === mutatedObj); // Output: true (original object mutated)

// Example 2: Create a new object with property removed
const newObj = removePropertyFromObject(originalObj, keys);
console.log(newObj); // Output: { a: { b: { c: 5 } } }
console.log(originalObj === newObj); // Output: false (new object created)

clone

Parameters

  • obj: The object to be cloned.
  • deep (optional): A boolean flag indicating whether to perform a deep clone (true) or a shallow clone (false). Default is false.

Return Value Returns a shallow or deep clone of the input object based on the deep parameter.

Example Usage

const originalObj = { a: 1, b: { c: 2 } };

// Shallow clone
const shallowClonedObj = clone(originalObj);
console.log(shallowClonedObj); // Output: { a: 1, b: { c: 2 } }
console.log(originalObj === shallowClonedObj); // Output: false (new object created)

// Deep clone
const deepClonedObj = clone(originalObj, true);
console.log(deepClonedObj); // Output: { a: 1, b: { c: 2 } }
console.log(originalObj === deepClonedObj); // Output: false (new object created)

compareObjectsWrapper

Parameters

  • obj1: The first object to compare.
  • obj2: The second object to compare.
  • config (optional): An optional configuration object with the following properties:
    • ignoreKeys: An array of keys to ignore during the comparison.

Return Value Returns an object containing the following properties:

  • status: A boolean indicating whether the two objects are identical (true) or not (false).
  • differentKeys: An array containing the keys that have different values in the two objects.
  • differentValues: An object containing the different values along with their corresponding keys.

Example Usage

const obj1 = { a: 1, b: { c: 2 }, d: { e: 3 } };
const obj2 = { a: 1, b: { c: 2 }, d: { e: 4 } };
const result = compareObjectsWrapper(obj1, obj2, { ignoreKeys: ['d'] });
console.log(result.status); // Output: false (objects are not identical)
console.log(result.differentKeys); // Output: ["d.e"]
console.log(result.differentValues); // Output: { "d.e": { obj1Value: 3, obj2Value: 4 } }

getAllKeys

Parameters

  • obj: The object for which keys will be retrieved.
  • prefix (optional): A string to be prefixed to each key. Default is an empty string.
  • separator (optional): A string used to separate the prefix and the keys. Default is "_".

Return Value Returns an array of strings representing all keys in the object and its nested objects, with optional prefixes.

Example Usage

const obj = { a: 1, b: { c: 2, d: { e: 3 } }, f: { g: 4 } };
const keys = getAllKeys(obj, "prefix", ".");
console.log(keys); // Output: ["prefix.a", "prefix.b.c", "prefix.b.d.e", "prefix.f.g"]

`serializeToJSON``

Parameters

  • obj: The object to be serialized to JSON.

Return Value

  • Returns a string representing the JSON serialization of the input object.

Example Usage

const obj = { key: 'value', nested: { inner: 'data' } };
const jsonString = serializeToJSON(obj);
console.log(jsonString); // Output: '{"key":"value","nested":{"inner":"data"}}'

deserializeFromJSON

Parameters

  • json: The JSON string to be deserialized.

Return Value

  • Returns an object representing the deserialized JSON string.

Example Usage

const jsonString = '{"key":"value","nested":{"inner":"data"}}';
const obj = deserializeFromJSON(jsonString);
console.log(obj); // Output: { key: 'value', nested: { inner: 'data' } }

Issue/Error

For any hiccups with the package, drop an email to [email protected] with "Error || Object-Ninja-Npm" as the subject. 📧

Check out these other fantastic packages developed by Me:

  • Debounce Throttling 🕒 - Smooth out your application's performance by managing function calls with debouncing and throttling techniques.

  • LocalSafe 🔒 - Securely store sensitive data locally in the user's browser with encryption and other protective measures.

  • Online Status JS 🌐 - Keep track of your user's online status and enhance your application's real-time capabilities.

🌟 Made with ❤️ by Ashish