@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.
Files changed (73) hide show
  1. package/README.md +1 -1
  2. package/array/join.js +3 -6
  3. package/array/mapFn.js +5 -10
  4. package/array/mapKey.js +9 -11
  5. package/array/mapPrimitive.js +3 -8
  6. package/array/sort.js +10 -10
  7. package/boolean/is.js +1 -1
  8. package/caching/memoize.js +1 -3
  9. package/date/addUTC.js +3 -3
  10. package/date/diff.js +3 -3
  11. package/date/endOfUTC.js +3 -3
  12. package/date/is.js +1 -1
  13. package/date/startOfUTC.js +3 -3
  14. package/date/toUTC.js +3 -1
  15. package/date/toUnix.js +3 -1
  16. package/deep/freeze.js +2 -3
  17. package/deep/get.js +7 -9
  18. package/deep/seal.js +2 -3
  19. package/deep/set.js +6 -8
  20. package/equal.js +2 -4
  21. package/function/is.js +1 -1
  22. package/hash/fnv1A.js +4 -9
  23. package/object/define.js +1 -2
  24. package/object/is.js +1 -2
  25. package/object/isNotEmpty.js +1 -2
  26. package/object/merge.js +2 -3
  27. package/object/pick.js +8 -8
  28. package/package.json +4 -4
  29. package/regexp/is.js +1 -3
  30. package/regexp/sanitize.js +1 -4
  31. package/src/array/join.mjs +5 -7
  32. package/src/array/mapFn.mjs +5 -9
  33. package/src/array/mapKey.mjs +11 -11
  34. package/src/array/mapPrimitive.mjs +7 -6
  35. package/src/array/sort.mjs +11 -12
  36. package/src/boolean/is.mjs +1 -1
  37. package/src/caching/memoize.mjs +1 -3
  38. package/src/date/addUTC.mjs +3 -3
  39. package/src/date/diff.mjs +4 -4
  40. package/src/date/endOfUTC.mjs +3 -3
  41. package/src/date/is.mjs +1 -1
  42. package/src/date/startOfUTC.mjs +3 -3
  43. package/src/date/toUTC.mjs +3 -1
  44. package/src/date/toUnix.mjs +3 -1
  45. package/src/deep/freeze.mjs +2 -4
  46. package/src/deep/get.mjs +9 -8
  47. package/src/deep/seal.mjs +2 -4
  48. package/src/deep/set.mjs +8 -7
  49. package/src/equal.mjs +5 -7
  50. package/src/function/is.mjs +1 -1
  51. package/src/hash/fnv1A.mjs +4 -9
  52. package/src/object/define.mjs +2 -4
  53. package/src/object/is.mjs +1 -1
  54. package/src/object/isNotEmpty.mjs +1 -3
  55. package/src/object/merge.mjs +3 -5
  56. package/src/object/pick.mjs +13 -11
  57. package/src/regexp/is.mjs +1 -3
  58. package/src/regexp/sanitize.mjs +1 -3
  59. package/src/string/humanizeBytes.mjs +4 -6
  60. package/src/string/humanizeNumber.mjs +9 -11
  61. package/src/string/is.mjs +1 -1
  62. package/src/string/isBetween.mjs +1 -3
  63. package/src/string/isNotEmpty.mjs +1 -1
  64. package/src/string/shorten.mjs +7 -8
  65. package/string/humanizeBytes.js +4 -6
  66. package/string/humanizeNumber.js +6 -8
  67. package/string/is.js +1 -1
  68. package/string/isBetween.js +1 -4
  69. package/string/isNotEmpty.js +1 -1
  70. package/string/shorten.js +3 -7
  71. package/CHANGELOG.md +0 -469
  72. package/data/continents.json +0 -9
  73. package/data/countries.json +0 -251
@@ -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) !== PROTO_OBJ ||
8
- Object.prototype.toString.call(obj) !== PROTO_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,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- export const PROTO_OBJ = '[object Object]';
3
+ const PROTO_OBJ = '[object Object]';
4
4
 
5
5
  export default function isObject (val) {
6
6
  return Object.prototype.toString.call(val) === PROTO_OBJ;
@@ -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) === PROTO_OBJ && Object.keys(val).length > 0;
4
+ return Object.prototype.toString.call(val) === '[object Object]' && Object.keys(val).length > 0;
7
5
  }
@@ -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) !== PROTO_OBJ ||
8
- Object.prototype.toString.call(source) !== PROTO_OBJ
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]) === PROTO_OBJ &&
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] || {});
@@ -1,13 +1,11 @@
1
1
  'use strict';
