@naturalcycles/js-lib 14.152.0 → 14.153.1

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.
@@ -5,7 +5,7 @@ const string_util_1 = require("../string/string.util");
5
5
  const app_error_1 = require("./app.error");
6
6
  class JsonParseError extends app_error_1.AppError {
7
7
  constructor(data, cause) {
8
- super(['Failed to parse', data?.text && (0, string_util_1._truncateMiddle)(data.text, 200)].filter(Boolean).join(': '), data, cause, 'JsonParseError');
8
+ super(['Failed to parse', data.text && (0, string_util_1._truncateMiddle)(data.text, 200)].filter(Boolean).join(': '), data, cause, 'JsonParseError');
9
9
  }
10
10
  }
11
11
  exports.JsonParseError = JsonParseError;
@@ -4,10 +4,15 @@ import { Reviver } from '../types';
4
4
  * Returns original object if JSON parse failed (silently).
5
5
  */
6
6
  export declare function _jsonParseIfPossible(obj: any, reviver?: Reviver): any;
7
+ /**
8
+ * Convenience function that does JSON.parse, but doesn't throw on error,
9
+ * instead - safely returns `undefined`.
10
+ */
11
+ export declare function _jsonParseOrUndefined<T = unknown>(obj: any, reviver?: Reviver): T | undefined;
7
12
  /**
8
13
  * Same as JSON.parse, but throws JsonParseError:
9
14
  *
10
15
  * 1. It's message includes a piece of source text (truncated)
11
16
  * 2. It's data.text contains full source text
12
17
  */
13
- export declare function _jsonParse(s: string, reviver?: Reviver): unknown;
18
+ export declare function _jsonParse<T = unknown>(s: string, reviver?: Reviver): T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._jsonParse = exports._jsonParseIfPossible = void 0;
3
+ exports._jsonParse = exports._jsonParseOrUndefined = exports._jsonParseIfPossible = void 0;
4
4
  const jsonParseError_1 = require("../error/jsonParseError");
5
5
  // const possibleJsonStartTokens = ['{', '[', '"']
6
6
  const DETECT_JSON = /^\s*[{["\-\d]/;
@@ -19,6 +19,20 @@ function _jsonParseIfPossible(obj, reviver) {
19
19
  return obj;
20
20
  }
21
21
  exports._jsonParseIfPossible = _jsonParseIfPossible;
22
+ /**
23
+ * Convenience function that does JSON.parse, but doesn't throw on error,
24
+ * instead - safely returns `undefined`.
25
+ */
26
+ function _jsonParseOrUndefined(obj, reviver) {
27
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
28
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
29
+ try {
30
+ return JSON.parse(obj, reviver);
31
+ }
32
+ catch { }
33
+ }
34
+ }
35
+ exports._jsonParseOrUndefined = _jsonParseOrUndefined;
22
36
  /**
23
37
  * Same as JSON.parse, but throws JsonParseError:
24
38
  *
@@ -2,6 +2,6 @@ import { _truncateMiddle } from '../string/string.util';
2
2
  import { AppError } from './app.error';
3
3
  export class JsonParseError extends AppError {
4
4
  constructor(data, cause) {
5
- super(['Failed to parse', (data === null || data === void 0 ? void 0 : data.text) && _truncateMiddle(data.text, 200)].filter(Boolean).join(': '), data, cause, 'JsonParseError');
5
+ super(['Failed to parse', data.text && _truncateMiddle(data.text, 200)].filter(Boolean).join(': '), data, cause, 'JsonParseError');
6
6
  }
7
7
  }
@@ -15,6 +15,19 @@ export function _jsonParseIfPossible(obj, reviver) {
15
15
  }
16
16
  return obj;
17
17
  }
18
+ /**
19
+ * Convenience function that does JSON.parse, but doesn't throw on error,
20
+ * instead - safely returns `undefined`.
21
+ */
22
+ export function _jsonParseOrUndefined(obj, reviver) {
23
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
24
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
25
+ try {
26
+ return JSON.parse(obj, reviver);
27
+ }
28
+ catch (_a) { }
29
+ }
30
+ }
18
31
  /**
19
32
  * Same as JSON.parse, but throws JsonParseError:
20
33
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.152.0",
3
+ "version": "14.153.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -12,7 +12,7 @@ export interface JsonParseErrorData extends ErrorData {
12
12
  export class JsonParseError extends AppError<JsonParseErrorData> {
13
13
  constructor(data: JsonParseErrorData, cause?: ErrorObject) {
14
14
  super(
15
- ['Failed to parse', data?.text && _truncateMiddle(data.text, 200)].filter(Boolean).join(': '),
15
+ ['Failed to parse', data.text && _truncateMiddle(data.text, 200)].filter(Boolean).join(': '),
16
16
  data,
17
17
  cause,
18
18
  'JsonParseError',
@@ -19,13 +19,26 @@ export function _jsonParseIfPossible(obj: any, reviver?: Reviver): any {
19
19
  return obj
20
20
  }
21
21
 
22
+ /**
23
+ * Convenience function that does JSON.parse, but doesn't throw on error,
24
+ * instead - safely returns `undefined`.
25
+ */
26
+ export function _jsonParseOrUndefined<T = unknown>(obj: any, reviver?: Reviver): T | undefined {
27
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
28
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
29
+ try {
30
+ return JSON.parse(obj, reviver)
31
+ } catch {}
32
+ }
33
+ }
34
+
22
35
  /**
23
36
  * Same as JSON.parse, but throws JsonParseError:
24
37
  *
25
38
  * 1. It's message includes a piece of source text (truncated)
26
39
  * 2. It's data.text contains full source text
27
40
  */
28
- export function _jsonParse(s: string, reviver?: Reviver): unknown {
41
+ export function _jsonParse<T = unknown>(s: string, reviver?: Reviver): T {
29
42
  try {
30
43
  return JSON.parse(s, reviver)
31
44
  } catch {