or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-defaults

Assigns own enumerable properties of source objects to destination object for all destination properties that resolve to undefined

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.defaults@3.1.x

To install, run

npx @tessl/cli install tessl/npm-lodash-defaults@3.1.0

index.mddocs/

lodash.defaults

lodash.defaults provides a modular implementation of lodash's _.defaults function, which assigns own enumerable properties of source objects to a destination object for all destination properties that resolve to undefined. This function performs shallow object merging where existing properties are preserved and only missing properties are filled in from source objects.

Package Information

  • Package Name: lodash.defaults
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.defaults

Core Imports

const defaults = require('lodash.defaults');

For ES modules (if using a bundler that supports CommonJS imports):

import defaults from 'lodash.defaults';

Basic Usage

const defaults = require('lodash.defaults');

// Setting default values for a configuration object
const config = defaults({ host: 'localhost' }, { port: 3000, host: 'example.com' });
// Result: { host: 'localhost', port: 3000 }

// Multiple source objects
const userDefaults = defaults(
  { name: 'User' }, 
  { age: 18, role: 'guest' }, 
  { status: 'active', age: 21 }
);
// Result: { name: 'User', age: 18, role: 'guest', status: 'active' }

// Working with undefined values
const options = defaults(
  { timeout: undefined, retries: 3 },
  { timeout: 5000, maxSize: 1024 }
);
// Result: { timeout: 5000, retries: 3, maxSize: 1024 }

Capabilities

defaults

Assigns own enumerable properties of source objects to the destination object for all destination properties that resolve to undefined. Once a property is set, additional values of the same property are ignored.

/**
 * Assigns own enumerable properties of source object(s) to the destination
 * object for all destination properties that resolve to `undefined`. Once a
 * property is set, additional values of the same property are ignored.
 * 
 * Note: This method mutates `object`.
 * 
 * @param {Object} object - The destination object.
 * @param {...Object} [sources] - The source objects.
 * @returns {Object} Returns `object`.
 */
function defaults(object, ...sources);

Parameters:

  • object (Object): The destination object to assign default values to. This object is mutated.
  • ...sources (...Object): One or more source objects containing default values.

Returns:

  • (Object): Returns the mutated destination object.

Behavior:

  • Only assigns properties where the destination value is undefined
  • Existing properties (including null, false, 0, "") are preserved
  • Source objects are processed left-to-right, with earlier sources taking precedence
  • The function mutates the first argument (object)
  • Properties from source objects are copied using own enumerable property iteration

Usage Examples:

const defaults = require('lodash.defaults');

// Original lodash example (adapted for modular usage)
const result0 = defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
// Result: { 'user': 'barney', 'age': 36 }

// Basic default assignment
const result1 = defaults({ a: 1 }, { a: 2, b: 2 });
// Result: { a: 1, b: 2 }

// Multiple sources with precedence
const result2 = defaults({ a: 1 }, { a: 2, b: 2 }, { a: 3, b: 3, c: 3 });
// Result: { a: 1, b: 2, c: 3 }

// Undefined values get replaced
const result3 = defaults({ a: undefined, b: 1 }, { a: 2, c: 3 });
// Result: { a: 2, b: 1, c: 3 }

// Falsy values (except undefined) are preserved
const result4 = defaults({ a: 0, b: false, c: null }, { a: 1, b: true, c: 'default' });
// Result: { a: 0, b: false, c: null }

// Empty object as destination
const result5 = defaults({}, { name: 'John', age: 30 });
// Result: { name: 'John', age: 30 }