2
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';
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) !== PROTO_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 (!isNotEmptyString(key)) continue;
20
- key_deep = key.match(/(\.|\[)/g);
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, key.trim())
23
- : obj[key.trim()];
24
+ ? deepGet(obj, sanitized)
25
+ : obj[sanitized];
24
26
  if (val === undefined) continue;
25
27
 
26
28
  if (key_deep) {
27
- deepSet(map, key.trim(), val);
29
+ deepSet(map, sanitized, val);
28
30
  } else {
29
- map[key.trim()] = val;
31
+ map[sanitized] = val;
30
32
  }
31
33
  }
32
34
  return map;
package/src/regexp/is.mjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- export const PROTO_RGX = '[object RegExp]';
4
-
5
3
  export default function isRegExp (val) {
6
- return Object.prototype.toString.call(val) === PROTO_RGX;
4
+ return Object.prototype.toString.call(val) === '[object RegExp]';
7
5
  }
@@ -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 (!isNotEmptyString(val)) return false;
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) === PROTO_OBJ;
10
+ const has_opts = Object.prototype.toString.call(options) === '[object Object]';
13
11
  return humanizeNumber(val, {
14
- delim: has_opts && isString(options.delim)
12
+ delim: has_opts && typeof options.delim === 'string'
15
13
  ? options.delim
16
14
  : ',',
17
- separator: has_opts && isNotEmptyString(options.separator)
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 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';
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) === PROTO_OBJ;
11
+ const has_opts = Object.prototype.toString.call(options) === '[object Object]';
14
12
  const OPTS = {
15
- delim: has_opts && isString(options.delim)
13
+ delim: has_opts && typeof options.delim === 'string'
16
14
  ? options.delim
17
15
  : ',',
18
- separator: has_opts && isNotEmptyString(options.separator)
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 (!(Number.isFinite(val) || isNotEmptyString(val))) {
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(isString(val) ? val.trim() : val) || 0;
43
+ normalized = parseInt(typeof val === 'string' ? val.trim() : val) || 0;
46
44
  } else {
47
- normalized = parseFloat(isString(val) ? val.trim() : val) || 0;
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
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
3
  export default function isString (val) {
4
- return typeof val === 'string' || val instanceof String;
4
+ return typeof val === 'string';
5
5
  }
@@ -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
- !isString(val) ||
6
+ typeof val !== 'string' ||
9
7
  !Number.isFinite(min) ||
10
8
  min < 0 ||
11
9
  !Number.isFinite(max) ||
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  export default function isNotEmptyString (val, trimmed = true) {
4
- if (typeof val !== 'string' && !(val instanceof String)) return false;
4
+ if (typeof val !== 'string') return false;
5
5
  return (trimmed === true ? val.trim() : val).length > 0;
6
6
  }
@@ -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
- !isString(val) ||
14
- !isString(postfix) ||
15
- !isNumberAbove(length, 0)
10
+ typeof val !== 'string' ||
11
+ typeof postfix !== 'string' ||
12
+ !Number.isFinite(length) ||
13
+ length <= 0
16
14
  ) return false;
17
15
 
18
- if (val.trim().length <= length) return val.trim();
16
+ // Trim first
17
+ const sanitized = val.trim();
19
18
 
20
- return `${val.trim().substr(0, length)}${postfix}`;
19
+ return sanitized.length <= length ? sanitized : `${sanitized.substr(0, length)}${postfix}`;
21
20
  }
@@ -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) === _is2.PROTO_OBJ;
12
+ var has_opts = Object.prototype.toString.call(options) === '[object Object]';
15
13
  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 : '.',
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,
@@ -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) === _is3.PROTO_OBJ;
13
+ var has_opts = Object.prototype.toString.call(options) === '[object Object]';
16
14
  var OPTS = {
17
- delim: has_opts && (0, _is2["default"])(options.delim) ? options.delim : ',',
18
- separator: has_opts && (0, _isNotEmpty["default"])(options.separator) ? options.separator : '.',
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 (!(Number.isFinite(val) || (0, _isNotEmpty["default"])(val))) {
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((0, _is2["default"])(val) ? val.trim() : val) || 0;
27
+ normalized = parseInt(typeof val === 'string' ? val.trim() : val) || 0;
30
28
  } else {
31
- normalized = parseFloat((0, _is2["default"])(val) ? val.trim() : val) || 0;
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
@@ -5,5 +5,5 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = isString;
7
7
  function isString(val) {
8
- return typeof val === 'string' || val instanceof String;
8
+ return typeof val === 'string';
9
9
  }
@@ -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 (!(0, _is["default"])(val) || !Number.isFinite(min) || min < 0 || !Number.isFinite(max) || max < 0 || min >= max) return !1;
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
  }
@@ -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' && !(val instanceof String)) return !1;
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 (!(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);
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
  }