@valkyriestudios/utils 12.13.0 → 12.15.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 +6 -18
- package/array/sort.d.ts +4 -4
- package/array/sort.js +23 -17
- package/index.d.ts +11 -11
- package/package.json +1 -1
- package/string/humanizeNumber.js +9 -6
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
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
type sortOptions<T> = {
|
|
2
2
|
/**
|
|
3
3
|
* Filter function to apply to the array before sorting
|
|
4
4
|
* (default=isNotEmptyObject)
|
|
5
5
|
*/
|
|
6
|
-
filter_fn?: (el:
|
|
6
|
+
filter_fn?: (el: T) => boolean;
|
|
7
7
|
/**
|
|
8
8
|
* Remove objects that don't have the key or where the key is falsy
|
|
9
9
|
* (default=false)
|
|
@@ -14,7 +14,7 @@ interface sortOptions {
|
|
|
14
14
|
* (default=true)
|
|
15
15
|
*/
|
|
16
16
|
nokey_atend?: boolean;
|
|
17
|
-
}
|
|
17
|
+
};
|
|
18
18
|
type sortByFunction = (el: Record<string, any>) => string;
|
|
19
19
|
/**
|
|
20
20
|
* Sort an array of objects.
|
|
@@ -60,5 +60,5 @@ type sortByFunction = (el: Record<string, any>) => string;
|
|
|
60
60
|
*/
|
|
61
61
|
declare function sort<T extends {
|
|
62
62
|
[key: string]: any;
|
|
63
|
-
}[]>(arr: T, by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions): T;
|
|
63
|
+
}[]>(arr: T, by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions<T>): T;
|
|
64
64
|
export { sort, sort as default };
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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/
|
|
30
|
-
function
|
|
31
|
-
export {
|
|
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;
|
|
@@ -86,15 +82,15 @@ declare module "array/shuffle" {
|
|
|
86
82
|
export { shuffle, shuffle as default };
|
|
87
83
|
}
|
|
88
84
|
declare module "array/sort" {
|
|
89
|
-
|
|
90
|
-
filter_fn?: (el:
|
|
85
|
+
type sortOptions<T> = {
|
|
86
|
+
filter_fn?: (el: T) => boolean;
|
|
91
87
|
nokey_hide?: boolean;
|
|
92
88
|
nokey_atend?: boolean;
|
|
93
|
-
}
|
|
89
|
+
};
|
|
94
90
|
type sortByFunction = (el: Record<string, any>) => string;
|
|
95
91
|
function sort<T extends {
|
|
96
92
|
[key: string]: any;
|
|
97
|
-
}[]>(arr: T, by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions): T;
|
|
93
|
+
}[]>(arr: T, by: string | sortByFunction, dir?: 'asc' | 'desc', opts?: sortOptions<T>): T;
|
|
98
94
|
export { sort, sort as default };
|
|
99
95
|
}
|
|
100
96
|
declare module "array/index" {
|
|
@@ -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.
|
|
1
|
+
{ "name": "@valkyriestudios/utils", "version": "12.15.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" }
|
package/string/humanizeNumber.js
CHANGED
|
@@ -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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
}
|