@straylightagency/utils 1.1.8 → 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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +469 -0
  3. package/package.json +4 -3
  4. package/src/array/difference.js +13 -0
  5. package/src/array/flatten.js +18 -0
  6. package/src/array/groupBy.js +22 -0
  7. package/src/array/index.js +8 -1
  8. package/src/array/initial.js +4 -4
  9. package/src/array/intersection.js +18 -0
  10. package/src/array/last.js +3 -4
  11. package/src/array/move.js +27 -0
  12. package/src/array/remove.js +11 -8
  13. package/src/array/replace.js +6 -6
  14. package/src/array/shuffle.js +23 -0
  15. package/src/array/unique.js +10 -0
  16. package/src/date/getWeekNumber.js +1 -1
  17. package/src/date/reviver.js +2 -1
  18. package/src/dom/addEventListeners.js +2 -2
  19. package/src/dom/closestElement.js +1 -1
  20. package/src/dom/delegate.js +11 -8
  21. package/src/dom/index.js +4 -1
  22. package/src/dom/onEachElements.js +1 -1
  23. package/src/dom/stringToHtml.js +1 -1
  24. package/src/dom/unlessElementExists.js +1 -1
  25. package/src/dom/whenIntersect.js +24 -0
  26. package/src/dom/whenMutation.js +28 -0
  27. package/src/dom/whenResize.js +14 -13
  28. package/src/dom/whenScroll.js +14 -13
  29. package/src/dom/whenVisible.js +30 -0
  30. package/src/index.js +54 -23
  31. package/src/misc/attempt.js +2 -2
  32. package/src/misc/base64.js +8 -5
  33. package/src/misc/clipboard.js +1 -1
  34. package/src/misc/debounce.js +9 -7
  35. package/src/misc/index.js +7 -14
  36. package/src/misc/is.js +20 -15
  37. package/src/misc/match.js +36 -21
  38. package/src/misc/milliseconds.js +1 -1
  39. package/src/misc/once.js +24 -0
  40. package/src/misc/replacer.js +4 -3
  41. package/src/misc/restoreAfter.js +28 -0
  42. package/src/misc/retry.js +4 -4
  43. package/src/misc/reviver.js +3 -2
  44. package/src/misc/seconds.js +1 -1
  45. package/src/misc/sleep.js +34 -0
  46. package/src/misc/throttle.js +4 -2
  47. package/src/number/clamp.js +11 -0
  48. package/src/number/format.js +7 -5
  49. package/src/number/index.js +1 -0
  50. package/src/number/random.js +14 -4
  51. package/src/object/deepClone.js +71 -0
  52. package/src/object/deepEqual.js +57 -0
  53. package/src/object/entries.js +10 -0
  54. package/src/object/filter.js +2 -2
  55. package/src/object/get.js +9 -6
  56. package/src/object/has.js +16 -8
  57. package/src/object/index.js +11 -1
  58. package/src/object/invert.js +22 -0
  59. package/src/object/isObject.js +9 -0
  60. package/src/object/isPlainObject.js +9 -0
  61. package/src/object/isType.js +14 -0
  62. package/src/object/map.js +2 -2
  63. package/src/object/merge.js +46 -0
  64. package/src/object/omit.js +14 -0
  65. package/src/object/only.js +3 -3
  66. package/src/object/pick.js +14 -0
  67. package/src/object/serialize.js +12 -3
  68. package/src/object/set.js +4 -4
  69. package/src/object/unserialize.js +30 -5
  70. package/src/string/capitalFirst.js +2 -2
  71. package/src/string/capitalWords.js +2 -2
  72. package/src/string/interpolate.js +3 -3
  73. package/src/string/slugify.js +11 -19
  74. package/readme.MD +0 -0
  75. package/src/misc/restoreBooleanAfter.js +0 -17
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Replace a value from the given array by another one using a callback to find the index
3
3
  *
4
- * @param {[]} array
5
- * @param {*} callback
6
- * @param {*} value
7
- * @returns {[]}
4
+ * @param {Array} array
5
+ * @param {any} callbackFn
6
+ * @param {any} value
7
+ * @returns {Array}
8
8
  */
