icambron / twix.js

:hourglass::left_right_arrow: A date range plugin for moment.js
https://isaaccambron.com/twix.js/
MIT License
379 stars 54 forks source link

Array.from(moment(from).twix(to)).iterate("days")) == [] #104

Open tobybrad opened 6 years ago

tobybrad commented 6 years ago

Hey,

I now know about toArray() but I got caught out today when iterate() didn't return an iterable suitable for Array.from()

@home:~/src$ cat test.js
const moment = require("moment")
const twix = require("./lib/twix.min.js")

let start = new Date("2018-01-01");
let end = new Date("2018-06-01");

console.log(moment(start).twix(end).toArray("days").length);
console.log(Array.from(moment(start).twix(end).iterate("days")).length);
console.log(Array.from(moment(start).twix(end).iterate("days")));
@home:~/src$ node test.js
152
0
[]

Array.from() should expand the iterable:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

so I guess the object returned from iterate is not truly 'iterable' in the way required by the spec.

Nice library though.. saved me a bunch of time elsewhere so I'm still net positive on time-savings thanks to twix. :-)

icambron commented 6 years ago

Yeah, the iterable does not actually implement the Iterable protocol (and predates its implementation, IIRC). It could though! I'd take a PR making that true.