Skip to content

Commit 858c948

Browse files
committed
fix: percent transform not transforming
1 parent 03c5c75 commit 858c948

3 files changed

Lines changed: 28 additions & 17 deletions

File tree

.changeset/fifty-news-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": patch
3+
---
4+
5+
Fix broken percent transform (`tr.transforms.percent`)

docs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,8 @@ The function should return the transformed string synchronously and will be call
26712671
The generic `TTrKey` can be used to enforce type safety for the keys.
26722672
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.
26732673
2674+
For more examples, check out the predefined transforms in the file [`lib/translation.ts`](./lib/translation.ts)
2675+
26742676
<details><summary><b>Example - click to view</b></summary>
26752677
26762678
```ts
@@ -2770,6 +2772,8 @@ Currently available transforms:
27702772
| `templateLiteral` | `${key}` | Keyed / Positional |
27712773
| `percent` | `%n` | Positional |
27722774

2775+
For more examples, check out the predefined transforms in the file [`lib/translation.ts`](./lib/translation.ts)
2776+
27732777
<details><summary><b>Example - click to view</b></summary>
27742778

27752779
```ts

lib/translation.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -401,31 +401,33 @@ const templateLiteralTransform: TransformTuple<string> = [
401401

402402
/**
403403
* This transform will replace `%n` placeholders with the value of the passed arguments.
404-
* The `%n` placeholders are 1-indexed, meaning `%1` will be replaced by the first argument, `%2` by the second, and so on.
404+
* 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.
405405
* Objects will be stringified via `String()` before being inserted.
406406
*
407407
* @example ```ts
408-
* tr.addTranslations("en", {
409-
* "greeting": "Hello, %1!\nYou have %2 notifications.",
410-
* });
411-
* tr.addTransform(tr.transforms.percent);
412-
*
413-
* const t = tr.use("en");
414-
*
415-
* // arguments are inserted in the order they're passed:
416-
* t("greeting", "John", 5); // "Hello, John!\nYou have 5 notifications."
417-
*
418-
* // when a value isn't found, the placeholder will be left as-is:
419-
* t("greeting", "John"); // "Hello, John!\nYou have %2 notifications."
420-
* ```
421-
*/
408+
* tr.addTranslations("en", {
409+
* "greeting": "Hello, %1!\nYou have %2 notifications.",
410+
* });
411+
* tr.addTransform(tr.transforms.percent);
412+
*
413+
* const t = tr.use("en");
414+
*
415+
* // arguments are inserted in the order they're passed:
416+
* t("greeting", "John", 5); // "Hello, John!\nYou have 5 notifications."
417+
*
418+
* // when a value isn't found, the placeholder will be left as-is:
419+
* t("greeting", "John"); // "Hello, John!\nYou have %2 notifications."
420+
* ```
421+
*/
422422
const percentTransform: TransformTuple<string> = [
423-
/\$\{([a-zA-Z0-9$_-]+)\}/gm,
423+
/%(\d+)/gm,
424424
({ matches, trArgs, trValue }) => {
425425
let str = String(trValue);
426426

427427
for(const match of matches) {
428-
const repl = match[1] !== undefined ? (trArgs[0] as Record<string, string>)[match[1]] : undefined;
428+
const repl = match[1] !== undefined
429+
? (trArgs as Stringifiable[])?.[Number(match[1]) - 1]
430+
: undefined;
429431
if(typeof repl !== "undefined")
430432
str = str.replace(match[0], String(repl));
431433
}

0 commit comments

Comments
 (0)