Skip to content

Commit

Permalink
pluck
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Sep 20, 2018
1 parent 5a77d3f commit 416ee55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<p align="center">
<a href="https://www.npmjs.com/package/tinyfunk"><img src="https://img.shields.io/npm/v/tinyfunk.svg" alt="npm version" style="max-width:100%;"></a>
<a href="https://www.npmjs.com/package/tinyfunk"><img src="https://img.shields.io/npm/dm/tinyfunk.svg" alt="npm downloads" style="max-width:100%;"></a>
<a href="#"><img src="https://img.shields.io/badge/gzip--size-1.44%20kB-blue.svg" alt="gzip-size" style="max-width:100%;"></a>
<a href="#"><img src="https://img.shields.io/badge/gzip--size-1.45%20kB-blue.svg" alt="gzip-size" style="max-width:100%;"></a>
<br />
<a href="https://travis-ci.org/flintinatux/tinyfunk"><img src="https://travis-ci.org/flintinatux/tinyfunk.svg?branch=master" alt="Build Status" style="max-width:100%;"></a>
<a href="https://coveralls.io/github/flintinatux/tinyfunk?branch=master"><img src="https://coveralls.io/repos/github/flintinatux/tinyfunk/badge.svg?branch=master" alt="Coverage Status" style="max-width:100%;"></a>
Expand Down Expand Up @@ -117,6 +117,7 @@ If you've lived with FP long enough, you are likely familiar with most of the fu
| `pick` | `[k] -> { k: v } -> { k: v }` |
| `pipe` | `((a -> b), ..., (y -> z)) -> a -> z` |
| `pipeP` | `((a -> Promise b), ..., (y -> Promise z)) -> a -> Promise z` |
| `pluck` | `k -> [{ k: v }] -> [v]` |
| `prepend` | `a -> [a] -> [a]` |
| `prop` | `k -> { k: v } -> v` |
| `propEq` | `k -> v -> { k: v } -> Boolean` |
Expand Down
6 changes: 6 additions & 0 deletions src.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ const pipeP = unapply(flip(reduce(flip(then))))
// cond :: [[(a -> Boolean), (a -> b)]] -> a -> b
const cond = compose(reduceRight(thrush, unit), map(apply(ifElse)))

// pluck :: k -> [{ k: v }] -> [v]
const pluck = curry((key, list) =>
map(prop(key), list)
)

// slice :: Number -> Number -> [a] -> [a]
const slice = curry((from, to, list) =>
list.slice(from, to)
Expand Down Expand Up @@ -427,6 +432,7 @@ _assign(exports, {
pick,
pipe,
pipeP,
pluck,
prepend,
prop,
propEq,
Expand Down
15 changes: 15 additions & 0 deletions test/pluck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { expect } = require('chai')

const { pluck } = require('..')

describe('pluck', () => {
const list = [{ a: 'b' }, { a: 'c' }, { b: 'a' }]

it('plucks the values from a list for a given key', () =>
expect(pluck('a', list)).to.eql(['b', 'c', undefined])
)

it('is curried', () =>
expect(pluck('a')(list)).to.eql(['b', 'c', undefined])
)
})
2 changes: 1 addition & 1 deletion tinyfunk.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 416ee55

Please sign in to comment.