@@ -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+ */
422422const percentTransform : TransformTuple < string > = [
423- / \$ \{ ( [ a - z A - Z 0 - 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