Skip to content

Commit

Permalink
Make parsers look like parse option fix #18
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Apr 29, 2016
1 parent e2baa9d commit b8e1387
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
20 changes: 11 additions & 9 deletions src/menquery-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class MenqueryParam {
* @param {string} name - Param name.
* @param {*} [value] - The value of the param.
* @param {Object} [options] - Options of the param.
* @param {Object} [schema] - Schema of the param.
*/
constructor (name, value, options = {}, schema = {}) {
this.name = name
Expand Down Expand Up @@ -209,17 +210,18 @@ export default class MenqueryParam {
}

_.forIn(this.options, (optionValue, option) => {
let parser = this.handlers.parsers[option]
if (_.isFunction(parser)) {
if (_.isArray(value)) {
value = value.map((v) => parser(optionValue, v, this, this.schema))
} else {
value = parser(optionValue, value, this, this.schema)
}
let parser
if (option === 'parse' && _.isFunction(optionValue)) {
parser = optionValue
} else if (_.isFunction(this.handlers.parsers[option])) {
parser = this.handlers.parsers[option].bind(this, optionValue)
} else {
return
}
query = parser(value, path, operator, this, this.schema)
})

return this.options.parse(value, path, operator, this, this.schema)
return query
}

/**
Expand Down Expand Up @@ -256,7 +258,7 @@ export default class MenqueryParam {
_.forIn(this.options, (optionValue, option) => {
let formatter = this.handlers.formatters[option]
if (_.isFunction(formatter)) {
value = formatter(optionValue, value, this)
value = formatter(optionValue, value, this, this.schema)
}
})

Expand Down
25 changes: 14 additions & 11 deletions test/menquery-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,21 @@ test('MenqueryParam formatter', (t) => {

test('MenqueryParam parser', (t) => {
let pParam = param(null, {multiple: true})
pParam.parser('toLower', (toLower, value, param) => {
t.equal(param.name, 'test', 'should pass param object to parser method')
return pParam.formatter('lowercase')(toLower, value, param)
pParam.parser('elemMatch', (elemMatch, value, path, operator, param, schema) => {
t.true(path, 'should pass path to parser')
t.true(operator, 'should pass operator to parser')
return elemMatch
? {[path]: {$elemMatch: {[elemMatch]: {[operator]: value}}}}
: {[path]: operator !== '$eq' ? {[operator]: value} : value}
})
t.false(param().parser('toLower'), 'should not get nonexistent parser')
t.true(pParam.parser('toLower'), 'should get custom parser')
t.same(pParam.parse('TEST'), {test: 'TEST'}, 'should not apply custom parser if option was not set')
t.true(pParam.option('toLower', true), 'should set parser option to true')
t.same(pParam.parse('TEST'), {test: 'test'}, 'should apply custom parser')
t.same(pParam.parse('TEST,FOO'), {test: {$in: ['test', 'foo']}}, 'should apply custom parser to multiple values')
t.false(pParam.option('toLower', false), 'should set parser option to false')
t.same(pParam.parse('TEST'), {test: 'TEST'}, 'should not apply custom parser if option is false')
t.false(param().parser('elemMatch'), 'should not get nonexistent parser')
t.true(pParam.parser('elemMatch'), 'should get custom parser')
t.same(pParam.parse('test'), {test: 'test'}, 'should not apply custom parser if option was not set')
t.true(pParam.option('elemMatch', 'path'), 'should set parser option to true')
t.same(pParam.parse('test'), {test: {$elemMatch: {path: {$eq: 'test'}}}}, 'should apply custom parser')
t.same(pParam.parse('test,foo'), {test: {$elemMatch: {path: {$in: ['test', 'foo']}}}}, 'should apply custom parser to multiple values')
t.false(pParam.option('elemMatch', false), 'should set parser option to false')
t.same(pParam.parse('test'), {test: 'test'}, 'should not apply custom parser if option is false')
t.end()
})

Expand Down
4 changes: 2 additions & 2 deletions test/menquery-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ test('MenquerySchema validator', (t) => {

test('MenquerySchema parser', (t) => {
let mySchema = schema({test: 'help'})
let parser = mySchema.parser('elemMatch', (elemMatch, value) => {
return elemMatch ? {$elemMatch: {[elemMatch]: value}} : value
let parser = mySchema.parser('elemMatch', (elemMatch, value, path) => {
return elemMatch ? {[path]: {$elemMatch: {[elemMatch]: value}}} : value
})
t.true(parser, 'should create a parser')
t.false(schema().parser('elemMatch'), 'should not get a nonexistent parser')
Expand Down

0 comments on commit b8e1387

Please sign in to comment.