@naturalcycles/js-lib 15.105.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.
@@ -195,26 +195,28 @@ export declare function _mapToObject<T, V>(array: Iterable<T>, mapper: (item: T)
195
195
  * Based on: https://stackoverflow.com/a/12646864/4919972
196
196
  */
197
197
  export declare function _shuffle<T>(array: T[], opt?: MutateOptions): T[];
198
- export declare function _firstLast<T>(array: readonly T[]): [first: T, last: T];
199
- export declare function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last: T] | undefined;
198
+ export declare function _firstLast<T>(array: ArrayLike<T>): [first: T, last: T];
199
+ export declare function _firstLastOrUndefined<T>(array: ArrayLike<T>): [first: T, last: T] | undefined;
200
200
  /**
201
201
  * Returns last item of non-empty array.
202
202
  * Throws if array is empty.
203
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
203
204
  */
204
- export declare function _last<T>(array: readonly T[]): T;
205
+ export declare function _last<T>(array: ArrayLike<T>): T;
205
206
  /**
206
207
  * Returns last item of the array (or undefined if array is empty).
207
208
  */
208
- export declare function _lastOrUndefined<T>(array: readonly T[]): T | undefined;
209
+ export declare function _lastOrUndefined<T>(array: ArrayLike<T>): T | undefined;
209
210
  /**
210
211
  * Returns the first item of non-empty array.
211
212
  * Throws if array is empty.
213
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
212
214
  */
213
- export declare function _first<T>(array: readonly T[]): T;
215
+ export declare function _first<T>(array: ArrayLike<T>): T;
214
216
  /**
215
217
  * Returns first item of the array (or undefined if array is empty).
216
218
  */
217
- export declare function _firstOrUndefined<T>(array: readonly T[]): T | undefined;
219
+ export declare function _firstOrUndefined<T>(array: ArrayLike<T>): T | undefined;
218
220
  /**
219
221
  * Type guard that narrows an array to NonEmptyArray (array with at least one element).
220
222
  * Readonly arrays are narrowed to `Readonly<NonEmptyArray<T>>`.
@@ -378,6 +378,7 @@ export function _firstLastOrUndefined(array) {
378
378
  /**
379
379
  * Returns last item of non-empty array.
380
380
  * Throws if array is empty.
381
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
381
382
  */
382
383
  export function _last(array) {
383
384
  _assert(array.length, '_last called on empty array');
@@ -392,6 +393,7 @@ export function _lastOrUndefined(array) {
392
393
  /**
393
394
  * Returns the first item of non-empty array.
394
395
  * Throws if array is empty.
396
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
395
397
  */
396
398
  export function _first(array) {
397
399
  _assert(array.length, '_first called on empty array');
@@ -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.105.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.73.0",
17
+ "@naturalcycles/dev-lib": "0.0.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "rolldown": "^1",
@@ -432,12 +432,12 @@ export function _shuffle<T>(array: T[], opt: MutateOptions = {}): T[] {
432
432
  return a
433
433
  }
434
434
 
435
- export function _firstLast<T>(array: readonly T[]): [first: T, last: T] {
435
+ export function _firstLast<T>(array: ArrayLike<T>): [first: T, last: T] {
436
436
  _assert(array.length, '_firstLast called on empty array')
437
437
  return [array[0]!, array[array.length - 1]!]
438
438
  }
439
439
 
440
- export function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last: T] | undefined {
440
+ export function _firstLastOrUndefined<T>(array: ArrayLike<T>): [first: T, last: T] | undefined {
441
441
  if (!array.length) return
442
442
  return [array[0]!, array[array.length - 1]!]
443
443
  }
@@ -445,8 +445,9 @@ export function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last:
445
445
  /**
446
446
  * Returns last item of non-empty array.
447
447
  * Throws if array is empty.
448
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
448
449
  */
449
- export function _last<T>(array: readonly T[]): T {
450
+ export function _last<T>(array: ArrayLike<T>): T {
450
451
  _assert(array.length, '_last called on empty array')
451
452
  return array[array.length - 1]!
452
453
  }
@@ -454,15 +455,16 @@ export function _last<T>(array: readonly T[]): T {
454
455
  /**
455
456
  * Returns last item of the array (or undefined if array is empty).
456
457
  */
457
- export function _lastOrUndefined<T>(array: readonly T[]): T | undefined {
458
+ export function _lastOrUndefined<T>(array: ArrayLike<T>): T | undefined {
458
459
  return array[array.length - 1]
459
460
  }
460
461
 
461
462
  /**
462
463
  * Returns the first item of non-empty array.
463
464
  * Throws if array is empty.
465
+ * Accepts any array-like value, e.g. a Uint8Array, a NodeList or a string.
464
466
  */
465
- export function _first<T>(array: readonly T[]): T {
467
+ export function _first<T>(array: ArrayLike<T>): T {
466
468
  _assert(array.length, '_first called on empty array')
467
469
  return array[0]!
468
470
  }
@@ -470,7 +472,7 @@ export function _first<T>(array: readonly T[]): T {
470
472
  /**
471
473
  * Returns first item of the array (or undefined if array is empty).
472
474
  */
473
- export function _firstOrUndefined<T>(array: readonly T[]): T | undefined {
475
+ export function _firstOrUndefined<T>(array: ArrayLike<T>): T | undefined {
474
476
  return array[0]
475
477
  }
476
478
 
@@ -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
  }