@valkyriestudios/utils 10.0.0 → 11.0.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 +1 -0
- package/dist/array/dedupe.js +19 -0
- package/dist/array/is.d.ts +1 -0
- package/dist/array/is.js +6 -0
- package/dist/array/isNotEmpty.d.ts +1 -0
- package/dist/array/isNotEmpty.js +6 -0
- package/dist/array/join.d.ts +8 -0
- package/dist/array/join.js +24 -0
- package/dist/array/mapFn.d.ts +14 -0
- package/{src/array/mapFn.mjs → dist/array/mapFn.js} +13 -15
- package/dist/array/mapKey.d.ts +12 -0
- package/dist/array/mapKey.js +27 -0
- package/dist/array/mapPrimitive.d.ts +10 -0
- package/dist/array/mapPrimitive.js +27 -0
- package/dist/array/shuffle.d.ts +1 -0
- package/dist/array/shuffle.js +11 -0
- package/dist/array/sort.d.ts +11 -0
- package/dist/array/sort.js +92 -0
- package/dist/boolean/is.d.ts +1 -0
- package/dist/boolean/is.js +6 -0
- package/dist/caching/memoize.d.ts +1 -0
- package/dist/caching/memoize.js +15 -0
- package/dist/date/addUTC.d.ts +1 -0
- package/{src/date/addUTC.mjs → dist/date/addUTC.js} +11 -26
- package/dist/date/diff.d.ts +1 -0
- package/dist/date/diff.js +38 -0
- package/dist/date/endOfUTC.d.ts +1 -0
- package/dist/date/endOfUTC.js +52 -0
- package/dist/date/is.d.ts +1 -0
- package/dist/date/is.js +6 -0
- package/dist/date/nowUnix.d.ts +1 -0
- package/dist/date/nowUnix.js +6 -0
- package/dist/date/nowUnixMs.d.ts +1 -0
- package/dist/date/nowUnixMs.js +6 -0
- package/dist/date/startOfUTC.d.ts +1 -0
- package/dist/date/startOfUTC.js +52 -0
- package/dist/date/toUTC.d.ts +1 -0
- package/dist/date/toUTC.js +9 -0
- package/dist/date/toUnix.d.ts +1 -0
- package/dist/date/toUnix.js +9 -0
- package/dist/deep/freeze.d.ts +7 -0
- package/dist/deep/freeze.js +23 -0
- package/dist/deep/get.d.ts +5 -0
- package/dist/deep/get.js +41 -0
- package/dist/deep/seal.d.ts +7 -0
- package/dist/deep/seal.js +23 -0
- package/dist/deep/set.d.ts +5 -0
- package/dist/deep/set.js +52 -0
- package/dist/equal.d.ts +2 -0
- package/dist/equal.js +43 -0
- package/dist/function/is.d.ts +1 -0
- package/dist/function/is.js +6 -0
- package/dist/function/noop.d.ts +1 -0
- package/dist/function/noop.js +5 -0
- package/dist/function/noopresolve.d.ts +1 -0
- package/dist/function/noopresolve.js +6 -0
- package/dist/function/noopreturn.d.ts +1 -0
- package/dist/function/noopreturn.js +6 -0
- package/dist/function/sleep.d.ts +1 -0
- package/dist/function/sleep.js +8 -0
- package/dist/hash/fnv1A.d.ts +1 -0
- package/dist/hash/fnv1A.js +51 -0
- package/dist/hash/guid.d.ts +1 -0
- package/{src/hash/guid.mjs → dist/hash/guid.js} +9 -14
- package/dist/is.d.ts +75 -0
- package/dist/is.js +77 -0
- package/dist/number/is.d.ts +1 -0
- package/dist/number/is.js +6 -0
- package/dist/number/isAbove.d.ts +1 -0
- package/dist/number/isAbove.js +6 -0
- package/dist/number/isAboveOrEqual.d.ts +1 -0
- package/dist/number/isAboveOrEqual.js +6 -0
- package/dist/number/isBelow.d.ts +1 -0
- package/dist/number/isBelow.js +6 -0
- package/dist/number/isBelowOrEqual.d.ts +1 -0
- package/dist/number/isBelowOrEqual.js +6 -0
- package/dist/number/isBetween.d.ts +1 -0
- package/dist/number/isBetween.js +11 -0
- package/dist/number/isInteger.d.ts +1 -0
- package/dist/number/isInteger.js +6 -0
- package/dist/number/isIntegerAbove.d.ts +1 -0
- package/dist/number/isIntegerAbove.js +6 -0
- package/dist/number/isIntegerAboveOrEqual.d.ts +1 -0
- package/dist/number/isIntegerAboveOrEqual.js +6 -0
- package/dist/number/isIntegerBelow.d.ts +1 -0
- package/dist/number/isIntegerBelow.js +6 -0
- package/dist/number/isIntegerBelowOrEqual.d.ts +1 -0
- package/dist/number/isIntegerBelowOrEqual.js +6 -0
- package/dist/number/isIntegerBetween.d.ts +1 -0
- package/dist/number/isIntegerBetween.js +11 -0
- package/dist/number/isNumericalNaN.d.ts +1 -0
- package/dist/number/isNumericalNaN.js +6 -0
- package/dist/number/randomBetween.d.ts +1 -0
- package/dist/number/randomBetween.js +9 -0
- package/dist/number/randomIntBetween.d.ts +1 -0
- package/dist/number/randomIntBetween.js +9 -0
- package/dist/number/round.d.ts +1 -0
- package/dist/number/round.js +10 -0
- package/dist/number/toPercentage.d.ts +1 -0
- package/dist/number/toPercentage.js +11 -0
- package/dist/object/define.d.ts +7 -0
- package/dist/object/define.js +9 -0
- package/dist/object/is.d.ts +1 -0
- package/dist/object/is.js +7 -0
- package/dist/object/isNotEmpty.d.ts +1 -0
- package/dist/object/isNotEmpty.js +6 -0
- package/dist/object/merge.d.ts +8 -0
- package/dist/object/merge.js +20 -0
- package/dist/object/pick.d.ts +5 -0
- package/dist/object/pick.js +34 -0
- package/dist/regexp/is.d.ts +1 -0
- package/dist/regexp/is.js +6 -0
- package/dist/regexp/sanitize.d.ts +1 -0
- package/dist/regexp/sanitize.js +8 -0
- package/dist/string/humanizeBytes.d.ts +8 -0
- package/{src/string/humanizeBytes.mjs → dist/string/humanizeBytes.js} +7 -10
- package/dist/string/humanizeNumber.d.ts +10 -0
- package/dist/string/humanizeNumber.js +60 -0
- package/dist/string/is.d.ts +1 -0
- package/dist/string/is.js +6 -0
- package/dist/string/isBetween.d.ts +1 -0
- package/dist/string/isBetween.js +14 -0
- package/dist/string/isNotEmpty.d.ts +1 -0
- package/dist/string/isNotEmpty.js +8 -0
- package/dist/string/shorten.d.ts +1 -0
- package/dist/string/shorten.js +11 -0
- package/package.json +303 -46
- 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 @@
|
|
|
1
|
+
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 @@
|
|
|
1
|
+
export default function isArray(val: any): boolean;
|
package/dist/array/is.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isNotEmptyArray(val: any): boolean;
|
|
@@ -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,14 @@
|
|
|
1
|
+
interface mapOptions {
|
|
2
|
+
merge?: boolean;
|
|
3
|
+
}
|
|
4
|
+
interface kvMap {
|
|
5
|
+
[key: string]: {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export default function mapFn(arr: {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}[], fn: (entry: {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}) => (string | number | boolean), opts?: mapOptions): kvMap;
|
|
14
|
+
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,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,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 @@
|
|
|
1
|
+
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,11 @@
|
|
|
1
|
+
interface sortOptions {
|
|
2
|
+
filter_fn?: (el: any) => boolean;
|
|
3
|
+
nokey_hide?: boolean;
|
|
4
|
+
nokey_atend?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface sortObject {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
type sortByFunction = (el: sortObject) => string;
|
|
10
|
+
export default function sort(arr: sortObject[], by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions): sortObject[];
|
|
11
|
+
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 @@
|
|
|
1
|
+
export default function isBoolean(val: any): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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 @@
|
|
|
1
|
+
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 @@
|
|
|
1
|
+
export default function diff(val_a: Date, val_b: Date, key?: 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds'): number;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const is_1 = require("./is");
|
|
4
|
+
const SECOND_IN_MILLISECONDS = 1000;
|
|
5
|
+
const MINUTE_IN_MILLISECONDS = SECOND_IN_MILLISECONDS * 60;
|
|
6
|
+
const HOUR_IN_MILLISECONDS = MINUTE_IN_MILLISECONDS * 60;
|
|
7
|
+
const DAY_IN_MILLISECONDS = HOUR_IN_MILLISECONDS * 24;
|
|
8
|
+
const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
|
|
9
|
+
function diff(val_a, val_b, key = 'millisecond') {
|
|
10
|
+
if (!(0, is_1.default)(val_a) ||
|
|
11
|
+
!(0, is_1.default)(val_b))
|
|
12
|
+
throw new TypeError('Diff requires date objects for both values');
|
|
13
|
+
if (typeof key !== 'string')
|
|
14
|
+
throw new TypeError('Key needs to be a string');
|
|
15
|
+
const diff_in_ms = val_a.valueOf() - val_b.valueOf();
|
|
16
|
+
switch (key) {
|
|
17
|
+
case 'week':
|
|
18
|
+
case 'weeks':
|
|
19
|
+
return diff_in_ms / WEEK_IN_MILLISECONDS;
|
|
20
|
+
case 'day':
|
|
21
|
+
case 'days':
|
|
22
|
+
return diff_in_ms / DAY_IN_MILLISECONDS;
|
|
23
|
+
case 'hour':
|
|
24
|
+
case 'hours':
|
|
25
|
+
return diff_in_ms / HOUR_IN_MILLISECONDS;
|
|
26
|
+
case 'minute':
|
|
27
|
+
case 'minutes':
|
|
28
|
+
return diff_in_ms / MINUTE_IN_MILLISECONDS;
|
|
29
|
+
case 'second':
|
|
30
|
+
case 'seconds':
|
|
31
|
+
return diff_in_ms / SECOND_IN_MILLISECONDS;
|
|
32
|
+
case 'millisecond':
|
|
33
|
+
case 'milliseconds':
|
|
34
|
+
default:
|
|
35
|
+
return diff_in_ms;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.default = diff;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function endOfUTC(val: Date, key?: 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | 'week_mon' | 'week_tue' | 'week_wed' | 'week_thu' | 'week_fri' | 'week_sat' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'): Date;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const is_1 = require("./is");
|
|
4
|
+
const WEEK_END = {
|
|
5
|
+
week: 0,
|
|
6
|
+
week_sun: 6,
|
|
7
|
+
week_mon: 0,
|
|
8
|
+
week_tue: 1,
|
|
9
|
+
week_wed: 2,
|
|
10
|
+
week_thu: 3,
|
|
11
|
+
week_fri: 4,
|
|
12
|
+
week_sat: 5,
|
|
13
|
+
};
|
|
14
|
+
function endOfUTC(val, key = 'millisecond') {
|
|
15
|
+
if (!(0, is_1.default)(val))
|
|
16
|
+
throw new TypeError('endOfUTC requires a date object');
|
|
17
|
+
if (typeof key !== 'string')
|
|
18
|
+
throw new TypeError('Key needs to be a string with content');
|
|
19
|
+
switch (key) {
|
|
20
|
+
case 'year':
|
|
21
|
+
return new Date(Date.UTC(val.getUTCFullYear(), 11, 31, 23, 59, 59, 999));
|
|
22
|
+
case 'quarter': {
|
|
23
|
+
return new Date(Date.UTC(val.getUTCFullYear(), (val.getUTCMonth() - (val.getUTCMonth() % 3)) + 3, 0, 23, 59, 59, 999));
|
|
24
|
+
}
|
|
25
|
+
case 'month':
|
|
26
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth() + 1, 0, 23, 59, 59, 999));
|
|
27
|
+
case 'week':
|
|
28
|
+
case 'week_sun':
|
|
29
|
+
case 'week_mon':
|
|
30
|
+
case 'week_tue':
|
|
31
|
+
case 'week_wed':
|
|
32
|
+
case 'week_thu':
|
|
33
|
+
case 'week_fri':
|
|
34
|
+
case 'week_sat': {
|
|
35
|
+
const UTC_DAY = val.getUTCDay();
|
|
36
|
+
const UTC_EOD = WEEK_END[key];
|
|
37
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate() + (UTC_DAY <= UTC_EOD ? UTC_EOD - UTC_DAY : (7 - UTC_DAY) + UTC_EOD), 23, 59, 59, 999));
|
|
38
|
+
}
|
|
39
|
+
case 'day':
|
|
40
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), 23, 59, 59, 999));
|
|
41
|
+
case 'hour':
|
|
42
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), 59, 59, 999));
|
|
43
|
+
case 'minute':
|
|
44
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), 59, 999));
|
|
45
|
+
case 'second':
|
|
46
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), 999));
|
|
47
|
+
case 'millisecond':
|
|
48
|
+
default:
|
|
49
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.default = endOfUTC;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function isDate(val: any): boolean;
|
package/dist/date/is.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function nowUnix(): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function nowUnixMs(): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function startOfUTC(val: Date, key?: 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | 'week_mon' | 'week_tue' | 'week_wed' | 'week_thu' | 'week_fri' | 'week_sat' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'): Date;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const is_1 = require("./is");
|
|
4
|
+
const WEEK_START = {
|
|
5
|
+
week: 1,
|
|
6
|
+
week_sun: 0,
|
|
7
|
+
week_mon: 1,
|
|
8
|
+
week_tue: 2,
|
|
9
|
+
week_wed: 3,
|
|
10
|
+
week_thu: 4,
|
|
11
|
+
week_fri: 5,
|
|
12
|
+
week_sat: 6,
|
|
13
|
+
};
|
|
14
|
+
function startOfUTC(val, key = 'millisecond') {
|
|
15
|
+
if (!(0, is_1.default)(val))
|
|
16
|
+
throw new TypeError('startOfUTC requires a date object');
|
|
17
|
+
if (typeof key !== 'string')
|
|
18
|
+
throw new TypeError('Key needs to be a string with content');
|
|
19
|
+
switch (key) {
|
|
20
|
+
case 'year':
|
|
21
|
+
return new Date(Date.UTC(val.getUTCFullYear(), 0, 1, 0, 0, 0, 0));
|
|
22
|
+
case 'quarter': {
|
|
23
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth() - (val.getUTCMonth() % 3), 1, 0, 0, 0, 0));
|
|
24
|
+
}
|
|
25
|
+
case 'month':
|
|
26
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), 1, 0, 0, 0, 0));
|
|
27
|
+
case 'week':
|
|
28
|
+
case 'week_sun':
|
|
29
|
+
case 'week_mon':
|
|
30
|
+
case 'week_tue':
|
|
31
|
+
case 'week_wed':
|
|
32
|
+
case 'week_thu':
|
|
33
|
+
case 'week_fri':
|
|
34
|
+
case 'week_sat': {
|
|
35
|
+
const UTC_DAY = val.getUTCDay();
|
|
36
|
+
const UTC_SOD = WEEK_START[key];
|
|
37
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate() - (UTC_DAY < UTC_SOD ? (7 - UTC_SOD) + UTC_DAY : UTC_DAY - UTC_SOD), 0, 0, 0, 0));
|
|
38
|
+
}
|
|
39
|
+
case 'day':
|
|
40
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), 0, 0, 0, 0));
|
|
41
|
+
case 'hour':
|
|
42
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), 0, 0, 0));
|
|
43
|
+
case 'minute':
|
|
44
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), 0, 0));
|
|
45
|
+
case 'second':
|
|
46
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), 0));
|
|
47
|
+
case 'millisecond':
|
|
48
|
+
default:
|
|
49
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.default = startOfUTC;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function toUTC(val: Date): Date;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const is_1 = require("./is");
|
|
4
|
+
function toUTC(val) {
|
|
5
|
+
if (!(0, is_1.default)(val))
|
|
6
|
+
throw new TypeError('toUTC requires a date object');
|
|
7
|
+
return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
|
|
8
|
+
}
|
|
9
|
+
exports.default = toUTC;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function toUnix(val: Date): number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const is_1 = require("./is");
|
|
4
|
+
function toUnix(val) {
|
|
5
|
+
if (!(0, is_1.default)(val))
|
|
6
|
+
throw new TypeError('toUnix requires a date object');
|
|
7
|
+
return Math.floor(val.valueOf() / 1000);
|
|
8
|
+
}
|
|
9
|
+
exports.default = toUnix;
|