Kaynağa Gözat

feat: insertValues()

Sven 1 yıl önce
ebeveyn
işleme
142c5e2
2 değiştirilmiş dosya ile 18 ekleme ve 0 silme
  1. 5 0
      .changeset/happy-pens-agree.md
  2. 13 0
      lib/misc.ts

+ 5 - 0
.changeset/happy-pens-agree.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Added function insertValues() to insert values into a string with placeholders

+ 13 - 0
lib/misc.ts

@@ -52,3 +52,16 @@ export async function fetchAdvanced(url: string, options: FetchAdvancedOpts = {}
   clearTimeout(id);
   return res;
 }
+
+/**
+ * Inserts the passed values into a string at the respective placeholders.  
+ * The placeholder format is `%n`, where `n` is the 1-indexed argument number.
+ * @param str The string to insert the values into
+ * @param values The values to insert, in order, starting at `%1`
+ */
+export function insertValues(str: string, ...values: Stringifiable[]) {
+  return str.replace(/%\d/gm, (match) => {
+    const argIndex = Number(match.substring(1)) - 1;
+    return (values[argIndex] ?? match)?.toString();
+  });
+}