Skip to content

Add a variant of ListFormat#formatToParts that returns the source objects #1004

Open
@dead-claudia

Description

@dead-claudia

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

No one assigned

    Labels

    c: textComponent: case mapping, collation, propertiesenhancements: discussStatus: TG2 must discuss to move forward

    Type

    No type

    Projects

    Status

    Other Issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions