JavaScript Deep Utility Functions

1.1.4 · maintenance · verified Sun Apr 19

js-utils-deep is a lightweight JavaScript utility package providing a focused set of functions for deep, recursive manipulation of objects. It includes `recursiveOmit`, which removes null, empty string, and undefined values from all nested levels of an object; `deepExtend`, designed for deeply merging properties from a source object into a target object; and `diffObject`, which identifies key-value differences between two objects, including nested structures. Currently at version 1.1.4, the library appears to be in a maintenance phase, offering stable and tested functionalities without frequent updates. Its primary differentiator is its straightforward API for common deep object tasks, providing essential recursive utilities without the added complexity of larger, more generalized utility libraries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the core functionalities of `js-utils-deep`: `recursiveOmit` to clean deeply nested objects, `deepExtend` to merge objects recursively, and `diffObject` to find differences, highlighting their usage and mutation behavior.

import { recursiveOmit, deepExtend, diffObject } from 'js-utils-deep';

// Example for recursiveOmit: Removes null, '', undefined from deeply nested objects
let objToOmit = {
  x: {
    y: {
      z: ''
    },
    a: {
      b: null,
      c: undefined
    },
    d: null
  },
  e: 0,
  f: 'hello',
  g: []
};
console.log('Original object for omit:', JSON.stringify(objToOmit));
const omitted = recursiveOmit(objToOmit); // Mutates objToOmit
console.log('Object after recursiveOmit:', JSON.stringify(omitted));
// Expected: {"e":0,"f":"hello","g":[]}

// Example for deepExtend: Deeply merges source into target object
let targetObj = {
  x: {
    y: { z: '' },
    a: { b: null, c: undefined }
  },
  existing: 'value'
};
let sourceObj = {
  x: {
    y: { z: 'new_z' },
    a: { b: 'new_b', c: 'new_c' },
    d: 'new_d'
  },
  newProp: 'added'
};
console.log('Target object before extend:', JSON.stringify(targetObj));
deepExtend(targetObj, sourceObj); // Mutates targetObj
console.log('Target object after deepExtend:', JSON.stringify(targetObj));
// Expected: {"x":{"y":{"z":"new_z"},"a":{"b":"new_b","c":"new_c"},"d":"new_d"},"existing":"value","newProp":"added"}

// Example for diffObject: Compares two objects and returns differing key-value pairs
let obj1 = {
  id: 1,
  name: 'Alpha',
  details: { version: '1.0', status: 'active' }
};
let obj2 = {
  id: 1,
  name: 'Beta',
  details: { version: '1.1', owner: 'Org' },
  tags: ['new']
};
console.log('Object 1 for diff:', JSON.stringify(obj1));
console.log('Object 2 for diff:', JSON.stringify(obj2));
const differences = diffObject(obj1, obj2);
console.log('Differences between objects:', JSON.stringify(differences));
// Expected: {"name":"Beta","details":{"version":"1.1","status":"active","owner":"Org"},"tags":["new"]}

view raw JSON →