@xelqun/forms 0.3.0 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xelqun/forms",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Vue renderer for Xelqun Forms: PHP-defined, schema-driven forms for Laravel + Inertia.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -9,9 +9,16 @@ export function translate(
9
9
  text: string,
10
10
  replacements: Replacements = {},
11
11
  ): string {
12
- return Object.entries(replacements).reduce(
13
- // A function, so a "$&" in a file name isn't read as a pattern.
14
- (line, [name, value]) => line.replaceAll(`:${name}`, () => String(value)),
15
- messages?.[text] ?? text,
12
+ const line = messages?.[text] ?? text;
13
+ const names = Object.keys(replacements).sort((first, second) => second.length - first.length);
14
+
15
+ if (names.length === 0) {
16
+ return line;
17
+ }
18
+
19
+ // One pass, longest name first, so :to can't match inside :total and a value
20
+ // holding ":name" isn't filled in again. A function, so "$&" isn't a pattern.
21
+ return line.replace(new RegExp(`:(${names.join('|')})`, 'g'), (_, name: string) =>
22
+ String(replacements[name]),
16
23
  );
17
24
  }