@straylightagency/utils 1.1.1 → 1.1.4

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.1",
3
+ "version": "1.1.4",
4
4
  "description": "Some JavaScript utilities functions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -2,10 +2,12 @@
2
2
  * Function that can be used with JSON.parse to revive Date objects.
3
3
  *
4
4
  * @param {*} value
5
- * @returns {Date|undefined}
5
+ * @returns {Date|*}
6
6
  */
7
7
  export default function reviver(value) {
8
8
  if ( typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test( value ) ) {
9
9
  return new Date( value );
10
10
  }
11
+
12
+ return value;
11
13
  }
@@ -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/dom/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  export { default as addEventListeners } from "./addEventListeners.js";
2
2
  export { default as closestElement } from "./closestElement.js";
3
3
  export { default as delegate } from "./delegate.js";
4
- export { default as getFieldsValues } from "./getFieldsValues.js";
5
4
  export { default as getElementSizing } from "./getElementSizing.js";
5
+ export { default as getFieldsValues } from "./getFieldsValues.js";
6
6
  export { default as onEachElements } from "./onEachElements.js";
7
+ export { default as stringToHtml } from "./stringToHtml.js";
7
8
  export { default as unlessElementExists } from "./unlessElementExists.js";
8
9
  export { default as whenReady } from "./whenReady.js";
9
- export { default as whenResize } from "./whenResize.js";
10
+ export { default as whenResize } from "./whenResize.js";
11
+ export { default as whenScroll } from "./whenScroll.js";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @example stringToHtml('<p>my content</p>')
3
+ *
4
+ * @param {string} htmlString
5
+ * @returns {Document|DocumentFragment}
6
+ */
7
+ export default function stringToHtml(htmlString)
8
+ {
9
+ const isFullDocument = /<html/i.test( htmlString );
10
+
11
+ if ( isFullDocument ) {
12
+ const parser = new DOMParser();
13
+ return parser.parseFromString( htmlString, 'text/html' );
14
+ } else {
15
+ const range = document.createRange();
16
+ return range.createContextualFragment( htmlString );
17
+ }
18
+ };
@@ -1,18 +1,27 @@
1
1
  /**
2
- * Execute the given callback when the window is resized. Second parameter allows to run the callback immediately.
2
+ * Listen when the window is resized. Returned an array with two methods:
3
+ * - init() : callback is called immediately with width and height values
4
+ * - cancel() : cancel the event listener
3
5
  *
4
6
  * @param {Function} callbackFn
5
- * @param {boolean} init
7
+ * @returns {[]}
6
8
  */
7
- export default function whenResize(callbackFn, init = true) {
8
- let ww = window.innerWidth, wh = window.innerHeight;
9
+ export default function whenResize(callbackFn) {
10
+ let ww, wh;
9
11
 
10
- window.addEventListener( 'resize', () => {
12
+ const wrappedFn = e => {
11
13
  ww = window.innerWidth;
12
14
  wh = window.innerHeight;
13
15
 
14
- callbackFn( { ww, wh } );
15
- } );
16
+ callbackFn( { ww, wh }, e );
17
+ };
16
18
 
17
- init ? callbackFn( { ww, wh } ) : null;
18
- }
19
+ window.addEventListener( 'resize', wrappedFn );
20
+
21
+ const init = () => wrappedFn();
22
+ const cancel = () => window.removeEventListener( 'resize', wrappedFn );
23
+
24
+ return [
25
+ init, cancel
26
+ ];
27
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Listen when the window document is scrolled. Returned an array with two methods:
3
+ * - init() : callback is called immediately
4
+ * - cancel() : cancel the event listener
5
+ *
6
+ * @param {Function} callbackFn
7
+ * @returns {[]}
8
+ */
9
+ export default function whenScroll(callbackFn) {
10
+ let x, y;
11
+
12
+ const wrappedFn = e => {
13
+ x = window.scrollX;
14
+ y = window.scrollY;
15
+
16
+ callbackFn( { x, y }, e );
17
+ };
18
+
19
+ document.addEventListener( 'scroll', wrappedFn );
20
+
21
+ const init = () => wrappedFn();
22
+ const cancel = () => document.removeEventListener( 'scroll', wrappedFn );
23
+
24
+ return [
25
+ init, cancel
26
+ ];
27
+ }
package/src/index.js CHANGED
@@ -15,6 +15,9 @@ 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";
20
23
  export { default as functionReplacer } from "./misc/replacer.js";
@@ -27,6 +30,7 @@ export { default as onEachElements } from "./dom/onEachElements.js";
27
30
  export { default as unlessElementExists } from "./dom/unlessElementExists.js";
28
31
  export { default as whenReady } from "./dom/whenReady.js";
29
32
  export { default as whenResize } from "./dom/whenResize.js";
33
+ export { default as whenScroll } from "./dom/whenScroll.js";
30
34
  export { default as getWeekNumber } from "./date/getWeekNumber.js";
31
35
  export { default as clone } from "./date/clone.js";
32
36
  export { default as dateReviver } from "./date/reviver.js";
@@ -34,14 +38,10 @@ export { default as initial } from "./array/initial.js";
34
38
  export { default as last } from "./array/last.js";
35
39
  export { default as remove } from "./array/remove.js";
36
40
 
37
- import {wait, seconds, milliseconds} from "./misc/wait.js";
38
41
  import {isClass, isObject, isPlainObject} from "./misc/is.js";
39
42
  import {decodeBase64, encodeBase64, isBase64} from "./misc/base64.js";
40
43
 
41
44
  export {
42
- wait,
43
- seconds,
44
- milliseconds,
45
45
  isClass,
46
46
  isObject,
47
47
  isPlainObject,
package/src/misc/index.js CHANGED
@@ -6,15 +6,14 @@ 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
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";
9
12
 
10
- import {wait, seconds, milliseconds} from "./wait.js";
11
13
  import {isClass, isObject, isPlainObject} from "./is.js";
12
14
  import {decodeBase64, encodeBase64, isBase64} from "./base64.js";
13
15
 
14
16
  export {
15
- wait,
16
- seconds,
17
- milliseconds,
18
17
  isClass,
19
18
  isObject,
20
19
  isPlainObject,
@@ -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,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
+ }
@@ -2,10 +2,12 @@
2
2
  * Function that can be used with JSON.parse to revive functions.
3
3
  *
4
4
  * @param {*} value
5
- * @returns {*|undefined}
5
+ * @returns {*}
6
6
  */
7
7
  export default function reviver(value) {
8
8
  if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
9
- return eval( `(${value})` );
9
+ return new Function(`return ${value}`)();
10
10
  }
11
+
12
+ return value;
11
13
  }
@@ -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
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;