inukshuk/edtf.js

Add variants to .between() and .until()

Crissov opened this issue · 5 comments

[...edtf('2016-05').between('2016-07')]
#-> [2016-06]

[...edtf('2016-05').until('2016-07')]
#-> [2016-05, 2016-06, 2016-07]

.between() and .until() return a closed/exclusive and and open/inclusive interval, respectively. Maybe it would be useful to add two methods that return a half-open interval:

#-> [2016-05, 2016-06]

#-> [2016-06, 2016-07]

Obvious possible additions in English are .from(), .since(), .through(), .till(),.unto(), .to(), .by(), unless a name is already used or reserved. As a non-native speaker, I’ll refrain from suggesting exact semantics for these.

While .between() should work no matter the temporal order of EDTFs, .until() should arguably not.

[...edtf('2016-07').between('2016-06')]
#-> [2016-06]

[...edtf('2016-07').until('2016-05')]
#-> null

I absolutely agree that such methods would be useful and I'd be happy to implement them, but I would appreciate help in specifying the exact semantics. The names between and until were not picked after long deliberation so I'd be open to changing them, if necessary, for a more consistently named API.

Like I said, I’m not the best person to help there. I think between is fine as it is and it should give the same result regardless of the temporal sequence of the dates provided. It may make more sense to use through for the semantics that until currently has.

I’m not sure which of these would really be needed. Anyhow, here’s something.

[...edtf('2016-05').⟨name⟩('2016-07')]

2016-05  2016-06 2016-07 English
not_in dates in May that are not in July
before dates in May that are before July
between dates between May and July (~ neither_in / nor_in)
but_in dates that are not in May but in July
by, until by, until dates from [the start of] May by/until [before the start of] July
? ? dates from [after the end of] May to [the end of] July
or or dates either in May or July [or both], i.e. merge/combine/add
through, to through, to through, to dates from [the start of] May through/[un/up]to [the end of] July

[...edtf('2016-07').⟨name⟩('2016-05')]

2016-05  2016-06 2016-07 English
not_in dates in July that are not in May
but_in dates that are not in July but in May
between dates between July and May (exclusive)
after dates in July that are after May
? ? dates to [before the start of] July since [the start of] May
by, since by, since dates to [the start of] July by/since [after the end of] May
or or dates either in July or May [or both]
through, from through, from through, from dates to [the start of] May through/from [the end of] July

[...edtf('2016-05/06').⟨name⟩('2016-06/07')]

2016-05  2016-06 2016-07 English
not_in dates in May/June that are not in June/July
before dates in May/June that are before June/July
dates between May/June and June/July (~ neither_in / nor_in)
but_in dates that are not in May/June but in June/July
by, until dates from [the start of] May/June by/until [before the start of] June/July
? dates from [after the end of] May/June to [the end of] June/July
or or or dates in May/June or June/July or both
xor xor dates either in May/June or June/July
through, to through, to through, to dates from [the start of] May/June through/[un/up]to [the end of] June/July

[...edtf('2016-06/07').⟨name⟩('2016-05/06')]

Composite pairs

  • from and since
  • not_in and but_in
  • before and after
  • or and between

Here are some interesting notes by Dijkstra on a similar topic. In order to keep the API concise I would suggest to keep between as it is (and, like you say, make it work irrespective of temporal order); rename until to through and have until return a half-open interval by (with inclusive lower bound); this way, until would be the method that is most useful by Dijkstra's reasoning.

If we can think of a good name, we could add a method for the other half-open interval. @abbymullen can you think of a verb that would make sense here? That is, "June something August", which would imply the months July to August (without June)?

If there is no suitable name, I'd just not add that variant to the API, and you can still do [...edtf('2016-06').next().through('2016-08')].

OK added between, until and through as described above. All of the methods work in reverse order too, which is not intuitive given the names until and through as pointed out earlier, but I am inclined to to see that as a feature. We could add aliases which make more sense linguistically for reverse order specifically (i.e., edtf('2016').backUntil('2010')), but that might be overdoing it.

Actually, one cool detail about the reverse order is that it gives you the missing variant in reverse, e.g.: [...edtf('2016-08').until('2016-06')].reverse().