source: node_modules/es-toolkit/dist/compat/object/defaults.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.0 KB
Line 
1import { isNil } from '../../predicate/isNil.mjs';
2import { isIterateeCall } from '../_internal/isIterateeCall.mjs';
3import { isEqualsSameValueZero } from '../../_internal/isEqualsSameValueZero.mjs';
4
5function defaults(object, ...sources) {
6 object = Object(object);
7 const objectProto = Object.prototype;
8 let length = sources.length;
9 const guard = length > 2 ? sources[2] : undefined;
10 if (guard && isIterateeCall(sources[0], sources[1], guard)) {
11 length = 1;
12 }
13 for (let i = 0; i < length; i++) {
14 if (isNil(sources[i])) {
15 continue;
16 }
17 const source = sources[i];
18 const keys = Object.keys(source);
19 for (let j = 0; j < keys.length; j++) {
20 const key = keys[j];
21 const value = object[key];
22 if (value === undefined ||
23 (!Object.hasOwn(object, key) && isEqualsSameValueZero(value, objectProto[key]))) {
24 object[key] = source[key];
25 }
26 }
27 }
28 return object;
29}
30
31export { defaults };
Note: See TracBrowser for help on using the repository browser.