@naturalcycles/js-lib 15.106.0 → 15.107.0

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.
@@ -309,13 +309,7 @@ export class LocalDate {
309
309
  }
310
310
  else {
311
311
  let monLen = localDate.getMonthLength(year, month);
312
- if (unit !== 'day') {
313
- if (day > monLen) {
314
- // Case of 2022-05-31 plus 1 month should be 2022-06-30, not 31
315
- day = monLen;
316
- }
317
- }
318
- else {
312
+ if (unit === 'day') {
319
313
  while (day > monLen) {
320
314
  day -= monLen;
321
315
  month += 1;
@@ -326,6 +320,12 @@ export class LocalDate {
326
320
  monLen = localDate.getMonthLength(year, month);
327
321
  }
328
322
  }
323
+ else {
324
+ if (day > monLen) {
325
+ // Case of 2022-05-31 plus 1 month should be 2022-06-30, not 31
326
+ day = monLen;
327
+ }
328
+ }
329
329
  }
330
330
  if (opt.mutate) {
331
331
  this.year = year;
@@ -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
  }
@@ -24,14 +24,14 @@ function serializer(replacer, cycleReplacer) {
24
24
  return function (key, value) {
25
25
  if (stack.length > 0) {
26
26
  const thisPos = stack.indexOf(this);
27
- if (thisPos !== -1) {
28
- stack.splice(thisPos + 1);
29
- keys.splice(thisPos, Infinity, key);
30
- }
31
- else {
27
+ if (thisPos === -1) {
32
28
  stack.push(this);
33
29
  keys.push(key);
34
30
  }
31
+ else {
32
+ stack.splice(thisPos + 1);
33
+ keys.splice(thisPos, Infinity, key);
34
+ }
35
35
  if (stack.includes(value)) {
36
36
  value = cycleReplacer.call(this, key, value);
37
37
  }
@@ -64,19 +64,19 @@ export function _truncateMiddle(s, maxLen, omission = '...') {
64
64
  // These functions are modeled after Kotlin's String API
65
65
  export function _substringBefore(s, delimiter) {
66
66
  const pos = s.indexOf(delimiter);
67
- return s.slice(0, pos !== -1 ? pos : undefined);
67
+ return s.slice(0, pos === -1 ? undefined : pos);
68
68
  }
69
69
  export function _substringBeforeLast(s, delimiter) {
70
70
  const pos = s.lastIndexOf(delimiter);
71
- return s.slice(0, pos !== -1 ? pos : undefined);
71
+ return s.slice(0, pos === -1 ? undefined : pos);
72
72
  }
73
73
  export function _substringAfter(s, delimiter) {
74
74
  const pos = s.indexOf(delimiter);
75
- return pos !== -1 ? s.slice(pos + delimiter.length) : s;
75
+ return pos === -1 ? s : s.slice(pos + delimiter.length);
76
76
  }
77
77
  export function _substringAfterLast(s, delimiter) {
78
78
  const pos = s.lastIndexOf(delimiter);
79
- return pos !== -1 ? s.slice(pos + delimiter.length) : s;
79
+ return pos === -1 ? s : s.slice(pos + delimiter.length);
80
80
  }
81
81
  /**
82
82
  * Returns the substring between LAST `leftDelimiter` and then FIRST `rightDelimiter`.
package/dist/ua/ua.js CHANGED
@@ -71,7 +71,7 @@ export function uaParser(userAgent, extensions) {
71
71
  */
72
72
  export class UAParser {
73
73
  constructor(ua, extensions) {
74
- this.nav = typeof globalThis.window !== 'undefined' ? globalThis.navigator : undefined;
74
+ this.nav = typeof globalThis.window === 'undefined' ? undefined : globalThis.navigator;
75
75
  this.rgxMap = extensions ? extend(regexes, extensions) : regexes;
76
76
  const initialUa = ua || this.nav?.userAgent || '';
77
77
  // Whether we're parsing the environment's own UA - only then the
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.107.0",
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": "20.79.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "rolldown": "^1",
@@ -373,12 +373,7 @@ export class LocalDate {
373
373
  } else {
374
374
  let monLen = localDate.getMonthLength(year, month)
375
375
 
376
- if (unit !== 'day') {
377
- if (day > monLen) {
378
- // Case of 2022-05-31 plus 1 month should be 2022-06-30, not 31
379
- day = monLen
380
- }
381
- } else {
376
+ if (unit === 'day') {
382
377
  while (day > monLen) {
383
378
  day -= monLen
384
379
  month += 1
@@ -389,6 +384,11 @@ export class LocalDate {
389
384
 
390
385
  monLen = localDate.getMonthLength(year, month)
391
386
  }
387
+ } else {
388
+ if (day > monLen) {
389
+ // Case of 2022-05-31 plus 1 month should be 2022-06-30, not 31
390
+ day = monLen
391
+ }
392
392
  }
393
393
  }
394
394
 
@@ -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
  }
@@ -32,12 +32,12 @@ function serializer(replacer?: Reviver, cycleReplacer?: Reviver): Reviver {
32
32
  return function (key, value) {
33
33
  if (stack.length > 0) {
34
34
  const thisPos = stack.indexOf(this)
35
- if (thisPos !== -1) {
36
- stack.splice(thisPos + 1)
37
- keys.splice(thisPos, Infinity, key)
38
- } else {
35
+ if (thisPos === -1) {
39
36
  stack.push(this)
40
37
  keys.push(key)
38
+ } else {
39
+ stack.splice(thisPos + 1)
40
+ keys.splice(thisPos, Infinity, key)
41
41
  }
42
42
 
43
43
  if (stack.includes(value)) {
@@ -70,22 +70,22 @@ export function _truncateMiddle(s: string, maxLen: number, omission = '...'): st
70
70
 
71
71
  export function _substringBefore(s: string, delimiter: string): string {
72
72
  const pos = s.indexOf(delimiter)
73
- return s.slice(0, pos !== -1 ? pos : undefined)
73
+ return s.slice(0, pos === -1 ? undefined : pos)
74
74
  }
75
75
 
76
76
  export function _substringBeforeLast(s: string, delimiter: string): string {
77
77
  const pos = s.lastIndexOf(delimiter)
78
- return s.slice(0, pos !== -1 ? pos : undefined)
78
+ return s.slice(0, pos === -1 ? undefined : pos)
79
79
  }
80
80
 
81
81
  export function _substringAfter(s: string, delimiter: string): string {
82
82
  const pos = s.indexOf(delimiter)
83
- return pos !== -1 ? s.slice(pos + delimiter.length) : s
83
+ return pos === -1 ? s : s.slice(pos + delimiter.length)
84
84
  }
85
85
 
86
86
  export function _substringAfterLast(s: string, delimiter: string): string {
87
87
  const pos = s.lastIndexOf(delimiter)
88
- return pos !== -1 ? s.slice(pos + delimiter.length) : s
88
+ return pos === -1 ? s : s.slice(pos + delimiter.length)
89
89
  }
90
90
 
91
91
  /**
package/src/ua/ua.ts CHANGED
@@ -180,7 +180,7 @@ export function uaParser(userAgent?: string, extensions?: UAParserExtensions): U
180
180
  */
181
181
  export class UAParser {
182
182
  constructor(ua?: string, extensions?: UAParserExtensions) {
183
- this.nav = typeof globalThis.window !== 'undefined' ? globalThis.navigator : undefined
183
+ this.nav = typeof globalThis.window === 'undefined' ? undefined : globalThis.navigator
184
184
  this.rgxMap = extensions ? extend(regexes, extensions) : regexes
185
185
  const initialUa = ua || this.nav?.userAgent || ''
186
186
  // Whether we're parsing the environment's own UA - only then the