npm.io
2.3.4 • Published 1 month ago

@jsopen/objects

Licence
MIT
Version
2.3.4
Deps
1
Size
44 kB
Vulns
0
Weekly
0

@jsopen/objects

NPM Version NPM Downloads CI Tests Test Coverage

A 'swiss army knife' solution for working with JavaScript objects and arrays — deep merging, cloning, omitting keys/nullish values, and a set of dependency-free type guards, all in one small, fully-typed package.

Features

  • merge — deep or shallow merging of objects/arrays, multiple sources, array merge strategies (append/unique), property-descriptor cloning, custom filters, and built-in prototype-pollution protection.
  • clone / deepClone — copy objects and arrays (including class instances with deepClone), with full support for top-level arrays as the value being cloned.
  • omit / omitUndefined / omitNull / omitNullish — drop specific keys or null/undefined values, shallow or deep, from objects or arrays.
  • Type guards — isObject, isPlainObject, isBuiltIn, isConstructor, isIterable, isAsyncIterable — none of them throw on unexpected input.
  • updateErrorMessage — change an Error's message and keep its stack trace header consistent, without losing the original stack frames.
  • Zero runtime dependencies, ESM-only, written in TypeScript.

Quick Start

import { merge, clone, deepClone, omit, omitUndefined } from '@jsopen/objects';

// Deep merge
merge({ a: 1 }, { b: 2 }, { deep: true });
// => { a: 1, b: 2 }

// Clone (objects or top-level arrays, deeply by default)
clone({ a: 1, b: { c: 2 } });
clone([1, 2, { x: 1 }]);

// Deep-clone including class instances
class Point { constructor(public x: number, public y: number) {} }
deepClone({ point: new Point(1, 2) });

// Exclude keys / nullish values
omit({ a: 1, b: 2, c: 3 }, ['b']);          // => { a: 1, c: 3 }
omitUndefined({ a: 1, b: undefined }, true); // => { a: 1 }

Documentation

See the Full API Reference for every function, option, and edge case, or jump to a focused guide:

merge

A powerful, flexible tool for merging objects, arrays, and their nested properties.

clone / deepClone

Easy ways to create shallow or deep copies of objects and arrays.

omit / omitUndefined / omitNull / omitNullish

Easily exclude specific keys or nullish values from objects.

updateErrorMessage

Update an Error object's message while correctly refreshing the stack trace.

Utilities

Various utility functions for object and type checking.

Installation

$ npm install @jsopen/objects

Node Compatibility

  • node >= 16;
License

MIT

Keywords