@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 +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/strings.d.ts +11 -0
- package/dist/strings.js +26 -0
- package/package.json +1 -1
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
package/dist/index.js
CHANGED
|
@@ -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;
|
package/dist/strings.js
ADDED
|
@@ -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
|
+
}
|