comparisons
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,3 +1,12 @@ | ||
const sift = require('sift').default; | ||
const Comparison = require('..'); | ||
const exp = [ | ||
'foo=bar', | ||
'age<30', | ||
'age>=21', | ||
'language=EN', | ||
'bestFriend.name=jon', | ||
]; | ||
const stub = { | ||
@@ -22,17 +31,57 @@ foo: 'bar', | ||
it('should return truthy', () => | ||
expect( | ||
new Comparison([ | ||
'foo=bar', | ||
'age<30', | ||
'age>=21', | ||
'language=EN', | ||
'bestFriend.name=jon', | ||
]).eval(stub), | ||
).toBeTruthy()); | ||
describe('eval', () => { | ||
it('should return truthy', () => | ||
expect(new Comparison(exp).eval(stub)).toBeTruthy()); | ||
it('should return falsy', () => | ||
expect( | ||
new Comparison(['age>22', 'language=FR']).eval(stub), | ||
).toBeFalsy()); | ||
it('should return falsy', () => | ||
expect( | ||
new Comparison(['age>22', 'language=FR']).eval(stub), | ||
).toBeFalsy()); | ||
}); | ||
describe('query', () => { | ||
it('should return as mongo query', () => | ||
expect(new Comparison(exp).query(stub)).toMatchObject({ | ||
$and: [ | ||
{ foo: /bar/gi }, | ||
{ age: { $lt: 30 } }, | ||
{ age: { $gte: 21 } }, | ||
{ language: /EN/gi }, | ||
{ 'bestFriend.name': /jon/gi }, | ||
], | ||
})); | ||
it('should return the stub on query', () => { | ||
const query = new Comparison(exp).query(); | ||
expect([stub].filter(sift(query))).toHaveLength(1); | ||
}); | ||
it('should match with several stubs', () => { | ||
const date = new Date('2018-03-22').toISOString(); | ||
const query = new Comparison([ | ||
'low>2', | ||
'high<10', | ||
'word=hi', | ||
`date=${date}`, | ||
]).query(); | ||
const result = [ | ||
{ | ||
low: 1, | ||
high: 12, | ||
word: 'hi', | ||
date: new Date().toISOString(), | ||
}, | ||
{ low: 3, high: 6, word: 'hi', date }, | ||
{ low: 4, high: 8, word: 'hi', date }, | ||
{ | ||
low: 4, | ||
high: 22, | ||
word: 'hi', | ||
}, | ||
].filter(sift(query)); | ||
expect(result).toHaveLength(2); | ||
}); | ||
}); | ||
}); |
@@ -9,2 +9,3 @@ const flat = require('flat'); | ||
isLessThanOrEqualTo, | ||
cast, | ||
} = require('./utils'); | ||
@@ -20,2 +21,18 @@ | ||
const getAssignment = { | ||
'equals': cast, | ||
'isLessThan': (v) => ({ | ||
$lt: cast(v), | ||
}), | ||
'isGreaterThan': (v) => ({ | ||
$gt: cast(v), | ||
}), | ||
'isLessThanOrEqualTo': (v) => ({ | ||
$lte: cast(v), | ||
}), | ||
'isGreaterThanOrEqualTo': (v) => ({ | ||
$gte: cast(v), | ||
}), | ||
}; | ||
const keys = Object.keys(ops); | ||
@@ -55,2 +72,14 @@ const re = new RegExp(`[${keys.map((v) => `(${v})`).join('')}]`, 'i'); | ||
query() { | ||
return { | ||
$and: this.eligible.reduce( | ||
(a, [key, value, fn]) => | ||
a.concat({ | ||
[key]: getAssignment[fn.name](value), | ||
}), | ||
[], | ||
), | ||
}; | ||
} | ||
eval(obj) { | ||
@@ -57,0 +86,0 @@ return this.eligible.every(([key, value, validator]) => |
@@ -86,2 +86,9 @@ const moment = require('moment'); | ||
const cast = (v) => { | ||
const test = validate(v); | ||
if (test.date()) return v; | ||
if (test.numeric()) return parseFloat(v); | ||
return new RegExp(v, 'i'); | ||
}; | ||
module.exports = { | ||
@@ -93,2 +100,4 @@ equals, | ||
isLessThanOrEqualTo, | ||
validate, | ||
cast, | ||
}; |
{ | ||
"name": "comparisons", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "lib/index.js", | ||
@@ -22,3 +22,4 @@ "license": "MIT", | ||
"jest": "^24.9.0", | ||
"prettier": "1.18.2" | ||
"prettier": "1.18.2", | ||
"sift": "^9.0.0" | ||
}, | ||
@@ -25,0 +26,0 @@ "dependencies": { |
@@ -5,2 +5,4 @@ | ||
<img src="https://github.com/MikeIbberson/comparisons/workflows/Node%20CI/badge.svg" alt="Status" /> | ||
<a href='https://coveralls.io/github/MikeIbberson/comparisons?branch=master'><img src='https://coveralls.io/repos/github/MikeIbberson/comparisons/badge.svg?branch=master' alt='Coverage Status' /></a> | ||
<img src='https://bettercodehub.com/edge/badge/MikeIbberson/comparisons?branch=master'> | ||
</p> | ||
@@ -19,3 +21,4 @@ | ||
const runner = new Comparison(tests); | ||
runner.eval(stub); | ||
runner.eval(stub); // returns true or false | ||
runner.query(); // returns a Mongo friendly query | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13045
10
306
22
10