/
Formatter.ts
31 lines (30 loc) 路 905 Bytes
/
Formatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { ReadOnlyArray } from "@lou.codes/types";
/**
* Formatter function that takes a string or a tagged template.
*
* @category Common
* @remarks
* Type to represent a function that works both directly on a string or as a
* tagged template function.
* @example
* ```typescript
* const formatter: Formatter = (input, ...expressions) =>
* typeof input === "string"
* ? input
* : input
* .flatMap((string, index) => [string, expressions[index] ?? ""])
* .join("");
*
* formatter("Lou"); // "Lou"
* formatter`Lou`; // "Lou"
* ```
* @see [Tagged templates](https://mdn.io/Tagged%20templates)
*
* @param input Array of strings for template strings or a single string,
* followed by an array of expressions (if any).
* @returns A plain string.
*/
export type Formatter = (
templateStrings: TemplateStringsArray | string,
...expressions: ReadOnlyArray
) => string;