Open
Description
This would be immensely useful for things like virtual DOM lists and polyfilling whatwg/html#4630 without a ton of pain.
I'm thinking of an API like this:
interface ListFormat {
// Existing
formatToParts(list: string[]): ListFormatPart<string>
// New
formatToElements<T>(list: Array<{value: T, part: string}>): ListFormatPart<T>
}
type ListFormatPart<T> =
| {type: "element", value: T}
| {type: "literal", value: string}
The key functional difference is that formatToElements
, while it coerces its arguments, it does not return the coerced results.
Polyfilling this is very awkward and only works because Unicode explicitly states that previous parts must come before next parts:
Intl.ListFormat.prototype.formatToElements = function (list) {
const parts = []
const values = []
for (const {part, value} of list) {
parts.push(part)
values.push(value)
}
const result = this.formatToParts(parts)
let index = 0
for (const part of result) {
if (part.type === "element") {
part.value = values[index++]
}
}
return result
}
Metadata
Metadata
Assignees
Type
Projects
Status