Skip to content

Commit

Permalink
feat(fields/filter): add in filter type
Browse files Browse the repository at this point in the history
Also updated README.md.
  • Loading branch information
nothinking authored and estliberitas committed Aug 25, 2017
1 parent 076d56e commit 0310068
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -481,7 +481,7 @@ Create filter spec.

__Arguments__

* type `string | object` - Filter type: `and`, `javascript`, `not`, `or`, `regex`, `selector`, `search` - or raw filter object.
* type `string | object` - Filter type: `and`, `javascript`, `not`, `or`, `regex`, `selector`, `search`, `in` - or raw filter object.
* args `...*` - Filter-specific arguments. Described below.

__Query.filter('and', filters...)__
Expand Down Expand Up @@ -518,6 +518,13 @@ __Query.filter('search', dimension, query)__

---

__Query.filter('in', dimension, values)__

* dimensions `string` - Dimension to which filter is applied.
* values `object[]` - Values to match.

---

#### `static` `object` having(type, [args...])

Create `having` spec.
Expand Down
26 changes: 26 additions & 0 deletions lib/fields/filters/in.js
@@ -0,0 +1,26 @@
'use strict'

var FieldError = require('../../errors').FieldError


/**
* In filter
*
* @see http://druid.io/docs/0.6.120/Filters.html
*
* @param {string} dimension Dimension to which filter is applied
* @param {*} values Values to match
*/
module.exports = function(dimension, values) {
if (!dimension || typeof values === 'undefined') {
throw new FieldError('Dimension or value is not specified')
}

// convert to empty string, null/empty string are the same thing to druid
if (values === null) {
values = []
}

this.dimension = dimension
this.values = values
}
24 changes: 24 additions & 0 deletions spec/fields/filters.js
Expand Up @@ -245,4 +245,28 @@ describe('Filters', function() {
}).to.throwException()
})
})

describe('In', function() {
it('should create filter', function() {
var spec = Query.filter('in', 'dim', ['value1', 'value2'])

expect(spec).to.eql({
type: 'in',
dimension: 'dim',
values: ['value1', 'value2']
})
})

it('should throw error if dimension is not specified', function() {
expect(function() {
Query.filter('in', null, 'value')
}).to.throwException()
})

it('should throw error if value is not specified', function() {
expect(function() {
Query.filter('in', 'dimension')
}).to.throwException()
})
})
})

0 comments on commit 0310068

Please sign in to comment.