es-toolkit 1.23.0-dev.728 → 1.23.0-dev.729

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -143,6 +143,7 @@ export { fromPairs } from './object/fromPairs.mjs';
143
143
  export { unset } from './object/unset.mjs';
144
144
  export { cloneDeep } from './object/cloneDeep.mjs';
145
145
  export { invertBy } from './object/invertBy.mjs';
146
+ export { defaults } from './object/defaults.mjs';
146
147
  export { isPlainObject } from './predicate/isPlainObject.mjs';
147
148
  export { isArray } from './predicate/isArray.mjs';
148
149
  export { isArguments } from './predicate/isArguments.mjs';
@@ -143,6 +143,7 @@ export { fromPairs } from './object/fromPairs.js';
143
143
  export { unset } from './object/unset.js';
144
144
  export { cloneDeep } from './object/cloneDeep.js';
145
145
  export { invertBy } from './object/invertBy.js';
146
+ export { defaults } from './object/defaults.js';
146
147
  export { isPlainObject } from './predicate/isPlainObject.js';
147
148
  export { isArray } from './predicate/isArray.js';
148
149
  export { isArguments } from './predicate/isArguments.js';
@@ -1454,6 +1454,28 @@ function invertBy(object, iteratee) {
1454
1454
  return result;
1455
1455
  }
1456
1456
 
1457
+ function eq(value, other) {
1458
+ return value === other || (Number.isNaN(value) && Number.isNaN(other));
1459
+ }
1460
+
1461
+ function defaults(object, ...sources) {
1462
+ object = Object(object);
1463
+ const objectProto = Object.prototype;
1464
+ for (let i = 0; i < sources.length; i++) {
1465
+ const source = sources[i];
1466
+ const keys = Object.keys(source);
1467
+ for (let j = 0; j < keys.length; j++) {
1468
+ const key = keys[j];
1469
+ const value = object[key];
1470
+ if (value === undefined ||
1471
+ (!Object.hasOwn(object, key) && eq(value, objectProto[key]))) {
1472
+ object[key] = source[key];
1473
+ }
1474
+ }
1475
+ }
1476
+ return object;
1477
+ }
1478
+
1457
1479
  function isArrayBuffer(value) {
1458
1480
  return isWeakSet$1.isArrayBuffer(value);
1459
1481
  }
@@ -1866,10 +1888,6 @@ function random(...args) {
1866
1888
  }
1867
1889
  }
1868
1890
 
1869
- function eq(value, other) {
1870
- return value === other || (Number.isNaN(value) && Number.isNaN(other));
1871
- }
1872
-
1873
1891
  exports.at = zipWith.at;
1874
1892
  exports.compact = zipWith.compact;
1875
1893
  exports.countBy = zipWith.countBy;
@@ -1980,6 +1998,7 @@ exports.conforms = conforms;
1980
1998
  exports.conformsTo = conformsTo;
1981
1999
  exports.curry = curry;
1982
2000
  exports.debounce = debounce;
2001
+ exports.defaults = defaults;
1983
2002
  exports.defer = defer;
1984
2003
  exports.difference = difference;
1985
2004
  exports.drop = drop;
@@ -144,6 +144,7 @@ export { fromPairs } from './object/fromPairs.mjs';
144
144
  export { unset } from './object/unset.mjs';
145
145
  export { cloneDeep } from './object/cloneDeep.mjs';
146
146
  export { invertBy } from './object/invertBy.mjs';
147
+ export { defaults } from './object/defaults.mjs';
147
148
  export { isPlainObject } from './predicate/isPlainObject.mjs';
148
149
  export { isArray } from './predicate/isArray.mjs';
