JSPM

object-ninja

0.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q36747F
  • License MIT

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 hassle-free. Need to access properties swiftly or validate your objects? Object Ninja has got you covered. Merge and combine objects seamlessly, filter through data effortlessly, and handle events like a champ. Clone objects effortlessly, transform them at will, and utilize a range of manipulation utilities to tackle any task. Say goodbye to object-related headaches and embrace the power of Object Ninja for all your JavaScript projects!

Package Exports

  • object-ninja
  • object-ninja/dist/index.js
  • object-ninja/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-ninja) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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 Kumarashish87998@gmail.com 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