Deep operations on nested objects and arrays.
Deep Equality
Compare nested structures for structural equality:
import { equals } from '@metreeca/core/nested';// Objects and arraysequals({ a: [1, 2] }, { a: [1, 2] }); // trueequals({ a: 1, b: 2 }, { b: 2, a: 1 }); // true (order-independent)equals([1, [2, 3]], [1, [2, 3]]); // true (nested arrays)// Primitives and functionsequals(42, 42); // trueequals(-0, +0); // false (distinguishes -0 from +0)const fn = () => {};equals(fn, fn); // true (same reference) Copy
import { equals } from '@metreeca/core/nested';// Objects and arraysequals({ a: [1, 2] }, { a: [1, 2] }); // trueequals({ a: 1, b: 2 }, { b: 2, a: 1 }); // true (order-independent)equals([1, [2, 3]], [1, [2, 3]]); // true (nested arrays)// Primitives and functionsequals(42, 42); // trueequals(-0, +0); // false (distinguishes -0 from +0)const fn = () => {};equals(fn, fn); // true (same reference)
Deep Freezing
Create deeply frozen structures that prevent all mutations:
import { immutable } from '@metreeca/core/nested';// Objects and arraysconst original = { a: [1, 2, 3], b: { c: 4 } };const frozen = immutable(original);frozen.a[0] = 999; // throws Errorfrozen.b.c = 999; // throws Error// Primitives and functionsimmutable(42); // 42immutable("hello"); // "hello"const fn = () => "hello";fn.config = { port: 3000 };const frozenFn = immutable(fn);frozenFn(); // "hello" (function still works)frozenFn.config.port = 8080; // throws Error Copy
import { immutable } from '@metreeca/core/nested';// Objects and arraysconst original = { a: [1, 2, 3], b: { c: 4 } };const frozen = immutable(original);frozen.a[0] = 999; // throws Errorfrozen.b.c = 999; // throws Error// Primitives and functionsimmutable(42); // 42immutable("hello"); // "hello"const fn = () => "hello";fn.config = { port: 3000 };const frozenFn = immutable(fn);frozenFn(); // "hello" (function still works)frozenFn.config.port = 8080; // throws Error
Type-Safe Freezing
Validate and freeze with optional type guards:
import { immutable } from '@metreeca/core/nested';import { isObject, isString, isNumber } from '@metreeca/core';// Define a type guardconst isUser = (v: unknown): v is { name: string; age: number } => isObject(v, { name: isString, age: isNumber });// Validate and freeze in one stepconst user = immutable(data, isUser);// Memoized: repeated calls with same guard return same referenceimmutable(user, isUser) === user; // true (no re-validation)// Different guard triggers revalidationconst isAdmin = (v: unknown): v is { name: string; age: number } => isUser(v) && v.age >= 18;immutable(user, isAdmin); // revalidates Copy
import { immutable } from '@metreeca/core/nested';import { isObject, isString, isNumber } from '@metreeca/core';// Define a type guardconst isUser = (v: unknown): v is { name: string; age: number } => isObject(v, { name: isString, age: isNumber });// Validate and freeze in one stepconst user = immutable(data, isUser);// Memoized: repeated calls with same guard return same referenceimmutable(user, isUser) === user; // true (no re-validation)// Different guard triggers revalidationconst isAdmin = (v: unknown): v is { name: string; age: number } => isUser(v) && v.age >= 18;immutable(user, isAdmin); // revalidates
Checks deep object equality.
Creates an immutable deep clone, optionally validating against a type guard.
Deep operations on nested objects and arrays.
Deep Equality
Compare nested structures for structural equality:
Deep Freezing
Create deeply frozen structures that prevent all mutations:
Type-Safe Freezing
Validate and freeze with optional type guards: