@straylightagency/utils 1.1.9 → 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 (74) 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 -24
  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 +7 -5
  48. package/src/number/format.js +7 -5
  49. package/src/number/random.js +14 -4
  50. package/src/object/deepClone.js +71 -0
  51. package/src/object/deepEqual.js +57 -0
  52. package/src/object/entries.js +10 -0
  53. package/src/object/filter.js +2 -2
  54. package/src/object/get.js +9 -6
  55. package/src/object/has.js +16 -8
  56. package/src/object/index.js +11 -1
  57. package/src/object/invert.js +22 -0
  58. package/src/object/isObject.js +9 -0
  59. package/src/object/isPlainObject.js +9 -0
  60. package/src/object/isType.js +14 -0
  61. package/src/object/map.js +2 -2
  62. package/src/object/merge.js +46 -0
  63. package/src/object/omit.js +14 -0
  64. package/src/object/only.js +3 -3
  65. package/src/object/pick.js +14 -0
  66. package/src/object/serialize.js +12 -3
  67. package/src/object/set.js +4 -4
  68. package/src/object/unserialize.js +30 -5
  69. package/src/string/capitalFirst.js +2 -2
  70. package/src/string/capitalWords.js +2 -2
  71. package/src/string/interpolate.js +3 -3
  72. package/src/string/slugify.js +11 -19
  73. package/readme.MD +0 -0
  74. package/src/misc/restoreBooleanAfter.js +0 -17
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Return an object with specified keys.
3
3
  *
4
- * @param {{}} object
5
- * @param {[]} keys
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
+ }
@@ -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 {{}} object
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 {{}} object
8
- * @param {string} keys
9
- * @param {*} value
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 {string} object
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
  }
@@ -4,8 +4,8 @@
4
4
  * @example capitalFirst("lazy dog")
5
5
  * => "Lazy dog"
6
6
  *
7
- * @param {string} string
8
- * @returns {string}
7
+ * @param {String} string
8
+ * @returns {String}
9
9
  */
10
10
  export default function capitalFirst(string) {
11
11
  string = string.toString();
@@ -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 {string} string
10
- * @returns {string}
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 {string} string
8
- * @param {object} replace
9
- * @returns {string}
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 ) =>
@@ -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 {string} string
8
- * @param {string} separator
9
- * @returns {string}
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
- .replace(/[^a-z0-9 -]/g, '') /** remove any non-alphanumeric characters */
26
- .replace(/\s+/g, separator ) /** replace spaces with hyphens */
27
- .replace(/-+/g, separator ); /** remove consecutive hyphens */
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
- }