Object Utilities
Object transformation, picking, omitting, deep cloning, and deep merging in sc-fns/object.
The sc-fns/object module provides type-safe object transformations, property filtering, and deep cloning.
ts
import {
isEmpty,
pick,
omit,
deepClone,
deepMerge,
removeUndefined,
removeNull,
} from "sc-fns/object";
Checks if a value is empty. Returns true for:
null and undefined
- Empty strings
""
- Empty arrays
[]
- Empty Maps and Sets
- Empty plain objects
{}
ts
function isEmpty(value: unknown): boolean;
ts
isEmpty({}); // true
isEmpty([]); // true
isEmpty(""); // true
isEmpty(null); // true
isEmpty({ a: 1 }); // false
isEmpty([10]); // false
Creates a new shallow copy of an object containing only the specified keys. Full TypeScript type inference is preserved.
ts
function pick<T extends object, K extends keyof T>(object: T, keys: readonly K[]): Pick<T, K>;
ts
const user = {
id: 101,
name: "Jane Doe",
email: "jane@example.com",
passwordHash: "secret",
role: "admin",
};
const publicUser = pick(user, ["id", "name", "email"]);
// { id: 101, name: "Jane Doe", email: "jane@example.com" }
Creates a new shallow copy of an object excluding the specified keys.
ts
function omit<T extends object, K extends keyof T>(object: T, keys: readonly K[]): Omit<T, K>;
ts
const userWithoutSecrets = omit(user, ["passwordHash"]);
// Excludes 'passwordHash'
Deeply clones objects, arrays, Dates, RegExps, Maps, and Sets. Uses structuredClone when available natively with a comprehensive fallback.
ts
function deepClone<T>(value: T): T;
ts
const original = {
user: { name: "Alice", prefs: { darkTheme: true } },
tags: ["admin", "beta"],
};
const clone = deepClone(original);
clone.user.prefs.darkTheme = false;
console.log(original.user.prefs.darkTheme); // true (Original is unaffected!)
Recursively merges multiple source objects into a target object without prototype pollution vulnerabilities.
ts
function deepMerge<T extends object>(target: T, ...sources: Partial<T>[]): T;
ts
const defaults = { api: { timeout: 5000, retries: 3 }, debug: false };
const overrides = { api: { timeout: 10000 }, debug: true };
const config = deepMerge(defaults, overrides);
// {
// api: { timeout: 10000, retries: 3 },
// debug: true
// }
Creates a clean object with all undefined or null values removed. Supports shallow and deep recursion.
ts
removeUndefined({ id: 1, name: "Bob", title: undefined });
// { id: 1, name: "Bob" }
removeNull({ id: 2, profile: null, active: true });
// { id: 2, active: true }
// Deep removal
removeUndefined({ a: 1, nested: { b: undefined, c: 2 } }, true);
// { a: 1, nested: { c: 2 } }
Last updated on October 25, 2026