@valkyriestudios/utils 10.0.0 → 11.1.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 (255) hide show
  1. package/README.md +1 -1
  2. package/dist/array/dedupe.d.ts +8 -0
  3. package/dist/array/dedupe.js +19 -0
  4. package/dist/array/is.d.ts +8 -0
  5. package/dist/array/is.js +6 -0
  6. package/dist/array/isNotEmpty.d.ts +8 -0
  7. package/dist/array/isNotEmpty.js +6 -0
  8. package/dist/array/join.d.ts +36 -0
  9. package/dist/array/join.js +24 -0
  10. package/dist/array/mapFn.d.ts +40 -0
  11. package/{src/array/mapFn.mjs → dist/array/mapFn.js} +13 -15
  12. package/dist/array/mapKey.d.ts +38 -0
  13. package/dist/array/mapKey.js +27 -0
  14. package/dist/array/mapPrimitive.d.ts +40 -0
  15. package/dist/array/mapPrimitive.js +27 -0
  16. package/dist/array/shuffle.d.ts +9 -0
  17. package/dist/array/shuffle.js +11 -0
  18. package/dist/array/sort.d.ts +58 -0
  19. package/dist/array/sort.js +92 -0
  20. package/dist/boolean/is.d.ts +8 -0
  21. package/dist/boolean/is.js +6 -0
  22. package/dist/caching/memoize.d.ts +10 -0
  23. package/dist/caching/memoize.js +15 -0
  24. package/dist/date/addUTC.d.ts +10 -0
  25. package/{src/date/addUTC.mjs → dist/date/addUTC.js} +11 -26
  26. package/dist/date/diff.d.ts +10 -0
  27. package/dist/date/diff.js +38 -0
  28. package/dist/date/endOfUTC.d.ts +9 -0
  29. package/dist/date/endOfUTC.js +52 -0
  30. package/dist/date/is.d.ts +8 -0
  31. package/dist/date/is.js +6 -0
  32. package/dist/date/nowUnix.d.ts +6 -0
  33. package/dist/date/nowUnix.js +6 -0
  34. package/dist/date/nowUnixMs.d.ts +6 -0
  35. package/dist/date/nowUnixMs.js +6 -0
  36. package/dist/date/startOfUTC.d.ts +9 -0
  37. package/dist/date/startOfUTC.js +52 -0
  38. package/dist/date/toUTC.d.ts +8 -0
  39. package/dist/date/toUTC.js +9 -0
  40. package/dist/date/toUnix.d.ts +8 -0
  41. package/dist/date/toUnix.js +9 -0
  42. package/dist/deep/freeze.d.ts +14 -0
  43. package/dist/deep/freeze.js +23 -0
  44. package/dist/deep/get.d.ts +27 -0
  45. package/dist/deep/get.js +41 -0
  46. package/dist/deep/seal.d.ts +14 -0
  47. package/dist/deep/seal.js +23 -0
  48. package/dist/deep/set.d.ts +42 -0
  49. package/dist/deep/set.js +52 -0
  50. package/dist/equal.d.ts +10 -0
  51. package/dist/equal.js +43 -0
  52. package/dist/function/is.d.ts +8 -0
  53. package/dist/function/is.js +6 -0
  54. package/dist/function/noop.d.ts +6 -0
  55. package/dist/function/noop.js +5 -0
  56. package/dist/function/noopresolve.d.ts +9 -0
  57. package/dist/function/noopresolve.js +6 -0
  58. package/dist/function/noopreturn.d.ts +8 -0
  59. package/dist/function/noopreturn.js +6 -0
  60. package/dist/function/sleep.d.ts +9 -0
  61. package/dist/function/sleep.js +8 -0
  62. package/dist/hash/fnv1A.d.ts +10 -0
  63. package/dist/hash/fnv1A.js +51 -0
  64. package/dist/hash/guid.d.ts +6 -0
  65. package/{src/hash/guid.mjs → dist/hash/guid.js} +9 -14
  66. package/dist/index.d.ts +358 -0
  67. package/dist/is.d.ts +75 -0
  68. package/dist/is.js +77 -0
  69. package/dist/number/is.d.ts +8 -0
  70. package/dist/number/is.js +6 -0
  71. package/dist/number/isAbove.d.ts +9 -0
  72. package/dist/number/isAbove.js +6 -0
  73. package/dist/number/isAboveOrEqual.d.ts +9 -0
  74. package/dist/number/isAboveOrEqual.js +6 -0
  75. package/dist/number/isBelow.d.ts +9 -0
  76. package/dist/number/isBelow.js +6 -0
  77. package/dist/number/isBelowOrEqual.d.ts +10 -0
  78. package/dist/number/isBelowOrEqual.js +6 -0
  79. package/dist/number/isBetween.d.ts +12 -0
  80. package/dist/number/isBetween.js +11 -0
  81. package/dist/number/isInteger.d.ts +8 -0
  82. package/dist/number/isInteger.js +6 -0
  83. package/dist/number/isIntegerAbove.d.ts +9 -0
  84. package/dist/number/isIntegerAbove.js +6 -0
  85. package/dist/number/isIntegerAboveOrEqual.d.ts +10 -0
  86. package/dist/number/isIntegerAboveOrEqual.js +6 -0
  87. package/dist/number/isIntegerBelow.d.ts +9 -0
  88. package/dist/number/isIntegerBelow.js +6 -0
  89. package/dist/number/isIntegerBelowOrEqual.d.ts +10 -0
  90. package/dist/number/isIntegerBelowOrEqual.js +6 -0
  91. package/dist/number/isIntegerBetween.d.ts +12 -0
  92. package/dist/number/isIntegerBetween.js +11 -0
  93. package/dist/number/isNumericalNaN.d.ts +8 -0
  94. package/dist/number/isNumericalNaN.js +6 -0
  95. package/dist/number/randomBetween.d.ts +9 -0
  96. package/dist/number/randomBetween.js +9 -0
  97. package/dist/number/randomIntBetween.d.ts +10 -0
  98. package/dist/number/randomIntBetween.js +9 -0
  99. package/dist/number/round.d.ts +9 -0
  100. package/dist/number/round.js +10 -0
  101. package/dist/number/toPercentage.d.ts +11 -0
  102. package/dist/number/toPercentage.js +11 -0
  103. package/dist/object/define.d.ts +15 -0
  104. package/dist/object/define.js +9 -0
  105. package/dist/object/is.d.ts +8 -0
  106. package/dist/object/is.js +7 -0
  107. package/dist/object/isNotEmpty.d.ts +8 -0
  108. package/dist/object/isNotEmpty.js +6 -0
  109. package/dist/object/merge.d.ts +18 -0
  110. package/dist/object/merge.js +20 -0
  111. package/dist/object/pick.d.ts +13 -0
  112. package/dist/object/pick.js +34 -0
  113. package/dist/regexp/is.d.ts +8 -0
  114. package/dist/regexp/is.js +6 -0
  115. package/dist/regexp/sanitize.d.ts +11 -0
  116. package/dist/regexp/sanitize.js +8 -0
  117. package/dist/string/humanizeBytes.d.ts +40 -0
  118. package/{src/string/humanizeBytes.mjs → dist/string/humanizeBytes.js} +7 -10
  119. package/dist/string/humanizeNumber.d.ts +52 -0
  120. package/dist/string/humanizeNumber.js +60 -0
  121. package/dist/string/is.d.ts +8 -0
  122. package/dist/string/is.js +6 -0
  123. package/dist/string/isBetween.d.ts +13 -0
  124. package/dist/string/isBetween.js +14 -0
  125. package/dist/string/isNotEmpty.d.ts +9 -0
  126. package/dist/string/isNotEmpty.js +8 -0
  127. package/dist/string/shorten.d.ts +10 -0
  128. package/dist/string/shorten.js +11 -0
  129. package/package.json +112 -46
  130. package/tsconfig.types.json +12 -0
  131. package/tsconfig.types_all.json +12 -0
  132. package/array/dedupe.js +0 -33
  133. package/array/is.js +0 -9
  134. package/array/isNotEmpty.js +0 -9
  135. package/array/join.js +0 -38
  136. package/array/mapFn.js +0 -37
  137. package/array/mapKey.js +0 -36
  138. package/array/mapPrimitive.js +0 -38
  139. package/array/shuffle.js +0 -14
  140. package/array/sort.js +0 -125
  141. package/boolean/is.js +0 -9
  142. package/caching/memoize.js +0 -19
  143. package/date/addUTC.js +0 -60
  144. package/date/diff.js +0 -40
  145. package/date/endOfUTC.js +0 -55
  146. package/date/is.js +0 -9
  147. package/date/nowUnix.js +0 -9
  148. package/date/nowUnixMs.js +0 -9
  149. package/date/startOfUTC.js +0 -55
  150. package/date/toUTC.js +0 -12
  151. package/date/toUnix.js +0 -12
  152. package/deep/define.js +0 -12
  153. package/deep/freeze.js +0 -17
  154. package/deep/get.js +0 -29
  155. package/deep/seal.js +0 -17
  156. package/deep/set.js +0 -26
  157. package/equal.js +0 -35
  158. package/function/is.js +0 -8
  159. package/function/noop.js +0 -7
  160. package/function/noopresolve.js +0 -10
  161. package/function/noopreturn.js +0 -8
  162. package/function/sleep.js +0 -13
  163. package/hash/fnv1A.js +0 -44
  164. package/hash/guid.js +0 -16
  165. package/is.js +0 -319
  166. package/number/is.js +0 -9
  167. package/number/isAbove.js +0 -8
  168. package/number/isAboveOrEqual.js +0 -8
  169. package/number/isBelow.js +0 -8
  170. package/number/isBelowOrEqual.js +0 -8
  171. package/number/isBetween.js +0 -9
  172. package/number/isInteger.js +0 -9
  173. package/number/isIntegerAbove.js +0 -8
  174. package/number/isIntegerAboveOrEqual.js +0 -8
  175. package/number/isIntegerBelow.js +0 -8
  176. package/number/isIntegerBelowOrEqual.js +0 -8
  177. package/number/isIntegerBetween.js +0 -9
  178. package/number/isNumericalNaN.js +0 -9
  179. package/number/randomBetween.js +0 -11
  180. package/number/randomIntBetween.js +0 -11
  181. package/number/round.js +0 -12
  182. package/number/toPercentage.js +0 -15
  183. package/object/define.js +0 -11
  184. package/object/is.js +0 -10
  185. package/object/isNotEmpty.js +0 -9
  186. package/object/merge.js +0 -19
  187. package/object/pick.js +0 -41
  188. package/regexp/is.js +0 -9
  189. package/regexp/sanitize.js +0 -9
  190. package/src/array/dedupe.mjs +0 -24
  191. package/src/array/is.mjs +0 -5
  192. package/src/array/isNotEmpty.mjs +0 -5
  193. package/src/array/join.mjs +0 -29
  194. package/src/array/mapKey.mjs +0 -31
  195. package/src/array/mapPrimitive.mjs +0 -30
  196. package/src/array/shuffle.mjs +0 -14
  197. package/src/array/sort.mjs +0 -101
  198. package/src/boolean/is.mjs +0 -5
  199. package/src/caching/memoize.mjs +0 -22
  200. package/src/date/diff.mjs +0 -46
  201. package/src/date/endOfUTC.mjs +0 -130
  202. package/src/date/is.mjs +0 -5
  203. package/src/date/nowUnix.mjs +0 -5
  204. package/src/date/nowUnixMs.mjs +0 -5
  205. package/src/date/startOfUTC.mjs +0 -129
  206. package/src/date/toUTC.mjs +0 -19
  207. package/src/date/toUnix.mjs +0 -11
  208. package/src/deep/define.mjs +0 -7
  209. package/src/deep/freeze.mjs +0 -20
  210. package/src/deep/get.mjs +0 -52
  211. package/src/deep/seal.mjs +0 -20
  212. package/src/deep/set.mjs +0 -46
  213. package/src/equal.mjs +0 -65
  214. package/src/function/is.mjs +0 -6
  215. package/src/function/noop.mjs +0 -5
  216. package/src/function/noopresolve.mjs +0 -6
  217. package/src/function/noopreturn.mjs +0 -6
  218. package/src/function/sleep.mjs +0 -12
  219. package/src/hash/fnv1A.mjs +0 -53
  220. package/src/is.mjs +0 -86
  221. package/src/number/is.mjs +0 -5
  222. package/src/number/isAbove.mjs +0 -6
  223. package/src/number/isAboveOrEqual.mjs +0 -6
  224. package/src/number/isBelow.mjs +0 -6
  225. package/src/number/isBelowOrEqual.mjs +0 -6
  226. package/src/number/isBetween.mjs +0 -13
  227. package/src/number/isInteger.mjs +0 -5
  228. package/src/number/isIntegerAbove.mjs +0 -6
  229. package/src/number/isIntegerAboveOrEqual.mjs +0 -6
  230. package/src/number/isIntegerBelow.mjs +0 -6
  231. package/src/number/isIntegerBelowOrEqual.mjs +0 -6
  232. package/src/number/isIntegerBetween.mjs +0 -13
  233. package/src/number/isNumericalNaN.mjs +0 -5
  234. package/src/number/randomBetween.mjs +0 -11
  235. package/src/number/randomIntBetween.mjs +0 -11
  236. package/src/number/round.mjs +0 -10
  237. package/src/number/toPercentage.mjs +0 -14
  238. package/src/object/define.mjs +0 -10
  239. package/src/object/is.mjs +0 -7
  240. package/src/object/isNotEmpty.mjs +0 -5
  241. package/src/object/merge.mjs +0 -25
  242. package/src/object/pick.mjs +0 -35
  243. package/src/regexp/is.mjs +0 -5
  244. package/src/regexp/sanitize.mjs +0 -10
  245. package/src/string/humanizeNumber.mjs +0 -82
  246. package/src/string/is.mjs +0 -5
  247. package/src/string/isBetween.mjs +0 -16
  248. package/src/string/isNotEmpty.mjs +0 -6
  249. package/src/string/shorten.mjs +0 -20
  250. package/string/humanizeBytes.js +0 -21
  251. package/string/humanizeNumber.js +0 -54
  252. package/string/is.js +0 -9
  253. package/string/isBetween.js +0 -11
  254. package/string/isNotEmpty.js +0 -11
  255. package/string/shorten.js +0 -11
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![npm](https://img.shields.io/npm/v/@valkyriestudios/utils.svg)](https://www.npmjs.com/package/@valkyriestudios/utils)
6
6
  [![npm](https://img.shields.io/npm/dm/@valkyriestudios/utils.svg)](https://www.npmjs.com/package/@valkyriestudios/utils)
7
7
 
8
- A collection of single-function utilities for common tasks
8
+ Zero-dependency collection of single-function utilities for common tasks
9
9
 
10
10
  `npm install @valkyriestudios/utils`
11
11
 
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Dedupes the provided array
3
+ *
4
+ * @param val - Array to dedupe
5
+ *
6
+ * @returns Deduped array
7
+ */
8
+ export default function dedupe(val: any[]): any[];
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fnv1A_1 = require("../hash/fnv1A");
4
+ function dedupe(val) {
5
+ if (!Array.isArray(val) || val.length === 0)
6
+ return [];
7
+ const map = new Map();
8
+ const acc = [];
9
+ let hash;
10
+ for (const item of val) {
11
+ hash = (0, fnv1A_1.default)(item);
12
+ if (map.has(hash))
13
+ continue;
14
+ map.set(hash, true);
15
+ acc.push(item);
16
+ }
17
+ return acc;
18
+ }
19
+ exports.default = dedupe;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Check whether or not a provided value is an array
3
+ *
4
+ * @param val - Value to verify
5
+ *
6
+ * @returns Whether or not the value is an array
7
+ */
8
+ export default function isArray(val: any): boolean;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isArray(val) {
4
+ return Array.isArray(val);
5
+ }
6
+ exports.default = isArray;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Check whether or not a provided value is an array with content
3
+ *
4
+ * @param val - Value to verify
5
+ *
6
+ * @returns Whether or not the value is an array with content
7
+ */
8
+ export default function isNotEmptyArray(val: any): boolean;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isNotEmptyArray(val) {
4
+ return Array.isArray(val) && val.length !== 0;
5
+ }
6
+ exports.default = isNotEmptyArray;
@@ -0,0 +1,36 @@
1
+ interface joinOptions {
2
+ /**
3
+ * Delimiter to join with
4
+ * (default=' ')
5
+ * eg: join(['hello', 'world', {delim: '_'}]) -> 'hello_world'
6
+ */
7
+ delim?: string;
8
+ /**
9
+ * Trim after joining or not
10
+ * (default=true)
11
+ * eg: join([' hello', 'world '], {trim: true}) -> 'hello world'
12
+ */
13
+ trim?: boolean;
14
+ /**
15
+ * Automatically trim all string values
16
+ * (default=true)
17
+ * eg: join([' hello ', ' world '], {valtrim: true}) -> 'hello world'
18
+ */
19
+ valtrim?: boolean;
20
+ /**
21
+ * Automatically round all numeric values
22
+ * (default=false)
23
+ * eg: join([5.432, 'world', 1.2], {valround: 1}) -> '5.4 world 1.2'
24
+ */
25
+ valround?: number;
26
+ }
27
+ /**
28
+ * Join an array of values while autofiltering any non-string/non-number elements
29
+ *
30
+ * @param val - Array of values to join
31
+ * @param opts - Join options
32
+ *
33
+ * @returns Joined array as string
34
+ */
35
+ export default function join(val: any[], opts?: joinOptions): string;
36
+ export {};
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const round_1 = require("../number/round");
4
+ function join(val, opts = {}) {
5
+ if (!Array.isArray(val) || val.length === 0)
6
+ return '';
7
+ const OPTS = Object.assign({
8
+ delim: ' ',
9
+ trim: true,
10
+ valtrim: true,
11
+ valround: false,
12
+ }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
13
+ const filtered = [];
14
+ for (const el of val) {
15
+ if (typeof el === 'string' && el.trim().length > 0) {
16
+ filtered.push(OPTS.valtrim === true ? el.trim() : el);
17
+ }
18
+ else if (Number.isFinite(el)) {
19
+ filtered.push(Number.isFinite(OPTS.valround) ? (0, round_1.default)(el, OPTS.valround) : el);
20
+ }
21
+ }
22
+ return OPTS.trim === true ? filtered.join(OPTS.delim).trim() : filtered.join(OPTS.delim);
23
+ }
24
+ exports.default = join;
@@ -0,0 +1,40 @@
1
+ interface mapOptions {
2
+ /**
3
+ * Allow merging existing keys or not, if not keys will be overriden if they exist
4
+ * (default=false)
5
+ *
6
+ * Example:
7
+ * mapFn([{uid: 12, a: 'hi'}, {uid: 12, b: 'ho'}], el => el.uid, {merge: true})
8
+ * Output:
9
+ * {12: {uid: 12, a: 'hi', b: 'ho'}}
10
+ * Output if merge is false
11
+ * {12: {uid: 12, b: 'ho'}}
12
+ */
13
+ merge?: boolean;
14
+ }
15
+ interface kvMap {
16
+ [key: string]: {
17
+ [key: string]: any;
18
+ };
19
+ }
20
+ /**
21
+ * Map an object array into a kv-object through a function that generates a key. Returning a non-string,
22
+ * non-numeric value from the function (eg: false) will filter out the object.
23
+ *
24
+ * Example:
25
+ * mapFn([{uid: 12, name: 'Peter'}, {uid: 15, name: 'Jonas'}], el => el.uid);
26
+ * Output:
27
+ * {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
28
+ *
29
+ * @param val - Array to map
30
+ * @param fn - Handler function which is run for each of the objects and should return a string or number
31
+ * @param opts - Options object to override built-in defaults
32
+ *
33
+ * @returns KV-Map object
34
+ */
35
+ export default function mapFn(arr: {
36
+ [key: string]: any;
37
+ }[], fn: (entry: {
38
+ [key: string]: any;
39
+ }) => (string | number | boolean), opts?: mapOptions): kvMap;
40
+ export {};
@@ -1,30 +1,28 @@
1
1
  'use strict';
2
-
3
- export default function mapFn (arr, fn, opts) {
4
- if (
5
- (!Array.isArray(arr) || arr.length === 0) ||
6
- typeof fn !== 'function'
7
- ) return {};
8
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function mapFn(arr, fn, opts = {}) {
4
+ if ((!Array.isArray(arr) || arr.length === 0) ||
5
+ typeof fn !== 'function')
6
+ return {};
9
7
  const OPTS = Object.assign({
10
8
  merge: false,
11
9
  }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
12
-
13
10
  const map = {};
14
11
  let hash = false;
15
12
  for (const el of arr) {
16
- if (Object.prototype.toString.call(el) !== '[object Object]') continue;
17
-
18
- // Get hash
13
+ if (Object.prototype.toString.call(el) !== '[object Object]')
14
+ continue;
19
15
  hash = fn(el);
20
- if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length > 0)) continue;
21
-
16
+ if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length > 0))
17
+ continue;
18
+ hash = `${hash}`;
22
19
  if (OPTS.merge === true && map.hasOwnProperty(hash)) {
23
20
  map[hash] = Object.assign(map[hash], el);
24
- } else {
21
+ }
22
+ else {
25
23
  map[hash] = el;
26
24
  }
27
25
  }
28
-
29
26
  return map;
30
27
  }
28
+ exports.default = mapFn;
@@ -0,0 +1,38 @@
1
+ interface mapOptions {
2
+ /**
3
+ * Allow merging existing keys or not, if not keys will be overriden if they exist
4
+ * (default=false)
5
+ *
6
+ * Example:
7
+ * mapKey([{uid: 12, a: 'hi'}, {uid: 12, b: 'ho'}], 'uid', {merge: true})
8
+ * Output:
9
+ * {12: {uid: 12, a: 'hi', b: 'ho'}}
10
+ * Output if merge is false
11
+ * {12: {uid: 12, b: 'ho'}}
12
+ */
13
+ merge?: boolean;
14
+ }
15
+ interface kvMap {
16
+ [key: string]: {
17
+ [key: string]: any;
18
+ };
19
+ }
20
+ /**
21
+ * Map an object array into a kv-object by passing a common key that exists on the objects. Objects for
22
+ * which the key doesn't exist will be filtered out automatically
23
+ *
24
+ * Example:
25
+ * mapKey([{uid: 12, name: 'Peter'}, {uid: 15, name: 'Jonas'}], 'uid');
26
+ * Output:
27
+ * {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
28
+ *
29
+ * @param val - Array to map
30
+ * @param key - Key to map by
31
+ * @param opts - Options object to override built-in defaults
32
+ *
33
+ * @returns KV-Map object
34
+ */
35
+ export default function mapKey(arr: {
36
+ [key: string]: any;
37
+ }[], key: string, opts?: mapOptions): kvMap;
38
+ export {};
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function mapKey(arr, key, opts = {}) {
4
+ if ((!Array.isArray(arr) || arr.length === 0) ||
5
+ typeof key !== 'string')
6
+ return {};
7
+ const key_s = key.trim();
8
+ if (key_s.length === 0)
9
+ return {};
10
+ const OPTS = Object.assign({
11
+ merge: false,
12
+ }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
13
+ const map = {};
14
+ for (const el of arr) {
15
+ if (Object.prototype.toString.call(el) !== '[object Object]' ||
16
+ !Object.prototype.hasOwnProperty.call(el, key_s))
17
+ continue;
18
+ if (OPTS.merge === true && map.hasOwnProperty(el[key_s])) {
19
+ map[el[key_s]] = Object.assign(map[el[key_s]], el);
20
+ }
21
+ else {
22
+ map[el[key_s]] = el;
23
+ }
24
+ }
25
+ return map;
26
+ }
27
+ exports.default = mapKey;
@@ -0,0 +1,40 @@
1
+ interface mapOptions {
2
+ /**
3
+ * Automatically trim all string values
4
+ * (default=false)
5
+ * eg: join([' hello ', ' world '], {valtrim: true}) -> 'hello world'
6
+ */
7
+ valtrim?: boolean;
8
+ /**
9
+ * Automatically round all numeric values
10
+ * (default=false)
11
+ * eg: mapPrimitive([5.432, 5.4, 5.43, 4.2, 4.1], {valround: true}) -> {'5.432': 5, '5.4': 5, '5.43': 5, '4.2': 4, '4.1': 4}
12
+ * eg: mapPrimitive([5.432, 5.43, 4.21, 4.1], {valround: 1}) -> {'5.432': 5.4, '5.43': 5.4, '4.21': 4.2, '4.1': 4.1}
13
+ */
14
+ valround?: boolean | number;
15
+ /**
16
+ * Automaticaly round all keys from numeric values
17
+ * (default=false)
18
+ * eg: mapPrimitive([5.432, 5.4, 5.43, 4.2, 4.1], {keyround: true}) -> {5: 5.43, 4: 4.1}
19
+ */
20
+ keyround?: boolean;
21
+ }
22
+ interface mapReturn {
23
+ [key: string]: string | number;
24
+ }
25
+ /**
26
+ * Map an array of primitive values (numbers/strings) into a kv-object
27
+ * non-numeric and non-string values will be filtered out
28
+ *
29
+ * Example:
30
+ * mapPrimitive(['hello', 'hello', 'foo', 'bar']);
31
+ * Output:
32
+ * {hello: 'hello', foo: 'foo', bar: 'bar'}
33
+ *
34
+ * @param val - Array to map
35
+ * @param opts - Options object to override built-in defaults
36
+ *
37
+ * @returns KV-Map object
38
+ */
39
+ export default function mapPrimitive(arr: any[], opts?: mapOptions): mapReturn;
40
+ export {};
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const round_1 = require("../number/round");
4
+ function mapPrimitive(arr, opts = {}) {
5
+ if (!Array.isArray(arr) || arr.length === 0)
6
+ return {};
7
+ const OPTS = Object.assign({
8
+ valtrim: false,
9
+ valround: false,
10
+ keyround: false,
11
+ }, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
12
+ const map = {};
13
+ for (const el of arr) {
14
+ if (typeof el === 'string' && el.trim().length > 0) {
15
+ map[el.trim()] = OPTS.valtrim ? el.trim() : el;
16
+ }
17
+ else if (Number.isFinite(el)) {
18
+ map[OPTS.keyround === true ? Math.round(el) : el] = OPTS.valround === false
19
+ ? el
20
+ : OPTS.valround === true
21
+ ? Math.round(el)
22
+ : (0, round_1.default)(el, OPTS.valround);
23
+ }
24
+ }
25
+ return map;
26
+ }
27
+ exports.default = mapPrimitive;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Shuffle an array using a Fisher-Yates shuffle O(n)
3
+ * https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
4
+ *
5
+ * Take Note: The passed array will be changed and edited in place
6
+ *
7
+ * @param val - Array to shuffle
8
+ */
9
+ export default function shuffle(arr: any[]): void;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function shuffle(arr) {
4
+ if (!Array.isArray(arr) || arr.length === 0)
5
+ return;
6
+ for (let i = arr.length - 1; i > 0; i--) {
7
+ const j = Math.floor(Math.random() * (i + 1));
8
+ [arr[i], arr[j]] = [arr[j], arr[i]];
9
+ }
10
+ }
11
+ exports.default = shuffle;
@@ -0,0 +1,58 @@
1
+ interface sortOptions {
2
+ /**
3
+ * Filter function to apply to the array before sorting
4
+ * (default=isNotEmptyObject)
5
+ */
6
+ filter_fn?: (el: any) => boolean;
7
+ /**
8
+ * Remove objects that don't have the key or where the key is falsy
9
+ * (default=false)
10
+ */
11
+ nokey_hide?: boolean;
12
+ /**
13
+ * Move invalid values (eg: non-objects or objects that don't match the key/function passed) to the end of the sorted array
14
+ * (default=true)
15
+ */
16
+ nokey_atend?: boolean;
17
+ }
18
+ interface sortObject {
19
+ [key: string]: any;
20
+ }
21
+ type sortByFunction = (el: sortObject) => string;
22
+ /**
23
+ * Sort an array of objects, uses an implementation of Tony Hoare's quicksort
24
+ * (https://cs.stanford.edu/people/eroberts/courses/soco/projects/2008-09/tony-hoare/quicksort.html)
25
+ *
26
+ * Example:
27
+ * sort([
28
+ * {test: 'Peter'},
29
+ * {test: 'Jack'},
30
+ * {test: 'Pony'},
31
+ * {test: 'John'},
32
+ * {test: 'Joe'},
33
+ * {test: 'Bob'},
34
+ * {test: 'Alice'},
35
+ * ], 'test', 'desc');
36
+ * Output:
37
+ * [{test: 'Pony'}, {test: 'Peter'}, {test: 'John'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
38
+ *
39
+ * Example w/ Function:
40
+ * sort([
41
+ * {test: 'Peter'},
42
+ * {test: 'Pony'},
43
+ * {test: 'JOHn'},
44
+ * {test: 'Joe'},
45
+ * ], el => el.test.toLowerCase(), 'desc');
46
+ * Output:
47
+ * [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Joe'}]
48
+ *
49
+ * @param val - Array to sort
50
+ * @param by - Either a string (key) or a function
51
+ * @param dir - (default='asc') Direction to sort in (asc or desc)
52
+ * @param opts - Sort options
53
+ *
54
+ * @returns Sorted array
55
+ * @throws {Error}
56
+ */
57
+ export default function sort(arr: sortObject[], by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions): sortObject[];
58
+ export {};
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const is_1 = require("../boolean/is");
4
+ const isNotEmpty_1 = require("../object/isNotEmpty");
5
+ function partition(arr, start_ix, end_ix) {
6
+ const pivot_val = arr[Math.floor((start_ix + end_ix) / 2)].t;
7
+ while (start_ix <= end_ix) {
8
+ while (arr[start_ix].t < pivot_val) {
9
+ start_ix++;
10
+ }
11
+ while (arr[end_ix].t > pivot_val) {
12
+ end_ix--;
13
+ }
14
+ if (start_ix <= end_ix) {
15
+ const temp = arr[start_ix];
16
+ arr[start_ix] = arr[end_ix];
17
+ arr[end_ix] = temp;
18
+ start_ix++;
19
+ end_ix--;
20
+ }
21
+ }
22
+ return start_ix;
23
+ }
24
+ function quickSort(arr, start_ix = 0, end_ix = arr.length - 1) {
25
+ if (start_ix < end_ix) {
26
+ const ix = partition(arr, start_ix, end_ix);
27
+ quickSort(arr, start_ix, ix - 1);
28
+ quickSort(arr, ix, end_ix);
29
+ }
30
+ return arr;
31
+ }
32
+ function sort(arr, by, dir = 'asc', opts = {}) {
33
+ if (!Array.isArray(arr) || arr.length === 0)
34
+ return [];
35
+ if (dir !== 'asc' && dir !== 'desc')
36
+ throw new Error('Direction should be either asc or desc');
37
+ const has_opts = Object.prototype.toString.call(opts) === '[object Object]';
38
+ const OPTS = {
39
+ filter_fn: has_opts && typeof opts.filter_fn === 'function'
40
+ ? el => (0, isNotEmpty_1.default)(el) && opts.filter_fn(el)
41
+ : isNotEmpty_1.default,
42
+ nokey_hide: has_opts && (0, is_1.default)(opts.nokey_hide) ? opts.nokey_hide : false,
43
+ nokey_atend: has_opts && (0, is_1.default)(opts.nokey_atend) ? opts.nokey_atend : true,
44
+ };
45
+ const prepared_arr = [];
46
+ const nokey_arr = [];
47
+ if (typeof by === 'string') {
48
+ const by_s = by.trim();
49
+ if (by_s.length === 0)
50
+ throw new Error('Sort by as string should contain content');
51
+ for (const el of arr) {
52
+ if (!OPTS.filter_fn(el))
53
+ continue;
54
+ if (!Object.prototype.hasOwnProperty.call(el, by_s) || el[by_s] === undefined) {
55
+ nokey_arr.push(el);
56
+ }
57
+ else {
58
+ prepared_arr.push({ t: el[by_s], el });
59
+ }
60
+ }
61
+ }
62
+ else if (typeof by === 'function') {
63
+ let key;
64
+ for (const el of arr) {
65
+ if (!OPTS.filter_fn(el))
66
+ continue;
67
+ key = by(el);
68
+ if (key === undefined) {
69
+ nokey_arr.push(el);
70
+ }
71
+ else {
72
+ prepared_arr.push({ t: by(el), el });
73
+ }
74
+ }
75
+ }
76
+ else {
77
+ throw new Error('Sort by should either be a string with content or a function');
78
+ }
79
+ quickSort(prepared_arr);
80
+ if (dir === 'desc')
81
+ prepared_arr.reverse();
82
+ if (OPTS.nokey_hide) {
83
+ return prepared_arr.map(obj => obj.el);
84
+ }
85
+ else if (OPTS.nokey_atend) {
86
+ return [...prepared_arr.map(obj => obj.el), ...nokey_arr];
87
+ }
88
+ else {
89
+ return [...nokey_arr, ...prepared_arr.map(obj => obj.el)];
90
+ }
91
+ }
92
+ exports.default = sort;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Check whether or not a provided value is a boolean
3
+ *
4
+ * @param val - Value to verify
5
+ *
6
+ * @returns Whether or not the value is a boolean
7
+ */
8
+ export default function isBoolean(val: any): boolean;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isBoolean(val) {
4
+ return val === true || val === false;
5
+ }
6
+ exports.default = isBoolean;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Turn a function into a memoized function. An optional resolver function can be passed which allows custom cache key generation.
3
+ *
4
+ * Example:
5
+ * const memoized_function = memoize((a) => fnv1A(a));
6
+ *
7
+ * @param fn - Function to memoize
8
+ * @param resolver - Optional resolver function to generate cache key. If not passed the first argument is used as map key
9
+ */
10
+ export default function memoize(fn: Function, resolver?: Function): Function;
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function memoize(fn, resolver) {
4
+ const memoized = function () {
5
+ const key = typeof resolver === 'function' ? resolver.apply(this, arguments) : arguments[0];
6
+ if (memoized.cache.has(key))
7
+ return memoized.cache.get(key);
8
+ const result = fn.apply(this, arguments);
9
+ memoized.cache.set(key, result);
10
+ return result;
11
+ };
12
+ memoized.cache = new Map();
13
+ return memoized;
14
+ }
15
+ exports.default = memoize;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Adds the provided amount of a specific key to the provided date
3
+ *
4
+ * @param val - Date to set to end of
5
+ * @param amount - (default=0) Amount of key to add
6
+ * @param key - (default='millisecond') Key to set
7
+ *
8
+ * @returns New date with provided amount of key added
9
+ */
10
+ export default function addUTC(val: Date, amount?: number, key?: 'years' | 'year' | 'months' | 'month' | 'days' | 'day' | 'hours' | 'hour' | 'minutes' | 'minute' | 'seconds' | 'second' | 'milliseconds' | 'millisecond'): Date;
@@ -1,30 +1,14 @@
1
1
  'use strict';
2
-
3
- import isDate from './is.mjs';
4
-
5
- export default function addUTC (val, amount, key) {
6
- if (
7
- !isDate(val)
8
- ) throw new TypeError('addUTC requires a date object');
9
-
10
- if (
11
- !Number.isInteger(amount)
12
- ) throw new TypeError('Amount needs to be an integer');
13
-
14
- if (
15
- typeof key !== 'string'
16
- ) throw new TypeError('Key needs to be a string with content');
17
-
18
- const copy = new Date(Date.UTC(
19
- val.getUTCFullYear(),
20
- val.getUTCMonth(),
21
- val.getUTCDate(),
22
- val.getUTCHours(),
23
- val.getUTCMinutes(),
24
- val.getUTCSeconds(),
25
- val.getUTCMilliseconds()
26
- ));
27
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const is_1 = require("./is");
4
+ function addUTC(val, amount = 0, key = 'millisecond') {
5
+ if (!(0, is_1.default)(val))
6
+ throw new TypeError('addUTC requires a date object');
7
+ if (!Number.isInteger(amount))
8
+ throw new TypeError('Amount needs to be an integer');
9
+ if (typeof key !== 'string')
10
+ throw new TypeError('Key needs to be a string with content');
11
+ const copy = new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
28
12
  switch (key) {
29
13
  case 'years':
30
14
  case 'year': {
@@ -65,3 +49,4 @@ export default function addUTC (val, amount, key) {
65
49
  return copy;
66
50
  }
67
51
  }
52
+ exports.default = addUTC;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Compute the diff between two dates in the provided key
3
+ *
4
+ * @param val_a - Date to diff against
5
+ * @param val_b - Date to diff with
6
+ * @param key - (default='millisecond') Key to diff in
7
+ *
8
+ * @returns Numerical diff between two dates
9
+ */
10
+ export default function diff(val_a: Date, val_b: Date, key?: 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds'): number;