@straylightagency/utils 1.1.9 → 1.2.1
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/object/only.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Return an object with specified keys.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
4
|
+
* @param {Object} object
|
|
5
|
+
* @param {Array} keys
|
|
6
|
+
* @returns {Object}
|
|
7
7
|
*/
|
|
8
8
|
export default function only(object, keys) {
|
|
9
9
|
return Object.entries( object ).reduce( (acc, [key, value]) => {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a shallow object containing only 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 pick(object, ...keys) {
|
|
10
|
+
const selected = new Set(keys.flat());
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(object).filter(([key]) => selected.has(key))
|
|
13
|
+
);
|
|
14
|
+
}
|
package/src/object/serialize.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
import replacer from "../misc/replacer.js";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Serialize an object into a string, transforms functions into string.
|
|
5
3
|
*
|
|
6
4
|
* @example serialize({foo: {bar: {doe: "Bob", {smith: "John"}}}})
|
|
7
5
|
* => {"foo":{"bar":{"doe":"Bob","smith":"John"}}}
|
|
8
6
|
*
|
|
9
|
-
* @param {
|
|
7
|
+
* @param {Object} object
|
|
10
8
|
* @returns {String}
|
|
11
9
|
*/
|
|
12
10
|
export default function serialize(object) {
|
|
13
11
|
return JSON.stringify( object, replacer );
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Function that can be used with JSON.stringify to replace {function} into string equivalent.
|
|
16
|
+
*
|
|
17
|
+
* @param {String} key
|
|
18
|
+
* @param {any} value
|
|
19
|
+
* @returns {*|undefined}
|
|
20
|
+
*/
|
|
21
|
+
function replacer(key, value) {
|
|
22
|
+
return typeof value === 'function' ? value.toString : value;
|
|
14
23
|
}
|
package/src/object/set.js
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* @example set({foo: {bar: {doe: "Bob"}}}, "foo.bar.smith", "John");
|
|
5
5
|
* => {foo: {bar: {doe: "Bob", {smith: "John"}}}}
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {
|
|
10
|
-
* @returns {
|
|
7
|
+
* @param {Object} object
|
|
8
|
+
* @param {String} keys
|
|
9
|
+
* @param {any} value
|
|
10
|
+
* @returns {any}
|
|
11
11
|
*/
|
|
12
12
|
export default function (object, keys, value) {
|
|
13
13
|
const arrKeys = keys.split('.');
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import dateReviver from '../date/reviver.js'
|
|
2
|
-
import functionReviver from '../misc/reviver.js'
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* Serialize an object into a string, handling functions and Date objects.
|
|
6
3
|
*
|
|
7
4
|
* @example serialize("{"foo":{"bar":{"doe":"Bob","smith":"John"}}}")
|
|
8
5
|
* => {"foo":{"bar":{"doe":"Bob","smith":"John"}}}
|
|
9
6
|
*
|
|
10
|
-
* @param {
|
|
11
|
-
* @returns {
|
|
7
|
+
* @param {String} object
|
|
8
|
+
* @returns {Object}
|
|
12
9
|
*/
|
|
13
10
|
export default function unserialize(object) {
|
|
14
11
|
return JSON.parse( object, (key, value) => {
|
|
@@ -27,4 +24,32 @@ export default function unserialize(object) {
|
|
|
27
24
|
|
|
28
25
|
return value;
|
|
29
26
|
} );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Function that can be used with JSON.parse to revive Date objects.
|
|
31
|
+
*
|
|
32
|
+
* @param {any} value
|
|
33
|
+
* @returns {Date|*}
|
|
34
|
+
*/
|
|
35
|
+
function dateReviver(value) {
|
|
36
|
+
if ( typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test( value ) ) {
|
|
37
|
+
return new Date( value );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Function that can be used with JSON.parse to revive functions.
|
|
45
|
+
*
|
|
46
|
+
* @param {any} value
|
|
47
|
+
* @returns {any}
|
|
48
|
+
*/
|
|
49
|
+
function functionReviver(value) {
|
|
50
|
+
if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
|
|
51
|
+
return new Function(`return ${value}`)();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
30
55
|
}
|
|
@@ -6,8 +6,8 @@ import capitalFirst from "./capitalFirst.js";
|
|
|
6
6
|
* @example capitalWords("The quick brown fox jumps over the lazy dog")
|
|
7
7
|
* => "The Quick Brown Fox Jumps Over The Lazy Dog"
|
|
8
8
|
*
|
|
9
|
-
* @param {
|
|
10
|
-
* @returns {
|
|
9
|
+
* @param {String} string
|
|
10
|
+
* @returns {String}
|
|
11
11
|
*/
|
|
12
12
|
export default function capitalWords(string) {
|
|
13
13
|
const str_arr = string.split(' ');
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
* @example interpolate("The quick :color fox jumps over :who", {color: "brown", who: "the lazy dog"});
|
|
5
5
|
* => "The quick brown fox jumps over the lazy dog"
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
7
|
+
* @param {String} string
|
|
8
|
+
* @param {Object} replace
|
|
9
|
+
* @returns {String}
|
|
10
10
|
*/
|
|
11
11
|
export default function interpolate(string, replace = {}) {
|
|
12
12
|
return string.replace( /:(\w+)/g, ( match, p1 ) =>
|
package/src/string/slugify.js
CHANGED
|
@@ -4,25 +4,17 @@
|
|
|
4
4
|
* @example slugify("The quick brown fox jumps over the lazy dog")
|
|
5
5
|
* => "the-quick-brown-fox-jumps-over-the-lazy-dog"
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
7
|
+
* @param {String} string
|
|
8
|
+
* @param {String} separator
|
|
9
|
+
* @returns {String}
|
|
10
10
|
*/
|
|
11
11
|
export default function slugify(string, separator = '-') {
|
|
12
|
-
string = string.trim().toLowerCase();
|
|
13
|
-
|
|
14
|
-
/** replaces specials characters with hyphens */
|
|
15
|
-
const from = "ãàáäâẽèéëêìíïîõòóöôøùúüûñç·/_,:;.";
|
|
16
|
-
const to = "aaaaaeeeeeiiiioooooouuuunc-------";
|
|
17
|
-
|
|
18
|
-
for ( let i = 0, l = from.length; i < l; i++ ) {
|
|
19
|
-
string = string.replace( from.charAt( i ), to.charAt( i ) );
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
string = string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
|
23
|
-
|
|
24
12
|
return string
|
|
25
|
-
.
|
|
26
|
-
.replace(
|
|
27
|
-
.
|
|
28
|
-
|
|
13
|
+
.normalize('NFD')
|
|
14
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
15
|
+
.toLowerCase()
|
|
16
|
+
.trim()
|
|
17
|
+
.replace(/[^a-z0-9]+/g, separator)
|
|
18
|
+
.replace(new RegExp(`${separator}+$`), '')
|
|
19
|
+
.replace(new RegExp(`^${separator}+`), '');
|
|
20
|
+
}
|
package/readme.MD
DELETED
|
File without changes
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Change a boolean value then restore that value after a timeout on miliseconds.
|
|
3
|
-
*
|
|
4
|
-
* Works fine with VueJS and signal based frameworks :
|
|
5
|
-
* restoreBooleanAfter( bool => messageSent.value = bool, true, 15_000 );
|
|
6
|
-
*
|
|
7
|
-
* messageSent will be set to "true" than restored to "false" after 15 sec.
|
|
8
|
-
*
|
|
9
|
-
* @param {Function} callbackFn
|
|
10
|
-
* @param {boolean} boolean
|
|
11
|
-
* @param {number} timeoutMs
|
|
12
|
-
*/
|
|
13
|
-
export default function restoreBooleanAfter(callbackFn, boolean, timeoutMs) {
|
|
14
|
-
const value = callbackFn( boolean );
|
|
15
|
-
|
|
16
|
-
setTimeout( () => callbackFn( ! value ), timeoutMs );
|
|
17
|
-
}
|