@pdfme/common 6.2.4-dev.3 → 6.2.4-dev.6

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,4 +1,10 @@
1
1
  import type { Schema, SchemaPageArray } from './types.js';
2
+ /**
3
+ * False only for strings JSON.parse would reject, so parseData can skip the throw. Index scans, not
4
+ * regexes: V8 keeps the subject of the last successful match (RegExp.input), which would pin a large
5
+ * value after the call.
6
+ */
7
+ export declare const mayBeJson: (value: string) => boolean;
2
8
  /**
3
9
  * Resolve a read-only schema's render value.
4
10
  * Text `content` is an expression template. MVT `content` is a variable JSON
package/dist/index.js CHANGED
@@ -665,6 +665,50 @@ var checkGenerateProps = (data) => {
665
665
  //#endregion
666
666
  //#region src/expression.ts
667
667
  var expressionCache = /* @__PURE__ */ new Map();
668
+ var isJsonWhitespace = (ch) => ch === " " || ch === " " || ch === "\n" || ch === "\r";
669
+ var isDigit = (ch) => ch !== void 0 && ch >= "0" && ch <= "9";
670
+ var isJsonNumber = (value, start, end) => {
671
+ let i = start;
672
+ if (value[i] === "-") i++;
673
+ if (!isDigit(value[i])) return false;
674
+ if (value[i] === "0") i++;
675
+ else while (i < end && isDigit(value[i])) i++;
676
+ if (value[i] === ".") {
677
+ const digits = ++i;
678
+ while (i < end && isDigit(value[i])) i++;
679
+ if (i === digits) return false;
680
+ }
681
+ if (value[i] === "e" || value[i] === "E") {
682
+ i++;
683
+ if (value[i] === "+" || value[i] === "-") i++;
684
+ const digits = i;
685
+ while (i < end && isDigit(value[i])) i++;
686
+ if (i === digits) return false;
687
+ }
688
+ return i === end;
689
+ };
690
+ /**
691
+ * False only for strings JSON.parse would reject, so parseData can skip the throw. Index scans, not
692
+ * regexes: V8 keeps the subject of the last successful match (RegExp.input), which would pin a large
693
+ * value after the call.
694
+ */
695
+ var mayBeJson = (value) => {
696
+ let start = 0;
697
+ let end = value.length;
698
+ while (start < end && isJsonWhitespace(value[start])) start++;
699
+ while (end > start && isJsonWhitespace(value[end - 1])) end--;
700
+ const first = value[start];
701
+ if (first === "{") {
702
+ let i = start + 1;
703
+ while (i < end && isJsonWhitespace(value[i])) i++;
704
+ return value[i] === "\"" || value[i] === "}";
705
+ }
706
+ if (first === "[" || first === "\"") return true;
707
+ if (first === "t") return end - start === 4 && value.startsWith("true", start);
708
+ if (first === "f") return end - start === 5 && value.startsWith("false", start);
709
+ if (first === "n") return end - start === 4 && value.startsWith("null", start);
710
+ return isJsonNumber(value, start, end);
711
+ };
668
712
  /**
669
713
  * Parse each string value in `data` as JSON, falling back to the original
670
714
  * string on failure. Previously memoized via a module-level `parseDataCache`
@@ -674,10 +718,14 @@ var expressionCache = /* @__PURE__ */ new Map();
674
718
  * base64 (e.g. image schemas) or inputs containing base64 values. Every
675
719
  * unique inputs state pinned its own multi-MB key for the app lifetime.
676
720
  * Parsing is O(fields) and cheap; removing the cache is strictly a win.
721
+ *
722
+ * It is called once per placeholder field, over every field, so most strings are not JSON and
723
+ * a thrown SyntaxError each made it the bulk of a render. Strings that cannot be JSON skip
724
+ * JSON.parse (see mayBeJson); the result is the same.
677
725
  */
678
726
  var parseData = (data) => {
679
727
  return Object.fromEntries(Object.entries(data).map(([key, value]) => {
680
- if (typeof value === "string") try {
728
+ if (typeof value === "string" && mayBeJson(value)) try {
681
729
  return [key, JSON.parse(value)];
682
730
  } catch {
683
731
  return [key, value];