@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.
Files changed (255) hide show
  1. package/README.md +2 -2
  2. package/dist/array/dedupe.d.ts +1 -0
  3. package/dist/array/dedupe.js +19 -0
  4. package/dist/array/is.d.ts +1 -0
  5. package/dist/array/is.js +6 -0
  6. package/dist/array/isNotEmpty.d.ts +1 -0
  7. package/dist/array/isNotEmpty.js +6 -0
  8. package/dist/array/join.d.ts +8 -0
  9. package/dist/array/join.js +24 -0
  10. package/dist/array/mapFn.d.ts +14 -0
  11. package/dist/array/mapFn.js +28 -0
  12. package/dist/array/mapKey.d.ts +12 -0
  13. package/dist/array/mapKey.js +27 -0
  14. package/dist/array/mapPrimitive.d.ts +10 -0
  15. package/dist/array/mapPrimitive.js +27 -0
  16. package/dist/array/shuffle.d.ts +1 -0
  17. package/dist/array/shuffle.js +11 -0
  18. package/dist/array/sort.d.ts +11 -0
  19. package/dist/array/sort.js +92 -0
  20. package/dist/boolean/is.d.ts +1 -0
  21. package/dist/boolean/is.js +6 -0
  22. package/dist/caching/memoize.d.ts +1 -0
  23. package/dist/caching/memoize.js +15 -0
  24. package/dist/date/addUTC.d.ts +1 -0
  25. package/{src/date/addUTC.mjs → dist/date/addUTC.js} +11 -26
  26. package/dist/date/diff.d.ts +1 -0
  27. package/dist/date/diff.js +38 -0
  28. package/dist/date/endOfUTC.d.ts +1 -0
  29. package/dist/date/endOfUTC.js +52 -0
  30. package/dist/date/is.d.ts +1 -0
  31. package/dist/date/is.js +6 -0
  32. package/dist/date/nowUnix.d.ts +1 -0
  33. package/dist/date/nowUnix.js +6 -0
  34. package/dist/date/nowUnixMs.d.ts +1 -0
  35. package/dist/date/nowUnixMs.js +6 -0
  36. package/dist/date/startOfUTC.d.ts +1 -0
  37. package/dist/date/startOfUTC.js +52 -0
  38. package/dist/date/toUTC.d.ts +1 -0
  39. package/dist/date/toUTC.js +9 -0
  40. package/dist/date/toUnix.d.ts +1 -0
  41. package/dist/date/toUnix.js +9 -0
  42. package/dist/deep/freeze.d.ts +7 -0
  43. package/dist/deep/freeze.js +23 -0
  44. package/dist/deep/get.d.ts +5 -0
  45. package/dist/deep/get.js +41 -0
  46. package/dist/deep/seal.d.ts +7 -0
  47. package/dist/deep/seal.js +23 -0
  48. package/dist/deep/set.d.ts +5 -0
  49. package/dist/deep/set.js +52 -0
  50. package/dist/equal.d.ts +2 -0
  51. package/dist/equal.js +43 -0
  52. package/dist/function/is.d.ts +1 -0
  53. package/dist/function/is.js +6 -0
  54. package/dist/function/noop.d.ts +1 -0
  55. package/dist/function/noop.js +5 -0
  56. package/dist/function/noopresolve.d.ts +1 -0
  57. package/dist/function/noopresolve.js +6 -0
  58. package/dist/function/noopreturn.d.ts +1 -0
  59. package/dist/function/noopreturn.js +6 -0
  60. package/dist/function/sleep.d.ts +1 -0
  61. package/dist/function/sleep.js +8 -0
  62. package/dist/hash/fnv1A.d.ts +1 -0
  63. package/dist/hash/fnv1A.js +51 -0
  64. package/dist/hash/guid.d.ts +1 -0
  65. package/{src/hash/guid.mjs → dist/hash/guid.js} +9 -14
  66. package/dist/is.d.ts +75 -0
  67. package/dist/is.js +77 -0
  68. package/dist/number/is.d.ts +1 -0
  69. package/dist/number/is.js +6 -0
  70. package/dist/number/isAbove.d.ts +1 -0
  71. package/dist/number/isAbove.js +6 -0
  72. package/dist/number/isAboveOrEqual.d.ts +1 -0
  73. package/dist/number/isAboveOrEqual.js +6 -0
  74. package/dist/number/isBelow.d.ts +1 -0
  75. package/dist/number/isBelow.js +6 -0
  76. package/dist/number/isBelowOrEqual.d.ts +1 -0
  77. package/dist/number/isBelowOrEqual.js +6 -0
  78. package/dist/number/isBetween.d.ts +1 -0
  79. package/dist/number/isBetween.js +11 -0
  80. package/dist/number/isInteger.d.ts +1 -0
  81. package/dist/number/isInteger.js +6 -0
  82. package/dist/number/isIntegerAbove.d.ts +1 -0
  83. package/dist/number/isIntegerAbove.js +6 -0
  84. package/dist/number/isIntegerAboveOrEqual.d.ts +1 -0
  85. package/dist/number/isIntegerAboveOrEqual.js +6 -0
  86. package/dist/number/isIntegerBelow.d.ts +1 -0
  87. package/dist/number/isIntegerBelow.js +6 -0
  88. package/dist/number/isIntegerBelowOrEqual.d.ts +1 -0
  89. package/dist/number/isIntegerBelowOrEqual.js +6 -0
  90. package/dist/number/isIntegerBetween.d.ts +1 -0
  91. package/dist/number/isIntegerBetween.js +11 -0
  92. package/dist/number/isNumericalNaN.d.ts +1 -0
  93. package/dist/number/isNumericalNaN.js +6 -0
  94. package/dist/number/randomBetween.d.ts +1 -0
  95. package/dist/number/randomBetween.js +9 -0
  96. package/dist/number/randomIntBetween.d.ts +1 -0
  97. package/dist/number/randomIntBetween.js +9 -0
  98. package/dist/number/round.d.ts +1 -0
  99. package/dist/number/round.js +10 -0
  100. package/dist/number/toPercentage.d.ts +1 -0
  101. package/dist/number/toPercentage.js +11 -0
  102. package/dist/object/define.d.ts +7 -0
  103. package/dist/object/define.js +9 -0
  104. package/dist/object/is.d.ts +1 -0
  105. package/dist/object/is.js +7 -0
  106. package/dist/object/isNotEmpty.d.ts +1 -0
  107. package/dist/object/isNotEmpty.js +6 -0
  108. package/dist/object/merge.d.ts +8 -0
  109. package/dist/object/merge.js +20 -0
  110. package/dist/object/pick.d.ts +5 -0
  111. package/dist/object/pick.js +34 -0
  112. package/dist/regexp/is.d.ts +1 -0
  113. package/dist/regexp/is.js +6 -0
  114. package/dist/regexp/sanitize.d.ts +1 -0
  115. package/dist/regexp/sanitize.js +8 -0
  116. package/dist/string/humanizeBytes.d.ts +8 -0
  117. package/dist/string/humanizeBytes.js +24 -0
  118. package/dist/string/humanizeNumber.d.ts +10 -0
  119. package/dist/string/humanizeNumber.js +60 -0
  120. package/dist/string/is.d.ts +1 -0
  121. package/dist/string/is.js +6 -0
  122. package/dist/string/isBetween.d.ts +1 -0
  123. package/dist/string/isBetween.js +14 -0
  124. package/dist/string/isNotEmpty.d.ts +1 -0
  125. package/dist/string/isNotEmpty.js +8 -0
  126. package/dist/string/shorten.d.ts +1 -0
  127. package/dist/string/shorten.js +11 -0
  128. package/package.json +303 -46
  129. package/CHANGELOG.md +0 -474
  130. package/array/dedupe.js +0 -33
  131. package/array/is.js +0 -9
  132. package/array/isNotEmpty.js +0 -9
  133. package/array/join.js +0 -41
  134. package/array/mapFn.js +0 -42
  135. package/array/mapKey.js +0 -38
  136. package/array/mapPrimitive.js +0 -43
  137. package/array/shuffle.js +0 -14
  138. package/array/sort.js +0 -125
  139. package/boolean/is.js +0 -9
  140. package/caching/memoize.js +0 -21
  141. package/date/addUTC.js +0 -60
  142. package/date/diff.js +0 -40
  143. package/date/endOfUTC.js +0 -55
  144. package/date/is.js +0 -9
  145. package/date/nowUnix.js +0 -9
  146. package/date/nowUnixMs.js +0 -9
  147. package/date/startOfUTC.js +0 -55
  148. package/date/toUTC.js +0 -10
  149. package/date/toUnix.js +0 -10
  150. package/deep/define.js +0 -12
  151. package/deep/freeze.js +0 -18
  152. package/deep/get.js +0 -31
  153. package/deep/seal.js +0 -18
  154. package/deep/set.js +0 -28
  155. package/equal.js +0 -37
  156. package/function/is.js +0 -8
  157. package/function/noop.js +0 -7
  158. package/function/noopresolve.js +0 -10
  159. package/function/noopreturn.js +0 -8
  160. package/function/sleep.js +0 -13
  161. package/hash/fnv1A.js +0 -49
  162. package/hash/guid.js +0 -16
  163. package/is.js +0 -319
  164. package/number/is.js +0 -9
  165. package/number/isAbove.js +0 -8
  166. package/number/isAboveOrEqual.js +0 -8
  167. package/number/isBelow.js +0 -8
  168. package/number/isBelowOrEqual.js +0 -8
  169. package/number/isBetween.js +0 -9
  170. package/number/isInteger.js +0 -9
  171. package/number/isIntegerAbove.js +0 -8
  172. package/number/isIntegerAboveOrEqual.js +0 -8
  173. package/number/isIntegerBelow.js +0 -8
  174. package/number/isIntegerBelowOrEqual.js +0 -8
  175. package/number/isIntegerBetween.js +0 -9
  176. package/number/isNumericalNaN.js +0 -9
  177. package/number/randomBetween.js +0 -11
  178. package/number/randomIntBetween.js +0 -11
  179. package/number/round.js +0 -12
  180. package/number/toPercentage.js +0 -15
  181. package/object/define.js +0 -12
  182. package/object/is.js +0 -11
  183. package/object/isNotEmpty.js +0 -10
  184. package/object/merge.js +0 -20
  185. package/object/pick.js +0 -41
  186. package/regexp/is.js +0 -11
  187. package/regexp/sanitize.js +0 -12
  188. package/src/array/dedupe.mjs +0 -24
  189. package/src/array/is.mjs +0 -5
  190. package/src/array/isNotEmpty.mjs +0 -5
  191. package/src/array/join.mjs +0 -31
  192. package/src/array/mapFn.mjs +0 -34
  193. package/src/array/mapKey.mjs +0 -31
  194. package/src/array/mapPrimitive.mjs +0 -29
  195. package/src/array/shuffle.mjs +0 -14
  196. package/src/array/sort.mjs +0 -102
  197. package/src/boolean/is.mjs +0 -5
  198. package/src/caching/memoize.mjs +0 -24
  199. package/src/date/diff.mjs +0 -46
  200. package/src/date/endOfUTC.mjs +0 -130
  201. package/src/date/is.mjs +0 -5
  202. package/src/date/nowUnix.mjs +0 -5
  203. package/src/date/nowUnixMs.mjs +0 -5
  204. package/src/date/startOfUTC.mjs +0 -129
  205. package/src/date/toUTC.mjs +0 -17
  206. package/src/date/toUnix.mjs +0 -9
  207. package/src/deep/define.mjs +0 -7
  208. package/src/deep/freeze.mjs +0 -22
  209. package/src/deep/get.mjs +0 -51
  210. package/src/deep/seal.mjs +0 -22
  211. package/src/deep/set.mjs +0 -45
  212. package/src/equal.mjs +0 -67
  213. package/src/function/is.mjs +0 -6
  214. package/src/function/noop.mjs +0 -5
  215. package/src/function/noopresolve.mjs +0 -6
  216. package/src/function/noopreturn.mjs +0 -6
  217. package/src/function/sleep.mjs +0 -12
  218. package/src/hash/fnv1A.mjs +0 -58
  219. package/src/is.mjs +0 -86
  220. package/src/number/is.mjs +0 -5
  221. package/src/number/isAbove.mjs +0 -6
  222. package/src/number/isAboveOrEqual.mjs +0 -6
  223. package/src/number/isBelow.mjs +0 -6
  224. package/src/number/isBelowOrEqual.mjs +0 -6
  225. package/src/number/isBetween.mjs +0 -13
  226. package/src/number/isInteger.mjs +0 -5
  227. package/src/number/isIntegerAbove.mjs +0 -6
  228. package/src/number/isIntegerAboveOrEqual.mjs +0 -6
  229. package/src/number/isIntegerBelow.mjs +0 -6
  230. package/src/number/isIntegerBelowOrEqual.mjs +0 -6
  231. package/src/number/isIntegerBetween.mjs +0 -13
  232. package/src/number/isNumericalNaN.mjs +0 -5
  233. package/src/number/randomBetween.mjs +0 -11
  234. package/src/number/randomIntBetween.mjs +0 -11
  235. package/src/number/round.mjs +0 -10
  236. package/src/number/toPercentage.mjs +0 -14
  237. package/src/object/define.mjs +0 -12
  238. package/src/object/is.mjs +0 -7
  239. package/src/object/isNotEmpty.mjs +0 -7
  240. package/src/object/merge.mjs +0 -27
  241. package/src/object/pick.mjs +0 -33
  242. package/src/regexp/is.mjs +0 -7
  243. package/src/regexp/sanitize.mjs +0 -12
  244. package/src/string/humanizeBytes.mjs +0 -29
  245. package/src/string/humanizeNumber.mjs +0 -84
  246. package/src/string/is.mjs +0 -5
  247. package/src/string/isBetween.mjs +0 -18
  248. package/src/string/isNotEmpty.mjs +0 -6
  249. package/src/string/shorten.mjs +0 -21
  250. package/string/humanizeBytes.js +0 -23
  251. package/string/humanizeNumber.js +0 -56
  252. package/string/is.js +0 -9
  253. package/string/isBetween.js +0 -14
  254. package/string/isNotEmpty.js +0 -11
  255. 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,5 +0,0 @@
