comparisons
Advanced tools
Comparing version 2.2.10 to 2.3.0
@@ -87,2 +87,20 @@ const sift = require('sift').default; | ||
it('should return nin', () => { | ||
const query = new Comparison(['language!=en,es']).query(); | ||
expect(query.$and).toEqual([ | ||
{ | ||
language: { $nin: ['en', 'es'] }, | ||
}, | ||
]); | ||
}); | ||
it('should return in', () => { | ||
const query = new Comparison(['language=en,es']).query(); | ||
expect(query.$and).toEqual([ | ||
{ | ||
language: { $in: ['en', 'es'] }, | ||
}, | ||
]); | ||
}); | ||
it('should return the stub on query', () => { | ||
@@ -89,0 +107,0 @@ const query = new Comparison(exp).query(); |
@@ -50,2 +50,17 @@ const Utils = require('../utils'); | ||
expect(Utils.doesNotEqual(2, 2)).toBeFalsy()); | ||
it('should return falsy when one in list matches', () => | ||
expect(Utils.doesNotEqual('1', '1,2')).toBeFalsy()); | ||
it('should return truthy none in list matches', () => | ||
expect(Utils.doesNotEqual('1', [2, 3])).toBeTruthy()); | ||
it('should return truthy on wildcard', () => | ||
expect(Utils.doesNotEqual('', '*')).toBeTruthy()); | ||
it('should return truthy on wildcard', () => | ||
expect(Utils.doesNotEqual(undefined, '*')).toBeTruthy()); | ||
it('should return truthy on wildcard', () => | ||
expect(Utils.doesNotEqual(null, '*')).toBeTruthy()); | ||
}); | ||
@@ -52,0 +67,0 @@ |
@@ -29,2 +29,5 @@ const flat = require('flat'); | ||
}), | ||
'nin': (v) => ({ | ||
$nin: v, | ||
}), | ||
'equals': cast, | ||
@@ -130,4 +133,8 @@ 'isEmpty': cast, | ||
const v = isSerialized(value) ? value.split(',') : value; | ||
const name = Array.isArray(v) ? 'in' : fn.name; | ||
let { name } = fn; | ||
if (Array.isArray(v)) { | ||
name = name === 'doesNotEqual' ? 'nin' : 'in'; | ||
} | ||
return a.concat({ | ||
@@ -134,0 +141,0 @@ [key]: getAssignment[name](v), |
@@ -78,17 +78,33 @@ const moment = require('moment'); | ||
const equals = (a, b, locale) => { | ||
const comp = (input) => { | ||
const exec = (value) => runCompare(value, input, locale) === 0; | ||
return Array.isArray(a) ? a.some(exec) : exec(a); | ||
const withRunCompare = (fn, name) => { | ||
const output = { | ||
[name](a, b, locale) { | ||
return fn(a, b, (input) => { | ||
const exec = (value) => runCompare(value, input, locale) === 0; | ||
return Array.isArray(a) ? a.some(exec) : exec(a); | ||
}); | ||
}, | ||
}; | ||
// refactored to become HOC | ||
// but later discovered fn names are required | ||
return output[name]; | ||
}; | ||
const equals = withRunCompare((a, b, test) => { | ||
if (b === '*') return a !== '' && a !== undefined && a !== null; | ||
if (typeof b === 'string' && b.includes(',')) | ||
return b.split(',').some(comp); | ||
return b.split(',').some(test); | ||
return comp(b); | ||
}; | ||
return test(b); | ||
}, 'equals'); | ||
const doesNotEqual = (a, b, locale) => runCompare(a, b, locale) !== 0; | ||
const doesNotEqual = withRunCompare((a, b, test) => { | ||
if (b === '*') return a === '' || a === undefined || a === null; | ||
if (typeof b === 'string' && b.includes(',')) | ||
return !b.split(',').some(test); | ||
return !test(b); | ||
}, 'doesNotEqual'); | ||
const isGreaterThan = (a, b) => | ||
@@ -95,0 +111,0 @@ new ValidatorRunner([a, b]).sequence([ |
{ | ||
"name": "comparisons", | ||
"version": "2.2.10", | ||
"version": "2.3.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
19934
490