defaults
Assigns default values from source to target for properties that are undefined or missing. Existing null, 0, "", and false values in the target are preserved — only undefined triggers replacement.
Import
Section titled “Import”import { defaults } from "1o1-utils";import { defaults } from "1o1-utils/defaults";Signature
Section titled “Signature”function defaults({ target, source }: DefaultsParams): Record<string, unknown>Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
target | Record<string, unknown> | Yes | The base object whose defined values win |
source | Record<string, unknown> | Yes | The object supplying defaults |
Returns
Section titled “Returns”Record<string, unknown> — A new object (neither input is mutated).
Examples
Section titled “Examples”defaults({ target: { a: 1 }, source: { a: 99, b: 2 },});// => { a: 1, b: 2 }defaults({ target: { a: null, b: 0, c: "" }, source: { a: 1, b: 99, c: "fallback", d: 4 },});// => { a: null, b: 0, c: "", d: 4 }Edge Cases
Section titled “Edge Cases”- Throws if
targetis not a plain object. - Throws if
sourceis not a plain object. - Only replaces values that are strictly
undefined—null,0,"", andfalseare preserved. - Nested objects are copied by reference. Use
defaultsDeepfor recursive merging.
Also known as
Section titled “Also known as”default values, fill undefined, fallback object, with defaults
Prompt suggestion
Section titled “Prompt suggestion”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 defaults to merge a config object with fallback values without overriding explicit nulls