1
- 'use strict';
2
-
3
- export default function isNumericalNaN (val) {
4
- return Number.isNaN(val) || val === Infinity;
5
- }
@@ -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
- }
@@ -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
- }
@@ -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
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- export const PROTO_OBJ = '[object Object]';
4
-
5
- export default function isObject (val) {
6
- return Object.prototype.toString.call(val) === PROTO_OBJ;
7
- }
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- import {PROTO_OBJ} from './is.mjs';
4
-
5
- export default function isNotEmptyObject (val) {
6
- return Object.prototype.toString.call(val) === PROTO_OBJ && Object.keys(val).length > 0;
7
- }
@@ -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;
@@ -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
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- export const PROTO_RGX = '[object RegExp]';
4
-
5
- export default function isRegExp (val) {
6
- return Object.prototype.toString.call(val) === PROTO_RGX;
7
- }
@@ -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
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- export default function isString (val) {
4
- return typeof val === 'string' || val instanceof String;
5
- }
@@ -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
- }
@@ -1,6 +0,0 @@
1
- 'use strict';
2
-
3
- export default function isNotEmptyString (val, trimmed = true) {
4
- if (typeof val !== 'string' && !(val instanceof String)) return false;
5
- return (trimmed === true ? val.trim() : val).length > 0;
6
- }
@@ -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
- }
@@ -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
- }
@@ -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
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: !0
5
- });
6
- exports["default"] = isString;
7
- function isString(val) {
8
- return typeof val === 'string' || val instanceof String;
9
- }
@@ -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
- }
@@ -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
- }