Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge 384368d into 19c1457
Browse files Browse the repository at this point in the history
  • Loading branch information
sammdec committed Feb 10, 2017
2 parents 19c1457 + 384368d commit fe0af8e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ Will return beers which match the name of the malt of the string passed in (we u
Type: `string`
Will return beers which match the name of the yeast of the string passed in (we use fuzzy matching to find the yeast names).

##### ids
Type: `string`
Pattern: `id|id|id`
Will return beers which match the given ids, ids should be separated by a pipe symbol.


```
const options = {
Expand Down
12 changes: 12 additions & 0 deletions src/filters/ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const filter = require('lodash/filter')
const includes = require('lodash/includes')
const curry = require('lodash/curry')

function idsFilter (val, db) {
if (val == null) return db

const idArray = val.split('|').map(Number)
return filter(db, (b) => includes(idArray, b.id))
}

module.exports = curry(idsFilter)
7 changes: 5 additions & 2 deletions src/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const foodFilter = require('./food')
const hopsFilter = require('./hops')
const maltFilter = require('./malt')
const yeastFilter = require('./yeast')
const idsFilter = require('./ids')

function filters (db, opts) {
const {
Expand All @@ -25,7 +26,8 @@ function filters (db, opts) {
brewed_after,
hops,
malt,
food
food,
ids
} = opts

return pipe(
Expand All @@ -41,7 +43,8 @@ function filters (db, opts) {
foodFilter(food),
hopsFilter(hops),
maltFilter(malt),
yeastFilter(yeast)
yeastFilter(yeast),
idsFilter(ids)
)(db)
}

Expand Down
13 changes: 13 additions & 0 deletions test/filters/ids.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const db = require('punkapi-db')
const idsFilter = require('../../dist/filters/ids')

describe('idsFilter', function() {
it('should return beers with ids 2,8,20', function () {
idsFilter('2|8|20', db).should.containDeep([{id: 2}, {id: 8}, {id: 20}])
})

it('should not return beers with ids 300', function () {
idsFilter('2|8|300', db).should.containDeep([{id: 2}, {id: 8}])
idsFilter('2|8|300', db).should.not.containDeep([{id: 300}])
})
})
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,15 @@ describe('.beers()', function() {
beers(opts)[0].ingredients.yeast.should.match(/Wyeast 1056/i)
done()
})

it('should return beers with ids of 1,4,29', function (done) {
const opts = {
ids: '1|4|29'
}
beers(opts).should.be.a.Array()
beers(opts)[0].id.should.be.equal(1)
beers(opts)[1].id.should.be.equal(4)
beers(opts)[2].id.should.be.equal(29)
done()
})
})

0 comments on commit fe0af8e

Please sign in to comment.