Преглед изворни кода

fix: percent transform not transforming

Sv443 пре 3 месеци
родитељ
комит
858c948cea
3 измењених фајлова са 28 додато и 17 уклоњено
  1. 5 0
      .changeset/fifty-news-draw.md
  2. 4 0
      docs.md
  3. 19 17
      lib/translation.ts

+ 5 - 0
.changeset/fifty-news-draw.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": patch
+---
+
+Fix broken percent transform (`tr.transforms.percent`)

+ 4 - 0
docs.md

@@ -2671,6 +2671,8 @@ The function should return the transformed string synchronously and will be call
 The generic `TTrKey` can be used to enforce type safety for the keys.  
 You can pass the result of the generic type [`TrKeys`](#trkeys) to easily generate a union type of all keys in the given translation object.  
   
+For more examples, check out the predefined transforms in the file [`lib/translation.ts`](./lib/translation.ts)
+  
 <details><summary><b>Example - click to view</b></summary>
 
 ```ts
@@ -2770,6 +2772,8 @@ Currently available transforms:
 | `templateLiteral` | `${key}` | Keyed / Positional |
 | `percent` | `%n` | Positional |
 
+For more examples, check out the predefined transforms in the file [`lib/translation.ts`](./lib/translation.ts)
+
 <details><summary><b>Example - click to view</b></summary>
 
 ```ts

+ 19 - 17
lib/translation.ts

@@ -401,31 +401,33 @@ const templateLiteralTransform: TransformTuple<string> = [
 
 /**
  * This transform will replace `%n` placeholders with the value of the passed arguments.  
- * The `%n` placeholders are 1-indexed, meaning `%1` will be replaced by the first argument, `%2` by the second, and so on.  
+ * The `%n` placeholders are 1-indexed, meaning `%1` will be replaced by the first argument (index 0), `%2` by the second (index 1), and so on.  
  * Objects will be stringified via `String()` before being inserted.  
  *   
  * @example ```ts
-* tr.addTranslations("en", {
-*  "greeting": "Hello, %1!\nYou have %2 notifications.",
-* });
-* tr.addTransform(tr.transforms.percent);
-* 
-* const t = tr.use("en");
-* 
-* // arguments are inserted in the order they're passed:
-* t("greeting", "John", 5); // "Hello, John!\nYou have 5 notifications."
-* 
-* // when a value isn't found, the placeholder will be left as-is:
-* t("greeting", "John"); // "Hello, John!\nYou have %2 notifications."
-* ```
-*/
+ * tr.addTranslations("en", {
+ *  "greeting": "Hello, %1!\nYou have %2 notifications.",
+ * });
+ * tr.addTransform(tr.transforms.percent);
+ * 
+ * const t = tr.use("en");
+ * 
+ * // arguments are inserted in the order they're passed:
+ * t("greeting", "John", 5); // "Hello, John!\nYou have 5 notifications."
+ * 
+ * // when a value isn't found, the placeholder will be left as-is:
+ * t("greeting", "John"); // "Hello, John!\nYou have %2 notifications."
+ * ```
+ */
 const percentTransform: TransformTuple<string> = [
-  /\$\{([a-zA-Z0-9$_-]+)\}/gm,
+  /%(\d+)/gm,
   ({ matches, trArgs, trValue }) => {
     let str = String(trValue);
 
     for(const match of matches) {
-      const repl = match[1] !== undefined ? (trArgs[0] as Record<string, string>)[match[1]] : undefined;
+      const repl = match[1] !== undefined
+        ? (trArgs as Stringifiable[])?.[Number(match[1]) - 1]
+        : undefined;
       if(typeof repl !== "undefined")
         str = str.replace(match[0], String(repl));
     }