@valkyriestudios/utils 8.4.0 → 10.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/array/join.js +3 -6
- package/array/mapFn.js +5 -10
- package/array/mapKey.js +9 -11
- package/array/mapPrimitive.js +3 -8
- package/array/sort.js +10 -10
- package/boolean/is.js +1 -1
- package/caching/memoize.js +1 -3
- package/date/addUTC.js +3 -3
- package/date/diff.js +3 -3
- package/date/endOfUTC.js +3 -3
- package/date/is.js +1 -1
- package/date/startOfUTC.js +3 -3
- package/date/toUTC.js +3 -1
- package/date/toUnix.js +3 -1
- package/deep/freeze.js +2 -3
- package/deep/get.js +7 -9
- package/deep/seal.js +2 -3
- package/deep/set.js +6 -8
- package/equal.js +2 -4
- package/function/is.js +1 -1
- package/hash/fnv1A.js +4 -9
- package/object/define.js +1 -2
- package/object/is.js +1 -2
- package/object/isNotEmpty.js +1 -2
- package/object/merge.js +2 -3
- package/object/pick.js +8 -8
- package/package.json +4 -4
- package/regexp/is.js +1 -3
- package/regexp/sanitize.js +1 -4
- package/src/array/join.mjs +5 -7
- package/src/array/mapFn.mjs +5 -9
- package/src/array/mapKey.mjs +11 -11
- package/src/array/mapPrimitive.mjs +7 -6
- package/src/array/sort.mjs +11 -12
- package/src/boolean/is.mjs +1 -1
- package/src/caching/memoize.mjs +1 -3
- package/src/date/addUTC.mjs +3 -3
- package/src/date/diff.mjs +4 -4
- package/src/date/endOfUTC.mjs +3 -3
- package/src/date/is.mjs +1 -1
- package/src/date/startOfUTC.mjs +3 -3
- package/src/date/toUTC.mjs +3 -1
- package/src/date/toUnix.mjs +3 -1
- package/src/deep/freeze.mjs +2 -4
- package/src/deep/get.mjs +9 -8
- package/src/deep/seal.mjs +2 -4
- package/src/deep/set.mjs +8 -7
- package/src/equal.mjs +5 -7
- package/src/function/is.mjs +1 -1
- package/src/hash/fnv1A.mjs +4 -9
- package/src/object/define.mjs +2 -4
- package/src/object/is.mjs +1 -1
- package/src/object/isNotEmpty.mjs +1 -3
- package/src/object/merge.mjs +3 -5
- package/src/object/pick.mjs +13 -11
- package/src/regexp/is.mjs +1 -3
- package/src/regexp/sanitize.mjs +1 -3
- package/src/string/humanizeBytes.mjs +4 -6
- package/src/string/humanizeNumber.mjs +9 -11
- package/src/string/is.mjs +1 -1
- package/src/string/isBetween.mjs +1 -3
- package/src/string/isNotEmpty.mjs +1 -1
- package/src/string/shorten.mjs +7 -8
- package/string/humanizeBytes.js +4 -6
- package/string/humanizeNumber.js +6 -8
- package/string/is.js +1 -1
- package/string/isBetween.js +1 -4
- package/string/isNotEmpty.js +1 -1
- package/string/shorten.js +3 -7
- package/CHANGELOG.md +0 -469
- package/data/continents.json +0 -9
- package/data/countries.json +0 -251
package/src/object/define.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
4
|
-
|
|
5
3
|
export default function define (props, obj = {}) {
|
|
6
4
|
if (
|
|
7
|
-
Object.prototype.toString.call(props) !==
|
|
8
|
-
Object.prototype.toString.call(obj) !==
|
|
5
|
+
Object.prototype.toString.call(props) !== '[object Object]' ||
|
|
6
|
+
Object.prototype.toString.call(obj) !== '[object Object]'
|
|
9
7
|
) throw new TypeError('Please pass an object as the value for props and obj');
|
|
10
8
|
|
|
11
9
|
return Object.defineProperties(obj, props);
|
package/src/object/is.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
4
|
-
|
|
5
3
|
export default function isNotEmptyObject (val) {
|
|
6
|
-
return Object.prototype.toString.call(val) ===
|
|
4
|
+
return Object.prototype.toString.call(val) === '[object Object]' && Object.keys(val).length > 0;
|
|
7
5
|
}
|
package/src/object/merge.mjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
4
|
-
|
|
5
3
|
const merge = (target, source = {}) => {
|
|
6
4
|
if (
|
|
7
|
-
Object.prototype.toString.call(target) !==
|
|
8
|
-
Object.prototype.toString.call(source) !==
|
|
5
|
+
Object.prototype.toString.call(target) !== '[object Object]' ||
|
|
6
|
+
Object.prototype.toString.call(source) !== '[object Object]'
|
|
9
7
|
) throw new TypeError('Please pass a target and object to merge');
|
|
10
8
|
|
|
11
9
|
return Object.keys(target).reduce((acc, key) => {
|
|
12
10
|
if (
|
|
13
|
-
Object.prototype.toString.call(target[key]) ===
|
|
11
|
+
Object.prototype.toString.call(target[key]) === '[object Object]' &&
|
|
14
12
|
!Array.isArray(target[key])
|
|
15
13
|
) {
|
|
16
14
|
acc[key] = merge(target[key], source[key] || {});
|
package/src/object/pick.mjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import deepGet
|
|
4
|
-
import deepSet
|
|
5
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
6
|
-
import {PROTO_OBJ} from './is.mjs';
|
|
3
|
+
import deepGet from '../deep/get.mjs';
|
|
4
|
+
import deepSet from '../deep/set.mjs';
|
|
7
5
|
|
|
8
6
|
export default function pick (obj, keys) {
|
|
9
7
|
if (
|
|
10
|
-
Object.prototype.toString.call(obj) !==
|
|
8
|
+
Object.prototype.toString.call(obj) !== '[object Object]' ||
|
|
11
9
|
!Array.isArray(keys) ||
|
|
12
10
|
keys.length === 0
|
|
13
11
|
) throw new TypeError('Please pass an object to pick from and a keys array');
|
|
@@ -16,17 +14,21 @@ export default function pick (obj, keys) {
|
|
|
16
14
|
let key_deep = false;
|
|
17
15
|
let val;
|
|
18
16
|
for (const key of keys) {
|
|
19
|
-
if (
|
|
20
|
-
|
|
17
|
+
if (typeof key !== 'string') continue;
|
|
18
|
+
|
|
19
|
+
const sanitized = key.trim();
|
|
20
|
+
if (sanitized.length === 0) continue;
|
|
21
|
+
|
|
22
|
+
key_deep = sanitized.match(/(\.|\[)/g);
|
|
21
23
|
val = key_deep
|
|
22
|
-
? deepGet(obj,
|
|
23
|
-
: obj[
|
|
24
|
+
? deepGet(obj, sanitized)
|
|
25
|
+
: obj[sanitized];
|
|
24
26
|
if (val === undefined) continue;
|
|
25
27
|
|
|
26
28
|
if (key_deep) {
|
|
27
|
-
deepSet(map,
|
|
29
|
+
deepSet(map, sanitized, val);
|
|
28
30
|
} else {
|
|
29
|
-
map[
|
|
31
|
+
map[sanitized] = val;
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
return map;
|
package/src/regexp/is.mjs
CHANGED
package/src/regexp/sanitize.mjs
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
4
|
-
|
|
5
3
|
// Escapes a value to be used inside of a regular expression (eg: new RegExp(...))
|
|
6
4
|
// For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
|
|
7
5
|
//
|
|
8
6
|
// @param string val Value to escape
|
|
9
7
|
export default function sanitizeRegExp (val) {
|
|
10
|
-
if (
|
|
8
|
+
if (typeof val !== 'string') return false;
|
|
11
9
|
return val.trim().replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
|
12
10
|
}
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isString from '../string/is.mjs';
|
|
4
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
5
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
6
3
|
import humanizeNumber from './humanizeNumber.mjs';
|
|
4
|
+
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
7
5
|
|
|
8
6
|
// Humanize a numerical byte value into a readable file size
|
|
9
7
|
//
|
|
10
8
|
// @param int val Amount of bytes
|
|
11
9
|
export default function humanizeBytes (val, options = {}) {
|
|
12
|
-
const has_opts = Object.prototype.toString.call(options) ===
|
|
10
|
+
const has_opts = Object.prototype.toString.call(options) === '[object Object]';
|
|
13
11
|
return humanizeNumber(val, {
|
|
14
|
-
delim: has_opts &&
|
|
12
|
+
delim: has_opts && typeof options.delim === 'string'
|
|
15
13
|
? options.delim
|
|
16
14
|
: ',',
|
|
17
|
-
separator: has_opts &&
|
|
15
|
+
separator: has_opts && typeof options.separator === 'string' && options.separator.trim().length > 0
|
|
18
16
|
? options.separator
|
|
19
17
|
: '.',
|
|
20
18
|
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isBoolean
|
|
4
|
-
import isString
|
|
5
|
-
import
|
|
6
|
-
import round from '../number/round.mjs';
|
|
7
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
3
|
+
import isBoolean from '../boolean/is.mjs';
|
|
4
|
+
import isString from '../string/is.mjs';
|
|
5
|
+
import round from '../number/round.mjs';
|
|
8
6
|
|
|
9
7
|
// Humanize a numerical value into a unit base
|
|
10
8
|
//
|
|
11
9
|
// @param int val Amount of bytes
|
|
12
10
|
export default function humanizeNumber (val, options = {}) {
|
|
13
|
-
const has_opts = Object.prototype.toString.call(options) ===
|
|
11
|
+
const has_opts = Object.prototype.toString.call(options) === '[object Object]';
|
|
14
12
|
const OPTS = {
|
|
15
|
-
delim: has_opts &&
|
|
13
|
+
delim: has_opts && typeof options.delim === 'string'
|
|
16
14
|
? options.delim
|
|
17
15
|
: ',',
|
|
18
|
-
separator: has_opts &&
|
|
16
|
+
separator: has_opts && typeof options.separator === 'string' && options.separator.trim().length > 0
|
|
19
17
|
? options.separator
|
|
20
18
|
: '.',
|
|
21
19
|
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0
|
|
@@ -35,16 +33,16 @@ export default function humanizeNumber (val, options = {}) {
|
|
|
35
33
|
};
|
|
36
34
|
|
|
37
35
|
// If not a valid value, return
|
|
38
|
-
if (!
|
|
36
|
+
if (!Number.isFinite(val) && typeof val !== 'string') {
|
|
39
37
|
return `0${OPTS.units.length > 0 ? OPTS.units[0] : ''}`;
|
|
40
38
|
}
|
|
41
39
|
|
|
42
40
|
// Ensure we are working with an integer
|
|
43
41
|
let normalized;
|
|
44
42
|
if (OPTS.real) {
|
|
45
|
-
normalized = parseInt(
|
|
43
|
+
normalized = parseInt(typeof val === 'string' ? val.trim() : val) || 0;
|
|
46
44
|
} else {
|
|
47
|
-
normalized = parseFloat(
|
|
45
|
+
normalized = parseFloat(typeof val === 'string' ? val.trim() : val) || 0;
|
|
48
46
|
}
|
|
49
47
|
if (!Number.isFinite(normalized) || normalized === 0) {
|
|
50
48
|
return `0${OPTS.units.length > 0 ? OPTS.units[0] : ''}`;
|
package/src/string/is.mjs
CHANGED
package/src/string/isBetween.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isString from './is.mjs';
|
|
4
|
-
|
|
5
3
|
// Check if a string is between a range in length
|
|
6
4
|
export default function isStringBetween (val, min, max, trimmed = true) {
|
|
7
5
|
if (
|
|
8
|
-
|
|
6
|
+
typeof val !== 'string' ||
|
|
9
7
|
!Number.isFinite(min) ||
|
|
10
8
|
min < 0 ||
|
|
11
9
|
!Number.isFinite(max) ||
|
package/src/string/shorten.mjs
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isString from './is.mjs';
|
|
4
|
-
import isNumberAbove from '../number/isAbove.mjs';
|
|
5
|
-
|
|
6
3
|
// Shorten a string and add a postfix if it goes over a specific length, will autotrim value
|
|
7
4
|
//
|
|
8
5
|
// @param string val Value to shorten, returns false a string is not passed
|
|
@@ -10,12 +7,14 @@ import isNumberAbove from '../number/isAbove.mjs';
|
|
|
10
7
|
// @param string postfix (default='...') Postfix to use when shortened
|
|
11
8
|
export default function shorten (val, length, postfix = '...') {
|
|
12
9
|
if (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
!
|
|
10
|
+
typeof val !== 'string' ||
|
|
11
|
+
typeof postfix !== 'string' ||
|
|
12
|
+
!Number.isFinite(length) ||
|
|
13
|
+
length <= 0
|
|
16
14
|
) return false;
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
// Trim first
|
|
17
|
+
const sanitized = val.trim();
|
|
19
18
|
|
|
20
|
-
return `${
|
|
19
|
+
return sanitized.length <= length ? sanitized : `${sanitized.substr(0, length)}${postfix}`;
|
|
21
20
|
}
|
package/string/humanizeBytes.js
CHANGED
|
@@ -4,17 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: !0
|
|
5
5
|
});
|
|
6
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
7
|
var _humanizeNumber = _interopRequireDefault(require("./humanizeNumber.js"));
|
|
8
|
+
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
11
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
10
|
function humanizeBytes(val) {
|
|
13
11
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
|
-
var has_opts = Object.prototype.toString.call(options) ===
|
|
12
|
+
var has_opts = Object.prototype.toString.call(options) === '[object Object]';
|
|
15
13
|
return (0, _humanizeNumber["default"])(val, {
|
|
16
|
-
delim: has_opts &&
|
|
17
|
-
separator: has_opts &&
|
|
14
|
+
delim: has_opts && typeof options.delim === 'string' ? options.delim : ',',
|
|
15
|
+
separator: has_opts && typeof options.separator === 'string' && options.separator.trim().length > 0 ? options.separator : '.',
|
|
18
16
|
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0 ? options.precision : 2,
|
|
19
17
|
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
18
|
divider: 1024,
|
package/string/humanizeNumber.js
CHANGED
|
@@ -6,29 +6,27 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = humanizeNumber;
|
|
7
7
|
var _is = _interopRequireDefault(require("../boolean/is.js"));
|
|
8
8
|
var _is2 = _interopRequireDefault(require("../string/is.js"));
|
|
9
|
-
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
10
9
|
var _round = _interopRequireDefault(require("../number/round.js"));
|
|
11
|
-
var _is3 = require("../object/is.js");
|
|
12
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
11
|
function humanizeNumber(val) {
|
|
14
12
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
15
|
-
var has_opts = Object.prototype.toString.call(options) ===
|
|
13
|
+
var has_opts = Object.prototype.toString.call(options) === '[object Object]';
|
|
16
14
|
var OPTS = {
|
|
17
|
-
delim: has_opts &&
|
|
18
|
-
separator: has_opts &&
|
|
15
|
+
delim: has_opts && typeof options.delim === 'string' ? options.delim : ',',
|
|
16
|
+
separator: has_opts && typeof options.separator === 'string' && options.separator.trim().length > 0 ? options.separator : '.',
|
|
19
17
|
precision: has_opts && Number.isInteger(options.precision) && options.precision >= 0 ? options.precision : 2,
|
|
20
18
|
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
19
|
divider: has_opts && Number.isInteger(options.divider) && options.divider > 1 ? options.divider : 1000,
|
|
22
20
|
real: has_opts && (0, _is["default"])(options.real) ? options.real : !1
|
|
23
21
|
};
|
|
24
|
-
if (!
|
|
22
|
+
if (!Number.isFinite(val) && typeof val !== 'string') {
|
|
25
23
|
return "0".concat(OPTS.units.length > 0 ? OPTS.units[0] : '');
|
|
26
24
|
}
|
|
27
25
|
var normalized;
|
|
28
26
|
if (OPTS.real) {
|
|
29
|
-
normalized = parseInt(
|
|
27
|
+
normalized = parseInt(typeof val === 'string' ? val.trim() : val) || 0;
|
|
30
28
|
} else {
|
|
31
|
-
normalized = parseFloat(
|
|
29
|
+
normalized = parseFloat(typeof val === 'string' ? val.trim() : val) || 0;
|
|
32
30
|
}
|
|
33
31
|
if (!Number.isFinite(normalized) || normalized === 0) {
|
|
34
32
|
return "0".concat(OPTS.units.length > 0 ? OPTS.units[0] : '');
|
package/string/is.js
CHANGED
package/string/isBetween.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
3
|
value: !0
|
|
5
4
|
});
|
|
6
5
|
exports["default"] = isStringBetween;
|
|
7
|
-
var _is = _interopRequireDefault(require("./is.js"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
9
6
|
function isStringBetween(val, min, max) {
|
|
10
7
|
var trimmed = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : !0;
|
|
11
|
-
if (
|
|
8
|
+
if (typeof val !== 'string' || !Number.isFinite(min) || min < 0 || !Number.isFinite(max) || max < 0 || min >= max) return !1;
|
|
12
9
|
var length = (trimmed === !0 ? val.trim() : val).length;
|
|
13
10
|
return length >= min && length <= max;
|
|
14
11
|
}
|
package/string/isNotEmpty.js
CHANGED
|
@@ -6,6 +6,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = isNotEmptyString;
|
|
7
7
|
function isNotEmptyString(val) {
|
|
8
8
|
var trimmed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !0;
|
|
9
|
-
if (typeof val !== 'string'
|
|
9
|
+
if (typeof val !== 'string') return !1;
|
|
10
10
|
return (trimmed === !0 ? val.trim() : val).length > 0;
|
|
11
11
|
}
|
package/string/shorten.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
3
|
value: !0
|
|
5
4
|
});
|
|
6
5
|
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
6
|
function shorten(val, length) {
|
|
11
7
|
var postfix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '...';
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
return "".concat(
|
|
8
|
+
if (typeof val !== 'string' || typeof postfix !== 'string' || !Number.isFinite(length) || length <= 0) return !1;
|
|
9
|
+
var sanitized = val.trim();
|
|
10
|
+
return sanitized.length <= length ? sanitized : "".concat(sanitized.substr(0, length)).concat(postfix);
|
|
15
11
|
}
|