@naturalcycles/js-lib 14.60.1 → 14.64.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/dist/array/array.util.d.ts +1 -1
- package/dist/array/array.util.js +4 -3
- package/dist/error/error.model.d.ts +15 -0
- package/dist/number/number.util.d.ts +1 -1
- package/dist/number/number.util.js +3 -2
- package/dist-esm/array/array.util.js +4 -3
- package/dist-esm/number/number.util.js +3 -2
- package/package.json +1 -1
- package/src/array/array.util.ts +9 -3
- package/src/error/error.model.ts +18 -0
- package/src/number/number.util.ts +3 -2
|
@@ -93,7 +93,7 @@ export declare function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>)
|
|
|
93
93
|
* Same:
|
|
94
94
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
95
95
|
*/
|
|
96
|
-
export declare function _sortBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean): T[];
|
|
96
|
+
export declare function _sortBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean, descending?: boolean): T[];
|
|
97
97
|
/**
|
|
98
98
|
* Like items.find(), but it tries to find from the END of the array.
|
|
99
99
|
*/
|
package/dist/array/array.util.js
CHANGED
|
@@ -140,12 +140,13 @@ exports._groupBy = _groupBy;
|
|
|
140
140
|
* Same:
|
|
141
141
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
142
142
|
*/
|
|
143
|
-
function _sortBy(items, mapper, mutate = false) {
|
|
143
|
+
function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
144
|
+
const mod = descending ? -1 : 1;
|
|
144
145
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
145
146
|
const [a, b] = [_a, _b].map(mapper); // eslint-disable-line unicorn/no-array-callback-reference
|
|
146
147
|
if (typeof a === 'number' && typeof b === 'number')
|
|
147
|
-
return a - b;
|
|
148
|
-
return String(a).localeCompare(String(b));
|
|
148
|
+
return (a - b) * mod;
|
|
149
|
+
return String(a).localeCompare(String(b)) * mod;
|
|
149
150
|
});
|
|
150
151
|
}
|
|
151
152
|
exports._sortBy = _sortBy;
|
|
@@ -15,6 +15,11 @@ export interface ErrorData {
|
|
|
15
15
|
* Error id in some error tracking system (e.g Sentry).
|
|
16
16
|
*/
|
|
17
17
|
errorId?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Set to true to force reporting this error (e.g to Sentry).
|
|
20
|
+
* Useful to be able to force-report e.g a 4xx error, which by default wouldn't be reported.
|
|
21
|
+
*/
|
|
22
|
+
report?: boolean;
|
|
18
23
|
/**
|
|
19
24
|
* Sometimes error.message gets "decorated" with extra information
|
|
20
25
|
* (e.g frontend-lib adds a method, url, etc for all the errors)
|
|
@@ -31,6 +36,16 @@ export interface HttpErrorData extends ErrorData {
|
|
|
31
36
|
* @default 500
|
|
32
37
|
*/
|
|
33
38
|
httpStatusCode: number;
|
|
39
|
+
/**
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* GET /api/some-endpoint
|
|
43
|
+
*/
|
|
44
|
+
endpoint?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Set to true when the error was thrown after response headers were sent.
|
|
47
|
+
*/
|
|
48
|
+
headersSent?: boolean;
|
|
34
49
|
}
|
|
35
50
|
export interface Admin401ErrorData extends HttpErrorData {
|
|
36
51
|
adminAuthRequired: true;
|
|
@@ -23,7 +23,7 @@ export declare function _clamp(x: number, minIncl: number, maxIncl: number): num
|
|
|
23
23
|
* _sortNumbers([1, 3, 2])
|
|
24
24
|
* // [1, 2, 3]
|
|
25
25
|
*/
|
|
26
|
-
export declare function _sortNumbers(numbers: number[], mutate?: boolean): number[];
|
|
26
|
+
export declare function _sortNumbers(numbers: number[], mutate?: boolean, descending?: boolean): number[];
|
|
27
27
|
/**
|
|
28
28
|
* Same as .toFixed(), but conveniently casts the output to Number.
|
|
29
29
|
*
|
|
@@ -39,8 +39,9 @@ exports._clamp = _clamp;
|
|
|
39
39
|
* _sortNumbers([1, 3, 2])
|
|
40
40
|
* // [1, 2, 3]
|
|
41
41
|
*/
|
|
42
|
-
function _sortNumbers(numbers, mutate = false) {
|
|
43
|
-
|
|
42
|
+
function _sortNumbers(numbers, mutate = false, descending = false) {
|
|
43
|
+
const mod = descending ? -1 : 1;
|
|
44
|
+
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
|
|
44
45
|
}
|
|
45
46
|
exports._sortNumbers = _sortNumbers;
|
|
46
47
|
/**
|
|
@@ -130,12 +130,13 @@ export function _groupBy(items, mapper) {
|
|
|
130
130
|
* Same:
|
|
131
131
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
132
132
|
*/
|
|
133
|
-
export function _sortBy(items, mapper, mutate = false) {
|
|
133
|
+
export function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
134
|
+
const mod = descending ? -1 : 1;
|
|
134
135
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
135
136
|
const [a, b] = [_a, _b].map(mapper); // eslint-disable-line unicorn/no-array-callback-reference
|
|
136
137
|
if (typeof a === 'number' && typeof b === 'number')
|
|
137
|
-
return a - b;
|
|
138
|
-
return String(a).localeCompare(String(b));
|
|
138
|
+
return (a - b) * mod;
|
|
139
|
+
return String(a).localeCompare(String(b)) * mod;
|
|
139
140
|
});
|
|
140
141
|
}
|
|
141
142
|
/**
|
|
@@ -32,8 +32,9 @@ export function _clamp(x, minIncl, maxIncl) {
|
|
|
32
32
|
* _sortNumbers([1, 3, 2])
|
|
33
33
|
* // [1, 2, 3]
|
|
34
34
|
*/
|
|
35
|
-
export function _sortNumbers(numbers, mutate = false) {
|
|
36
|
-
|
|
35
|
+
export function _sortNumbers(numbers, mutate = false, descending = false) {
|
|
36
|
+
const mod = descending ? -1 : 1;
|
|
37
|
+
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Same as .toFixed(), but conveniently casts the output to Number.
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -140,11 +140,17 @@ export function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>): String
|
|
|
140
140
|
* Same:
|
|
141
141
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
142
142
|
*/
|
|
143
|
-
export function _sortBy<T>(
|
|
143
|
+
export function _sortBy<T>(
|
|
144
|
+
items: T[],
|
|
145
|
+
mapper: Mapper<T, any>,
|
|
146
|
+
mutate = false,
|
|
147
|
+
descending = false,
|
|
148
|
+
): T[] {
|
|
149
|
+
const mod = descending ? -1 : 1
|
|
144
150
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
145
151
|
const [a, b] = [_a, _b].map(mapper) // eslint-disable-line unicorn/no-array-callback-reference
|
|
146
|
-
if (typeof a === 'number' && typeof b === 'number') return a - b
|
|
147
|
-
return String(a).localeCompare(String(b))
|
|
152
|
+
if (typeof a === 'number' && typeof b === 'number') return (a - b) * mod
|
|
153
|
+
return String(a).localeCompare(String(b)) * mod
|
|
148
154
|
})
|
|
149
155
|
}
|
|
150
156
|
|
package/src/error/error.model.ts
CHANGED
|
@@ -18,6 +18,12 @@ export interface ErrorData {
|
|
|
18
18
|
*/
|
|
19
19
|
errorId?: string
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Set to true to force reporting this error (e.g to Sentry).
|
|
23
|
+
* Useful to be able to force-report e.g a 4xx error, which by default wouldn't be reported.
|
|
24
|
+
*/
|
|
25
|
+
report?: boolean
|
|
26
|
+
|
|
21
27
|
/**
|
|
22
28
|
* Sometimes error.message gets "decorated" with extra information
|
|
23
29
|
* (e.g frontend-lib adds a method, url, etc for all the errors)
|
|
@@ -36,6 +42,18 @@ export interface HttpErrorData extends ErrorData {
|
|
|
36
42
|
* @default 500
|
|
37
43
|
*/
|
|
38
44
|
httpStatusCode: number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @example
|
|
48
|
+
*
|
|
49
|
+
* GET /api/some-endpoint
|
|
50
|
+
*/
|
|
51
|
+
endpoint?: string
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Set to true when the error was thrown after response headers were sent.
|
|
55
|
+
*/
|
|
56
|
+
headersSent?: boolean
|
|
39
57
|
}
|
|
40
58
|
|
|
41
59
|
export interface Admin401ErrorData extends HttpErrorData {
|
|
@@ -37,8 +37,9 @@ export function _clamp(x: number, minIncl: number, maxIncl: number): number {
|
|
|
37
37
|
* _sortNumbers([1, 3, 2])
|
|
38
38
|
* // [1, 2, 3]
|
|
39
39
|
*/
|
|
40
|
-
export function _sortNumbers(numbers: number[], mutate = false): number[] {
|
|
41
|
-
|
|
40
|
+
export function _sortNumbers(numbers: number[], mutate = false, descending = false): number[] {
|
|
41
|
+
const mod = descending ? -1 : 1
|
|
42
|
+
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
/**
|