2026-10-13 10:32:33 +00:00
…
2026-10-13 10:32:33 +00:00
…
2026-10-13 10:32:33 +00:00
…
2026-10-13 10:32:33 +00:00
…

@push.rocks/smartobject

work with objects

Install

To install @push.rocks/smartobject, use the following npm command. It's recommended to install this package in a TypeScript project to make full use of its functionalities and type definitions.

npm install @push.rocks/smartobject --save

This command will add @push.rocks/smartobject to your project's package.json file and download the package to the node_modules directory.

Usage

This module provides a convenient way to work with JavaScript objects, offering utilities for deep object manipulation, comparison, and more. Below are detailed examples of how to utilize @push.rocks/smartobject in a variety of use cases. Always use ESM syntax in combination with TypeScript for the best developer experience.

Importing the Module

Before you can use the functionalities provided by smartobject, you need to import the required classes or functions into your TypeScript file.

import {
  SmartObject,
  fastDeepEqual,
  fastDeepEqualCore,
  smartGet,
  smartAdd,
  compareObjects,
  forEachMinimatch
} from '@push.rocks/smartobject';

Working with SmartObject

The SmartObject class is a wrapper around a standard JavaScript object that provides additional capabilities for manipulating and querying the object.

Creating a SmartObject

// Original JavaScript object
const originalObject = {
  level1: {
    level2: 'value',
  },
};

// Creating a SmartObject
const smartObj = new SmartObject(originalObject);

Adding and Retrieving Values

// Adding a new value
smartObj.addValueAtFlatPathString('level1.newKey', 'newValue');

// Retrieving a value
const value = smartObj.getValueAtFlatPathString('level1.level2');
console.log(value); // Output: 'value'

Converting to a Flat Object

const flatObject = smartObj.toFlatObject();
console.log(flatObject); // Output: { 'level1.level2': 'value', 'level1.newKey': 'newValue' }

Deep Object Comparison

compareObjects allows you to compare two objects and get detailed information about their differences.

const obj1 = {name: 'John', age: 30};
const obj2 = {name: 'John', age: 31};

const comparisonResult = compareObjects(obj1, obj2);
console.log(comparisonResult);

Deep Equality

fastDeepEqual and fastDeepEqualCore are (a: any, b: any) => boolean comparators that run in browsers and Node.js. They return the same results as fast-deep-equal 3.1.3: fastDeepEqual matches its es6 entry point and fastDeepEqualCore matches its default entry point. compareObjects uses fastDeepEqual.

fastDeepEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }); // true
fastDeepEqual(new Map([['a', 1]]), new Map([['a', 2]])); // false
fastDeepEqualCore(new Map([['a', 1]]), new Map([['a', 2]])); // true: no own enumerable keys

Both comparators:

  • return true for a === b, so 0 equals -0, and for two NaN values;
  • return false when the constructors differ, so {} never equals [] and instances of different classes never match;
  • compare arrays element by element and ignore non-index array properties;
  • compare RegExps by source and flags (not lastIndex);
  • compare objects whose valueOf is not Object.prototype.valueOf (Date, boxed primitives, classes overriding it) by valueOf(), and then those whose toString is overridden by toString(). Other properties of such objects are ignored, and two invalid Dates are not equal;
  • compare all other objects by their own enumerable string keys. Symbol keys, non-enumerable and inherited properties are ignored, and { a: undefined } does not equal {};
  • compare functions and primitives by identity;
  • throw TypeError for two null-prototype objects without an own valueOf, because there is no valueOf to call;
  • have no cycle detection: comparing two distinct cyclic structures overflows the stack (RangeError) unless a difference is found before the recursion reaches the cycle.

fastDeepEqual adds:

  • Map: same size, every key present in the other map by has (object keys by identity), and deeply equal values;
  • Set: same size and every member present in the other set by has (object members by identity);
  • typed arrays: same length and every element equal by !== (so NaN elements never match);
  • DataViews, including ones created in another realm (an iframe, a Node vm context): same byteLength and every byte equal. Other properties are ignored, like for typed arrays. This is smartobject's own behaviour: fast-deep-equal 3.1.3 never returns for two distinct DataViews.

fastDeepEqualCore has none of these. Maps and Sets have no own enumerable keys and are always equal to each other. Typed arrays compare by toString(), so NaN elements match.

Inherited limits from fast-deep-equal 3.1.3: ArrayBuffers have no own enumerable keys and are always equal to each other. fastDeepEqualCore returns true for any two DataViews for the same reason.

Deep Object Manipulation

smartAdd and smartGet enable deep manipulation and querying of objects using dot notation paths.

// Using smartAdd to add a deep-nested property
const obj = {};
smartAdd(obj, 'user.info.name', 'Jane Doe');

// Using smartGet to retrieve a deep-nested property
const name = smartGet(obj, 'user.info.name');
console.log(name); // Output: Jane Doe

Iterating over Object Properties with Minimatch Patterns

forEachMinimatch provides a way to execute a function for each object property that matches a given minimatch pattern.

const userObj = {
  'user.name': 'John Doe',
  'user.age': 30,
  'config.theme': 'dark',
};

forEachMinimatch(userObj, 'user.*', (value, key) => {
  console.log(key, value);
});

This module leverages the power of TypeScript to offer robust typing and intelligent auto-completion, making it easier to work with complex objects and avoid common pitfalls. Whether you are manipulating deeply nested properties, comparing object differences, or iterating over specific object keys, @push.rocks/smartobject provides a versatile toolkit to streamline your object handling logic.

For a complete list of features and functionalities, including additional utility functions not covered in these examples, please refer to the module's documentation and type definitions.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

S
Description
A library to simplify and enhance object manipulation and traversal.
Readme
829 KiB
Languages
TypeScript 100%