@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.
- package/README.md +1 -1
- package/dist/array/dedupe.d.ts +8 -0
- package/dist/array/dedupe.js +19 -0
- package/dist/array/is.d.ts +8 -0
- package/dist/array/is.js +6 -0
- package/dist/array/isNotEmpty.d.ts +8 -0
- package/dist/array/isNotEmpty.js +6 -0
- package/dist/array/join.d.ts +36 -0
- package/dist/array/join.js +24 -0
- package/dist/array/mapFn.d.ts +40 -0
- package/{src/array/mapFn.mjs → dist/array/mapFn.js} +13 -15
- package/dist/array/mapKey.d.ts +38 -0
- package/dist/array/mapKey.js +27 -0
- package/dist/array/mapPrimitive.d.ts +40 -0
- package/dist/array/mapPrimitive.js +27 -0
- package/dist/array/shuffle.d.ts +9 -0
- package/dist/array/shuffle.js +11 -0
- package/dist/array/sort.d.ts +58 -0
- package/dist/array/sort.js +92 -0
- package/dist/boolean/is.d.ts +8 -0
- package/dist/boolean/is.js +6 -0
- package/dist/caching/memoize.d.ts +10 -0
- package/dist/caching/memoize.js +15 -0
- package/dist/date/addUTC.d.ts +10 -0
- package/{src/date/addUTC.mjs → dist/date/addUTC.js} +11 -26
- package/dist/date/diff.d.ts +10 -0
- package/dist/date/diff.js +38 -0
- package/dist/date/endOfUTC.d.ts +9 -0
- package/dist/date/endOfUTC.js +52 -0
- package/dist/date/is.d.ts +8 -0
- package/dist/date/is.js +6 -0
- package/dist/date/nowUnix.d.ts +6 -0
- package/dist/date/nowUnix.js +6 -0
- package/dist/date/nowUnixMs.d.ts +6 -0
- package/dist/date/nowUnixMs.js +6 -0
- package/dist/date/startOfUTC.d.ts +9 -0
- package/dist/date/startOfUTC.js +52 -0
- package/dist/date/toUTC.d.ts +8 -0
- package/dist/date/toUTC.js +9 -0
- package/dist/date/toUnix.d.ts +8 -0
- package/dist/date/toUnix.js +9 -0
- package/dist/deep/freeze.d.ts +14 -0
- package/dist/deep/freeze.js +23 -0
- package/dist/deep/get.d.ts +27 -0
- package/dist/deep/get.js +41 -0
- package/dist/deep/seal.d.ts +14 -0
- package/dist/deep/seal.js +23 -0
- package/dist/deep/set.d.ts +42 -0
- package/dist/deep/set.js +52 -0
- package/dist/equal.d.ts +10 -0
- package/dist/equal.js +43 -0
- package/dist/function/is.d.ts +8 -0
- package/dist/function/is.js +6 -0
- package/dist/function/noop.d.ts +6 -0
- package/dist/function/noop.js +5 -0
- package/dist/function/noopresolve.d.ts +9 -0
- package/dist/function/noopresolve.js +6 -0
- package/dist/function/noopreturn.d.ts +8 -0
- package/dist/function/noopreturn.js +6 -0
- package/dist/function/sleep.d.ts +9 -0
- package/dist/function/sleep.js +8 -0
- package/dist/hash/fnv1A.d.ts +10 -0
- package/dist/hash/fnv1A.js +51 -0
- package/dist/hash/guid.d.ts +6 -0
- package/{src/hash/guid.mjs → dist/hash/guid.js} +9 -14
- package/dist/index.d.ts +358 -0
- package/dist/is.d.ts +75 -0
- package/dist/is.js +77 -0
- package/dist/number/is.d.ts +8 -0
- package/dist/number/is.js +6 -0
- package/dist/number/isAbove.d.ts +9 -0
- package/dist/number/isAbove.js +6 -0
- package/dist/number/isAboveOrEqual.d.ts +9 -0
- package/dist/number/isAboveOrEqual.js +6 -0
- package/dist/number/isBelow.d.ts +9 -0
- package/dist/number/isBelow.js +6 -0
- package/dist/number/isBelowOrEqual.d.ts +10 -0
- package/dist/number/isBelowOrEqual.js +6 -0
- package/dist/number/isBetween.d.ts +12 -0
- package/dist/number/isBetween.js +11 -0
- package/dist/number/isInteger.d.ts +8 -0
- package/dist/number/isInteger.js +6 -0
- package/dist/number/isIntegerAbove.d.ts +9 -0
- package/dist/number/isIntegerAbove.js +6 -0
- package/dist/number/isIntegerAboveOrEqual.d.ts +10 -0
- package/dist/number/isIntegerAboveOrEqual.js +6 -0
- package/dist/number/isIntegerBelow.d.ts +9 -0
- package/dist/number/isIntegerBelow.js +6 -0
- package/dist/number/isIntegerBelowOrEqual.d.ts +10 -0
- package/dist/number/isIntegerBelowOrEqual.js +6 -0
- package/dist/number/isIntegerBetween.d.ts +12 -0
- package/dist/number/isIntegerBetween.js +11 -0
- package/dist/number/isNumericalNaN.d.ts +8 -0
- package/dist/number/isNumericalNaN.js +6 -0
- package/dist/number/randomBetween.d.ts +9 -0
- package/dist/number/randomBetween.js +9 -0
- package/dist/number/randomIntBetween.d.ts +10 -0
- package/dist/number/randomIntBetween.js +9 -0
- package/dist/number/round.d.ts +9 -0
- package/dist/number/round.js +10 -0
- package/dist/number/toPercentage.d.ts +11 -0
- package/dist/number/toPercentage.js +11 -0
- package/dist/object/define.d.ts +15 -0
- package/dist/object/define.js +9 -0
- package/dist/object/is.d.ts +8 -0
- package/dist/object/is.js +7 -0
- package/dist/object/isNotEmpty.d.ts +8 -0
- package/dist/object/isNotEmpty.js +6 -0
- package/dist/object/merge.d.ts +18 -0
- package/dist/object/merge.js +20 -0
- package/dist/object/pick.d.ts +13 -0
- package/dist/object/pick.js +34 -0
- package/dist/regexp/is.d.ts +8 -0
- package/dist/regexp/is.js +6 -0
- package/dist/regexp/sanitize.d.ts +11 -0
- package/dist/regexp/sanitize.js +8 -0
- package/dist/string/humanizeBytes.d.ts +40 -0
- package/{src/string/humanizeBytes.mjs → dist/string/humanizeBytes.js} +7 -10
- package/dist/string/humanizeNumber.d.ts +52 -0
- package/dist/string/humanizeNumber.js +60 -0
- package/dist/string/is.d.ts +8 -0
- package/dist/string/is.js +6 -0
- package/dist/string/isBetween.d.ts +13 -0
- package/dist/string/isBetween.js +14 -0
- package/dist/string/isNotEmpty.d.ts +9 -0
- package/dist/string/isNotEmpty.js +8 -0
- package/dist/string/shorten.d.ts +10 -0
- package/dist/string/shorten.js +11 -0
- package/package.json +112 -46
- package/tsconfig.types.json +12 -0
- package/tsconfig.types_all.json +12 -0
- package/array/dedupe.js +0 -33
- package/array/is.js +0 -9
- package/array/isNotEmpty.js +0 -9
- package/array/join.js +0 -38
- package/array/mapFn.js +0 -37
- package/array/mapKey.js +0 -36
- package/array/mapPrimitive.js +0 -38
- package/array/shuffle.js +0 -14
- package/array/sort.js +0 -125
- package/boolean/is.js +0 -9
- package/caching/memoize.js +0 -19
- package/date/addUTC.js +0 -60
- package/date/diff.js +0 -40
- package/date/endOfUTC.js +0 -55
- package/date/is.js +0 -9
- package/date/nowUnix.js +0 -9
- package/date/nowUnixMs.js +0 -9
- package/date/startOfUTC.js +0 -55
- package/date/toUTC.js +0 -12
- package/date/toUnix.js +0 -12
- package/deep/define.js +0 -12
- package/deep/freeze.js +0 -17
- package/deep/get.js +0 -29
- package/deep/seal.js +0 -17
- package/deep/set.js +0 -26
- package/equal.js +0 -35
- package/function/is.js +0 -8
- package/function/noop.js +0 -7
- package/function/noopresolve.js +0 -10
- package/function/noopreturn.js +0 -8
- package/function/sleep.js +0 -13
- package/hash/fnv1A.js +0 -44
- package/hash/guid.js +0 -16
- package/is.js +0 -319
- package/number/is.js +0 -9
- package/number/isAbove.js +0 -8
- package/number/isAboveOrEqual.js +0 -8
- package/number/isBelow.js +0 -8
- package/number/isBelowOrEqual.js +0 -8
- package/number/isBetween.js +0 -9
- package/number/isInteger.js +0 -9
- package/number/isIntegerAbove.js +0 -8
- package/number/isIntegerAboveOrEqual.js +0 -8
- package/number/isIntegerBelow.js +0 -8
- package/number/isIntegerBelowOrEqual.js +0 -8
- package/number/isIntegerBetween.js +0 -9
- package/number/isNumericalNaN.js +0 -9
- package/number/randomBetween.js +0 -11
- package/number/randomIntBetween.js +0 -11
- package/number/round.js +0 -12
- package/number/toPercentage.js +0 -15
- package/object/define.js +0 -11
- package/object/is.js +0 -10
- package/object/isNotEmpty.js +0 -9
- package/object/merge.js +0 -19
- package/object/pick.js +0 -41
- package/regexp/is.js +0 -9
- package/regexp/sanitize.js +0 -9
- package/src/array/dedupe.mjs +0 -24
- package/src/array/is.mjs +0 -5
- package/src/array/isNotEmpty.mjs +0 -5
- package/src/array/join.mjs +0 -29
- package/src/array/mapKey.mjs +0 -31
- package/src/array/mapPrimitive.mjs +0 -30
- package/src/array/shuffle.mjs +0 -14
- package/src/array/sort.mjs +0 -101
- package/src/boolean/is.mjs +0 -5
- package/src/caching/memoize.mjs +0 -22
- package/src/date/diff.mjs +0 -46
- package/src/date/endOfUTC.mjs +0 -130
- package/src/date/is.mjs +0 -5
- package/src/date/nowUnix.mjs +0 -5
- package/src/date/nowUnixMs.mjs +0 -5
- package/src/date/startOfUTC.mjs +0 -129
- package/src/date/toUTC.mjs +0 -19
- package/src/date/toUnix.mjs +0 -11
- package/src/deep/define.mjs +0 -7
- package/src/deep/freeze.mjs +0 -20
- package/src/deep/get.mjs +0 -52
- package/src/deep/seal.mjs +0 -20
- package/src/deep/set.mjs +0 -46
- package/src/equal.mjs +0 -65
- package/src/function/is.mjs +0 -6
- package/src/function/noop.mjs +0 -5
- package/src/function/noopresolve.mjs +0 -6
- package/src/function/noopreturn.mjs +0 -6
- package/src/function/sleep.mjs +0 -12
- package/src/hash/fnv1A.mjs +0 -53
- package/src/is.mjs +0 -86
- package/src/number/is.mjs +0 -5
- package/src/number/isAbove.mjs +0 -6
- package/src/number/isAboveOrEqual.mjs +0 -6
- package/src/number/isBelow.mjs +0 -6
- package/src/number/isBelowOrEqual.mjs +0 -6
- package/src/number/isBetween.mjs +0 -13
- package/src/number/isInteger.mjs +0 -5
- package/src/number/isIntegerAbove.mjs +0 -6
- package/src/number/isIntegerAboveOrEqual.mjs +0 -6
- package/src/number/isIntegerBelow.mjs +0 -6
- package/src/number/isIntegerBelowOrEqual.mjs +0 -6
- package/src/number/isIntegerBetween.mjs +0 -13
- package/src/number/isNumericalNaN.mjs +0 -5
- package/src/number/randomBetween.mjs +0 -11
- package/src/number/randomIntBetween.mjs +0 -11
- package/src/number/round.mjs +0 -10
- package/src/number/toPercentage.mjs +0 -14
- package/src/object/define.mjs +0 -10
- package/src/object/is.mjs +0 -7
- package/src/object/isNotEmpty.mjs +0 -5
- package/src/object/merge.mjs +0 -25
- package/src/object/pick.mjs +0 -35
- package/src/regexp/is.mjs +0 -5
- package/src/regexp/sanitize.mjs +0 -10
- package/src/string/humanizeNumber.mjs +0 -82
- package/src/string/is.mjs +0 -5
- package/src/string/isBetween.mjs +0 -16
- package/src/string/isNotEmpty.mjs +0 -6
- package/src/string/shorten.mjs +0 -20
- package/string/humanizeBytes.js +0 -21
- package/string/humanizeNumber.js +0 -54
- package/string/is.js +0 -9
- package/string/isBetween.js +0 -11
- package/string/isNotEmpty.js +0 -11
- package/string/shorten.js +0 -11
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@valkyriestudios/utils)
|
|
6
6
|
[](https://www.npmjs.com/package/@valkyriestudios/utils)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Zero-dependency collection of single-function utilities for common tasks
|
|
9
9
|
|
|
10
10
|
`npm install @valkyriestudios/utils`
|
|
11
11
|
|
|
@@ -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;
|
package/dist/array/is.js
ADDED
|
@@ -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
|
-
|
|
4
|
-
if (
|
|
5
|
-
|
|
6
|
-
|
|
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]')
|
|
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))
|
|
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
|
-
}
|
|
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,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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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;
|