@straylightagency/utils 1.1.6 → 1.1.7
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/package.json +36 -36
- package/src/array/index.js +3 -3
- package/src/array/initial.js +10 -10
- package/src/array/last.js +11 -11
- package/src/array/remove.js +16 -16
- package/src/array/replace.js +16 -16
- package/src/date/clone.js +9 -9
- package/src/date/getWeekNumber.js +17 -17
- package/src/date/index.js +2 -2
- package/src/date/reviver.js +12 -12
- package/src/dom/addEventListeners.js +21 -21
- package/src/dom/closestElement.js +31 -31
- package/src/dom/delegate.js +19 -19
- package/src/dom/getElementSizing.js +77 -77
- package/src/dom/getFieldsValues.js +17 -17
- package/src/dom/index.js +10 -10
- package/src/dom/onEachElements.js +34 -34
- package/src/dom/stringToHtml.js +18 -18
- package/src/dom/unlessElementExists.js +34 -34
- package/src/dom/whenReady.js +9 -9
- package/src/dom/whenResize.js +31 -31
- package/src/dom/whenScroll.js +31 -31
- package/src/index.js +52 -52
- package/src/misc/attempt.js +20 -20
- package/src/misc/base64.js +47 -47
- package/src/misc/clipboard.js +24 -24
- package/src/misc/debounce.js +25 -25
- package/src/misc/index.js +23 -23
- package/src/misc/is.js +31 -31
- package/src/misc/match.js +32 -32
- package/src/misc/milliseconds.js +8 -8
- package/src/misc/nextTick.js +12 -12
- package/src/misc/replacer.js +9 -9
- package/src/misc/restoreBooleanAfter.js +16 -16
- package/src/misc/retry.js +42 -42
- package/src/misc/reviver.js +12 -12
- package/src/misc/seconds.js +10 -10
- package/src/misc/throttle.js +28 -28
- package/src/number/format.js +10 -10
- package/src/number/index.js +2 -2
- package/src/number/random.js +8 -8
- package/src/object/filter.js +10 -10
- package/src/object/get.js +13 -13
- package/src/object/has.js +12 -12
- package/src/object/index.js +7 -7
- package/src/object/map.js +12 -12
- package/src/object/only.js +16 -16
- package/src/object/serialize.js +13 -13
- package/src/object/set.js +29 -29
- package/src/object/unserialize.js +29 -29
- package/src/string/capitalFirst.js +14 -14
- package/src/string/capitalWords.js +20 -20
- package/src/string/index.js +3 -3
- package/src/string/interpolate.js +15 -15
- package/src/string/slugify.js +28 -28
package/src/misc/debounce.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Execute callback once if called many times during a defined delay.
|
|
3
|
-
*
|
|
4
|
-
* @param {Function} callbackFn
|
|
5
|
-
* @param {number} delay
|
|
6
|
-
* @param {boolean} immediate
|
|
7
|
-
* @returns {(function(): void)|*}
|
|
8
|
-
*/
|
|
9
|
-
export default function debounce(callbackFn, delay, immediate = false) {
|
|
10
|
-
let timer;
|
|
11
|
-
|
|
12
|
-
return (...args) => {
|
|
13
|
-
const now = immediate && !timer;
|
|
14
|
-
|
|
15
|
-
clearTimeout( timer );
|
|
16
|
-
|
|
17
|
-
timer = setTimeout( () => {
|
|
18
|
-
timer = undefined;
|
|
19
|
-
|
|
20
|
-
if ( !immediate ) callbackFn.apply( this, args );
|
|
21
|
-
}, delay );
|
|
22
|
-
|
|
23
|
-
if ( now ) callbackFn.apply( this, args );
|
|
24
|
-
};
|
|
25
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Execute callback once if called many times during a defined delay.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} callbackFn
|
|
5
|
+
* @param {number} delay
|
|
6
|
+
* @param {boolean} immediate
|
|
7
|
+
* @returns {(function(): void)|*}
|
|
8
|
+
*/
|
|
9
|
+
export default function debounce(callbackFn, delay, immediate = false) {
|
|
10
|
+
let timer;
|
|
11
|
+
|
|
12
|
+
return (...args) => {
|
|
13
|
+
const now = immediate && !timer;
|
|
14
|
+
|
|
15
|
+
clearTimeout( timer );
|
|
16
|
+
|
|
17
|
+
timer = setTimeout( () => {
|
|
18
|
+
timer = undefined;
|
|
19
|
+
|
|
20
|
+
if ( !immediate ) callbackFn.apply( this, args );
|
|
21
|
+
}, delay );
|
|
22
|
+
|
|
23
|
+
if ( now ) callbackFn.apply( this, args );
|
|
24
|
+
};
|
|
25
|
+
}
|
package/src/misc/index.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
export { default as clipboard } from "./clipboard.js";
|
|
2
|
-
export { default as debounce } from "./debounce.js";
|
|
3
|
-
export { default as nextTick } from "./nextTick.js";
|
|
4
|
-
export { default as throttle } from "./throttle.js";
|
|
5
|
-
export { default as attempt } from "./attempt.js";
|
|
6
|
-
export { default as retry } from "./retry.js";
|
|
7
|
-
export { default as reviver } from "./reviver.js";
|
|
8
|
-
export { default as replacer } from "./replacer.js";
|
|
9
|
-
export { default as match } from "./match.js";
|
|
10
|
-
export { default as seconds } from "./seconds.js";
|
|
11
|
-
export { default as milliseconds } from "./milliseconds.js";
|
|
12
|
-
export { default as restoreBooleanAfter } from "./restoreBooleanAfter.js";
|
|
13
|
-
|
|
14
|
-
import {isClass, isObject, isPlainObject} from "./is.js";
|
|
15
|
-
import {decodeBase64, encodeBase64, isBase64} from "./base64.js";
|
|
16
|
-
|
|
17
|
-
export {
|
|
18
|
-
isClass,
|
|
19
|
-
isObject,
|
|
20
|
-
isPlainObject,
|
|
21
|
-
decodeBase64,
|
|
22
|
-
encodeBase64,
|
|
23
|
-
isBase64,
|
|
1
|
+
export { default as clipboard } from "./clipboard.js";
|
|
2
|
+
export { default as debounce } from "./debounce.js";
|
|
3
|
+
export { default as nextTick } from "./nextTick.js";
|
|
4
|
+
export { default as throttle } from "./throttle.js";
|
|
5
|
+
export { default as attempt } from "./attempt.js";
|
|
6
|
+
export { default as retry } from "./retry.js";
|
|
7
|
+
export { default as reviver } from "./reviver.js";
|
|
8
|
+
export { default as replacer } from "./replacer.js";
|
|
9
|
+
export { default as match } from "./match.js";
|
|
10
|
+
export { default as seconds } from "./seconds.js";
|
|
11
|
+
export { default as milliseconds } from "./milliseconds.js";
|
|
12
|
+
export { default as restoreBooleanAfter } from "./restoreBooleanAfter.js";
|
|
13
|
+
|
|
14
|
+
import {isClass, isObject, isPlainObject} from "./is.js";
|
|
15
|
+
import {decodeBase64, encodeBase64, isBase64} from "./base64.js";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
isClass,
|
|
19
|
+
isObject,
|
|
20
|
+
isPlainObject,
|
|
21
|
+
decodeBase64,
|
|
22
|
+
encodeBase64,
|
|
23
|
+
isBase64,
|
|
24
24
|
};
|
package/src/misc/is.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
const toString = Object.prototype.toString;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Check if parameter is an object.
|
|
5
|
-
*
|
|
6
|
-
* @param {*} object
|
|
7
|
-
* @returns {boolean}
|
|
8
|
-
*/
|
|
9
|
-
export function isObject(object) {
|
|
10
|
-
return object !== null && typeof object === 'object'
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Check if parameter is the given className.
|
|
15
|
-
*
|
|
16
|
-
* @param {*} object
|
|
17
|
-
* @param {string} className
|
|
18
|
-
* @returns {boolean}
|
|
19
|
-
*/
|
|
20
|
-
export function isClass(object, className) {
|
|
21
|
-
return isObject( object ) && toString.call( object ) === '[object ' + className + ']';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Check if parameter is a plain object.
|
|
26
|
-
*
|
|
27
|
-
* @param {*} object
|
|
28
|
-
* @returns {boolean}
|
|
29
|
-
*/
|
|
30
|
-
export function isPlainObject(object) {
|
|
31
|
-
return isClass( object, 'Object' );
|
|
1
|
+
const toString = Object.prototype.toString;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if parameter is an object.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} object
|
|
7
|
+
* @returns {boolean}
|
|
8
|
+
*/
|
|
9
|
+
export function isObject(object) {
|
|
10
|
+
return object !== null && typeof object === 'object'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check if parameter is the given className.
|
|
15
|
+
*
|
|
16
|
+
* @param {*} object
|
|
17
|
+
* @param {string} className
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
*/
|
|
20
|
+
export function isClass(object, className) {
|
|
21
|
+
return isObject( object ) && toString.call( object ) === '[object ' + className + ']';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check if parameter is a plain object.
|
|
26
|
+
*
|
|
27
|
+
* @param {*} object
|
|
28
|
+
* @returns {boolean}
|
|
29
|
+
*/
|
|
30
|
+
export function isPlainObject(object) {
|
|
31
|
+
return isClass( object, 'Object' );
|
|
32
32
|
}
|
package/src/misc/match.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove a value from the given array, returning a copy.
|
|
3
|
-
*
|
|
4
|
-
* @param {*} value
|
|
5
|
-
* @returns {Object}
|
|
6
|
-
*/
|
|
7
|
-
export default function match(value) {
|
|
8
|
-
const matchable = function () {
|
|
9
|
-
this.value = value;
|
|
10
|
-
this.heap = new Map;
|
|
11
|
-
this.defaultCallback = () => this.value;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
matchable.prototype.is = function (key, callback) {
|
|
15
|
-
this.heap.set( key, callback );
|
|
16
|
-
|
|
17
|
-
return this;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
matchable.prototype.default = function (callback) {
|
|
21
|
-
this.defaultCallback = callback;
|
|
22
|
-
|
|
23
|
-
return this;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
matchable.prototype.return = function () {
|
|
27
|
-
const callback = this.heap.has( this.value ) ? this.heap.get( this.value ) : this.defaultCallback;
|
|
28
|
-
|
|
29
|
-
return callback();
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
return new matchable( value );
|
|
1
|
+
/**
|
|
2
|
+
* Remove a value from the given array, returning a copy.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} value
|
|
5
|
+
* @returns {Object}
|
|
6
|
+
*/
|
|
7
|
+
export default function match(value) {
|
|
8
|
+
const matchable = function () {
|
|
9
|
+
this.value = value;
|
|
10
|
+
this.heap = new Map;
|
|
11
|
+
this.defaultCallback = () => this.value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
matchable.prototype.is = function (key, callback) {
|
|
15
|
+
this.heap.set( key, callback );
|
|
16
|
+
|
|
17
|
+
return this;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
matchable.prototype.default = function (callback) {
|
|
21
|
+
this.defaultCallback = callback;
|
|
22
|
+
|
|
23
|
+
return this;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
matchable.prototype.return = function () {
|
|
27
|
+
const callback = this.heap.has( this.value ) ? this.heap.get( this.value ) : this.defaultCallback;
|
|
28
|
+
|
|
29
|
+
return callback();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return new matchable( value );
|
|
33
33
|
}
|
package/src/misc/milliseconds.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stop the main process for a moment defined in milliseconds.
|
|
3
|
-
*
|
|
4
|
-
* @param {number} timeMs
|
|
5
|
-
* @returns {Promise<*>}
|
|
6
|
-
*/
|
|
7
|
-
export default function milliseconds(timeMs) {
|
|
8
|
-
return new Promise( resolve => setTimeout( resolve, timeMs ) );
|
|
1
|
+
/**
|
|
2
|
+
* Stop the main process for a moment defined in milliseconds.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} timeMs
|
|
5
|
+
* @returns {Promise<*>}
|
|
6
|
+
*/
|
|
7
|
+
export default function milliseconds(timeMs) {
|
|
8
|
+
return new Promise( resolve => setTimeout( resolve, timeMs ) );
|
|
9
9
|
}
|
package/src/misc/nextTick.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Execute the given callback function during a browser's idle periods. Allow to perform background task on the main thread without impacting latency-critical events.
|
|
3
|
-
*
|
|
4
|
-
* @param {Function} callbackFn
|
|
5
|
-
* @returns {Promise<unknown>}
|
|
6
|
-
*/
|
|
7
|
-
export default async function nextTick(callbackFn) {
|
|
8
|
-
return new Promise(
|
|
9
|
-
async resolve => requestIdleCallback( () =>
|
|
10
|
-
setTimeout( async () => resolve( await callbackFn() ), 0 )
|
|
11
|
-
)
|
|
12
|
-
);
|
|
1
|
+
/**
|
|
2
|
+
* Execute the given callback function during a browser's idle periods. Allow to perform background task on the main thread without impacting latency-critical events.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} callbackFn
|
|
5
|
+
* @returns {Promise<unknown>}
|
|
6
|
+
*/
|
|
7
|
+
export default async function nextTick(callbackFn) {
|
|
8
|
+
return new Promise(
|
|
9
|
+
async resolve => requestIdleCallback( () =>
|
|
10
|
+
setTimeout( async () => resolve( await callbackFn() ), 0 )
|
|
11
|
+
)
|
|
12
|
+
);
|
|
13
13
|
}
|
package/src/misc/replacer.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
Function that can be used with JSON.stringify to replace {function} into string equivalent.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} key
|
|
5
|
-
* @param {*} value
|
|
6
|
-
* @returns {*|undefined}
|
|
7
|
-
*/
|
|
8
|
-
export default function replacer(key, value) {
|
|
9
|
-
return typeof value === 'function' ? value.toString : value;
|
|
1
|
+
/**
|
|
2
|
+
Function that can be used with JSON.stringify to replace {function} into string equivalent.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} key
|
|
5
|
+
* @param {*} value
|
|
6
|
+
* @returns {*|undefined}
|
|
7
|
+
*/
|
|
8
|
+
export default function replacer(key, value) {
|
|
9
|
+
return typeof value === 'function' ? value.toString : value;
|
|
10
10
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
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 );
|
|
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
17
|
}
|
package/src/misc/retry.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import random from "../number/random.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Retry a callback until success. Fourth parameter can be used to randomly choose the delay between each try.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* retry( 5, nbr_of_try => {
|
|
8
|
-
* if ( do_something() ) {
|
|
9
|
-
* return "ok";
|
|
10
|
-
* }
|
|
11
|
-
*
|
|
12
|
-
* throw "Failed after 5 tries";
|
|
13
|
-
* }, 5000, 10000 );
|
|
14
|
-
*
|
|
15
|
-
* @param {number} times
|
|
16
|
-
* @param {Function} callbackFn
|
|
17
|
-
* @param {number} sleepMsMin
|
|
18
|
-
* @param {number|null} sleepMsMax
|
|
19
|
-
* @returns {Promise<unknown>}
|
|
20
|
-
*/
|
|
21
|
-
export default function (times, callbackFn, sleepMsMin = 100, sleepMsMax = null) {
|
|
22
|
-
return new Promise( ( resolve, reject ) => {
|
|
23
|
-
const fn = () => {
|
|
24
|
-
times--;
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
resolve( callbackFn( times ) );
|
|
28
|
-
} catch ( e ) {
|
|
29
|
-
if ( times < 1 ) {
|
|
30
|
-
reject( e );
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const sleepMs = sleepMsMax !== null ? random( sleepMsMin, sleepMsMax ) : sleepMsMin ;
|
|
35
|
-
|
|
36
|
-
setTimeout( fn, sleepMs );
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
fn();
|
|
41
|
-
} );
|
|
42
|
-
};
|
|
1
|
+
import random from "../number/random.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retry a callback until success. Fourth parameter can be used to randomly choose the delay between each try.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* retry( 5, nbr_of_try => {
|
|
8
|
+
* if ( do_something() ) {
|
|
9
|
+
* return "ok";
|
|
10
|
+
* }
|
|
11
|
+
*
|
|
12
|
+
* throw "Failed after 5 tries";
|
|
13
|
+
* }, 5000, 10000 );
|
|
14
|
+
*
|
|
15
|
+
* @param {number} times
|
|
16
|
+
* @param {Function} callbackFn
|
|
17
|
+
* @param {number} sleepMsMin
|
|
18
|
+
* @param {number|null} sleepMsMax
|
|
19
|
+
* @returns {Promise<unknown>}
|
|
20
|
+
*/
|
|
21
|
+
export default function (times, callbackFn, sleepMsMin = 100, sleepMsMax = null) {
|
|
22
|
+
return new Promise( ( resolve, reject ) => {
|
|
23
|
+
const fn = () => {
|
|
24
|
+
times--;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
resolve( callbackFn( times ) );
|
|
28
|
+
} catch ( e ) {
|
|
29
|
+
if ( times < 1 ) {
|
|
30
|
+
reject( e );
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const sleepMs = sleepMsMax !== null ? random( sleepMsMin, sleepMsMax ) : sleepMsMin ;
|
|
35
|
+
|
|
36
|
+
setTimeout( fn, sleepMs );
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
fn();
|
|
41
|
+
} );
|
|
42
|
+
};
|
package/src/misc/reviver.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function that can be used with JSON.parse to revive functions.
|
|
3
|
-
*
|
|
4
|
-
* @param {*} value
|
|
5
|
-
* @returns {*}
|
|
6
|
-
*/
|
|
7
|
-
export default function reviver(value) {
|
|
8
|
-
if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
|
|
9
|
-
return new Function(`return ${value}`)();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return value;
|
|
1
|
+
/**
|
|
2
|
+
* Function that can be used with JSON.parse to revive functions.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} value
|
|
5
|
+
* @returns {*}
|
|
6
|
+
*/
|
|
7
|
+
export default function reviver(value) {
|
|
8
|
+
if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
|
|
9
|
+
return new Function(`return ${value}`)();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return value;
|
|
13
13
|
}
|
package/src/misc/seconds.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import milliseconds from "./milliseconds.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Stop the main process for a moment defined in seconds.
|
|
5
|
-
*
|
|
6
|
-
* @param {number} time
|
|
7
|
-
* @returns {Promise<*>}
|
|
8
|
-
*/
|
|
9
|
-
export default function seconds(time) {
|
|
10
|
-
return milliseconds( time * 1000 );
|
|
1
|
+
import milliseconds from "./milliseconds.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stop the main process for a moment defined in seconds.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} time
|
|
7
|
+
* @returns {Promise<*>}
|
|
8
|
+
*/
|
|
9
|
+
export default function seconds(time) {
|
|
10
|
+
return milliseconds( time * 1000 );
|
|
11
11
|
}
|
package/src/misc/throttle.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Limit the number of times a callback gets called in a certain time period.
|
|
3
|
-
*
|
|
4
|
-
* @param {Function} callbackFn
|
|
5
|
-
* @param {number} delay
|
|
6
|
-
* @returns {(function(): void)|*}
|
|
7
|
-
*/
|
|
8
|
-
export default function throttle(callbackFn, delay) {
|
|
9
|
-
let last;
|
|
10
|
-
let timer;
|
|
11
|
-
|
|
12
|
-
return (...args) => {
|
|
13
|
-
let context = this;
|
|
14
|
-
let now = +new Date();
|
|
15
|
-
|
|
16
|
-
if ( last && now < last + delay ) {
|
|
17
|
-
clearTimeout( timer );
|
|
18
|
-
|
|
19
|
-
timer = setTimeout( () => {
|
|
20
|
-
last = now;
|
|
21
|
-
callbackFn.apply( context, args );
|
|
22
|
-
}, delay );
|
|
23
|
-
} else {
|
|
24
|
-
last = now;
|
|
25
|
-
callbackFn.apply( context, args );
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Limit the number of times a callback gets called in a certain time period.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} callbackFn
|
|
5
|
+
* @param {number} delay
|
|
6
|
+
* @returns {(function(): void)|*}
|
|
7
|
+
*/
|
|
8
|
+
export default function throttle(callbackFn, delay) {
|
|
9
|
+
let last;
|
|
10
|
+
let timer;
|
|
11
|
+
|
|
12
|
+
return (...args) => {
|
|
13
|
+
let context = this;
|
|
14
|
+
let now = +new Date();
|
|
15
|
+
|
|
16
|
+
if ( last && now < last + delay ) {
|
|
17
|
+
clearTimeout( timer );
|
|
18
|
+
|
|
19
|
+
timer = setTimeout( () => {
|
|
20
|
+
last = now;
|
|
21
|
+
callbackFn.apply( context, args );
|
|
22
|
+
}, delay );
|
|
23
|
+
} else {
|
|
24
|
+
last = now;
|
|
25
|
+
callbackFn.apply( context, args );
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/number/format.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {number} number
|
|
3
|
-
* @param {string} thousandsSeparator
|
|
4
|
-
* @param {string} decimalSeparator
|
|
5
|
-
* @param {number} decimal
|
|
6
|
-
* @returns {string}
|
|
7
|
-
*/
|
|
8
|
-
export default function format(number, thousandsSeparator = ' ', decimalSeparator = ',', decimal = 2) {
|
|
9
|
-
return parseFloat( number.toString() ).toFixed( decimal ).replace( '.', decimalSeparator ).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + thousandsSeparator );
|
|
10
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @param {number} number
|
|
3
|
+
* @param {string} thousandsSeparator
|
|
4
|
+
* @param {string} decimalSeparator
|
|
5
|
+
* @param {number} decimal
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
export default function format(number, thousandsSeparator = ' ', decimalSeparator = ',', decimal = 2) {
|
|
9
|
+
return parseFloat( number.toString() ).toFixed( decimal ).replace( '.', decimalSeparator ).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + thousandsSeparator );
|
|
10
|
+
}
|
package/src/number/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default as format } from "./format.js";
|
|
2
|
-
export { default as random } from "./random.js";
|
|
1
|
+
export { default as format } from "./format.js";
|
|
2
|
+
export { default as random } from "./random.js";
|
package/src/number/random.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {number} min
|
|
3
|
-
* @param {number} max
|
|
4
|
-
* @returns {number}
|
|
5
|
-
*/
|
|
6
|
-
export default function random(min, max) {
|
|
7
|
-
return Math.floor( Math.random() * ( max - min + 1 ) + min );
|
|
8
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @param {number} min
|
|
3
|
+
* @param {number} max
|
|
4
|
+
* @returns {number}
|
|
5
|
+
*/
|
|
6
|
+
export default function random(min, max) {
|
|
7
|
+
return Math.floor( Math.random() * ( max - min + 1 ) + min );
|
|
8
|
+
}
|
package/src/object/filter.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Filter attributes of an object using a callback function.
|
|
3
|
-
*
|
|
4
|
-
* @param {{}} object
|
|
5
|
-
* @param {Function} callbackFn
|
|
6
|
-
* @returns {{[p: string]: unknown}}
|
|
7
|
-
*/
|
|
8
|
-
export default function filter(object, callbackFn) {
|
|
9
|
-
return Object.fromEntries( Object.entries( object ).
|
|
10
|
-
filter( ( [ key, value ] ) => callbackFn( value, key ) ) );
|
|
1
|
+
/**
|
|
2
|
+
* Filter attributes of an object using a callback function.
|
|
3
|
+
*
|
|
4
|
+
* @param {{}} object
|
|
5
|
+
* @param {Function} callbackFn
|
|
6
|
+
* @returns {{[p: string]: unknown}}
|
|
7
|
+
*/
|
|
8
|
+
export default function filter(object, callbackFn) {
|
|
9
|
+
return Object.fromEntries( Object.entries( object ).
|
|
10
|
+
filter( ( [ key, value ] ) => callbackFn( value, key ) ) );
|
|
11
11
|
}
|
package/src/object/get.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get a value using a dot-notated key string.
|
|
3
|
-
*
|
|
4
|
-
* @example get({foo: {bar: {doe: "Bob"}}}, "foo.bar.doe")
|
|
5
|
-
* => "Bob"
|
|
6
|
-
*
|
|
7
|
-
* @param {{}} object
|
|
8
|
-
* @param {string} keys
|
|
9
|
-
* @returns {*}
|
|
10
|
-
*/
|
|
11
|
-
export default function (object, keys) {
|
|
12
|
-
return keys.split('.').reduce( ( acc, prop ) => acc[ prop ], object );
|
|
13
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Get a value using a dot-notated key string.
|
|
3
|
+
*
|
|
4
|
+
* @example get({foo: {bar: {doe: "Bob"}}}, "foo.bar.doe")
|
|
5
|
+
* => "Bob"
|
|
6
|
+
*
|
|
7
|
+
* @param {{}} object
|
|
8
|
+
* @param {string} keys
|
|
9
|
+
* @returns {*}
|
|
10
|
+
*/
|
|
11
|
+
export default function (object, keys) {
|
|
12
|
+
return keys.split('.').reduce( ( acc, prop ) => acc[ prop ] || undefined, object );
|
|
13
|
+
}
|
package/src/object/has.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import get from "./get.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get a value using a dot-notated key string
|
|
5
|
-
*
|
|
6
|
-
* @param {{}} object
|
|
7
|
-
* @param {string} keys
|
|
8
|
-
* @returns {*}
|
|
9
|
-
*/
|
|
10
|
-
export default function (object, keys) {
|
|
11
|
-
return get( object, keys ) !== undefined;
|
|
12
|
-
}
|
|
1
|
+
import get from "./get.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get a value using a dot-notated key string
|
|
5
|
+
*
|
|
6
|
+
* @param {{}} object
|
|
7
|
+
* @param {string} keys
|
|
8
|
+
* @returns {*}
|
|
9
|
+
*/
|
|
10
|
+
export default function (object, keys) {
|
|
11
|
+
return get( object, keys ) !== undefined;
|
|
12
|
+
}
|
package/src/object/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { default as filter } from "./filter.js";
|
|
2
|
-
export { default as map } from "./map.js";
|
|
3
|
-
export { default as serialize } from "./serialize.js";
|
|
4
|
-
export { default as unserialize } from "./unserialize.js";
|
|
5
|
-
export { default as only } from "./only.js";
|
|
6
|
-
export { default as set } from "./set.js";
|
|
7
|
-
export { default as get } from "./get.js";
|
|
1
|
+
export { default as filter } from "./filter.js";
|
|
2
|
+
export { default as map } from "./map.js";
|
|
3
|
+
export { default as serialize } from "./serialize.js";
|
|
4
|
+
export { default as unserialize } from "./unserialize.js";
|
|
5
|
+
export { default as only } from "./only.js";
|
|
6
|
+
export { default as set } from "./set.js";
|
|
7
|
+
export { default as get } from "./get.js";
|
|
8
8
|
export { default as has } from "./has.js";
|