@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/object/pick.js
CHANGED
|
@@ -6,14 +6,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = pick;
|
|
7
7
|
var _get = _interopRequireDefault(require("../deep/get.js"));
|
|
8
8
|
var _set = _interopRequireDefault(require("../deep/set.js"));
|
|
9
|
-
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
10
|
-
var _is = require("./is.js");
|
|
11
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
10
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: !0 }; return { done: !1, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = !0, didErr = !1, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = !0; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
13
11
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
14
12
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
15
13
|
function pick(obj, keys) {
|
|
16
|
-
if (Object.prototype.toString.call(obj) !==
|
|
14
|
+
if (Object.prototype.toString.call(obj) !== '[object Object]' || !Array.isArray(keys) || keys.length === 0) throw new TypeError('Please pass an object to pick from and a keys array');
|
|
17
15
|
var map = {};
|
|
18
16
|
var key_deep = !1;
|
|
19
17
|
var val;
|
|
@@ -22,14 +20,16 @@ function pick(obj, keys) {
|
|
|
22
20
|
try {
|
|
23
21
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
24
22
|
var key = _step.value;
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
if (typeof key !== 'string') continue;
|
|
24
|
+
var sanitized = key.trim();
|
|
25
|
+
if (sanitized.length === 0) continue;
|
|
26
|
+
key_deep = sanitized.match(/(\.|\[)/g);
|
|
27
|
+
val = key_deep ? (0, _get["default"])(obj, sanitized) : obj[sanitized];
|
|
28
28
|
if (val === undefined) continue;
|
|
29
29
|
if (key_deep) {
|
|
30
|
-
(0, _set["default"])(map,
|
|
30
|
+
(0, _set["default"])(map, sanitized, val);
|
|
31
31
|
} else {
|
|
32
|
-
map[
|
|
32
|
+
map[sanitized] = val;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@valkyriestudios/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "A collection of single-function utilities for common tasks",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Peter Vermeulen",
|
|
8
|
-
"email": "contact@valkyriestudios.
|
|
8
|
+
"email": "contact@valkyriestudios.be",
|
|
9
9
|
"url": "www.valkyriestudios.be"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"homepage": "https://github.com/ValkyrieStudios/utils#readme",
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@babel/cli": "^7.23.4",
|
|
39
|
-
"@babel/core": "^7.23.
|
|
40
|
-
"@babel/preset-env": "^7.23.
|
|
39
|
+
"@babel/core": "^7.23.6",
|
|
40
|
+
"@babel/preset-env": "^7.23.6",
|
|
41
41
|
"@babel/register": "^7.22.15",
|
|
42
42
|
"babel-plugin-module-extension": "^0.1.3",
|
|
43
43
|
"babel-plugin-transform-minify-booleans": "^6.9.4",
|
package/regexp/is.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: !0
|
|
5
5
|
});
|
|
6
|
-
exports.PROTO_RGX = void 0;
|
|
7
6
|
exports["default"] = isRegExp;
|
|
8
|
-
var PROTO_RGX = exports.PROTO_RGX = '[object RegExp]';
|
|
9
7
|
function isRegExp(val) {
|
|
10
|
-
return Object.prototype.toString.call(val) ===
|
|
8
|
+
return Object.prototype.toString.call(val) === '[object RegExp]';
|
|
11
9
|
}
|
package/regexp/sanitize.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
3
|
value: !0
|
|
5
4
|
});
|
|
6
5
|
exports["default"] = sanitizeRegExp;
|
|
7
|
-
var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty.js"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
9
6
|
function sanitizeRegExp(val) {
|
|
10
|
-
if (
|
|
7
|
+
if (typeof val !== 'string') return !1;
|
|
11
8
|
return val.trim().replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
|
12
9
|
}
|
package/src/array/join.mjs
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import isNotEmptyObject from '../object/isNotEmpty.mjs';
|
|
5
|
-
import round from '../number/round.mjs';
|
|
3
|
+
import round from '../number/round.mjs';
|
|
6
4
|
|
|
7
5
|
// Join an array of values while autofiltering any non-string/non-number elements
|
|
8
6
|
//
|
|
9
7
|
// @param array arr Array of values to join
|
|
10
|
-
// @param object
|
|
11
|
-
export default function join (arr,
|
|
8
|
+
// @param object opts Override options
|
|
9
|
+
export default function join (arr, opts) {
|
|
12
10
|
if (!Array.isArray(arr) || arr.length === 0) return '';
|
|
13
11
|
|
|
14
12
|
const OPTS = Object.assign({
|
|
@@ -16,11 +14,11 @@ export default function join (arr, options = {}) {
|
|
|
16
14
|
trim : true, // Trim after joining
|
|
17
15
|
valtrim : true, // Automatically trim string values
|
|
18
16
|
valround : false, // Automatically round numbers
|
|
19
|
-
},
|
|
17
|
+
}, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
|
|
20
18
|
|
|
21
19
|
const filtered = [];
|
|
22
20
|
for (const el of arr) {
|
|
23
|
-
if (
|
|
21
|
+
if (typeof el === 'string' && el.trim().length > 0) {
|
|
24
22
|
filtered.push(OPTS.valtrim === true ? el.trim() : el);
|
|
25
23
|
} else if (Number.isFinite(el)) {
|
|
26
24
|
filtered.push(Number.isFinite(OPTS.valround) ? round(el, OPTS.valround) : el);
|
package/src/array/mapFn.mjs
CHANGED
|
@@ -1,27 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
5
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
6
|
-
|
|
7
|
-
export default function mapFn (arr, fn, opts = {}) {
|
|
3
|
+
export default function mapFn (arr, fn, opts) {
|
|
8
4
|
if (
|
|
9
5
|
(!Array.isArray(arr) || arr.length === 0) ||
|
|
10
|
-
|
|
6
|
+
typeof fn !== 'function'
|
|
11
7
|
) return {};
|
|
12
8
|
|
|
13
9
|
const OPTS = Object.assign({
|
|
14
10
|
merge: false,
|
|
15
|
-
}, Object.prototype.toString.call(opts) ===
|
|
11
|
+
}, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
|
|
16
12
|
|
|
17
13
|
const map = {};
|
|
18
14
|
let hash = false;
|
|
19
15
|
for (const el of arr) {
|
|
20
|
-
if (Object.prototype.toString.call(el) !==
|
|
16
|
+
if (Object.prototype.toString.call(el) !== '[object Object]') continue;
|
|
21
17
|
|
|
22
18
|
// Get hash
|
|
23
19
|
hash = fn(el);
|
|
24
|
-
if (!Number.isFinite(hash) && !
|
|
20
|
+
if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length > 0)) continue;
|
|
25
21
|
|
|
26
22
|
if (OPTS.merge === true && map.hasOwnProperty(hash)) {
|
|
27
23
|
map[hash] = Object.assign(map[hash], el);
|
package/src/array/mapKey.mjs
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
5
|
-
|
|
6
|
-
export default function mapKey (arr, key, opts = {}) {
|
|
3
|
+
export default function mapKey (arr, key, opts) {
|
|
7
4
|
if (
|
|
8
5
|
(!Array.isArray(arr) || arr.length === 0) ||
|
|
9
|
-
|
|
6
|
+
typeof key !== 'string'
|
|
10
7
|
) return {};
|
|
11
8
|
|
|
9
|
+
const key_s = key.trim();
|
|
10
|
+
if (key_s.length === 0) return {};
|
|
11
|
+
|
|
12
12
|
const OPTS = Object.assign({
|
|
13
13
|
merge: false,
|
|
14
|
-
}, Object.prototype.toString.call(opts) ===
|
|
14
|
+
}, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
|
|
15
15
|
|
|
16
16
|
const map = {};
|
|
17
17
|
for (const el of arr) {
|
|
18
18
|
if (
|
|
19
|
-
Object.prototype.toString.call(el) !==
|
|
20
|
-
!Object.prototype.hasOwnProperty.call(el,
|
|
19
|
+
Object.prototype.toString.call(el) !== '[object Object]' ||
|
|
20
|
+
!Object.prototype.hasOwnProperty.call(el, key_s)
|
|
21
21
|
) continue;
|
|
22
22
|
|
|
23
|
-
if (OPTS.merge === true && map.hasOwnProperty(el[
|
|
24
|
-
map[el[
|
|
23
|
+
if (OPTS.merge === true && map.hasOwnProperty(el[key_s])) {
|
|
24
|
+
map[el[key_s]] = Object.assign(map[el[key_s]], el);
|
|
25
25
|
} else {
|
|
26
|
-
map[el[
|
|
26
|
+
map[el[key_s]] = el;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
// Map an array of primitive values (numbers/strings)
|
|
4
|
+
//
|
|
5
|
+
// @param array arr Array of values to join
|
|
6
|
+
// @param object opts Override options
|
|
7
|
+
export default function mapPrimitive (arr, opts) {
|
|
7
8
|
if (!Array.isArray(arr) || arr.length === 0) return {};
|
|
8
9
|
|
|
9
10
|
const OPTS = Object.assign({
|
|
10
11
|
valtrim: false,
|
|
11
12
|
keyround: false,
|
|
12
13
|
valround: false,
|
|
13
|
-
},
|
|
14
|
+
}, Object.prototype.toString.call(opts) === '[object Object]' ? opts : {});
|
|
14
15
|
|
|
15
16
|
const map = {};
|
|
16
17
|
for (const el of arr) {
|
|
@@ -20,7 +21,7 @@ export default function mapPrimitive (arr, opts = {}) {
|
|
|
20
21
|
} else {
|
|
21
22
|
map[el] = OPTS.valround ? Math.round(el) : el;
|
|
22
23
|
}
|
|
23
|
-
} else if (
|
|
24
|
+
} else if (typeof el === 'string' && el.trim().length > 0) {
|
|
24
25
|
map[el.trim()] = OPTS.valtrim ? el.trim() : el;
|
|
25
26
|
}
|
|
26
27
|
}
|
package/src/array/sort.mjs
CHANGED
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
import isBoolean from '../boolean/is.mjs';
|
|
4
4
|
import isNotEmptyObject from '../object/isNotEmpty.mjs';
|
|
5
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
6
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
7
|
-
import isFunction from '../function/is.mjs';
|
|
8
5
|
|
|
9
6
|
function partition (arr, start_ix, end_ix) {
|
|
10
7
|
const pivot_val = arr[Math.floor((start_ix + end_ix) / 2)].t;
|
|
@@ -45,16 +42,13 @@ function quickSort (arr, start_ix = 0, end_ix = arr.length - 1) {
|
|
|
45
42
|
export default function sort (arr, by, dir = 'asc', options = {}) {
|
|
46
43
|
if (!Array.isArray(arr) || arr.length === 0) return [];
|
|
47
44
|
|
|
48
|
-
// Check by
|
|
49
|
-
if (!isNotEmptyString(by) && !isFunction(by)) throw new Error('Sort by should be either a string or a function');
|
|
50
|
-
|
|
51
45
|
// Check dir
|
|
52
46
|
if (dir !== 'asc' && dir !== 'desc') throw new Error('Direction should be either asc or desc');
|
|
53
47
|
|
|
54
|
-
const has_opts = Object.prototype.toString.call(options) ===
|
|
48
|
+
const has_opts = Object.prototype.toString.call(options) === '[object Object]';
|
|
55
49
|
|
|
56
50
|
const OPTS = {
|
|
57
|
-
filter_fn : has_opts &&
|
|
51
|
+
filter_fn : has_opts && typeof options.filter_fn === 'function'
|
|
58
52
|
? el => isNotEmptyObject(el) && options.filter_fn(el)
|
|
59
53
|
: isNotEmptyObject,
|
|
60
54
|
nokey_hide : has_opts && isBoolean(options.nokey_hide) ? options.nokey_hide : false,
|
|
@@ -64,17 +58,20 @@ export default function sort (arr, by, dir = 'asc', options = {}) {
|
|
|
64
58
|
// Prepare for sort
|
|
65
59
|
const prepared_arr = [];
|
|
66
60
|
const nokey_arr = [];
|
|
67
|
-
if (
|
|
61
|
+
if (typeof by === 'string') {
|
|
62
|
+
const by_s = by.trim();
|
|
63
|
+
if (by_s.length === 0) throw new Error('Sort by should either be a string with content or a function');
|
|
64
|
+
|
|
68
65
|
for (const el of arr) {
|
|
69
66
|
if (!OPTS.filter_fn(el)) continue;
|
|
70
67
|
|
|
71
|
-
if (!Object.prototype.hasOwnProperty.call(el,
|
|
68
|
+
if (!Object.prototype.hasOwnProperty.call(el, by_s) || el[by_s] === undefined) {
|
|
72
69
|
nokey_arr.push(el);
|
|
73
70
|
} else {
|
|
74
|
-
prepared_arr.push({t: el[
|
|
71
|
+
prepared_arr.push({t: el[by_s], el});
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
|
-
} else {
|
|
74
|
+
} else if (typeof by === 'function') {
|
|
78
75
|
let key;
|
|
79
76
|
for (const el of arr) {
|
|
80
77
|
if (!OPTS.filter_fn(el)) continue;
|
|
@@ -86,6 +83,8 @@ export default function sort (arr, by, dir = 'asc', options = {}) {
|
|
|
86
83
|
prepared_arr.push({t: by(el), el});
|
|
87
84
|
}
|
|
88
85
|
}
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error('Sort by should either be a string with content or a function');
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
// Sort
|
package/src/boolean/is.mjs
CHANGED
package/src/caching/memoize.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isFunction from '../function/is.mjs';
|
|
4
|
-
|
|
5
3
|
export default function memoize (fn, resolver = false) {
|
|
6
|
-
const has_resolver =
|
|
4
|
+
const has_resolver = typeof resolver === 'function';
|
|
7
5
|
const memoized = function () {
|
|
8
6
|
// Get key
|
|
9
7
|
const key = has_resolver ? resolver.apply(this, arguments) : arguments[0]; // eslint-disable-line
|
package/src/date/addUTC.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import isDate from './is.mjs';
|
|
4
4
|
|
|
5
5
|
export default function addUTC (val, amount, key) {
|
|
6
6
|
if (
|
|
7
|
-
!(val
|
|
7
|
+
!isDate(val)
|
|
8
8
|
) throw new TypeError('addUTC requires a date object');
|
|
9
9
|
|
|
10
10
|
if (
|
|
@@ -12,7 +12,7 @@ export default function addUTC (val, amount, key) {
|
|
|
12
12
|
) throw new TypeError('Amount needs to be an integer');
|
|
13
13
|
|
|
14
14
|
if (
|
|
15
|
-
|
|
15
|
+
typeof key !== 'string'
|
|
16
16
|
) throw new TypeError('Key needs to be a string with content');
|
|
17
17
|
|
|
18
18
|
const copy = new Date(Date.UTC(
|
package/src/date/diff.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import isDate from './is.mjs';
|
|
4
4
|
|
|
5
5
|
const SECOND_IN_MILLISECONDS = 1000;
|
|
6
6
|
const MINUTE_IN_MILLISECONDS = SECOND_IN_MILLISECONDS * 60;
|
|
@@ -10,13 +10,13 @@ const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
|
|
|
10
10
|
|
|
11
11
|
export default function diff (val_a, val_b, key = false) {
|
|
12
12
|
if (
|
|
13
|
-
!(val_a
|
|
14
|
-
!(val_b
|
|
13
|
+
!isDate(val_a) ||
|
|
14
|
+
!isDate(val_b)
|
|
15
15
|
) throw new TypeError('Diff requires date objects for both values');
|
|
16
16
|
|
|
17
17
|
if (
|
|
18
18
|
key !== false &&
|
|
19
|
-
|
|
19
|
+
typeof key !== 'string'
|
|
20
20
|
) throw new TypeError('Key needs to be a string or false');
|
|
21
21
|
|
|
22
22
|
// Get difference in milliseconds
|
package/src/date/endOfUTC.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import isDate from './is.mjs';
|
|
4
4
|
|
|
5
5
|
// Take note: this is the end of week key for weeks starting on key,
|
|
6
6
|
// eg: end of week for week_mon is sunday as the week starts on monday and ends on sunday
|
|
@@ -17,11 +17,11 @@ const WEEK_END = {
|
|
|
17
17
|
|
|
18
18
|
export default function endOfUTC (val, key) {
|
|
19
19
|
if (
|
|
20
|
-
!(val
|
|
20
|
+
!isDate(val)
|
|
21
21
|
) throw new TypeError('endOfUTC requires a date object');
|
|
22
22
|
|
|
23
23
|
if (
|
|
24
|
-
|
|
24
|
+
typeof key !== 'string'
|
|
25
25
|
) throw new TypeError('Key needs to be a string with content');
|
|
26
26
|
|
|
27
27
|
switch (key) {
|
package/src/date/is.mjs
CHANGED
package/src/date/startOfUTC.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import isDate from './is.mjs';
|
|
4
4
|
|
|
5
5
|
const WEEK_START = {
|
|
6
6
|
week : 1, // Original lib cases only contained week and historical was monday
|
|
@@ -15,11 +15,11 @@ const WEEK_START = {
|
|
|
15
15
|
|
|
16
16
|
export default function startOfUTC (val, key) {
|
|
17
17
|
if (
|
|
18
|
-
!(val
|
|
18
|
+
!isDate(val)
|
|
19
19
|
) throw new TypeError('startOfUTC requires a date object');
|
|
20
20
|
|
|
21
21
|
if (
|
|
22
|
-
|
|
22
|
+
typeof key !== 'string'
|
|
23
23
|
) throw new TypeError('Key needs to be a string with content');
|
|
24
24
|
|
|
25
25
|
switch (key) {
|
package/src/date/toUTC.mjs
CHANGED
package/src/date/toUnix.mjs
CHANGED
package/src/deep/freeze.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
4
|
-
|
|
5
3
|
function deep (obj) {
|
|
6
4
|
for (const key of Object.keys(obj)) {
|
|
7
5
|
if (
|
|
8
|
-
Object.prototype.toString.call(obj[key]) ===
|
|
6
|
+
Object.prototype.toString.call(obj[key]) === '[object Object]' ||
|
|
9
7
|
Array.isArray(obj[key])
|
|
10
8
|
) deep(obj[key]);
|
|
11
9
|
}
|
|
@@ -15,7 +13,7 @@ function deep (obj) {
|
|
|
15
13
|
// Freeze nested structures
|
|
16
14
|
export default function deepFreeze (obj) {
|
|
17
15
|
if (
|
|
18
|
-
Object.prototype.toString.call(obj) !==
|
|
16
|
+
Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
19
17
|
!Array.isArray(obj)
|
|
20
18
|
) throw new TypeError('Only objects/arrays can be frozen');
|
|
21
19
|
return deep(obj);
|
package/src/deep/get.mjs
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
4
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
5
|
-
|
|
6
3
|
// Get a value from a path in a json-like structure
|
|
7
4
|
export default function deepGet (obj, path, get_parent = false) {
|
|
8
5
|
if (
|
|
9
|
-
Object.prototype.toString.call(obj) !==
|
|
6
|
+
Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
10
7
|
!Array.isArray(obj)
|
|
11
8
|
) throw new TypeError('Deepget is only supported for objects');
|
|
12
9
|
|
|
13
10
|
// If no path is provided, do nothing
|
|
14
|
-
if (
|
|
11
|
+
if (typeof path !== 'string') throw new TypeError('No path was given');
|
|
12
|
+
|
|
13
|
+
// Check if path contains content
|
|
14
|
+
const path_s = path.trim();
|
|
15
|
+
if (path_s.length === 0) throw new TypeError('No path was given');
|
|
15
16
|
|
|
16
17
|
// Cleanup paths : a.b[2].c --> ['a', 'b', '2', 'c'] (faster processing)
|
|
17
|
-
const parts =
|
|
18
|
+
const parts = path_s
|
|
18
19
|
.replace(/\[/g, '.')
|
|
19
20
|
.replace(/(\.){2,}/g, '.')
|
|
20
21
|
.replace(/(^\.|\.$|\])/g, '')
|
|
@@ -32,7 +33,7 @@ export default function deepGet (obj, path, get_parent = false) {
|
|
|
32
33
|
const ix = parseInt(parts.shift());
|
|
33
34
|
if (!Number.isInteger(ix) || ix < 0 || ix > (cursor.length - 1)) return undefined;
|
|
34
35
|
cursor = cursor[ix];
|
|
35
|
-
} else if (Object.prototype.toString.call(cursor) ===
|
|
36
|
+
} else if (Object.prototype.toString.call(cursor) === '[object Object]') {
|
|
36
37
|
const key = parts.shift();
|
|
37
38
|
if (!Object.prototype.hasOwnProperty.call(cursor, key)) return undefined;
|
|
38
39
|
cursor = cursor[key];
|
|
@@ -40,7 +41,7 @@ export default function deepGet (obj, path, get_parent = false) {
|
|
|
40
41
|
|
|
41
42
|
// If we have more parts and cursor is not an array or object -> immediately return undefined
|
|
42
43
|
if (
|
|
43
|
-
(!Array.isArray(cursor) && Object.prototype.toString.call(cursor) !==
|
|
44
|
+
(!Array.isArray(cursor) && Object.prototype.toString.call(cursor) !== '[object Object]') &&
|
|
44
45
|
parts.length > 0
|
|
45
46
|
) return undefined;
|
|
46
47
|
}
|
package/src/deep/seal.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
4
|
-
|
|
5
3
|
function deep (obj) {
|
|
6
4
|
for (const key of Object.keys(obj)) {
|
|
7
5
|
if (
|
|
8
|
-
Object.prototype.toString.call(obj[key]) ===
|
|
6
|
+
Object.prototype.toString.call(obj[key]) === '[object Object]' ||
|
|
9
7
|
Array.isArray(obj[key])
|
|
10
8
|
) deep(obj[key]);
|
|
11
9
|
}
|
|
@@ -15,7 +13,7 @@ function deep (obj) {
|
|
|
15
13
|
// Seal nested structures
|
|
16
14
|
export default function deepSeal (obj) {
|
|
17
15
|
if (
|
|
18
|
-
Object.prototype.toString.call(obj) !==
|
|
16
|
+
Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
19
17
|
!Array.isArray(obj)
|
|
20
18
|
) throw new TypeError('Only objects/arrays can be sealed');
|
|
21
19
|
return deep(obj);
|
package/src/deep/set.mjs
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import isNotEmptyString from '../string/isNotEmpty.mjs';
|
|
4
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
5
|
-
|
|
6
3
|
// Set a value for a path in a json-like structure
|
|
7
4
|
export default function deepSet (obj, path, value = null, define = false) {
|
|
8
5
|
if (
|
|
9
|
-
Object.prototype.toString.call(obj) !==
|
|
6
|
+
Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
10
7
|
!Array.isArray(obj)
|
|
11
8
|
) throw new TypeError('Deepset is only supported for objects');
|
|
12
9
|
|
|
13
10
|
// If no path is provided, do nothing
|
|
14
|
-
if (
|
|
11
|
+
if (typeof path !== 'string') throw new TypeError('No path was given');
|
|
12
|
+
|
|
13
|
+
// Check if path contains content
|
|
14
|
+
const path_s = path.trim();
|
|
15
|
+
if (path_s.length === 0) throw new TypeError('No path was given');
|
|
15
16
|
|
|
16
17
|
// Cleanup paths : a.b[2].c --> ['a', 'b', '2', 'c'] (faster processing)
|
|
17
|
-
const parts =
|
|
18
|
+
const parts = path_s
|
|
18
19
|
.replace(/\[/g, '.')
|
|
19
20
|
.replace(/(\.){2,}/g, '.')
|
|
20
21
|
.replace(/(^\.|\.$|\])/g, '')
|
|
@@ -32,7 +33,7 @@ export default function deepSet (obj, path, value = null, define = false) {
|
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
// Prevent overriding of properties, eg: {d: 'hello'} -> deepSet('d.a.b', 'should not work')
|
|
35
|
-
if (!Array.isArray(obj) && Object.prototype.toString.call(obj) !==
|
|
36
|
+
if (!Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]') return false;
|
|
36
37
|
|
|
37
38
|
// Set the actual value on the cursor
|
|
38
39
|
if (define) {
|
package/src/equal.mjs
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
/* eslint-disable no-use-before-define */
|
|
4
4
|
|
|
5
|
-
import isNumericalNaN
|
|
6
|
-
import {PROTO_RGX} from './regexp/is.mjs';
|
|
7
|
-
import {PROTO_OBJ} from './object/is.mjs';
|
|
5
|
+
import isNumericalNaN from './number/isNumericalNaN.mjs';
|
|
8
6
|
|
|
9
7
|
function isArrayEqual (a, b) {
|
|
10
8
|
if (a.length !== b.length) return false;
|
|
@@ -39,8 +37,8 @@ function equal (a, b) {
|
|
|
39
37
|
|
|
40
38
|
// RegExp Check
|
|
41
39
|
if (
|
|
42
|
-
Object.prototype.toString.call(a) ===
|
|
43
|
-
Object.prototype.toString.call(b) ===
|
|
40
|
+
Object.prototype.toString.call(a) === '[object RegExp]' &&
|
|
41
|
+
Object.prototype.toString.call(b) === '[object RegExp]'
|
|
44
42
|
) return String(a) === String(b);
|
|
45
43
|
|
|
46
44
|
// Array as root equal
|
|
@@ -51,8 +49,8 @@ function equal (a, b) {
|
|
|
51
49
|
|
|
52
50
|
// Object as root equal
|
|
53
51
|
if (
|
|
54
|
-
Object.prototype.toString.call(a) ===
|
|
55
|
-
Object.prototype.toString.call(b) ===
|
|
52
|
+
Object.prototype.toString.call(a) === '[object Object]' &&
|
|
53
|
+
Object.prototype.toString.call(b) === '[object Object]'
|
|
56
54
|
) return isObjectEqual(a, b);
|
|
57
55
|
|
|
58
56
|
// NAN Check
|
package/src/function/is.mjs
CHANGED
package/src/hash/fnv1A.mjs
CHANGED
|
@@ -2,11 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
/* eslint-disable no-bitwise */
|
|
4
4
|
|
|
5
|
-
import isString from '../string/is.mjs';
|
|
6
|
-
import isNumericalNaN from '../number/isNumericalNaN.mjs';
|
|
7
|
-
import {PROTO_RGX} from '../regexp/is.mjs';
|
|
8
|
-
import {PROTO_OBJ} from '../object/is.mjs';
|
|
9
|
-
|
|
10
5
|
// https://tools.ietf.org/html/draft-eastlake-fnv-03
|
|
11
6
|
|
|
12
7
|
const FNV_32 = 2166136261;
|
|
@@ -21,17 +16,17 @@ export default function fnv1A (data, offset = FNV_32) {
|
|
|
21
16
|
let sanitized;
|
|
22
17
|
|
|
23
18
|
// Convert data to a format that is hashable
|
|
24
|
-
if (
|
|
19
|
+
if (typeof data === 'string') {
|
|
25
20
|
sanitized = data;
|
|
26
21
|
} else if (Number.isFinite(data)) {
|
|
27
22
|
sanitized = `${data}`;
|
|
28
|
-
} else if (Array.isArray(data) || Object.prototype.toString.call(data) ===
|
|
23
|
+
} else if (Array.isArray(data) || Object.prototype.toString.call(data) === '[object Object]') {
|
|
29
24
|
sanitized = JSON.stringify(data);
|
|
30
|
-
} else if (Object.prototype.toString.call(data) ===
|
|
25
|
+
} else if (Object.prototype.toString.call(data) === '[object RegExp]') {
|
|
31
26
|
sanitized = data.toString();
|
|
32
27
|
} else if (data instanceof Date) {
|
|
33
28
|
sanitized = `${data.getTime()}`;
|
|
34
|
-
} else if (
|
|
29
|
+
} else if (Number.isNaN(data) || data === Infinity) {
|
|
35
30
|
sanitized = REPL_NAN;
|
|
36
31
|
} else if (data === false) {
|
|
37
32
|
sanitized = REPL_FALSE;
|