From 9c5ca66a76b76f3dddfc29e4c3111d0f7855d770 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 7 Feb 2020 15:01:55 -0500 Subject: [PATCH 1/2] v2.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d6aa00d..b6d6fb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "comparisons", - "version": "2.2.0", + "version": "2.2.1", "main": "lib/index.js", "license": "MIT", "jest": { From e5aa40f34d618b3f07ea33622b0081aaed986620 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 11 Mar 2020 18:03:49 -0400 Subject: [PATCH 2/2] feat: serialized array for ops --- lib/__tests__/comparison.test.js | 2 +- lib/index.js | 19 ++++++++++++------- lib/utils.js | 8 +++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/__tests__/comparison.test.js b/lib/__tests__/comparison.test.js index 19adebc..40e9445 100644 --- a/lib/__tests__/comparison.test.js +++ b/lib/__tests__/comparison.test.js @@ -2,7 +2,7 @@ const sift = require('sift').default; const Comparison = require('..'); const exp = [ - 'foo=bar', + 'foo=bar,quuz', 'age<30', 'age>=21', 'language=EN', diff --git a/lib/index.js b/lib/index.js index 283046d..0e5a1bd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ const { isLessThanOrEqualTo, isEmpty, cast, + isSerialized, } = require('./utils'); const ops = { @@ -21,6 +22,9 @@ const ops = { }; const getAssignment = { + 'in': (v) => ({ + $in: v, + }), 'equals': cast, 'isEmpty': cast, 'isLessThan': (v) => ({ @@ -78,13 +82,14 @@ class Comparison { query() { return { - $and: this.eligible.reduce( - (a, [key, value, fn]) => - a.concat({ - [key]: getAssignment[fn.name](value), - }), - [], - ), + $and: this.eligible.reduce((a, [key, value, fn]) => { + const v = isSerialized(value) ? value.split(',') : value; + const name = Array.isArray(v) ? 'in' : fn.name; + + return a.concat({ + [key]: getAssignment[name](v), + }); + }, []), }; } diff --git a/lib/utils.js b/lib/utils.js index d8c6bc3..3fb8416 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -64,9 +64,12 @@ const runCompare = (a, b, locale) => }); const equals = (a, b, locale) => { + const comp = (v) => runCompare(a, v, locale) === 0; if (b === '*') return a !== '' && a !== undefined && a !== null; + if (typeof b === 'string' && b.includes(',')) + return b.split(',').some(comp); - return runCompare(a, b, locale) === 0; + return comp(b); }; const doesNotEqual = (a, b, locale) => runCompare(a, b, locale) !== 0; @@ -104,6 +107,8 @@ const cast = (v) => { const isEmpty = (v) => v === undefined || v === null || v === 'undefined' || v === 'null'; +const isSerialized = (v) => typeof v === 'string' && v.includes(','); + module.exports = { equals, doesNotEqual, @@ -114,4 +119,5 @@ module.exports = { isEmpty, validate, cast, + isSerialized, };