tc39 / proposal-iterator.range

A proposal for ECMAScript to add a built-in Iterator.range()

Home Page:https://tc39.es/proposal-iterator.range/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get start and end of range quickly for Intl.NumberFormat

sffc opened this issue · comments

It would be nice if you could do,

const nf = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
nf.format(Number.range(1, 5));  // "$1-5"  (or "$1-4" ??)

// or,
Number.range(1, 5).toLocaleString();  // "1-5"  (or "1-4" ??)

To do this, we'd need to efficiently get both the beginning and end of the range. To do this, the .range() functions could return a type that has a [Symbol.iterator] getter but also getters like .start and .end, for example.

It's unclear whether "1-5" is inclusive range or exclusive range. I guess most people would treat it as inclusive. But current proposed iteration semantic is exclusive.

I’d expect the same as slice: [1, 5)

The exclusive semantics is inspired by Python style

@sffc Hi, now the return value of range has a getter for from and to. Is that enough?

And if Intl.NumberFormat can read the internal slot directly to format the range?

The .from and .to getters are sufficient, thanks. If #17 or #22 is adopted, I imagine those getters might be moved to new types.