1.0.0 • Published 11 months ago

objectifyjs v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Objectifyjs

Static Badge GitHub Actions Jest TypeScript codecov Pivotal Tracker npm License

Overview

Objectifyjs is an npm package that provides a collection of utility functions to work with objects in JavaScript. It includes functions for deep cloning, merging, extracting differences, and more. This package aims to simplify common object manipulations and enhance productivity.

Installation

You can install Objectifyjs via npm:

npm install objectifyjs

Or via yarn:

yarn add objectifyjs

Features

  • Deep Clone: Create a deep copy of an object.
  • Merge: Merge two objects, including nested objects.
  • Extract Differences: Extract differences between two objects.
  • TypeScript Support: Fully typed with TypeScript.
  • Unit Tested: Thoroughly tested with Jest.

Usage

Deep Clone

import { deepClone } from 'objectifyjs';

const original = { a: 1, b: { c: 2 } };
const clone = deepClone(original);

console.log(clone); // { a: 1, b: { c: 2 } }

Merge

import { merge } from 'objectifyjs';

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { d: 3 } };

merge(obj1, obj2);
console.log(obj1); // { a: 1, b: { c: 2, d: 3 } }

Extract Differences

import { getDifference } from 'objectifyjs';

const obj1 = { a: 1, b: 2, c: { d: 3 } };
const obj2 = { a: 1, b: 3, c: { d: 4 } };

// Extract differences from obj1 to obj2
const diff1 = getDifference(obj1, obj2, 1);
console.log(diff1); // { b: 2, c: { d: 3 } }

// Extract differences from obj2 to obj1
const diff2 = getDifference(obj2, obj1, -1);
console.log(diff2); // { b: 3, c: { d: 4 } }

// Extract combined differences
const diff3 = getDifference(obj1, obj2, 0);
console.log(diff3); // { b: 2, c: { d: 3 }, b: 3, c: { d: 4 } }

API

deepClone(obj: T): T

Creates a deep copy of the provided object.

Parameters:

  • obj (T) - The object to clone.

Returns: A deep copy of the object.

merge(target: T, source: Partial): void

Merges the source object into the target object, including nested objects.

Parameters:

  • target (T) - The target object to merge into.
  • source (Partial) - The source object to merge from.

extractDifferences(obj1: T, obj2: T, flag: number): Partial

Extracts differences between two objects based on the flag value.

Parameters:

  • obj1 (T) - The first object to compare.
  • obj2 (T) - The second object to compare.
  • flag (number) - The flag to determine the direction of the difference extraction:
    • -1: Extract differences from obj2 to obj1.
    • 1: Extract differences from obj1 to obj2.
    • 0: Extract differences from both objects, excluding common properties with different values.

Returns: An object containing the differences.

Contributing

We welcome contributions! Please read our Contributing Guide to learn how you can help.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

  • TypeScript
  • Jest
  • Pivotal Tracker
  • GitHub Actions
1.0.0

11 months ago