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

Commit

Permalink
Merge 787575d into 19c1457
Browse files Browse the repository at this point in the history
  • Loading branch information
sammdec committed Jan 3, 2017
2 parents 19c1457 + 787575d commit 969f3b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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 969f3b1

Please sign in to comment.