@valkyriestudios/utils 9.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 +2 -2
- 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/dist/array/mapFn.js +28 -0
- 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/dist/string/humanizeBytes.js +24 -0
- 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/CHANGELOG.md +0 -474
- package/array/dedupe.js +0 -33
- package/array/is.js +0 -9
- package/array/isNotEmpty.js +0 -9
- package/array/join.js +0 -41
- package/array/mapFn.js +0 -42
- package/array/mapKey.js +0 -38
- package/array/mapPrimitive.js +0 -43
- package/array/shuffle.js +0 -14
- package/array/sort.js +0 -125
- package/boolean/is.js +0 -9
- package/caching/memoize.js +0 -21
- 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 -10
- package/date/toUnix.js +0 -10
- package/deep/define.js +0 -12
- package/deep/freeze.js +0 -18
- package/deep/get.js +0 -31
- package/deep/seal.js +0 -18
- package/deep/set.js +0 -28
- package/equal.js +0 -37
- 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 -49
- 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 -12
- package/object/is.js +0 -11
- package/object/isNotEmpty.js +0 -10
- package/object/merge.js +0 -20
- package/object/pick.js +0 -41
- package/regexp/is.js +0 -11
- package/regexp/sanitize.js +0 -12
- 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 -31
- package/src/array/mapFn.mjs +0 -34
- package/src/array/mapKey.mjs +0 -31
- package/src/array/mapPrimitive.mjs +0 -29
- package/src/array/shuffle.mjs +0 -14
- package/src/array/sort.mjs +0 -102
- package/src/boolean/is.mjs +0 -5
- package/src/caching/memoize.mjs +0 -24
- 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 -17
- package/src/date/toUnix.mjs +0 -9
- package/src/deep/define.mjs +0 -7
- package/src/deep/freeze.mjs +0 -22
- package/src/deep/get.mjs +0 -51
- package/src/deep/seal.mjs +0 -22
- package/src/deep/set.mjs +0 -45
- package/src/equal.mjs +0 -67
- 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 -58
- 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 -12
- package/src/object/is.mjs +0 -7
- package/src/object/isNotEmpty.mjs +0 -7
- package/src/object/merge.mjs +0 -27
- package/src/object/pick.mjs +0 -33
- package/src/regexp/is.mjs +0 -7
- package/src/regexp/sanitize.mjs +0 -12
- package/src/string/humanizeBytes.mjs +0 -29
- package/src/string/humanizeNumber.mjs +0 -84
- package/src/string/is.mjs +0 -5
- package/src/string/isBetween.mjs +0 -18
- package/src/string/isNotEmpty.mjs +0 -6
- package/src/string/shorten.mjs +0 -21
- package/string/humanizeBytes.js +0 -23
- package/string/humanizeNumber.js +0 -56
- package/string/is.js +0 -9
- package/string/isBetween.js +0 -14
- package/string/isNotEmpty.js +0 -11
- package/string/shorten.js +0 -15
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Check if a number is an integer between a range
|
|
4
|
-
export default function isIntegerBetween (val, min, max) {
|
|
5
|
-
if (
|
|
6
|
-
!Number.isInteger(val) ||
|
|
7
|
-
!Number.isFinite(min) ||
|
|
8
|
-
!Number.isFinite(max) ||
|
|
9
|
-
min >= max
|
|
10
|
-
) return false;
|
|
11
|
-
|
|
12
|
-
return val >= min && val <= max;
|
|
13
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Generate random between min and max
|
|
4
|
-
export default function randomBetween (min = 0, max = 10) {
|
|
5
|
-
if (
|
|
6
|
-
!Number.isFinite(min) ||
|
|
7
|
-
!Number.isFinite(max)
|
|
8
|
-
) throw new TypeError('Min/Max should be numeric');
|
|
9
|
-
|
|
10
|
-
return (Math.random() * (max - min)) + min;
|
|
11
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Generate random between min and max (max not inclusive)
|
|
4
|
-
export default function randomIntBetween (min = 0, max = 10) {
|
|
5
|
-
if (
|
|
6
|
-
!Number.isFinite(min) ||
|
|
7
|
-
!Number.isFinite(max)
|
|
8
|
-
) throw new TypeError('Min/Max should be numeric');
|
|
9
|
-
|
|
10
|
-
return Math.floor((Math.random() * (max - min)) + min);
|
|
11
|
-
}
|
package/src/number/round.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Round a numeric value to a certain precision
|
|
4
|
-
export default function round (val, precision = 0) {
|
|
5
|
-
if (!Number.isFinite(val)) throw new TypeError('Value should be numeric');
|
|
6
|
-
|
|
7
|
-
const exp = Math.pow(10, Number.isInteger(precision) && precision > 0 ? precision : 0);
|
|
8
|
-
const num = (val * exp) * (1 + Number.EPSILON);
|
|
9
|
-
return Math.round(num)/exp;
|
|
10
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import round from './round.mjs';
|
|
4
|
-
|
|
5
|
-
// Convert a float value to a percentage
|
|
6
|
-
export default function toPercentage (val, precision = 0, min = 0, max = 1) {
|
|
7
|
-
if (
|
|
8
|
-
!Number.isFinite(val) ||
|
|
9
|
-
!Number.isFinite(min) ||
|
|
10
|
-
!Number.isFinite(max)
|
|
11
|
-
) throw new TypeError('value/min/max should be numeric');
|
|
12
|
-
|
|
13
|
-
return round(((val - min)/ (max - min)) * 100, precision);
|
|
14
|
-
}
|
package/src/object/define.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
4
|
-
|
|
5
|
-
export default function define (props, obj = {}) {
|
|
6
|
-
if (
|
|
7
|
-
Object.prototype.toString.call(props) !== PROTO_OBJ ||
|
|
8
|
-
Object.prototype.toString.call(obj) !== PROTO_OBJ
|
|
9
|
-
) throw new TypeError('Please pass an object as the value for props and obj');
|
|
10
|
-
|
|
11
|
-
return Object.defineProperties(obj, props);
|
|
12
|
-
}
|
package/src/object/is.mjs
DELETED
package/src/object/merge.mjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
4
|
-
|
|
5
|
-
const merge = (target, source = {}) => {
|
|
6
|
-
if (
|
|
7
|
-
Object.prototype.toString.call(target) !== PROTO_OBJ ||
|
|
8
|
-
Object.prototype.toString.call(source) !== PROTO_OBJ
|
|
9
|
-
) throw new TypeError('Please pass a target and object to merge');
|
|
10
|
-
|
|
11
|
-
return Object.keys(target).reduce((acc, key) => {
|
|
12
|
-
if (
|
|
13
|
-
Object.prototype.toString.call(target[key]) === PROTO_OBJ &&
|
|
14
|
-
!Array.isArray(target[key])
|
|
15
|
-
) {
|
|
16
|
-
acc[key] = merge(target[key], source[key] || {});
|
|
17
|
-
} else {
|
|
18
|
-
acc[key] = Object.prototype.hasOwnProperty.call(source, key)
|
|
19
|
-
? source[key]
|
|
20
|
-
: target[key];
|
|
21
|
-
}
|
|
22
|
-
return acc;
|
|
23
|
-
}, {});
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export default merge;
|
package/src/object/pick.mjs
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import deepGet from '../deep/get.mjs';
|
|
4
|
-
import deepSet from '../deep/set.mjs';
|
|
5
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
6
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
7
|
-
|
|
8
|
-
export default function pick (obj, keys) {
|
|
9
|
-
if (
|
|
10
|
-
Object.prototype.toString.call(obj) !== PROTO_OBJ ||
|
|
11
|
-
!Array.isArray(keys) ||
|
|
12
|
-
keys.length === 0
|
|
13
|
-
) throw new TypeError('Please pass an object to pick from and a keys array');
|
|
14
|
-
|
|
15
|
-
const map = {};
|
|
16
|
-
let key_deep = false;
|
|
17
|
-
let val;
|
|
18
|
-
for (const key of keys) {
|
|
19
|
-
if (!isNotEmptyString(key)) continue;
|
|
20
|
-
key_deep = key.match(/(\.|\[)/g);
|
|
21
|
-
val = key_deep
|
|
22
|
-
? deepGet(obj, key.trim())
|
|
23
|
-
: obj[key.trim()];
|
|
24
|
-
if (val === undefined) continue;
|
|
25
|
-
|
|
26
|
-
if (key_deep) {
|
|
27
|
-
deepSet(map, key.trim(), val);
|
|
28
|
-
} else {
|
|
29
|
-
map[key.trim()] = val;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return map;
|
|
33
|
-
}
|
package/src/regexp/is.mjs
DELETED
package/src/regexp/sanitize.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
4
|
-
|
|
5
|
-
// Escapes a value to be used inside of a regular expression (eg: new RegExp(...))
|
|
6
|
-
// For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
|
|
7
|
-
//
|
|
8
|
-
// @param string val Value to escape
|
|
9
|
-
export default function sanitizeRegExp (val) {
|
|
10
|
-
if (!isNotEmptyString(val)) return false;
|
|
11
|
-
return val.trim().replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
|
12
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import isString from '../string/is.mjs';
|
|
4
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
5
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
6
|
-
import humanizeNumber from './humanizeNumber.mjs';
|
|
7
|
-
|
|
8
|
-
// Humanize a numerical byte value into a readable file size
|
|
9
|
-
//
|
|
10
|
-
// @param int val Amount of bytes
|
|
11
|
-
export default function humanizeBytes (val, options = {}) {
|
|
12
|
-
const has_opts = Object.prototype.toString.call(options) === PROTO_OBJ;
|
|
13
|
-
return humanizeNumber(val, {
|
|
14
|
-
delim: has_opts && isString(options.delim)
|
|
15
|
-
? options.delim
|
|
16
|
-
: ',',
|
|
17
|
-
separator: has_opts && isNotEmptyString(options.separator)
|
|
18
|
-
? options.separator
|
|
19
|
-
: '.',
|
|
20
|
-
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0
|
|
21
|
-
? options.precision
|
|
22
|
-
: 2,
|
|
23
|
-
units: has_opts && Array.isArray(options.units) && options.units.length > 0
|
|
24
|
-
? options.units.filter(isNotEmptyString)
|
|
25
|
-
: [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'],
|
|
26
|
-
divider: 1024,
|
|
27
|
-
real: true,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import isBoolean from '../boolean/is.mjs';
|
|
4
|
-
import isString from '../string/is.mjs';
|
|
5
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
6
|
-
import round from '../number/round.mjs';
|
|
7
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
8
|
-
|
|
9
|
-
// Humanize a numerical value into a unit base
|
|
10
|
-
//
|
|
11
|
-
// @param int val Amount of bytes
|
|
12
|
-
export default function humanizeNumber (val, options = {}) {
|
|
13
|
-
const has_opts = Object.prototype.toString.call(options) === PROTO_OBJ;
|
|
14
|
-
const OPTS = {
|
|
15
|
-
delim: has_opts && isString(options.delim)
|
|
16
|
-
? options.delim
|
|
17
|
-
: ',',
|
|
18
|
-
separator: has_opts && isNotEmptyString(options.separator)
|
|
19
|
-
? options.separator
|
|
20
|
-
: '.',
|
|
21
|
-
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0
|
|
22
|
-
? options.precision
|
|
23
|
-
: 2,
|
|
24
|
-
units: has_opts && ((Array.isArray(options.units) && options.units.length > 0) || options.units === false)
|
|
25
|
-
? options.units ? options.units.filter(isString) : false
|
|
26
|
-
: ['', 'k', 'm', 'b', 't', 'q'],
|
|
27
|
-
// Have to have at least bigger than 1 to not end in infinite loop
|
|
28
|
-
divider: has_opts && Number.isInteger(options.divider) && options.divider > 1
|
|
29
|
-
? options.divider
|
|
30
|
-
: 1000,
|
|
31
|
-
// Should we auto parse as integer (true) or not (false)
|
|
32
|
-
real: has_opts && isBoolean(options.real)
|
|
33
|
-
? options.real
|
|
34
|
-
: false,
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// If not a valid value, return
|
|
38
|
-
if (!(Number.isFinite(val) || isNotEmptyString(val))) {
|
|
39
|
-
return `0${OPTS.units.length > 0 ? OPTS.units[0] : ''}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Ensure we are working with an integer
|
|
43
|
-
let normalized;
|
|
44
|
-
if (OPTS.real) {
|
|
45
|
-
normalized = parseInt(isString(val) ? val.trim() : val) || 0;
|
|
46
|
-
} else {
|
|
47
|
-
normalized = parseFloat(isString(val) ? val.trim() : val) || 0;
|
|
48
|
-
}
|
|
49
|
-
if (!Number.isFinite(normalized) || normalized === 0) {
|
|
50
|
-
return `0${OPTS.units.length > 0 ? OPTS.units[0] : ''}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Determine sign
|
|
54
|
-
const sign = normalized < 0 ? '-' : '';
|
|
55
|
-
|
|
56
|
-
// Ensure val here is absolute
|
|
57
|
-
normalized = Math.abs(normalized);
|
|
58
|
-
|
|
59
|
-
// At each step, divide by divider, based on that we get the unit size
|
|
60
|
-
let postfix = '';
|
|
61
|
-
if (OPTS.units) {
|
|
62
|
-
let unit_ix = 0;
|
|
63
|
-
while (normalized >= OPTS.divider) {
|
|
64
|
-
unit_ix++;
|
|
65
|
-
normalized = normalized/OPTS.divider;
|
|
66
|
-
if (unit_ix === OPTS.units.length - 1) break;
|
|
67
|
-
}
|
|
68
|
-
postfix = OPTS.units[unit_ix];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Round with precision
|
|
72
|
-
normalized = round(normalized, OPTS.precision);
|
|
73
|
-
|
|
74
|
-
// Humanize from eg: 10023 to 10,023
|
|
75
|
-
normalized = `${normalized}`.split('.');
|
|
76
|
-
normalized[0] = normalized[0].split('').reverse().map((char, ix, original) => {
|
|
77
|
-
if (ix > 0 && ix < original.length && ix % 3 === 0) return char + OPTS.delim;
|
|
78
|
-
return char;
|
|
79
|
-
}).reverse().join('');
|
|
80
|
-
normalized = normalized.join(OPTS.separator);
|
|
81
|
-
|
|
82
|
-
// Include a decimal point and a tenths-place digit if presenting less than then of KB or greater units
|
|
83
|
-
return `${sign}${normalized}${postfix}`;
|
|
84
|
-
}
|
package/src/string/is.mjs
DELETED
package/src/string/isBetween.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import isString from './is.mjs';
|
|
4
|
-
|
|
5
|
-
// Check if a string is between a range in length
|
|
6
|
-
export default function isStringBetween (val, min, max, trimmed = true) {
|
|
7
|
-
if (
|
|
8
|
-
!isString(val) ||
|
|
9
|
-
!Number.isFinite(min) ||
|
|
10
|
-
min < 0 ||
|
|
11
|
-
!Number.isFinite(max) ||
|
|
12
|
-
max < 0 ||
|
|
13
|
-
min >= max
|
|
14
|
-
) return false;
|
|
15
|
-
|
|
16
|
-
const length = (trimmed === true ? val.trim() : val).length;
|
|
17
|
-
return length >= min && length <= max;
|
|
18
|
-
}
|
package/src/string/shorten.mjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import isString from './is.mjs';
|
|
4
|
-
import isNumberAbove from '../number/isAbove.mjs';
|
|
5
|
-
|
|
6
|
-
// Shorten a string and add a postfix if it goes over a specific length, will autotrim value
|
|
7
|
-
//
|
|
8
|
-
// @param string val Value to shorten, returns false a string is not passed
|
|
9
|
-
// @param int length Length to shorten it at
|
|
10
|
-
// @param string postfix (default='...') Postfix to use when shortened
|
|
11
|
-
export default function shorten (val, length, postfix = '...') {
|
|
12
|
-
if (
|
|
13
|
-
!isString(val) ||
|
|
14
|
-
!isString(postfix) ||
|
|
15
|
-
!isNumberAbove(length, 0)
|
|
16
|
-
) return false;
|
|
17
|
-
|
|
18
|
-
if (val.trim().length <= length) return val.trim();
|
|
19
|
-
|
|
20
|
-
return `${val.trim().substr(0, length)}${postfix}`;
|
|
21
|
-
}
|
package/string/humanizeBytes.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: !0
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = humanizeBytes;
|
|
7
|
-
var _is = _interopRequireDefault(require("../string/is.js"));
|
|
8
|
-
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
9
|
-
var _is2 = require("../object/is.js");
|
|
10
|
-
var _humanizeNumber = _interopRequireDefault(require("./humanizeNumber.js"));
|
|
11
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
|
-
function humanizeBytes(val) {
|
|
13
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
|
-
var has_opts = Object.prototype.toString.call(options) === _is2.PROTO_OBJ;
|
|
15
|
-
return (0, _humanizeNumber["default"])(val, {
|
|
16
|
-
delim: has_opts && (0, _is["default"])(options.delim) ? options.delim : ',',
|
|
17
|
-
separator: has_opts && (0, _isNotEmpty["default"])(options.separator) ? options.separator : '.',
|
|
18
|
-
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0 ? options.precision : 2,
|
|
19
|
-
units: has_opts && Array.isArray(options.units) && options.units.length > 0 ? options.units.filter(_isNotEmpty["default"]) : [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'],
|
|
20
|
-
divider: 1024,
|
|
21
|
-
real: !0
|
|
22
|
-
});
|
|
23
|
-
}
|
package/string/humanizeNumber.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: !0
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = humanizeNumber;
|
|
7
|
-
var _is = _interopRequireDefault(require("../boolean/is.js"));
|
|
8
|
-
var _is2 = _interopRequireDefault(require("../string/is.js"));
|
|
9
|
-
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
10
|
-
var _round = _interopRequireDefault(require("../number/round.js"));
|
|
11
|
-
var _is3 = require("../object/is.js");
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
-
function humanizeNumber(val) {
|
|
14
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
15
|
-
var has_opts = Object.prototype.toString.call(options) === _is3.PROTO_OBJ;
|
|
16
|
-
var OPTS = {
|
|
17
|
-
delim: has_opts && (0, _is2["default"])(options.delim) ? options.delim : ',',
|
|
18
|
-
separator: has_opts && (0, _isNotEmpty["default"])(options.separator) ? options.separator : '.',
|
|
19
|
-
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0 ? options.precision : 2,
|
|
20
|
-
units: has_opts && (Array.isArray(options.units) && options.units.length > 0 || options.units === !1) ? options.units ? options.units.filter(_is2["default"]) : !1 : ['', 'k', 'm', 'b', 't', 'q'],
|
|
21
|
-
divider: has_opts && Number.isInteger(options.divider) && options.divider > 1 ? options.divider : 1000,
|
|
22
|
-
real: has_opts && (0, _is["default"])(options.real) ? options.real : !1
|
|
23
|
-
};
|
|
24
|
-
if (!(Number.isFinite(val) || (0, _isNotEmpty["default"])(val))) {
|
|
25
|
-
return "0".concat(OPTS.units.length > 0 ? OPTS.units[0] : '');
|
|
26
|
-
}
|
|
27
|
-
var normalized;
|
|
28
|
-
if (OPTS.real) {
|
|
29
|
-
normalized = parseInt((0, _is2["default"])(val) ? val.trim() : val) || 0;
|
|
30
|
-
} else {
|
|
31
|
-
normalized = parseFloat((0, _is2["default"])(val) ? val.trim() : val) || 0;
|
|
32
|
-
}
|
|
33
|
-
if (!Number.isFinite(normalized) || normalized === 0) {
|
|
34
|
-
return "0".concat(OPTS.units.length > 0 ? OPTS.units[0] : '');
|
|
35
|
-
}
|
|
36
|
-
var sign = normalized < 0 ? '-' : '';
|
|
37
|
-
normalized = Math.abs(normalized);
|
|
38
|
-
var postfix = '';
|
|
39
|
-
if (OPTS.units) {
|
|
40
|
-
var unit_ix = 0;
|
|
41
|
-
while (normalized >= OPTS.divider) {
|
|
42
|
-
unit_ix++;
|
|
43
|
-
normalized = normalized / OPTS.divider;
|
|
44
|
-
if (unit_ix === OPTS.units.length - 1) break;
|
|
45
|
-
}
|
|
46
|
-
postfix = OPTS.units[unit_ix];
|
|
47
|
-
}
|
|
48
|
-
normalized = (0, _round["default"])(normalized, OPTS.precision);
|
|
49
|
-
normalized = "".concat(normalized).split('.');
|
|
50
|
-
normalized[0] = normalized[0].split('').reverse().map(function (_char, ix, original) {
|
|
51
|
-
if (ix > 0 && ix < original.length && ix % 3 === 0) return _char + OPTS.delim;
|
|
52
|
-
return _char;
|
|
53
|
-
}).reverse().join('');
|
|
54
|
-
normalized = normalized.join(OPTS.separator);
|
|
55
|
-
return "".concat(sign).concat(normalized).concat(postfix);
|
|
56
|
-
}
|
package/string/is.js
DELETED
package/string/isBetween.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: !0
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = isStringBetween;
|
|
7
|
-
var _is = _interopRequireDefault(require("./is.js"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
9
|
-
function isStringBetween(val, min, max) {
|
|
10
|
-
var trimmed = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : !0;
|
|
11
|
-
if (!(0, _is["default"])(val) || !Number.isFinite(min) || min < 0 || !Number.isFinite(max) || max < 0 || min >= max) return !1;
|
|
12
|
-
var length = (trimmed === !0 ? val.trim() : val).length;
|
|
13
|
-
return length >= min && length <= max;
|
|
14
|
-
}
|
package/string/isNotEmpty.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: !0
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = isNotEmptyString;
|
|
7
|
-
function isNotEmptyString(val) {
|
|
8
|
-
var trimmed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !0;
|
|
9
|
-
if (typeof val !== 'string' && !(val instanceof String)) return !1;
|
|
10
|
-
return (trimmed === !0 ? val.trim() : val).length > 0;
|
|
11
|
-
}
|
package/string/shorten.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: !0
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = shorten;
|
|
7
|
-
var _is = _interopRequireDefault(require("./is.js"));
|
|
8
|
-
var _isAbove = _interopRequireDefault(require("../number/isAbove.js"));
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
-
function shorten(val, length) {
|
|
11
|
-
var postfix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '...';
|
|
12
|
-
if (!(0, _is["default"])(val) || !(0, _is["default"])(postfix) || !(0, _isAbove["default"])(length, 0)) return !1;
|
|
13
|
-
if (val.trim().length <= length) return val.trim();
|
|
14
|
-
return "".concat(val.trim().substr(0, length)).concat(postfix);
|
|
15
|
-
}
|