Skip to content

Commit

Permalink
add aliases
Browse files Browse the repository at this point in the history
cc @jonahkagan not tested...not sure the best way to test it.
Could duplicate all existing tests and run them under aliases, but that
seems like overkill.
  • Loading branch information
Rafael Garcia committed Nov 16, 2013
1 parent 49a044d commit 245b5ab
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 14 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ _s.each(readable, console.log);
// 6
```

*aliases*: `forEach`

---
#### <a name="map">map</a> `_s.map(readable, iterator)`

Expand All @@ -146,6 +148,8 @@ mapped.on("data", console.log);
// 6
```

*aliases*: `collect`

---
#### <a name="reduce">reduce</a> `_s.reduce(readable, options)`

Expand Down Expand Up @@ -192,6 +196,8 @@ reduced.on('data', console.log);
// { a: 3, b: [ 2 ] }
```

*aliases*: `inject`, `foldl`

---
#### <a name="filter">filter</a> `_s.filter(readable, iterator)`

Expand All @@ -210,6 +216,8 @@ filtered.on('data', console.log);
// 4
```

*aliases*: `select`

---
#### <a name="where">where</a> `_s.where(readable, attrs)`

Expand Down Expand Up @@ -284,6 +292,8 @@ first.on('data', console.log);
// 3
```

*aliases*: `head`, `take`

---
#### <a name="rest">rest</a> `_s.rest(readable[, n])`

Expand All @@ -298,6 +308,8 @@ rest.on('data', console.log);
// 5
```

*aliases*: `tail`, `drop`

---
#### <a name="flatten">flatten</a> `_s.flatten(readable[, shallow])`

Expand Down Expand Up @@ -334,6 +346,8 @@ uniq.on('data', console.log);
// 1
```

*aliases*: `unique`

---
#### <a name="chain">chain</a> `_s.chain(obj)`

Expand Down
6 changes: 4 additions & 2 deletions lib/mixins/each.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Each extends Transform
@push chunk
cb()

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Each options, stream_opts)
module.exports =
each: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Each options, stream_opts)
each: fn
forEach: fn
7 changes: 5 additions & 2 deletions lib/mixins/filter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = class Filter extends Transform
@push chunk if @options.fn chunk
cb()

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Filter options, stream_opts)

module.exports =
filter: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Filter options, stream_opts)
filter: fn
select: fn
8 changes: 6 additions & 2 deletions lib/mixins/first.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = class First extends Transform
return
cb null, chunk

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new First options, stream_opts)

module.exports =
first: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new First options, stream_opts)
first: fn
head: fn
take: fn
7 changes: 5 additions & 2 deletions lib/mixins/map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Map extends Transform
@push @options.fn(chunk)
cb()

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Map options, stream_opts)

module.exports =
map: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Map options, stream_opts)
map: fn
collect: fn
8 changes: 6 additions & 2 deletions lib/mixins/reduce.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Reduce extends Transform
@_val = @options.fn @_val, chunk
cb()

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Reduce options, stream_opts)

module.exports =
reduce: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Reduce options, stream_opts)
reduce: fn
inject: fn
foldl: fn
8 changes: 6 additions & 2 deletions lib/mixins/rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Rest extends Transform
return cb() if @seen < @rest
cb null, chunk

fn = (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Rest options, stream_opts)

module.exports =
rest: (readable, options, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Rest options, stream_opts)
rest: fn
tail: fn
drop: fn

This comment has been minimized.

Copy link
@azylman

azylman Nov 18, 2013

Contributor

You could also do something like:

module.exports = _(['rest', 'tail', 'drop']).chain().map((name) -> [name, fn]).object().value()

This comment has been minimized.

Copy link
@jonahkagan

jonahkagan Nov 18, 2013

Contributor

I like to abstract this as _.mixin buildObj: (keys, fn) -> _.object keys, _.map keys, fn. Then you could write _(['rest', 'tail', 'drop']).buildObj -> fn

7 changes: 5 additions & 2 deletions lib/mixins/uniq.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Uniq
hash_fn ?= String
return new (if sorted then SortedUniq else UnsortedUniq) hash_fn, stream_opts

fn = (readable, sorted, hash_fn, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Uniq sorted, hash_fn, stream_opts)

module.exports =
uniq: (readable, sorted, hash_fn, stream_opts={objectMode:readable._readableState.objectMode}) ->
readable.pipe(new Uniq sorted, hash_fn, stream_opts)
uniq: fn
unique: fn

2 comments on commit 245b5ab

@jonahkagan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably just do an equality test. Something like:

_.each ['tail', 'drop'], (alias) -> assert.equal _s[alias], _s['rest']

@rgarcia
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.