@straylightagency/utils 1.1.0 → 1.1.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@straylightagency/utils",
3
- "version": "1.1.0",
3
+ "version": "1.1.3",
4
4
  "description": "Some JavaScript utilities functions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -12,7 +12,7 @@
12
12
  * @param {{}} listeners
13
13
  */
14
14
  export default function addEventListeners(element, listeners) {
15
- Object.fromEntries(
15
+ return Object.fromEntries(
16
16
  Object.entries( listeners ).map( ([event, callbackFn]) => {
17
17
  element.addEventListener( event, callbackFn );
18
18
 
package/src/index.js CHANGED
@@ -15,8 +15,12 @@ export { default as has } from "./object/has.js";
15
15
  export { default as clipboard } from "./misc/clipboard.js";
16
16
  export { default as debounce } from "./misc/debounce.js";
17
17
  export { default as nextTick } from "./misc/nextTick.js";
18
+ export { default as seconds } from "./misc/seconds.js";
19
+ export { default as milliseconds } from "./misc/milliseconds.js";
20
+ export { default as restoreBooleanAfter } from "./misc/restoreBooleanAfter.js";
18
21
  export { default as throttle } from "./misc/throttle.js";
19
22
  export { default as functionReviver } from "./misc/reviver.js";
23
+ export { default as functionReplacer } from "./misc/replacer.js";
20
24
  export { default as addEventListeners } from "./dom/addEventListeners.js";
21
25
  export { default as closestElement } from "./dom/closestElement.js";
22
26
  export { default as delegate } from "./dom/delegate.js";
@@ -33,14 +37,10 @@ export { default as initial } from "./array/initial.js";
33
37
  export { default as last } from "./array/last.js";
34
38
  export { default as remove } from "./array/remove.js";
35
39
 
36
- import {wait, seconds, milliseconds} from "./misc/wait.js";
37
40
  import {isClass, isObject, isPlainObject} from "./misc/is.js";
38
41
  import {decodeBase64, encodeBase64, isBase64} from "./misc/base64.js";
39
42
 
40
43
  export {
41
- wait,
42
- seconds,
43
- milliseconds,
44
44
  isClass,
45
45
  isObject,
46
46
  isPlainObject,
package/src/misc/index.js CHANGED
@@ -5,19 +5,19 @@ export { default as throttle } from "./throttle.js";
5
5
  export { default as attempt } from "./attempt.js";
6
6
  export { default as retry } from "./retry.js";
7
7
  export { default as reviver } from "./reviver.js";
8
+ export { default as replacer } from "./replacer.js";
9
+ export { default as seconds } from "./seconds.js";
10
+ export { default as milliseconds } from "./milliseconds.js";
11
+ export { default as restoreBooleanAfter } from "./restoreBooleanAfter.js";
8
12
 
9
- import {wait, seconds, milliseconds} from "./wait.js";
10
13
  import {isClass, isObject, isPlainObject} from "./is.js";
11
14
  import {decodeBase64, encodeBase64, isBase64} from "./base64.js";
12
15
 
13
16
  export {
14
- wait,
15
- seconds,
16
- milliseconds,
17
17
  isClass,
18
18
  isObject,
19
19
  isPlainObject,
20
20
  decodeBase64,
21
21
  encodeBase64,
22
- isBase64
22
+ isBase64,
23
23
  };
@@ -0,0 +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 ) );
9
+ }
@@ -6,7 +6,8 @@
6
6
  */
7
7
  export default async function nextTick(callbackFn) {
8
8
  return new Promise(
9
- async resolve => requestIdleCallback( () => async () => resolve( await callbackFn() )
9
+ async resolve => requestIdleCallback( () =>
10
+ setTimeout( async () => resolve( await callbackFn() ), 0 )
10
11
  )
11
12
  );
12
13
  }
@@ -0,0 +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;
10
+ }
@@ -0,0 +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 );
17
+ }
@@ -0,0 +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 );
11
+ }
package/src/object/has.js CHANGED
@@ -1,4 +1,4 @@
1
- import get from "./get";
1
+ import get from "./get.js";
2
2
 
3
3
  /**
4
4
  * Get a value using a dot-notated key string
@@ -1,3 +1,5 @@
1
+ import replacer from "../misc/replacer.js";
2
+
1
3
  /**
2
4
  * Serialize an object into a string, transforms functions into string.
3
5
  *
@@ -8,5 +10,5 @@
8
10
  * @returns {String}
9
11
  */
10
12
  export default function serialize(object) {
11
- return JSON.stringify( object, (key, value) => typeof value === 'function' ? value.toString : value );
13
+ return JSON.stringify( object, replacer );
12
14
  }
@@ -1,5 +1,5 @@
1
- import {reviver as dateReviver} from '../date/reviver.js'
2
- import {reviver as functionReviver} from '../misc/reviver.js'
1
+ import dateReviver from '../date/reviver.js'
2
+ import functionReviver from '../misc/reviver.js'
3
3
 
4
4
  /**
5
5
  * Serialize an object into a string, handling functions and Date objects.
package/src/misc/wait.js DELETED
@@ -1,31 +0,0 @@
1
- /**
2
- * Stop the main process for a moment defined in milliseconds.
3
- *
4
- * @param {number} timeMs
5
- * @returns {Promise<unknown>}
6
- */
7
- export function wait(timeMs) {
8
- return new Promise( resolve => setTimeout( resolve, timeMs ) );
9
- }
10
-
11
- /**
12
- * Stop the main process for a moment defined in seconds.
13
- *
14
- * @param {number} time
15
- * @returns {Promise<*>}
16
- */
17
- export function seconds(time) {
18
- return wait( time * 1000 );
19
- }
20
-
21
- /**
22
- * Alias to `wait`. Stop the main process for a moment defined in milliseconds.
23
- *
24
- * @param {number} timeMs
25
- * @returns {Promise<*>}
26
- */
27
- export function milliseconds(timeMs) {
28
- return wait( timeMs );
29
- }
30
-
31
- export default wait;