@valkyriestudios/utils 12.13.0 → 12.14.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/array/join.js CHANGED
@@ -3,27 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.join = join;
4
4
  exports.default = join;
5
5
  const round_1 = require("../number/round");
6
- const isInteger_1 = require("../number/isInteger");
6
+ const isIntegerAboveOrEqual_1 = require("../number/isIntegerAboveOrEqual");
7
7
  function join(val, opts) {
8
8
  if (!Array.isArray(val) || !val.length)
9
9
  return '';
10
- let DELIM = ' ';
11
- let TRIM = true;
12
- let VALTRIM = true;
13
- let VALROUND = false;
14
- let INNERTRIM = false;
15
- if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
16
- if (typeof opts.delim === 'string')
17
- DELIM = opts.delim;
18
- if (opts.trim === false)
19
- TRIM = opts.trim;
20
- if (opts.valtrim === false)
21
- VALTRIM = false;
22
- if (opts.innertrim === true)
23
- INNERTRIM = true;
24
- if ((0, isInteger_1.isInteger)(opts.valround) && opts.valround >= 0)
25
- VALROUND = opts.valround;
26
- }
10
+ const DELIM = typeof opts?.delim === 'string' ? opts.delim : ' ';
11
+ const TRIM = opts?.trim ?? true;
12
+ const VALTRIM = opts?.valtrim ?? true;
13
+ const INNERTRIM = opts?.innertrim ?? false;
14
+ const VALROUND = (0, isIntegerAboveOrEqual_1.isIntegerAboveOrEqual)(opts?.valround, 0) ? opts.valround : false;
27
15
  let result = '';
28
16
  for (let i = 0; i < val.length; i++) {
29
17
  const el = val[i];
package/array/sort.js CHANGED
@@ -58,33 +58,39 @@ function sort(arr, by, dir = 'asc', opts) {
58
58
  const fn = opts.filter_fn;
59
59
  FILTER_FN = (el => (0, isNotEmpty_1.isNotEmptyObject)(el) && fn(el));
60
60
  }
61
- let getKey;
61
+ const prepared_arr = [];
62
+ const nokey_arr = [];
62
63
  if (typeof by === 'string') {
63
64
  const by_s = by.trim();
64
65
  if (!by_s.length)
65
66
  throw new Error('Sort by as string should contain content');
66
- getKey = (el) => el[by_s];
67
+ for (const el of arr) {
68
+ if (!FILTER_FN(el))
69
+ continue;
70
+ if (el?.[by_s] === undefined) {
71
+ nokey_arr.push(el);
72
+ }
73
+ else {
74
+ prepared_arr.push([el[by_s], el]);
75
+ }
76
+ }
67
77
  }
68
78
  else if (typeof by === 'function') {
69
- getKey = by;
79
+ for (const el of arr) {
80
+ if (!FILTER_FN(el))
81
+ continue;
82
+ const key = by(el);
83
+ if (key === undefined) {
84
+ nokey_arr.push(el);
85
+ }
86
+ else {
87
+ prepared_arr.push([key, el]);
88
+ }
89
+ }
70
90
  }
71
91
  else {
72
92
  throw new Error('Sort by should either be a string with content or a function');
73
93
  }
74
- const prepared_arr = [];
75
- const nokey_arr = [];
76
- for (let i = 0; i < arr.length; i++) {
77
- const el = arr[i];
78
- if (!FILTER_FN(el))
79
- continue;
80
- const key = getKey(el);
81
- if (key === undefined) {
82
- nokey_arr.push(el);
83
- }
84
- else {
85
- prepared_arr.push([key, el]);
86
- }
87
- }
88
94
  quickSort(prepared_arr);
89
95
  if (dir === 'desc')
90
96
  prepared_arr.reverse();
package/index.d.ts CHANGED
@@ -26,9 +26,9 @@ declare module "number/round" {
26
26
  function round(val: number, precision?: number): number;
27
27
  export { round, round as default };
28
28
  }
29
- declare module "number/isInteger" {
30
- function isInteger(val: unknown): val is number;
31
- export { isInteger, isInteger as default };
29
+ declare module "number/isIntegerAboveOrEqual" {
30
+ function isIntegerAboveOrEqual(val: unknown, ref: number): val is number;
31
+ export { isIntegerAboveOrEqual, isIntegerAboveOrEqual as default };
32
32
  }
33
33
  declare module "array/join" {
34
34
  interface joinOptions {
@@ -56,10 +56,6 @@ declare module "array/mapKey" {
56
56
  function mapKey<T extends Record<string, any>>(arr: T[], key: string, opts?: mapOptions): Record<string, T>;
57
57
  export { mapKey, mapKey as default };
58
58
  }
59
- declare module "number/isIntegerAboveOrEqual" {
60
- function isIntegerAboveOrEqual(val: unknown, ref: number): val is number;
61
- export { isIntegerAboveOrEqual, isIntegerAboveOrEqual as default };
62
- }
63
59
  declare module "array/mapPrimitive" {
64
60
  interface mapOptions {
65
61
  valtrim?: boolean;
@@ -322,6 +318,10 @@ declare module "number/isBetween" {
322
318
  function isNumberBetween(val: unknown, min: number, max: number): val is number;
323
319
  export { isNumberBetween, isNumberBetween as default };
324
320
  }
321
+ declare module "number/isInteger" {
322
+ function isInteger(val: unknown): val is number;
323
+ export { isInteger, isInteger as default };
324
+ }
325
325
  declare module "number/isIntegerAbove" {
326
326
  function isIntegerAbove(val: unknown, ref: number): val is number;
327
327
  export { isIntegerAbove, isIntegerAbove as default };
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.13.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
1
+ { "name": "@valkyriestudios/utils", "version": "12.14.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
@@ -34,10 +34,13 @@ function humanizeNumber(val, options = {}) {
34
34
  }
35
35
  }
36
36
  const humanized = `${(0, round_1.round)(normalized, PRECISION)}`.split('.', 2);
37
- humanized[0] = humanized[0].split('').reverse().map((char, ix, original) => {
38
- if (ix > 0 && ix < original.length && ix % 3 === 0)
39
- return char + DELIM;
40
- return char;
41
- }).reverse().join('');
42
- return `${sign}${humanized.join(SEPARATOR)}${UNITS ? UNITS[unit_ix] : ''}`;
37
+ const integerPart = humanized[0];
38
+ let formattedIntegerPart = '';
39
+ for (let i = 0; i < integerPart.length; i++) {
40
+ if (i > 0 && (integerPart.length - i) % 3 === 0) {
41
+ formattedIntegerPart += DELIM;
42
+ }
43
+ formattedIntegerPart += integerPart[i];
44
+ }
45
+ return `${sign}${formattedIntegerPart}${humanized[1] ? SEPARATOR + humanized[1] : ''}${UNITS ? UNITS[unit_ix] : ''}`;
43
46
  }