Object Utilities

Table of Contents

Description

The object utilities module provides functions for manipulating, transforming, and analyzing JavaScript objects, including JSON serialization, flattening, cloning, comparison, and more.

toJson

Converts an object to JSON string with special handling for circular references, DOM elements, and functions.

import { toJson } from '../src/utils/object.js';

const obj = { name: 'test', func: () => {} };
toJson(obj); // '{"name":"test","func":"<<Function func>>"}'

flattenObject

Flattens a nested object into a single-level object with dot-notation keys.

import { flattenObject } from '../src/utils/object.js';

const obj = { user: { name: 'John', age: 30 } };
flattenObject(obj); // { "user.name": "John", "user.age": 30 }

flattenedObjects

Flattens multiple objects and returns them as an array.

import { flattenedObjects } from '../src/utils/object.js';

flattenedObjects({a: 1}, {b: 2}); // [{ "a": 1 }, { "b": 2 }]

objectSummary

Creates a summary string representation of an object.

import { objectSummary } from '../src/utils/object.js';

const obj = { name: 'John', age: 30 };
objectSummary(obj); // "name = John, age = 30"

clone

Creates a deep clone of an object with special handling for different types.

import { clone } from '../src/utils/object.js';

const obj = { items: [1, 2, 3] };
clone(obj); // { items: [1, 2, 3] } (new array)

equalObjs

Checks if multiple objects are equal by comparing their JSON representations.

import { equalObjs } from '../src/utils/object.js';

equalObjs({a: 1}, {a: 1}); // true
equalObjs({a: 1}, {a: 2}); // false

prune

Removes empty/undefined properties from an object recursively.

import { prune } from '../src/utils/object.js';

const obj = { name: 'John', empty: '', nested: { value: null } };
prune(obj); // { name: "John" }

getAllKeys

Gets all unique keys from multiple objects.

import { getAllKeys } from '../src/utils/object.js';

getAllKeys({a: 1, b: 2}, {b: 3, c: 4}); // ["a", "b", "c"]

getDifferencesKeys

Gets keys that have different values across multiple objects.

import { getDifferencesKeys } from '../src/utils/object.js';

getDifferencesKeys({a: 1, b: 2}, {a: 1, b: 3}); // ["b"]

diff

Computes the differences between two objects.

import { diff } from '../src/utils/object.js';

const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3 };
diff(obj1, obj2); // { "b": { newValue: 2, oldValue: 3 } }

mapObject

Maps over an object's entries using a function that returns [newKey, newValue].

import { mapObject } from '../src/utils/object.js';

const obj = { a: 1, b: 2 };
mapObject(obj, (k, v) => [`new_${k}`, v * 2]); // { new_a: 2, new_b: 4 }