@elgato/utils 0.4.0 → 0.4.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/README.md CHANGED
@@ -212,6 +212,18 @@ import { withResolvers } "@elgato/utils";
212
212
  const { promise, resolve, reject } = withResolvers<string>();
213
213
  ```
214
214
 
215
+ ## Strings
216
+
217
+ ### `format(format, ...args)`
218
+
219
+ Formats the specified string, replacing placeholders with their associated arguments.
220
+
221
+ ```js
222
+ import { format } from "@elgato/utils";
223
+
224
+ format("Hello {0}, from {1}", "world", "Elgato"); // Hello world, from Elgato
225
+ ```
226
+
215
227
  ## Timers
216
228
 
217
229
  ### `debounce(fn, delay)`
package/dist/index.d.ts CHANGED
@@ -6,5 +6,6 @@ export * from "./lazy.js";
6
6
  export * from "./objects.js";
7
7
  export * from "./parsers.js";
8
8
  export * from "./promises.js";
9
+ export * from "./strings.js";
9
10
  export * from "./timers.js";
10
11
  export * from "./types.js";
package/dist/index.js CHANGED
@@ -6,5 +6,6 @@ export * from "./lazy.js";
6
6
  export * from "./objects.js";
7
7
  export * from "./parsers.js";
8
8
  export * from "./promises.js";
9
+ export * from "./strings.js";
9
10
  export * from "./timers.js";
10
11
  export * from "./types.js";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Formats the specified string, replacing placeholders with their associated arguments.
3
+ * @example Formatting
4
+ * format("Hello {0}", "world") // "Hello world"
5
+ * @example Escaping braces
6
+ * format("Escaped {{0}}") // "Escaped {0}"
7
+ * @param format Source format string.
8
+ * @param args Arguments used to replace placeholders.
9
+ * @returns The formatted string.
10
+ */
11
+ export declare function format(format: string, ...args: unknown[]): string;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Formats the specified string, replacing placeholders with their associated arguments.
3
+ * @example Formatting
4
+ * format("Hello {0}", "world") // "Hello world"
5
+ * @example Escaping braces
6
+ * format("Escaped {{0}}") // "Escaped {0}"
7
+ * @param format Source format string.
8
+ * @param args Arguments used to replace placeholders.
9
+ * @returns The formatted string.
10
+ */
11
+ export function format(format, ...args) {
12
+ return (format
13
+ // Transform escaped braces, e.g. {{0}}, {{1}}, etc.
14
+ .replace(/{{(\d+)}}/g, (match, index) => {
15
+ return `\uE000${index}\uE001`;
16
+ })
17
+ // Replace placeholders, e.g. {0}, {1}, etc.
18
+ .replace(/{(\d+)}/g, (match, index) => {
19
+ const value = args.at(Number(index));
20
+ return value === undefined ? match : `${value}`;
21
+ })
22
+ // Restore escaped braces
23
+ .replace(/\uE000(\d+)\uE001/g, (match, index) => {
24
+ return `{${index}}`;
25
+ }));
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elgato/utils",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Utilities used throughout the Elgato ecosystem.",
5
5
  "type": "module",
6
6
  "files": [