abozhilov / es-iter Goto Github PK
View Code? Open in Web Editor NEWItertools for JavaScript
License: MIT License
Itertools for JavaScript
License: MIT License
How about the following changes?
// Current
static range (start, end, step) {
return new Iter(function* () {
let s = toInteger(start),
e = toInteger(end);
if (typeof end == 'undefined') {
e = s;
s = 0;
}
let k = toInteger(step) || (s < e ? 1 : -1)
if (k > 0) {
while (s < e) {
yield s;
s += k;
}
}
else {
while (s > e) {
yield s;
s += k;
}
}
});
}
// Proposed
static * range(start, end, step=undefined) {
// step = undefined signals that `step` is optional
if (start === undefined || end === undefined) {
throw new Error('Need at least two arguments');
}
start = toInteger(start);
end = toInteger(end);
step = toInteger(step) || (start < end ? 1 : -1);
if (step >= 0) {
while (start < end) {
yield start;
start += step;
}
}
else {
while (start > end) {
yield start;
start += step;
}
}
}
Provide polymorphic collect()
instead of bunch of the fns (toArray
, toString
, toObject
etc).
Api (can easily be described in ts or flow):
new Iter(...).collect(Array);
new Iter(...).collect(String);
new Iter(...).collect(Map);
new Iter(...).collect(Set);
Implementation:
collect(Class) {
const iter = Iter.getIterator(this);
// Array and subclasses.
if (Class === Array || Class.prototype instanceof Array) {
return Class.from(iter);
}
if (Class === Object) {
const result = {};
for (const [key, value] of iter) {
result[key] = value;
}
return result;
}
if (Class === String) {
return this.collect(Array).join('');
}
// Map, Set, WeakMap, WeakSet and custom types.
return new Class(iter);
}
For documenting, I prefer:
foo(start, end, step?)
Instead of:
foo(start, end[, step])
This is what I’d do myself, but, incidentally, it is also what TypeScript and Flow use. I generally find their notation easy to understand.
Have you considered giving range()
the following signature?
Iter.range(start=0, end=Infinity, step=1)
That way, you wouldn’t need count()
, right?
I also find it more intuitive if Iter.range(10)
starts at 10, instead of it being the upper boundary (similarly to how Array.prototype.slice()
etc. work).
I saw your lib published on npm, and was going to use it but sadly it requires transpilation because of its usage of modules. You should really publish a node-compatible version on npm. My advice: transpile it with rollup in a pre-publish hook, change the main
entry and set esnext:main
to src/Iter.js
in the package.json
(doc).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.