9
- export default function replace(array, callback, value) {
10
- const index = array.findIndex( callback );
9
+ export default function replace(array, callbackFn, value) {
10
+ const index = array.findIndex( callbackFn );
11
11
 
12
12
  if ( index !== -1 ) {
13
13
  array[ index ] = value;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Returns a shuffled copy using Fisher-Yates.
3
+ * `random` can be injected for deterministic tests; it must return [0, 1).
4
+ *
5
+ * @param {Array} array
6
+ * @param {Function} random
7
+ * @returns {Array}
8
+ */
9
+ export default function shuffle(array, random = Math.random) {
10
+ const result = [...array];
11
+
12
+ for (let i = result.length - 1; i > 0; i--) {
13
+ const value = random();
14
+ if (!(value >= 0 && value < 1)) {
15
+ throw new RangeError('shuffle() random function must return a value in [0, 1)');
16
+ }
17
+
18
+ const j = Math.floor(value * (i + 1));
19
+ [result[i], result[j]] = [result[j], result[i]];
20
+ }
21
+
22
+ return result;
23
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns a duplicate-free copy, preserving first occurrence order.
3
+ * Uses Set's SameValueZero equality.
4
+ *
5
+ * @param {Array} array
6
+ * @returns {Array}
7
+ */
8
+ export default function unique(array) {
9
+ return [...new Set(array)];
10
+ }
@@ -5,7 +5,7 @@
5
5
  * => 12
6
6
  *
7
7
  * @param {Date} date
8
- * @returns {number}
8
+ * @returns {Number}
9
9
  */
10
10
  export default function getWeekNumber(date) {
11
11
  date.setHours(0, 0, 0, 0);
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Function that can be used with JSON.parse to revive Date objects.
3
3
  *
4
- * @param {*} value
4
+ * @deprecated
5
+ * @param {any} value
5
6
  * @returns {Date|*}
6
7
  */
7
8
  export default function reviver(value) {
@@ -8,8 +8,8 @@
8
8
  * removeListeners.click();
9
9
  * => click event has been removed from the button
10
10
  *
11
- * @param {HTMLElement} element
12
- * @param {{}} listeners
11
+ * @param {EventTarget} element
12
+ * @param {Object} listeners
13
13
  */
14
14
  export default function addEventListeners(element, listeners) {
15
15
  return Object.fromEntries(
@@ -2,7 +2,7 @@
2
2
  * Select the closest element from another.
3
3
  *
4
4
  * @param {HTMLElement} element
5
- * @param {string} selector
5
+ * @param {String} selector
6
6
  * @returns {HTMLAnchorElement|HTMLElement|HTMLAreaElement|HTMLAudioElement|HTMLBaseElement|HTMLQuoteElement|HTMLBodyElement|HTMLBRElement|HTMLButtonElement|HTMLCanvasElement|HTMLTableCaptionElement|HTMLTableColElement|HTMLDataElement|HTMLDataListElement|HTMLModElement|HTMLDetailsElement|HTMLDialogElement|HTMLDivElement|HTMLDListElement|HTMLEmbedElement|HTMLFieldSetElement|HTMLFormElement|HTMLHeadingElement|HTMLHeadElement|HTMLHRElement|HTMLHtmlElement|HTMLIFrameElement|HTMLImageElement|HTMLInputElement|HTMLLabelElement|HTMLLegendElement|HTMLLIElement|HTMLLinkElement|HTMLMapElement|HTMLMenuElement|HTMLMetaElement|HTMLMeterElement|HTMLObjectElement|HTMLOListElement|HTMLOptGroupElement|HTMLOptionElement|HTMLOutputElement|HTMLParagraphElement|HTMLPictureElement|HTMLPreElement|HTMLProgressElement|HTMLScriptElement|HTMLSelectElement|HTMLSlotElement|HTMLSourceElement|HTMLSpanElement|HTMLStyleElement|HTMLTableElement|HTMLTableSectionElement|HTMLTableCellElement|HTMLTemplateElement|HTMLTextAreaElement|HTMLTimeElement|HTMLTitleElement|HTMLTableRowElement|HTMLTrackElement|HTMLUListElement|HTMLVideoElement|*}
7
7
  */
8
8
  export default function closestElement(element, selector) {
@@ -1,20 +1,23 @@
1
1
  /**
2
- * Delegate an event through the Document to a selected element. Useful to define an event on an element which is not already created.
2
+ * Delegate an event through an EventTarget container to a selected element. Useful to define an event on an element which is not already created.
3
3
  * Returns a callback function that can be used to remove the event listener from the Document.
4
4
  *
5
- * @param {string} selector
6
- * @param {string} event
5
+ * @param {String} selector
6
+ * @param {String} event
7
7
  * @param {Function} handler
8
+ * @param {EventTarget} target
8
9
  * @returns {function(): any}
9
10
  */
10
- export default function delegate(selector, event, handler) {
11
- const callback = function (event) {
12
- const matchingElement = event.target.closest( selector );
11
+ export default function delegate(selector, event, handler, target = document) {
12
+ const wrappedFn = function (event) {
13
+ const matchingElement = event.target instanceof Element
14
+ ? event.target.closest(selector)
15
+ : event.target.parentElement?.closest(selector);
13
16
 
14
17
  return matchingElement !== null ? handler.call( matchingElement, event ) : false;
15
18
  };
16
19
 
17
- Element.prototype.addEventListener.call( document, event, callback, true );
20
+ Element.prototype.addEventListener.call( target, event, wrappedFn, true );
18
21
 
19
- return () => Element.prototype.removeEventListener.call( document, event, callback, true );
22
+ return () => Element.prototype.removeEventListener.call( target, event, wrappedFn, true );
20
23
  };
package/src/dom/index.js CHANGED
@@ -8,4 +8,7 @@ export { default as stringToHtml } from "./stringToHtml.js";
8
8
  export { default as unlessElementExists } from "./unlessElementExists.js";
9
9
  export { default as whenReady } from "./whenReady.js";
10
10
  export { default as whenResize } from "./whenResize.js";
11
- export { default as whenScroll } from "./whenScroll.js";
11
+ export { default as whenScroll } from "./whenScroll.js";
12
+ export { default as whenIntersect } from './whenIntersect.js';
13
+ export { default as whenVisible } from './whenVisible.js';
14
+ export { default as whenMutation } from './whenMutation.js';
@@ -8,7 +8,7 @@
8
8
  * @param {string|HTMLElement} selectorOrParent
9
9
  * @param {Function|string} callbackOrSelector
10
10
  * @param {Function|null} callbackOrNull
11
- * @returns {*}
11
+ * @returns {any}
12
12
  */
13
13
  export default function onEachElements(selectorOrParent, callbackOrSelector, callbackOrNull = null) {
14
14
  if ( typeof selectorOrParent === 'string' && typeof callbackOrSelector === 'function' && typeof callbackOrNull !== 'function' ) {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @example stringToHtml('<p>my content</p>')
3
3
  *
4
- * @param {string} htmlString
4
+ * @param {String} htmlString
5
5
  * @returns {Document|DocumentFragment}
6
6
  */
7
7
  export default function stringToHtml(htmlString)
@@ -8,7 +8,7 @@
8
8
  * @param {String|HTMLElement} selectorOrParent
9
9
  * @param {Function|HTMLElement} callbackOrSelector
10
10
  * @param {Function|undefined} callbackOrNull
11
- * @returns {*}
11
+ * @returns {any}
12
12
  */
13
13
  export default function unlessElementExists(selectorOrParent, callbackOrSelector, callbackOrNull = null) {
14
14
  if ( typeof selectorOrParent === 'string' ) {
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Observes intersection changes for an element.
3
+ * Returns a function that disconnects the observer.
4
+ *
5
+ * @param {Element} target
6
+ * @param {(entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void} callbackFn
7
+ * @param {IntersectionObserverInit} options
8
+ */
9
+ export default function whenIntersect(target, callbackFn, options = {}) {
10
+ if (!(target instanceof Element)) {
11
+ throw new TypeError('whenIntersect() target must be an Element');
12
+ }
13
+ if (typeof callbackFn !== 'function') {
14
+ throw new TypeError('whenIntersect() callback must be a function');
15
+ }
16
+ if (typeof IntersectionObserver === 'undefined') {
17
+ throw new Error('IntersectionObserver is not supported in this environment');
18
+ }
19
+
20
+ const observer = new IntersectionObserver(callbackFn, options);
21
+ observer.observe(target);
22
+
23
+ return () => observer.disconnect();
24
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Observes DOM mutations on a target.
3
+ * `options` is passed to MutationObserver.observe().
4
+ * Returns a function that disconnects the observer.
5
+ *
6
+ * @param {Element} target
7
+ * @param {(mutations: MutationRecord[], observer: MutationObserver) => void} callbackFn
8
+ * @param {MutationObserverInit} options
9
+ */
10
+ export default function whenMutation(target, callbackFn, options = {
11
+ childList: true,
12
+ subtree: true
13
+ }) {
14
+ if (!(target instanceof Node)) {
15
+ throw new TypeError('whenMutation() target must be a Node');
16
+ }
17
+ if (typeof callbackFn !== 'function') {
18
+ throw new TypeError('whenMutation() callback must be a function');
19
+ }
20
+ if (typeof MutationObserver === 'undefined') {
21
+ throw new Error('MutationObserver is not supported in this environment');
22
+ }
23
+
24
+ const observer = new MutationObserver(callbackFn);
25
+ observer.observe(target, options);
26
+
27
+ return () => observer.disconnect();
28
+ }
@@ -2,22 +2,23 @@
2
2
  * Listen when the window is resized. Return a callback to cancel the event listener.
3
3
  *
4
4
  * @param {Function} callbackFn
5
- * @param {boolean} immediately
6
- * @param {Document|HTMLElement} container
5
+ * @param {Boolean} immediately
6
+ * @param {EventTarget} container
7
7
  * @returns {function(): void}
8
8
  */
9
- export default function whenResize(callbackFn, immediately = true, container = undefined) {
10
- let ww, wh;
9
+ export default function whenResize(callbackFn, immediately = true, container = window) {
10
+ const wrappedFn = event => {
11
+ const dimensions = container === window
12
+ ? {
13
+ width: window.innerWidth,
14
+ height: window.innerHeight
15
+ }
16
+ : {
17
+ width: container.clientWidth,
18
+ height: container.clientHeight
19
+ };
11
20
 
12
- if ( ! container ) {
13
- container = document;
14
- }
15
-
16
- const wrappedFn = e => {
17
- ww = window.innerWidth;
18
- wh = window.innerHeight;
19
-
20
- callbackFn( { ww, wh }, e );
21
+ callbackFn(dimensions, event);
21
22
  };
22
23
 
23
24
  container.addEventListener( 'resize', wrappedFn );
@@ -2,22 +2,23 @@
2
2
  * Listen when the window document is scrolled. Return a callback to cancel the event listener.
3
3
  *
4
4
  * @param {Function} callbackFn
5
- * @param {boolean} immediately
6
- * @param {Document|HTMLElement} container
5
+ * @param {Boolean} immediately
6
+ * @param {EventTarget} container
7
7
  * @returns {function(): void}
8
8
  */
9
- export default function whenScroll(callbackFn, immediately = true, container = undefined) {
10
- let x, y;
9
+ export default function whenScroll(callbackFn, immediately = true, container = document) {
10
+ const wrappedFn = event => {
11
+ const dimensions = container === window
12
+ ? {
13
+ width: window.innerWidth,
14
+ height: window.innerHeight
15
+ }
16
+ : {
17
+ width: container.clientWidth,
18
+ height: container.clientHeight
19
+ };
11
20
 
12
- if ( ! container ) {
13
- container = document;
14
- }
15
-
16
- const wrappedFn = e => {
17
- x = window.scrollX;
18
- y = window.scrollY;
19
-
20
- callbackFn( { x, y }, e );
21
+ callbackFn(dimensions, event);
21
22
  };
22
23
 
23
24
  container.addEventListener( 'scroll', wrappedFn );
@@ -0,0 +1,30 @@
1
+ import whenIntersect from './whenIntersect.js';
2
+
3
+ /**
4
+ * Calls `callbackFn` when the element enters the viewport.
5
+ * By default, disconnects after the first visible entry.
6
+ * Set { once: false } to receive later enter/leave changes as well.
7
+ * Returns a function that disconnects the observer.
8
+ *
9
+ * @param {Element} target
10
+ * @param {(entry: IntersectionObserverEntry) => void} callbackFn
11
+ * @param {Object} options
12
+ */
13
+ export default function whenVisible(target, callbackFn, options = {}) {
14
+ const { once = true, ...observerOptions } = options;
15
+
16
+ let stop = () => {};
17
+ stop = whenIntersect(target, entries => {
18
+ for (const entry of entries) {
19
+ if (entry.isIntersecting) {
20
+ callbackFn(entry);
21
+ if (once) {
22
+ stop();
23
+ return;
24
+ }
25
+ }
26
+ }
27
+ }, observerOptions);
28
+
29
+ return stop;
30
+ }
package/src/index.js CHANGED
@@ -1,53 +1,84 @@
1
+ /** ARRAY */
2
+ export { default as difference } from "./array/difference.js";
3
+ export { default as flatten } from "./array/flatten.js";
4
+ export { default as groupBy } from "./array/groupBy.js";
5
+ export { default as initial } from "./array/initial.js";
6
+ export { default as intersection } from "./array/intersection.js";
7
+ export { default as last } from "./array/last.js";
8
+ export { default as move } from "./array/move.js";
9
+ export { default as remove } from "./array/remove.js";
10
+ export { default as replace } from "./array/replace.js";
11
+ export { default as shuffle } from "./array/shuffle.js";
12
+ export { default as unique } from "./array/unique.js";
13
+
14
+ /** STRING */
1
15
  export { default as capitalFirst } from "./string/capitalFirst.js";
2
16
  export { default as capitalWords } from "./string/capitalWords.js";
3
17
  export { default as slugify } from "./string/slugify.js";
4
18
  export { default as interpolate } from "./string/interpolate.js";
19
+
20
+ /** NUMBER */
21
+ export { default as clamp } from "./number/clamp.js";
5
22
  export { default as format } from "./number/format.js";
6
23
  export { default as random } from "./number/random.js";
24
+
25
+ /** OBJECT */
26
+ export { default as deepClone } from "./object/deepClone.js";
27
+ export { default as deepEqual } from "./object/deepEqual.js";
28
+ export { default as entries } from "./object/entries.js";
7
29
  export { default as filter } from "./object/filter.js";
30
+ export { default as get } from "./object/get.js";
31
+ export { default as has } from "./object/has.js";
32
+ export { default as invert } from "./object/invert.js";
33
+ export { default as isObject } from "./object/isObject.js";
34
+ export { default as isPlainObject } from "./object/isPlainObject.js";
35
+ export { default as isType } from "./object/isType.js";
8
36
  export { default as map } from "./object/map.js";
37
+ export { default as merge } from "./object/merge.js";
38
+ export { default as omit } from "./object/omit.js";
9
39
  export { default as only } from "./object/only.js";
40
+ export { default as pick } from "./object/pick.js";
10
41
  export { default as serialize } from "./object/serialize.js";
11
- export { default as unserialize } from "./object/unserialize.js";
12
- export { default as get } from "./object/get.js";
13
42
  export { default as set } from "./object/set.js";
14
- export { default as has } from "./object/has.js";
43
+ export { default as unserialize } from "./object/unserialize.js";
44
+
45
+ /** MISCELLANEOUS */
46
+ export { default as attempt } from "./misc/attempt.js";
15
47
  export { default as clipboard } from "./misc/clipboard.js";
16
- export { default as match } from "./misc/match.js";
17
48
  export { default as debounce } from "./misc/debounce.js";
49
+ export { default as match } from "./misc/match.js";
50
+ export { default as milliseconds } from "./misc/milliseconds.js";
18
51
  export { default as nextTick } from "./misc/nextTick.js";
52
+ export { default as once } from "./misc/once.js";
53
+ export { default as restoreAfter } from "./misc/restoreAfter.js";
54
+ export { default as retry } from "./misc/retry.js";
19
55
  export { default as seconds } from "./misc/seconds.js";
20
- export { default as milliseconds } from "./misc/milliseconds.js";
21
- export { default as restoreBooleanAfter } from "./misc/restoreBooleanAfter.js";
56
+ export { default as sleep } from "./misc/sleep.js";
22
57
  export { default as throttle } from "./misc/throttle.js";
58
+
23
59
  export { default as functionReviver } from "./misc/reviver.js";
24
60
  export { default as functionReplacer } from "./misc/replacer.js";
61
+ export { isClass } from "./misc/is.js";
62
+ export { decodeBase64, encodeBase64, isBase64 } from "./misc/base64.js";
63
+
64
+ /** DOM */
25
65
  export { default as addEventListeners } from "./dom/addEventListeners.js";
26
66
  export { default as closestElement } from "./dom/closestElement.js";
27
67
  export { default as delegate } from "./dom/delegate.js";
28
- export { default as getFieldsValues } from "./dom/getFieldsValues.js";
29
68
  export { default as getElementSizing } from "./dom/getElementSizing.js";
69
+ export { default as getFieldsValues } from "./dom/getFieldsValues.js";
30
70
  export { default as onEachElements } from "./dom/onEachElements.js";
71
+ export { default as stringToHtml } from "./dom/stringToHtml.js";
31
72
  export { default as unlessElementExists } from "./dom/unlessElementExists.js";
73
+ export { default as whenIntersect } from "./dom/whenIntersect.js";
74
+ export { default as whenMutation } from "./dom/whenMutation.js";
32
75
  export { default as whenReady } from "./dom/whenReady.js";
33
76
  export { default as whenResize } from "./dom/whenResize.js";
34
77
  export { default as whenScroll } from "./dom/whenScroll.js";
78
+ export { default as whenVisible } from "./dom/whenVisible.js";
79
+
80
+ /** DATE */
35
81
  export { default as getWeekNumber } from "./date/getWeekNumber.js";
36
82
  export { default as clone } from "./date/clone.js";
37
- export { default as dateReviver } from "./date/reviver.js";
38
- export { default as initial } from "./array/initial.js";
39
- export { default as last } from "./array/last.js";
40
- export { default as remove } from "./array/remove.js";
41
- export { default as replace } from "./array/replace.js";
42
83
 
43
- import {isClass, isObject, isPlainObject} from "./misc/is.js";
44
- import {decodeBase64, encodeBase64, isBase64} from "./misc/base64.js";
45
-
46
- export {
47
- isClass,
48
- isObject,
49
- isPlainObject,
50
- decodeBase64,
51
- encodeBase64,
52
- isBase64
53
- };
84
+ export { default as dateReviver } from "./date/reviver.js";
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * @example const [result, error] = attempt(new Promise(...));
5
5
  *
6
- * @param {Promise} promise
7
- * @return {[]}
6
+ * @param {Promise<unknown>} promise
7
+ * @returns {Array}
8
8
  */
9
9
  export default async function attempt(promise) {
10
10
  let error = undefined;
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Decode a base64 value.
3
3
  *
4
- * @param {*} value
5
- * @return {string|null}
4
+ * @deprecated
5
+ * @param {any} value
6
+ * @returns {string|null}
6
7
  */
7
8
  export function decodeBase64(value) {
8
9
  try {
@@ -21,8 +22,9 @@ export function decodeBase64(value) {
21
22
  /**
22
23
  * Encode a value to base64.
23
24
  *
25
+ * @deprecated
24
26
  * @param value
25
- * @return {string|null}
27
+ * @returns {string|null}
26
28
  */
27
29
  export function encodeBase64(value) {
28
30
  try {
@@ -35,8 +37,9 @@ export function encodeBase64(value) {
35
37
  /**
36
38
  * Return true if given value is a base64.
37
39
  *
38
- * @param {*} value
39
- * @return {boolean}
40
+ * @deprecated
41
+ * @param {any} value
42
+ * @returns {Boolean}
40
43
  */
41
44
  export function isBase64(value) {
42
45
  try {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Push the given string parameter into the clipboard. Use a polyfill if the new 'navigator.clipboard' is not available.
3
3
  *
4
- * @param {string} string
4
+ * @param {String} string
5
5
  * @returns {Promise<void>}
6
6
  */
7
7
  export default async function clipboard(string) {
@@ -1,25 +1,27 @@
1
1
  /**
2
- * Execute callback once if called many times during a defined delay.
2
+ * Execute callback once if called many times during a defined delay. Returned function by the debouncer allows to cancel the internal timer.
3
3
  *
4
4
  * @param {Function} callbackFn
5
- * @param {number} delay
6
- * @param {boolean} immediate
5
+ * @param {Number} delay
6
+ * @param {Boolean} immediately
7
7
  * @returns {(function(): void)|*}
8
8
  */
9
- export default function debounce(callbackFn, delay, immediate = false) {
9
+ export default function debounce(callbackFn, delay, immediately = false) {
10
10
  let timer;
11
11
 
12
- return (...args) => {
13
- const now = immediate && !timer;
12
+ return function (...args) {
13
+ const now = immediately && !timer;
14
14
 
15
15
  clearTimeout( timer );
16
16
 
17
17
  timer = setTimeout( () => {
18
18
  timer = undefined;
19
19
 
20
- if ( !immediate ) callbackFn.apply( this, args );
20
+ if ( ! immediately ) callbackFn.apply( this, args );
21
21
  }, delay );
22
22
 
23
23
  if ( now ) callbackFn.apply( this, args );
24
+
25
+ return () => clearTimeout( timer );
24
26
  };
25
27
  }
package/src/misc/index.js CHANGED
@@ -4,21 +4,14 @@ export { default as nextTick } from "./nextTick.js";
4
4
  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
- export { default as reviver } from "./reviver.js";
8
- export { default as replacer } from "./replacer.js";
9
7
  export { default as match } from "./match.js";
10
8
  export { default as seconds } from "./seconds.js";
11
9
  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";
10
+ export { default as restoreAfter } from "./restoreAfter.js";
11
+ export { default as once } from './once.js';
12
+ export { default as sleep } from './sleep.js';
16
13
 
17
- export {
18
- isClass,
19
- isObject,
20
- isPlainObject,
21
- decodeBase64,
22
- encodeBase64,
23
- isBase64,
24
- };
14
+ export { default as reviver } from "./reviver.js";
15
+ export { default as replacer } from "./replacer.js";
16
+ export { isClass } from "./is.js";
17
+ export { decodeBase64, encodeBase64, isBase64 } from "./base64.js";
package/src/misc/is.js CHANGED
@@ -1,32 +1,37 @@
1
- const toString = Object.prototype.toString;
1
+ import newIsObject from "../object/isObject.js";
2
+ import newIsPlainObject from "../object/isPlainObject.js";
3
+ import isType from "../object/isType.js";
2
4
 
3
5
  /**
4
- * Check if parameter is an object.
6
+ * Check if parameter is the given className.
5
7
  *
6
- * @param {*} object
7
- * @returns {boolean}
8
+ * @deprecated use isType() instead.
9
+ * @param {any} object
10
+ * @param {String} className
11
+ * @returns {Boolean}
8
12
  */
9
- export function isObject(object) {
10
- return object !== null && typeof object === 'object'
13
+ export function isClass(object, className) {
14
+ return isType( object, className );
11
15
  }
12
16
 
13
17
  /**
14
- * Check if parameter is the given className.
18
+ * Check if parameter is an object.
15
19
  *
16
- * @param {*} object
17
- * @param {string} className
18
- * @returns {boolean}
20
+ * @deprecated use isObject() from separated file instead.
21
+ * @param {any} object
22
+ * @returns {Boolean}
19
23
  */
20
- export function isClass(object, className) {
21
- return isObject( object ) && toString.call( object ) === '[object ' + className + ']';
24
+ export function isObject(object) {
25
+ return newIsObject(object);
22
26
  }
23
27
 
24
28
  /**
25
29
  * Check if parameter is a plain object.
26
30
  *
27
- * @param {*} object
28
- * @returns {boolean}
31
+ * @deprecated use isPlainObject() from separated file instead.
32
+ * @param {any} object
33
+ * @returns {Boolean}
29
34
  */
30
35
  export function isPlainObject(object) {
31
- return isClass( object, 'Object' );
36
+ return newIsPlainObject(object);
32
37
  }