The object utilities module provides functions for manipulating, transforming, and analyzing JavaScript objects, including JSON serialization, flattening, cloning, comparison, and more.
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>>"}'
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 }
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 }]
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"
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)
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
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" }
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"]
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"]
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 } }
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 }