@naturalcycles/js-lib 14.70.1 → 14.70.2

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.
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._jsonParseIfPossible = void 0;
4
+ // const possibleJsonStartTokens = ['{', '[', '"']
5
+ const DETECT_JSON = /^\s*[{["\-\d]/;
4
6
  /**
5
7
  * Attempts to parse object as JSON.
6
8
  * Returns original object if JSON parse failed (silently).
7
9
  */
8
10
  function _jsonParseIfPossible(obj, reviver) {
9
- if (typeof obj === 'string' && obj) {
11
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
12
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
10
13
  try {
11
14
  return JSON.parse(obj, reviver);
12
15
  }
@@ -1,9 +1,12 @@
1
+ // const possibleJsonStartTokens = ['{', '[', '"']
2
+ const DETECT_JSON = /^\s*[{["\-\d]/;
1
3
  /**
2
4
  * Attempts to parse object as JSON.
3
5
  * Returns original object if JSON parse failed (silently).
4
6
  */
5
7
  export function _jsonParseIfPossible(obj, reviver) {
6
- if (typeof obj === 'string' && obj) {
8
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
9
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
7
10
  try {
8
11
  return JSON.parse(obj, reviver);
9
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.70.1",
3
+ "version": "14.70.2",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,3 +1,6 @@
1
+ // const possibleJsonStartTokens = ['{', '[', '"']
2
+ const DETECT_JSON = /^\s*[{["\-\d]/
3
+
1
4
  /**
2
5
  * Attempts to parse object as JSON.
3
6
  * Returns original object if JSON parse failed (silently).
@@ -6,7 +9,8 @@ export function _jsonParseIfPossible(
6
9
  obj: any,
7
10
  reviver?: (this: any, key: string, value: any) => any,
8
11
  ): any {
9
- if (typeof obj === 'string' && obj) {
12
+ // Optimization: only try to parse if it looks like JSON: starts with a json possible character
13
+ if (typeof obj === 'string' && obj && DETECT_JSON.test(obj)) {
10
14
  try {
11
15
  return JSON.parse(obj, reviver)
12
16
  } catch {}