Skip to content

compact

Creates a new object with all falsy values removed. Pass keep to preserve specific falsy values that should survive.

Falsy values are false, 0, -0, 0n, "", null, undefined, and NaN. Matches in keep use Object.is, so 0 vs -0 and NaN are handled exactly.

import { compact } from "1o1-utils";
import { compact } from "1o1-utils/compact";
function compact<T extends Record<string, unknown>>({ obj, keep }: CompactParams<T>): Partial<T>
NameTypeRequiredDescription
objTYesThe source object
keepunknown[]NoFalsy values to preserve (matched via Object.is)

Partial<T> — A new object without falsy entries (except those listed in keep).

compact({ obj: { a: 1, b: null, c: "", d: 0, e: false, f: "ok" } });
// => { a: 1, f: "ok" }
// Preserve specific falsy values
compact({ obj: { a: 0, b: null, c: "" }, keep: [0] });
// => { a: 0 }
// Match NaN exactly
compact({ obj: { a: NaN, b: 1 }, keep: [NaN] });
// => { a: NaN, b: 1 }
  • Throws if obj is not an object.
  • Throws if obj is an array (use .filter(Boolean) instead).
  • Throws if keep is provided but not an array.
  • Skips unsafe keys (__proto__, constructor, prototype) to prevent prototype pollution.
  • Symbol-keyed properties are ignored (only string-keyed enumerable own properties are processed).
  • Truthy values inside keep are no-ops — keep is only useful for falsy values you want to preserve.
  • Distinguishes 0 from -0 via Object.is.
  • Does not mutate the original object.

remove falsy, clean object, drop empty, prune null

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use compact to strip null/undefined fields from an object before serializing.