@straylightagency/utils 1.1.9 → 1.2.0
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.
- package/LICENSE +21 -0
- package/README.md +469 -0
- package/package.json +4 -3
- package/src/array/difference.js +13 -0
- package/src/array/flatten.js +18 -0
- package/src/array/groupBy.js +22 -0
- package/src/array/index.js +8 -1
- package/src/array/initial.js +4 -4
- package/src/array/intersection.js +18 -0
- package/src/array/last.js +3 -4
- package/src/array/move.js +27 -0
- package/src/array/remove.js +11 -8
- package/src/array/replace.js +6 -6
- package/src/array/shuffle.js +23 -0
- package/src/array/unique.js +10 -0
- package/src/date/getWeekNumber.js +1 -1
- package/src/date/reviver.js +2 -1
- package/src/dom/addEventListeners.js +2 -2
- package/src/dom/closestElement.js +1 -1
- package/src/dom/delegate.js +11 -8
- package/src/dom/index.js +4 -1
- package/src/dom/onEachElements.js +1 -1
- package/src/dom/stringToHtml.js +1 -1
- package/src/dom/unlessElementExists.js +1 -1
- package/src/dom/whenIntersect.js +24 -0
- package/src/dom/whenMutation.js +28 -0
- package/src/dom/whenResize.js +14 -13
- package/src/dom/whenScroll.js +14 -13
- package/src/dom/whenVisible.js +30 -0
- package/src/index.js +54 -24
- package/src/misc/attempt.js +2 -2
- package/src/misc/base64.js +8 -5
- package/src/misc/clipboard.js +1 -1
- package/src/misc/debounce.js +9 -7
- package/src/misc/index.js +7 -14
- package/src/misc/is.js +20 -15
- package/src/misc/match.js +36 -21
- package/src/misc/milliseconds.js +1 -1
- package/src/misc/once.js +24 -0
- package/src/misc/replacer.js +4 -3
- package/src/misc/restoreAfter.js +28 -0
- package/src/misc/retry.js +4 -4
- package/src/misc/reviver.js +3 -2
- package/src/misc/seconds.js +1 -1
- package/src/misc/sleep.js +34 -0
- package/src/misc/throttle.js +4 -2
- package/src/number/clamp.js +7 -5
- package/src/number/format.js +7 -5
- package/src/number/random.js +14 -4
- package/src/object/deepClone.js +71 -0
- package/src/object/deepEqual.js +57 -0
- package/src/object/entries.js +10 -0
- package/src/object/filter.js +2 -2
- package/src/object/get.js +9 -6
- package/src/object/has.js +16 -8
- package/src/object/index.js +11 -1
- package/src/object/invert.js +22 -0
- package/src/object/isObject.js +9 -0
- package/src/object/isPlainObject.js +9 -0
- package/src/object/isType.js +14 -0
- package/src/object/map.js +2 -2
- package/src/object/merge.js +46 -0
- package/src/object/omit.js +14 -0
- package/src/object/only.js +3 -3
- package/src/object/pick.js +14 -0
- package/src/object/serialize.js +12 -3
- package/src/object/set.js +4 -4
- package/src/object/unserialize.js +30 -5
- package/src/string/capitalFirst.js +2 -2
- package/src/string/capitalWords.js +2 -2
- package/src/string/interpolate.js +3 -3
- package/src/string/slugify.js +11 -19
- package/readme.MD +0 -0
- package/src/misc/restoreBooleanAfter.js +0 -17
package/src/misc/match.js
CHANGED
|
@@ -1,33 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @param {*} value
|
|
5
|
-
* @returns {Object}
|
|
6
|
-
*/
|
|
7
|
-
export default function match(value) {
|
|
8
|
-
const matchable = function () {
|
|
1
|
+
class Matcher {
|
|
2
|
+
constructor(value) {
|
|
9
3
|
this.value = value;
|
|
10
|
-
this.
|
|
4
|
+
this.cases = new Map();
|
|
11
5
|
this.defaultCallback = () => this.value;
|
|
12
6
|
}
|
|
13
7
|
|
|
14
|
-
|
|
15
|
-
this.
|
|
8
|
+
is(key, callbackFn) {
|
|
9
|
+
this.cases.set(key, callbackFn);
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
16
12
|
|
|
13
|
+
default(callbackFn) {
|
|
14
|
+
this.defaultCallback = callbackFn;
|
|
17
15
|
return this;
|
|
18
|
-
}
|
|
16
|
+
}
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
return() {
|
|
19
|
+
const callbackFn = this.cases.get(this.value)
|
|
20
|
+
?? this.defaultCallback;
|
|
22
21
|
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
22
|
+
return callbackFn(this.value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Functional switch, match a value based on a key from a Map.
|
|
28
|
+
*
|
|
29
|
+
* @param {any} value
|
|
30
|
+
* @param {Object} cases
|
|
31
|
+
* @returns {Object}
|
|
32
|
+
*/
|
|
33
|
+
export default function match(value, cases = {}) {
|
|
34
|
+
const matcher = new Matcher(value);
|
|
35
|
+
|
|
36
|
+
if ( Object.keys( cases ).length > 0 ) {
|
|
37
|
+
Object.entries( cases ).forEach( ([key, callbackFn]) => {
|
|
38
|
+
if ( key === 'default' ) {
|
|
39
|
+
matcher.default( callbackFn );
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
28
42
|
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
matcher.is( key, callbackFn );
|
|
44
|
+
} );
|
|
45
|
+
}
|
|
31
46
|
|
|
32
|
-
return
|
|
47
|
+
return matcher;
|
|
33
48
|
}
|
package/src/misc/milliseconds.js
CHANGED
package/src/misc/once.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that invokes `callback` at most once.
|
|
3
|
+
* The first return value (including a Promise) is cached.
|
|
4
|
+
*
|
|
5
|
+
* @param {Function} callbackFn
|
|
6
|
+
* @returns {Function}
|
|
7
|
+
*/
|
|
8
|
+
export default function once(callbackFn) {
|
|
9
|
+
if ( typeof callbackFn !== 'function' ) {
|
|
10
|
+
throw new TypeError('once() expects a function');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let called = false;
|
|
14
|
+
let result;
|
|
15
|
+
|
|
16
|
+
return function (...args) {
|
|
17
|
+
if ( ! called ) {
|
|
18
|
+
called = true;
|
|
19
|
+
result = callbackFn.apply( this, args );
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
}
|
package/src/misc/replacer.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
Function that can be used with JSON.stringify to replace {function} into string equivalent.
|
|
2
|
+
* Function that can be used with JSON.stringify to replace {function} into string equivalent.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @param {
|
|
4
|
+
* @deprecated
|
|
5
|
+
* @param {String} key
|
|
6
|
+
* @param {any} value
|
|
6
7
|
* @returns {*|undefined}
|
|
7
8
|
*/
|
|
8
9
|
export default function replacer(key, value) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Change a value then restore that value after a timeout on milliseconds.
|
|
3
|
+
*
|
|
4
|
+
* Works fine with Vue.js and signal based frameworks :
|
|
5
|
+
* restoreAfter( bool => messageSent.value = bool, true, false, 15_000 );
|
|
6
|
+
*
|
|
7
|
+
* messageSent will be set to "true" than restored to "false" after 15 sec.
|
|
8
|
+
*
|
|
9
|
+
* @param {Function} callbackFn
|
|
10
|
+
* @param {any} from
|
|
11
|
+
* @param {any} to
|
|
12
|
+
* @param {Number} timeoutMs
|
|
13
|
+
* @returns {{cancel: function(): void, flush: function(): void}}
|
|
14
|
+
*/
|
|
15
|
+
export default function restoreAfter(callbackFn, from, to, timeoutMs) {
|
|
16
|
+
callbackFn( from );
|
|
17
|
+
|
|
18
|
+
const flush = () => {
|
|
19
|
+
callbackFn(to);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const timer = setTimeout( flush, timeoutMs );
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
cancel: () => clearTimeout( timer ),
|
|
26
|
+
flush
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/misc/retry.js
CHANGED
|
@@ -12,11 +12,11 @@ import random from "../number/random.js";
|
|
|
12
12
|
* throw "Failed after 5 tries";
|
|
13
13
|
* }, 5000, 10000 );
|
|
14
14
|
*
|
|
15
|
-
* @param {
|
|
15
|
+
* @param {Number} times
|
|
16
16
|
* @param {Function} callbackFn
|
|
17
|
-
* @param {
|
|
18
|
-
* @param {
|
|
19
|
-
* @returns {Promise<
|
|
17
|
+
* @param {Number} sleepMsMin
|
|
18
|
+
* @param {Number|null} sleepMsMax
|
|
19
|
+
* @returns {Promise<void>}
|
|
20
20
|
*/
|
|
21
21
|
export default function (times, callbackFn, sleepMsMin = 100, sleepMsMax = null) {
|
|
22
22
|
return new Promise( ( resolve, reject ) => {
|
package/src/misc/reviver.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Function that can be used with JSON.parse to revive functions.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
4
|
+
* @deprecated
|
|
5
|
+
* @param {any} value
|
|
6
|
+
* @returns {any}
|
|
6
7
|
*/
|
|
7
8
|
export default function reviver(value) {
|
|
8
9
|
if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
|
package/src/misc/seconds.js
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits for `duration` milliseconds.
|
|
3
|
+
* Pass an AbortSignal as the second argument to cancel the wait.
|
|
4
|
+
*
|
|
5
|
+
* @param {Number} duration
|
|
6
|
+
* @param {signal: AbortSignal}
|
|
7
|
+
* @returns Promise
|
|
8
|
+
*/
|
|
9
|
+
export default function sleep(duration = 0, { signal } = {}) {
|
|
10
|
+
if (!Number.isFinite(duration) || duration < 0) {
|
|
11
|
+
throw new RangeError('sleep() duration must be a non-negative finite number');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (signal?.aborted) {
|
|
15
|
+
return Promise.reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const timer = setTimeout(done, duration);
|
|
20
|
+
|
|
21
|
+
function done() {
|
|
22
|
+
signal?.removeEventListener('abort', abort);
|
|
23
|
+
resolve();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function abort() {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
signal?.removeEventListener('abort', abort);
|
|
29
|
+
reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
33
|
+
});
|
|
34
|
+
}
|
package/src/misc/throttle.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Limit the number of times a callback gets called in a certain time period.
|
|
2
|
+
* Limit the number of times a callback gets called in a certain time period. Returned function by the throttler allows to cancel the internal timer.
|
|
3
3
|
*
|
|
4
4
|
* @param {Function} callbackFn
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {Number} delay
|
|
6
6
|
* @returns {(function(): void)|*}
|
|
7
7
|
*/
|
|
8
8
|
export default function throttle(callbackFn, delay) {
|
|
@@ -24,5 +24,7 @@ export default function throttle(callbackFn, delay) {
|
|
|
24
24
|
last = now;
|
|
25
25
|
callbackFn.apply( context, args );
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
return () => clearTimeout( timer );
|
|
27
29
|
};
|
|
28
30
|
}
|
package/src/number/clamp.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @
|
|
2
|
+
* Clamps a value within a range of values between a defined minimum bound and a maximum bound.
|
|
3
|
+
*
|
|
4
|
+
* @param {Number} value
|
|
5
|
+
* @param {Number} min
|
|
6
|
+
* @param {Number} max
|
|
7
|
+
* @returns {Number}
|
|
6
8
|
*/
|
|
7
|
-
export default function
|
|
9
|
+
export default function clamp(value, min, max) {
|
|
8
10
|
return Math.max(min, Math.min(max, value));
|
|
9
11
|
}
|
package/src/number/format.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
6
|
-
* @
|
|
2
|
+
* Format a number into a string with the given separators for thousands and decimal.
|
|
3
|
+
*
|
|
4
|
+
* @param {Number} number
|
|
5
|
+
* @param {String} thousandsSeparator
|
|
6
|
+
* @param {String} decimalSeparator
|
|
7
|
+
* @param {Number} decimal
|
|
8
|
+
* @returns {String}
|
|
7
9
|
*/
|
|
8
10
|
export default function format(number, thousandsSeparator = ' ', decimalSeparator = ',', decimal = 2) {
|
|
9
11
|
return parseFloat( number.toString() ).toFixed( decimal ).replace( '.', decimalSeparator ).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + thousandsSeparator );
|
package/src/number/random.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
2
|
+
* Return a random number between a min and a max number.
|
|
3
|
+
*
|
|
4
|
+
* @param {Number} min
|
|
5
|
+
* @param {Number} max
|
|
6
|
+
* @returns {Number}
|
|
5
7
|
*/
|
|
6
|
-
export default function random(min, max) {
|
|
8
|
+
export default function random(min = 0, max = 100) {
|
|
9
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
10
|
+
throw new TypeError('min and max must be finite numbers');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (min > max) {
|
|
14
|
+
throw new RangeError('min must be less than or equal to max');
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
return Math.floor( Math.random() * ( max - min + 1 ) + min );
|
|
8
18
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep-clones common built-in data structures and preserves circular references.
|
|
3
|
+
* Functions are returned by reference. For supported platform-specific objects,
|
|
4
|
+
* prefer native structuredClone() when its semantics suit your use case.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} value
|
|
7
|
+
* @param {WeakMap} seen
|
|
8
|
+
* @returns {Object}
|
|
9
|
+
*/
|
|
10
|
+
export default function deepClone(value, seen = new WeakMap()) {
|
|
11
|
+
if (value === null || typeof value !== 'object') return value;
|
|
12
|
+
if (seen.has(value)) return seen.get(value);
|
|
13
|
+
|
|
14
|
+
if (value instanceof Date) return new Date(value.getTime());
|
|
15
|
+
if (value instanceof RegExp) {
|
|
16
|
+
const clone = new RegExp(value.source, value.flags);
|
|
17
|
+
clone.lastIndex = value.lastIndex;
|
|
18
|
+
return clone;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (value instanceof Map) {
|
|
22
|
+
const clone = new Map();
|
|
23
|
+
seen.set(value, clone);
|
|
24
|
+
for (const [key, item] of value) {
|
|
25
|
+
clone.set(deepClone(key, seen), deepClone(item, seen));
|
|
26
|
+
}
|
|
27
|
+
return clone;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (value instanceof Set) {
|
|
31
|
+
const clone = new Set();
|
|
32
|
+
seen.set(value, clone);
|
|
33
|
+
for (const item of value) clone.add(deepClone(item, seen));
|
|
34
|
+
return clone;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (value instanceof ArrayBuffer) {
|
|
38
|
+
const clone = value.slice(0);
|
|
39
|
+
seen.set(value, clone);
|
|
40
|
+
return clone;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (ArrayBuffer.isView(value)) {
|
|
44
|
+
if (value instanceof DataView) {
|
|
45
|
+
const clone = new DataView(
|
|
46
|
+
deepClone(value.buffer, seen),
|
|
47
|
+
value.byteOffset,
|
|
48
|
+
value.byteLength
|
|
49
|
+
);
|
|
50
|
+
seen.set(value, clone);
|
|
51
|
+
return clone;
|
|
52
|
+
}
|
|
53
|
+
const clone = new value.constructor(value);
|
|
54
|
+
seen.set(value, clone);
|
|
55
|
+
return clone;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const clone = Array.isArray(value) ? [] : Object.create(Object.getPrototypeOf(value));
|
|
59
|
+
|
|
60
|
+
seen.set(value, clone);
|
|
61
|
+
|
|
62
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
63
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
64
|
+
if ('value' in descriptor) {
|
|
65
|
+
descriptor.value = deepClone(descriptor.value, seen);
|
|
66
|
+
}
|
|
67
|
+
Object.defineProperty(clone, key, descriptor);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return clone;
|
|
71
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep comparison for arrays, plain/class objects and common built-ins.
|
|
3
|
+
* Circular references are supported. Functions compare by reference.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} a
|
|
6
|
+
* @param {Object} b
|
|
7
|
+
* @param {WeakMap} pairs
|
|
8
|
+
* @returns {Boolean}
|
|
9
|
+
*/
|
|
10
|
+
export default function deepEqual(a, b, pairs = new WeakMap()) {
|
|
11
|
+
if (Object.is(a, b)) return true;
|
|
12
|
+
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
|
|
16
|
+
|
|
17
|
+
let paired = pairs.get(a);
|
|
18
|
+
if (paired?.has(b)) return true;
|
|
19
|
+
if (!paired) pairs.set(a, paired = new WeakSet());
|
|
20
|
+
paired.add(b);
|
|
21
|
+
|
|
22
|
+
if (a instanceof Date) return a.getTime() === b.getTime();
|
|
23
|
+
if (a instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
24
|
+
if (a instanceof ArrayBuffer) {
|
|
25
|
+
return a.byteLength === b.byteLength &&
|
|
26
|
+
new Uint8Array(a).every((byte, i) => byte === new Uint8Array(b)[i]);
|
|
27
|
+
}
|
|
28
|
+
if (ArrayBuffer.isView(a)) {
|
|
29
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
30
|
+
const av = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
|
31
|
+
const bv = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
32
|
+
return av.every((byte, i) => byte === bv[i]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (a instanceof Map) {
|
|
36
|
+
if (a.size !== b.size) return false;
|
|
37
|
+
for (const [key, value] of a) {
|
|
38
|
+
if (!b.has(key) || !deepEqual(value, b.get(key), pairs)) return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (a instanceof Set) {
|
|
44
|
+
if (a.size !== b.size) return false;
|
|
45
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const keysA = Reflect.ownKeys(a);
|
|
50
|
+
const keysB = Reflect.ownKeys(b);
|
|
51
|
+
if (keysA.length !== keysB.length) return false;
|
|
52
|
+
|
|
53
|
+
return keysA.every(key =>
|
|
54
|
+
Object.hasOwn(b, key) &&
|
|
55
|
+
deepEqual(a[key], b[key], pairs)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an array of an object's own enumerable string-keyed [key, value] pairs.
|
|
3
|
+
* This intentionally follows Object.entries() semantics.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} value
|
|
6
|
+
* @returns {Array}
|
|
7
|
+
*/
|
|
8
|
+
export default function entries(value) {
|
|
9
|
+
return Object.entries(value);
|
|
10
|
+
}
|
package/src/object/filter.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Filter attributes of an object using a callback function.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {Object} object
|
|
5
5
|
* @param {Function} callbackFn
|
|
6
|
-
* @returns {{[p:
|
|
6
|
+
* @returns {{[p: String]: unknown}}
|
|
7
7
|
*/
|
|
8
8
|
export default function filter(object, callbackFn) {
|
|
9
9
|
return Object.fromEntries( Object.entries( object ).
|
package/src/object/get.js
CHANGED
|
@@ -4,10 +4,13 @@
|
|
|
4
4
|
* @example get({foo: {bar: {doe: "Bob"}}}, "foo.bar.doe")
|
|
5
5
|
* => "Bob"
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
7
|
+
* @param {Object} object
|
|
8
|
+
* @param {String} path
|
|
9
|
+
* @returns {any}
|
|
10
10
|
*/
|
|
11
|
-
export default function (object,
|
|
12
|
-
return
|
|
13
|
-
|
|
11
|
+
export default function get(object, path) {
|
|
12
|
+
return path.split('.').reduce(
|
|
13
|
+
(acc, key) => acc?.[key],
|
|
14
|
+
object
|
|
15
|
+
);
|
|
16
|
+
}
|
package/src/object/has.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import get from "./get.js";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Get a value using a dot-notated key string
|
|
5
3
|
*
|
|
6
|
-
* @param {
|
|
7
|
-
* @param {
|
|
8
|
-
* @returns {
|
|
4
|
+
* @param {Object} object
|
|
5
|
+
* @param {String} path
|
|
6
|
+
* @returns {any}
|
|
9
7
|
*/
|
|
10
|
-
export default function (object,
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export default function has(object, path) {
|
|
9
|
+
const keys = path.split('.');
|
|
10
|
+
|
|
11
|
+
return keys.every((key, index) => {
|
|
12
|
+
if (object == null || !Object.hasOwn(object, key)) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
object = object[key];
|
|
17
|
+
|
|
18
|
+
return index === keys.length - 1 || object != null;
|
|
19
|
+
});
|
|
20
|
+
}
|
package/src/object/index.js
CHANGED
|
@@ -5,4 +5,14 @@ export { default as unserialize } from "./unserialize.js";
|
|
|
5
5
|
export { default as only } from "./only.js";
|
|
6
6
|
export { default as set } from "./set.js";
|
|
7
7
|
export { default as get } from "./get.js";
|
|
8
|
-
export { default as has } from "./has.js";
|
|
8
|
+
export { default as has } from "./has.js";
|
|
9
|
+
export { default as entries } from './entries.js';
|
|
10
|
+
export { default as deepClone } from './deepClone.js';
|
|
11
|
+
export { default as deepEqual } from './deepEqual.js';
|
|
12
|
+
export { default as invert } from './invert.js';
|
|
13
|
+
export { default as merge } from './merge.js';
|
|
14
|
+
export { default as omit } from './omit.js';
|
|
15
|
+
export { default as pick } from './pick.js';
|
|
16
|
+
export { default as isType } from './isType.js';
|
|
17
|
+
export { default as isObject } from './isObject.js';
|
|
18
|
+
export { default as isPlainObject } from './isPlainObject.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverses an object's enumerable own string-keyed entries.
|
|
3
|
+
* Values become property keys (therefore are string-coerced).
|
|
4
|
+
* Duplicate values: the last key wins.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} object
|
|
7
|
+
* @returns {Object}
|
|
8
|
+
*/
|
|
9
|
+
export default function invert(object) {
|
|
10
|
+
const result = {};
|
|
11
|
+
|
|
12
|
+
for (const [key, value] of Object.entries(object)) {
|
|
13
|
+
Object.defineProperty(result, String(value), {
|
|
14
|
+
value: key,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the given value is defined as a Plain Object
|
|
3
|
+
*
|
|
4
|
+
* @param {any} value
|
|
5
|
+
* @return {Boolean}
|
|
6
|
+
*/
|
|
7
|
+
export default function isPlainObject(value) {
|
|
8
|
+
return value !== null && typeof value === 'object' && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import isObject from "./isObject.js";
|
|
2
|
+
|
|
3
|
+
const toString = Object.prototype.toString;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Check if the given value is defined as an Object with given 'className'.
|
|
7
|
+
*
|
|
8
|
+
* @param {any} value
|
|
9
|
+
* @param {String} className
|
|
10
|
+
* @return {Boolean}
|
|
11
|
+
*/
|
|
12
|
+
export default function isType(value, className) {
|
|
13
|
+
return isObject( value ) && toString.call( value ) === '[object ' + className + ']';
|
|
14
|
+
}
|
package/src/object/map.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Apply a map callback function over an object.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {Object} object
|
|
5
5
|
* @param {Function} callbackFn
|
|
6
|
-
* @returns {
|
|
6
|
+
* @returns {Object}
|
|
7
7
|
*/
|
|
8
8
|
export default function map(object, callbackFn) {
|
|
9
9
|
return Object.keys( object ).reduce( ( result, key ) => {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {isPlainObject} from "../misc/index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Deep-merges source objects into a new object.
|
|
5
|
+
* Plain objects merge recursively; arrays and other values replace.
|
|
6
|
+
* Does not mutate the source objects.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} objects
|
|
9
|
+
* @returns {Object}
|
|
10
|
+
*/
|
|
11
|
+
export default function merge(...objects) {
|
|
12
|
+
return objects.reduce((result, source) => {
|
|
13
|
+
if (source == null) return result;
|
|
14
|
+
if (!isPlainObject(source)) {
|
|
15
|
+
throw new TypeError('merge() expects plain objects or nullish values');
|
|
16
|
+
}
|
|
17
|
+
return mergeInto(result, source);
|
|
18
|
+
}, {});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const define = (target, key, value) => {
|
|
22
|
+
Object.defineProperty(target, key, {
|
|
23
|
+
value,
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function mergeInto(target, source) {
|
|
31
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
32
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
33
|
+
|
|
34
|
+
const sourceValue = source[key];
|
|
35
|
+
const targetValue = Object.hasOwn(target, key) ? target[key] : undefined;
|
|
36
|
+
|
|
37
|
+
if (isPlainObject(sourceValue)) {
|
|
38
|
+
const base = isPlainObject(targetValue) ? targetValue : {};
|
|
39
|
+
define(target, key, mergeInto({ ...base }, sourceValue));
|
|
40
|
+
} else {
|
|
41
|
+
define(target, key, sourceValue);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return target;
|
|
46
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a shallow copy excluding the provided own enumerable keys.
|
|
3
|
+
* `keys` may be an array or a list of key arguments.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} object
|
|
6
|
+
* @param {Array} keys
|
|
7
|
+
* @returns {Object}
|
|
8
|
+
*/
|
|
9
|
+
export default function omit(object, ...keys) {
|
|
10
|
+
const excluded = new Set(keys.flat());
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(object).filter(([key]) => !excluded.has(key))
|
|
13
|
+
);
|
|
14
|
+
}
|