(str: string, ...args: T)
| 16 | * @param args Arguments to replace "{N}" instances with. |
| 17 | */ |
| 18 | export function formatString<T extends Arg[]>(str: string, ...args: T): any[] | string { |
| 19 | let onlyStrings = true; |
| 20 | const map = splitVariableString(str, opts).map((val, index) => { |
| 21 | if (index % 2 === 1) { |
| 22 | const i = parseInt(val, 10); |
| 23 | if (i >= 0 && i < args.length) { |
| 24 | const arg = args[i]; |
| 25 | if (React.isValidElement(arg)) { |
| 26 | onlyStrings = false; |
| 27 | return React.Children.toArray(arg).map(component => Object.assign({ key: index.toString() }, component)); |
| 28 | } |
| 29 | return arg; |
| 30 | } else { throw new Error(`Failed to format string. Index out of bounds (index: "${i}", string: "${str}").`); } |
| 31 | } else { |
| 32 | return val; |
| 33 | } |
| 34 | }); |
| 35 | return onlyStrings |
| 36 | ? map.join('') |
| 37 | : map; |
| 38 | } |
no test coverage detected