Assigns own enumerable properties of source objects to destination object for all destination properties that resolve to undefined
npx @tessl/cli install tessl/npm-lodash-defaults@3.1.0lodash.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.
npm install lodash.defaultsconst defaults = require('lodash.defaults');For ES modules (if using a bundler that supports CommonJS imports):
import defaults from 'lodash.defaults';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 }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.Behavior:
undefinednull, false, 0, "") are preservedobject)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 }