jussi-kalliokoski / trine

A utility library for modern JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add `dropHead` and `dropTail`.

jussi-kalliokoski opened this issue · comments

See #9 - dropHead(n) would basically be the equivalent of slice(n), and dropTail(n) the equivalent of slice(0, length - n).

What's the direction of this project, are you creating api base on Lodash and Underscore or not?

Lodash has drop and dropRight

The lodash/underscore API is definitely an inspiration, however I've decided to do some naming slightly differently because unlike lodash, trine operates mostly on iterators instead of collections. Hence for example the prefix Right doesn't quite semantically fit because there is no right, just an end. As such, the terminology commonly used for streams (e.g. bash) makes more sense. However, Unix CLI tools often do too many things making it harder to reason about them, while with Trine I attempt to make the "do one thing and do it well" as absolute as possible, which is why head or tail don't have overloads that take instead of dropping.

This brings nice symmetry as well:

  • take -> filter out items that don't satisfy the condition.
  • drop -> filter out items that satisfy the condition.
  • takeWhile -> filter out items after the condition is first not satisfied.
  • dropWhile -> filter out items before the condition is first satisfied.
  • head (could also be named takeHead) -> take only N first items.
  • tail (could also be named takeTail) -> take only N last items.
  • dropHead -> take all except N first items.
  • dropTail -> take all except N last items.

EDIT: comparison to lodash API:

  • _.filter (strange name for something that specifies what to take instead of what to filter out) / _.where.
  • N/A (you could use _.not(_.filter) to create your own).
  • _.takeWhile.
  • _.dropWhile.
  • _.take.
  • _.tail / _.rest.
  • _.drop.
  • _.dropRight.

however I've decided to do some naming slightly differently because unlike lodash, trine operates mostly on iterators instead of collections. Hence for example the prefix Right doesn't quite semantically fit because there is no right, just an end. As such, the terminology commonly used for streams (e.g. bash) makes more sense.

That sound even better, just what to understand more about this project and what direction you are going, keep up the good work :)