@naturalcycles/js-lib 15.106.0 → 15.106.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.
@@ -18,5 +18,9 @@ export interface PupaOptions {
18
18
  }
19
19
  /**
20
20
  * API: https://github.com/sindresorhus/pupa
21
+ *
22
+ * Differs from upstream: both brace styles are substituted in a single pass, so a substituted value
23
+ * is never scanned for placeholders. Upstream runs the `{key}` pass over the output of the
24
+ * `{{key}}` pass, which inserts a value unescaped when a double-braced value contains `{key}`.
21
25
  */
22
26
  export declare function pupa(template: string, data: readonly any[] | AnyObject, opt?: PupaOptions): string;
@@ -19,8 +19,15 @@ export class MissingValueError extends Error {
19
19
  this.name = 'MissingValueError';
20
20
  }
21
21
  }
22
+ // The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
23
+ // Group 1 is the key of `{{key}}`, group 2 is the key of `{key}`.
24
+ const PLACEHOLDER_REGEX = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}|{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi;
22
25
  /**
23
26
  * API: https://github.com/sindresorhus/pupa
27
+ *
28
+ * Differs from upstream: both brace styles are substituted in a single pass, so a substituted value
29
+ * is never scanned for placeholders. Upstream runs the `{key}` pass over the output of the
30
+ * `{{key}}` pass, which inserts a value unescaped when a double-braced value contains `{key}`.
24
31
  */
25
32
  export function pupa(template, data, opt = {}) {
26
33
  if (typeof template !== 'string') {
@@ -30,7 +37,8 @@ export function pupa(template, data, opt = {}) {
30
37
  throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
31
38
  }
32
39
  const { ignoreMissing = false, transform = ({ value }) => value } = opt;
33
- const replace = (placeholder, key) => {
40
+ return template.replaceAll(PLACEHOLDER_REGEX, (placeholder, escapedKey, rawKey) => {
41
+ const key = escapedKey ?? rawKey;
34
42
  let value = data;
35
43
  for (const property of key.split('.')) {
36
44
  value = value ? value[property] : undefined;
@@ -42,14 +50,7 @@ export function pupa(template, data, opt = {}) {
42
50
  }
43
51
  throw new MissingValueError(key);
44
52
  }
45
- return String(transformedValue);
46
- };
47
- const composeHtmlEscape = (replacer) => (...args) => htmlEscape(replacer(...args));
48
- // The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
49
- const doubleBraceRegex = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}/gi;
50
- if (doubleBraceRegex.test(template)) {
51
- template = template.replaceAll(doubleBraceRegex, composeHtmlEscape(replace));
52
- }
53
- const braceRegex = /{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi;
54
- return template.replaceAll(braceRegex, replace);
53
+ const stringValue = String(transformedValue);
54
+ return escapedKey === undefined ? stringValue : htmlEscape(stringValue);
55
+ });
55
56
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.106.0",
4
+ "version": "15.106.1",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -14,7 +14,7 @@
14
14
  }
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "20.78.0",
17
+ "@naturalcycles/dev-lib": "0.0.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "rolldown": "^1",
@@ -33,8 +33,17 @@ export interface PupaOptions {
33
33
  transform?: (data: { value: any; key: string }) => unknown
34
34
  }
35
35
 
36
+ // The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
37
+ // Group 1 is the key of `{{key}}`, group 2 is the key of `{key}`.
38
+ const PLACEHOLDER_REGEX =
39
+ /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}|{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi
40
+
36
41
  /**
37
42
  * API: https://github.com/sindresorhus/pupa
43
+ *
44
+ * Differs from upstream: both brace styles are substituted in a single pass, so a substituted value
45
+ * is never scanned for placeholders. Upstream runs the `{key}` pass over the output of the
46
+ * `{{key}}` pass, which inserts a value unescaped when a double-braced value contains `{key}`.
38
47
  */
39
48
  export function pupa(
40
49
  template: string,
@@ -53,37 +62,26 @@ export function pupa(
53
62
 
54
63
  const { ignoreMissing = false, transform = ({ value }) => value } = opt
55
64
 
56
- const replace = (placeholder: string, key: string): string => {
57
- let value = data
58
- for (const property of key.split('.')) {
59
- value = value ? (value as AnyObject)[property] : undefined
60
- }
61
-
62
- const transformedValue = transform({ value, key })
63
- if (transformedValue === undefined) {
64
- if (ignoreMissing) {
65
- return placeholder
65
+ return template.replaceAll(
66
+ PLACEHOLDER_REGEX,
67
+ (placeholder: string, escapedKey: string | undefined, rawKey: string | undefined) => {
68
+ const key = escapedKey ?? rawKey!
69
+ let value = data
70
+ for (const property of key.split('.')) {
71
+ value = value ? (value as AnyObject)[property] : undefined
66
72
  }
67
73
 
68
- throw new MissingValueError(key)
69
- }
70
-
71
- return String(transformedValue)
72
- }
73
-
74
- const composeHtmlEscape =
75
- (replacer: any) =>
76
- (...args: any[]) =>
77
- htmlEscape(replacer(...args))
74
+ const transformedValue = transform({ value, key })
75
+ if (transformedValue === undefined) {
76
+ if (ignoreMissing) {
77
+ return placeholder
78
+ }
78
79
 
79
- // The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
80
- const doubleBraceRegex = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}/gi
81
-
82
- if (doubleBraceRegex.test(template)) {
83
- template = template.replaceAll(doubleBraceRegex, composeHtmlEscape(replace))
84
- }
85
-
86
- const braceRegex = /{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi
80
+ throw new MissingValueError(key)
81
+ }
87
82
 
88
- return template.replaceAll(braceRegex, replace)
83
+ const stringValue = String(transformedValue)
84
+ return escapedKey === undefined ? stringValue : htmlEscape(stringValue)
85
+ },
86
+ )
89
87
  }