Hello everyone. I've searched for the mailing list, there are some discussions add `new Range` or `a ... b` into the language but not become a formal proposal.
I've made a full demo(polyfill) these days. Anyone interested? Preview: `for (let i of Number.range(1, 5)) console.log(i)` And there is an extended version that let range support number range check `if ( x in Number.range(0, 100) )`. About usage, test case, and demo of the extended version is posted on gist. https://gist.github.com/Jack-Works/aa8d4c5dbd0e8cdd415783de8d922451 ( This proposal can also/or add an new operator instead. `Number.range(x, y)` => `x...y` `Number.range(x, y, z)` => `(x...y).step(z)` or `x...z->y` or something else, and other behavior keep same with the Number.range implmentation in the gist ) Here is the base version of the proposal ```js Number.range = function*(from, to, step) { if ( typeof from !== 'number' && typeof from !== 'bigint' && (typeof to !== 'number' && typeof to !== 'bigint') && (typeof step !== 'number' && typeof step !== 'bigint' && typeof step !== 'undefined') ) throw new TypeError('All parameters must be a number or a BigInt') if (typeof from === 'bigint' && typeof step === 'undefined') step = 1n else if (typeof from === 'number' && typeof step === 'undefined') step = 1 if (typeof from !== typeof to || typeof from !== typeof step) throw new TypeError('Type of all parameters must be the same') if (typeof from === "number" && Number.isNaN(from) || Number.isNaN(to) || Number.isNaN(step)) return; // Quit early with no value yield // Math.abs does not support BigInt. const abs = x => (x >= (typeof x === 'bigint' ? 0n : 0) ? x : -x) const increase = to > from // Ignore the symbol if (increase) step = abs(step) else step = -abs(step) let count = typeof from === 'bigint' ? 1n : 1 let now = from while (increase ? !(now >= to) : !(to >= now)) { yield now now = from + step * count count++ } } ```
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

