@naturalcycles/js-lib 14.60.0 → 14.63.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.
@@ -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
  */
@@ -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,10 @@ export interface HttpErrorData extends ErrorData {
31
36
  * @default 500
32
37
  */
33
38
  httpStatusCode: number;
39
+ /**
40
+ * Set to true when the error was thrown after response headers were sent.
41
+ */
42
+ headersSent?: boolean;
34
43
  }
35
44
  export interface Admin401ErrorData extends HttpErrorData {
36
45
  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
- return (mutate ? numbers : [...numbers]).sort((a, b) => a - b);
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
  /**
@@ -7,7 +7,14 @@ exports._safeJsonStringify = void 0;
7
7
  * Based on: https://github.com/moll/json-stringify-safe/
8
8
  */
9
9
  function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
10
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
10
+ try {
11
+ // Try native first (as it's ~3 times faster)
12
+ return JSON.stringify(obj, replacer, spaces);
13
+ }
14
+ catch {
15
+ // Native failed - resort to the "safe" serializer
16
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
17
+ }
11
18
  }
12
19
  exports._safeJsonStringify = _safeJsonStringify;
13
20
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._stringifyAny = void 0;
4
4
  const error_util_1 = require("../error/error.util");
5
5
  const json_util_1 = require("./json.util");
6
- const jsonStringifyFn = (obj, reviver, space) => JSON.stringify(obj, reviver, space);
6
+ const safeJsonStringify_1 = require("./safeJsonStringify");
7
7
  /**
8
8
  * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
9
9
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
@@ -83,7 +83,7 @@ function _stringifyAny(obj, opt = {}) {
83
83
  // Other
84
84
  //
85
85
  try {
86
- const { stringifyFn = jsonStringifyFn } = opt;
86
+ const { stringifyFn = safeJsonStringify_1._safeJsonStringify } = opt;
87
87
  s = stringifyFn(obj, undefined, 2);
88
88
  }
89
89
  catch {
@@ -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
- return (mutate ? numbers : [...numbers]).sort((a, b) => a - b);
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.
@@ -4,7 +4,14 @@
4
4
  * Based on: https://github.com/moll/json-stringify-safe/
5
5
  */
6
6
  export function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
7
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
7
+ try {
8
+ // Try native first (as it's ~3 times faster)
9
+ return JSON.stringify(obj, replacer, spaces);
10
+ }
11
+ catch (_a) {
12
+ // Native failed - resort to the "safe" serializer
13
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
14
+ }
8
15
  }
9
16
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
10
17
  function serializer(replacer, cycleReplacer) {
@@ -1,6 +1,6 @@
1
1
  import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util';
2
2
  import { _jsonParseIfPossible } from './json.util';
3
- const jsonStringifyFn = (obj, reviver, space) => JSON.stringify(obj, reviver, space);
3
+ import { _safeJsonStringify } from './safeJsonStringify';
4
4
  /**
5
5
  * Inspired by inspectAny from nodejs-lib, which is based on util.inpect that is not available in the Browser.
6
6
  * Potentially can do this (with extra 2Kb gz size): https://github.com/deecewan/browser-util-inspect
@@ -80,7 +80,7 @@ export function _stringifyAny(obj, opt = {}) {
80
80
  // Other
81
81
  //
82
82
  try {
83
- const { stringifyFn = jsonStringifyFn } = opt;
83
+ const { stringifyFn = _safeJsonStringify } = opt;
84
84
  s = stringifyFn(obj, undefined, 2);
85
85
  }
86
86
  catch (_a) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.60.0",
3
+ "version": "14.63.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -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>(items: T[], mapper: Mapper<T, any>, mutate = false): 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
 
@@ -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,11 @@ export interface HttpErrorData extends ErrorData {
36
42
  * @default 500
37
43
  */
38
44
  httpStatusCode: number
45
+
46
+ /**
47
+ * Set to true when the error was thrown after response headers were sent.
48
+ */
49
+ headersSent?: boolean
39
50
  }
40
51
 
41
52
  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
- return (mutate ? numbers : [...numbers]).sort((a, b) => a - b)
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
  /**
@@ -11,7 +11,13 @@ export function _safeJsonStringify(
11
11
  spaces?: number,
12
12
  cycleReplacer?: Reviver,
13
13
  ): string {
14
- return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
14
+ try {
15
+ // Try native first (as it's ~3 times faster)
16
+ return JSON.stringify(obj, replacer, spaces)
17
+ } catch {
18
+ // Native failed - resort to the "safe" serializer
19
+ return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
20
+ }
15
21
  }
16
22
 
17
23
  /* eslint-disable @typescript-eslint/no-unused-expressions, no-bitwise */
@@ -1,12 +1,10 @@
1
1
  import { _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse } from '../error/error.util'
2
2
  import { Reviver } from '../types'
3
3
  import { _jsonParseIfPossible } from './json.util'
4
+ import { _safeJsonStringify } from './safeJsonStringify'
4
5
 
5
6
  export type JsonStringifyFunction = (obj: any, reviver?: Reviver, space?: number) => string
6
7
 
7
- const jsonStringifyFn: JsonStringifyFunction = (obj, reviver, space) =>
8
- JSON.stringify(obj, reviver, space)
9
-
10
8
  export interface StringifyAnyOptions {
11
9
  /**
12
10
  * @default 10_000
@@ -116,7 +114,7 @@ export function _stringifyAny(obj: any, opt: StringifyAnyOptions = {}): string {
116
114
  // Other
117
115
  //
118
116
  try {
119
- const { stringifyFn = jsonStringifyFn } = opt
117
+ const { stringifyFn = _safeJsonStringify } = opt
120
118
 
121
119
  s = stringifyFn(obj, undefined, 2)
122
120
  } catch {