149
150
  export { isArguments } from './predicate/isArguments.mjs';
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
3
+ *
4
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
5
+ * It ensures that the destination object is not `null` or `undefined`.
6
+ *
7
+ * Note: This method mutates a first argument `object`.
8
+ *
9
+ * @template T - The destination object type.
10
+ * @param {T} object - The destination object.
11
+ * @returns {NonNullable<T>} Returns `object`, ensuring that the destination object is not `null` or `undefined`.
12
+ */
13
+ declare function defaults<T extends object>(object: T): NonNullable<T>;
14
+ /**
15
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
16
+ *
17
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
18
+ * It ensures that the destination object is not `null` or `undefined`.
19
+ *
20
+ * Note: This method mutates a first argument `object`.
21
+ *
22
+ * @template T - The destination object type.
23
+ * @template S - The source object type.
24
+ * @param {T} object - The destination object.
25
+ * @param {S} source - The source object.
26
+ * @returns {NonNullable<T & S>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
27
+ */
28
+ declare function defaults<T extends object, S extends object>(object: T, source: S): NonNullable<T & S>;
29
+ /**
30
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
31
+ *
32
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
33
+ * It ensures that the destination object is not `null` or `undefined`.
34
+ *
35
+ * Note: This method mutates a first argument `object`.
36
+ *
37
+ * @template T - The destination object type.
38
+ * @template S1 - The first source object type.
39
+ * @template S2 - The second source object type.
40
+ * @param {T} object - The destination object.
41
+ * @param {S1} source1 - The first source object.
42
+ * @param {S2} source2 - The second source object.
43
+ * @returns {NonNullable<T & S1 & S2>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
44
+ */
45
+ declare function defaults<T extends object, S1 extends object, S2 extends object>(object: T, source1: S1, source2: S2): NonNullable<T & S1 & S2>;
46
+ /**
47
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
48
+ *
49
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
50
+ * It ensures that the destination object is not `null` or `undefined`.
51
+ *
52
+ * Note: This method mutates a first argument `object`.
53
+ *
54
+ * @template T - The destination object type.
55
+ * @template S1 - The first source object type.
56
+ * @template S2 - The second source object type.
57
+ * @template S3 - The third source object type.
58
+ * @param {T} object - The destination object.
59
+ * @param {S1} source1 - The first source object.
60
+ * @param {S2} source2 - The second source object.
61
+ * @param {S3} source3 - The third source object.
62
+ * @returns {NonNullable<T & S1 & S2 & S3>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
63
+ */
64
+ declare function defaults<T extends object, S1 extends object, S2 extends object, S3 extends object>(object: T, source1: S1, source2: S2, source3: S3): NonNullable<T & S1 & S2 & S3>;
65
+ /**
66
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
67
+ *
68
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
69
+ * It ensures that the destination object is not `null` or `undefined`.
70
+ *
71
+ * Note: This method mutates a first argument `object`.
72
+ *
73
+ * @template T - The destination object type.
74
+ * @template S1 - The first source object type.
75
+ * @template S2 - The second source object type.
76
+ * @template S3 - The third source object type.
77
+ * @template S4 - The fourth source object type.
78
+ * @param {T} object - The destination object.
79
+ * @param {S1} source1 - The first source object.
80
+ * @param {S2} source2 - The second source object.
81
+ * @param {S3} source3 - The third source object.
82
+ * @param {S4} source4 - The fourth source object.
83
+ * @returns {NonNullable<T & S1 & S2 & S3 & S4>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
84
+ */
85
+ declare function defaults<T extends object, S1 extends object, S2 extends object, S3 extends object, S4 extends object>(object: T, source1: S1, source2: S2, source3: S3, source4: S4): NonNullable<T & S1 & S2 & S3 & S4>;
86
+ /**
87
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
88
+ *
89
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
90
+ * It ensures that the destination object is not `null` or `undefined`.
91
+ *
92
+ * Note: This method mutates a first argument `object`.
93
+ *
94
+ * @template T - The destination object type.
95
+ * @template S - The source object type.
96
+ * @param {T} object - The destination object.
97
+ * @param {S[]} sources - The source objects.
98
+ * @returns {object} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
99
+ *
100
+ * @example
101
+ * defaults({ a: 1 }, { a: 2, b: 2 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
102
+ * defaults({ a: 1, b: 2 }, { b: 3 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
103
+ * defaults({ a: null }, { a: 1 }); // { a: null }
104
+ * defaults({ a: undefined }, { a: 1 }); // { a: 1 }
105
+ */
106
+ declare function defaults<T extends object, S extends object>(object: T, ...sources: S[]): object;
107
+
108
+ export { defaults };
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
3
+ *
4
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
5
+ * It ensures that the destination object is not `null` or `undefined`.
6
+ *
7
+ * Note: This method mutates a first argument `object`.
8
+ *
9
+ * @template T - The destination object type.
10
+ * @param {T} object - The destination object.
11
+ * @returns {NonNullable<T>} Returns `object`, ensuring that the destination object is not `null` or `undefined`.
12
+ */
13
+ declare function defaults<T extends object>(object: T): NonNullable<T>;
14
+ /**
15
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
16
+ *
17
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
18
+ * It ensures that the destination object is not `null` or `undefined`.
19
+ *
20
+ * Note: This method mutates a first argument `object`.
21
+ *
22
+ * @template T - The destination object type.
23
+ * @template S - The source object type.
24
+ * @param {T} object - The destination object.
25
+ * @param {S} source - The source object.
26
+ * @returns {NonNullable<T & S>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
27
+ */
28
+ declare function defaults<T extends object, S extends object>(object: T, source: S): NonNullable<T & S>;
29
+ /**
30
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
31
+ *
32
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
33
+ * It ensures that the destination object is not `null` or `undefined`.
34
+ *
35
+ * Note: This method mutates a first argument `object`.
36
+ *
37
+ * @template T - The destination object type.
38
+ * @template S1 - The first source object type.
39
+ * @template S2 - The second source object type.
40
+ * @param {T} object - The destination object.
41
+ * @param {S1} source1 - The first source object.
42
+ * @param {S2} source2 - The second source object.
43
+ * @returns {NonNullable<T & S1 & S2>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
44
+ */
45
+ declare function defaults<T extends object, S1 extends object, S2 extends object>(object: T, source1: S1, source2: S2): NonNullable<T & S1 & S2>;
46
+ /**
47
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
48
+ *
49
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
50
+ * It ensures that the destination object is not `null` or `undefined`.
51
+ *
52
+ * Note: This method mutates a first argument `object`.
53
+ *
54
+ * @template T - The destination object type.
55
+ * @template S1 - The first source object type.
56
+ * @template S2 - The second source object type.
57
+ * @template S3 - The third source object type.
58
+ * @param {T} object - The destination object.
59
+ * @param {S1} source1 - The first source object.
60
+ * @param {S2} source2 - The second source object.
61
+ * @param {S3} source3 - The third source object.
62
+ * @returns {NonNullable<T & S1 & S2 & S3>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
63
+ */
64
+ declare function defaults<T extends object, S1 extends object, S2 extends object, S3 extends object>(object: T, source1: S1, source2: S2, source3: S3): NonNullable<T & S1 & S2 & S3>;
65
+ /**
66
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
67
+ *
68
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
69
+ * It ensures that the destination object is not `null` or `undefined`.
70
+ *
71
+ * Note: This method mutates a first argument `object`.
72
+ *
73
+ * @template T - The destination object type.
74
+ * @template S1 - The first source object type.
75
+ * @template S2 - The second source object type.
76
+ * @template S3 - The third source object type.
77
+ * @template S4 - The fourth source object type.
78
+ * @param {T} object - The destination object.
79
+ * @param {S1} source1 - The first source object.
80
+ * @param {S2} source2 - The second source object.
81
+ * @param {S3} source3 - The third source object.
82
+ * @param {S4} source4 - The fourth source object.
83
+ * @returns {NonNullable<T & S1 & S2 & S3 & S4>} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
84
+ */
85
+ declare function defaults<T extends object, S1 extends object, S2 extends object, S3 extends object, S4 extends object>(object: T, source1: S1, source2: S2, source3: S3, source4: S4): NonNullable<T & S1 & S2 & S3 & S4>;
86
+ /**
87
+ * Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to `undefined`.
88
+ *
89
+ * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
90
+ * It ensures that the destination object is not `null` or `undefined`.
91
+ *
92
+ * Note: This method mutates a first argument `object`.
93
+ *
94
+ * @template T - The destination object type.
95
+ * @template S - The source object type.
96
+ * @param {T} object - The destination object.
97
+ * @param {S[]} sources - The source objects.
98
+ * @returns {object} Returns the merged object, ensuring that the destination object is not `null` or `undefined`.
99
+ *
100
+ * @example
101
+ * defaults({ a: 1 }, { a: 2, b: 2 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
102
+ * defaults({ a: 1, b: 2 }, { b: 3 }, { c: 3 }); // { a: 1, b: 2, c: 3 }
103
+ * defaults({ a: null }, { a: 1 }); // { a: null }
104
+ * defaults({ a: undefined }, { a: 1 }); // { a: 1 }
105
+ */
106
+ declare function defaults<T extends object, S extends object>(object: T, ...sources: S[]): object;
107
+
108
+ export { defaults };
@@ -0,0 +1,21 @@
1
+ import { eq } from '../util/eq.mjs';
2
+
3
+ function defaults(object, ...sources) {
4
+ object = Object(object);
5
+ const objectProto = Object.prototype;
6
+ for (let i = 0; i < sources.length; i++) {
7
+ const source = sources[i];
8
+ const keys = Object.keys(source);
9
+ for (let j = 0; j < keys.length; j++) {
10
+ const key = keys[j];
11
+ const value = object[key];
12
+ if (value === undefined ||
13
+ (!Object.hasOwn(object, key) && eq(value, objectProto[key]))) {
14
+ object[key] = source[key];
15
+ }
16
+ }
17
+ }
18
+ return object;
19
+ }
20
+
21
+ export { defaults };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.23.0-dev.728+0e7ad28d",
4
+ "version": "1.23.0-dev.729+c6130a41",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {