matches-where-query
Advanced tools
Comparing version 2.0.6 to 2.0.7
{ | ||
"extends" : "@rotundasoftware/eslint-config-rotundasoftware", | ||
"plugins": [ | ||
"mocha" | ||
], | ||
"extends" : ["@rotundasoftware/eslint-config-rotundasoftware", "plugin:mocha/recommended"], | ||
"plugins": ["mocha"], | ||
"rules" : { | ||
@@ -7,0 +5,0 @@ "mocha/no-exclusive-tests": "error" |
"use strict"; | ||
var _underscore = _interopRequireDefault(require("underscore")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } | ||
module.exports = function (object, whereQuery) { | ||
var _loop = function _loop() { | ||
var queryAttribute = whereQuery[attributeName]; | ||
var objectAttribute = object[attributeName]; | ||
if (_underscore["default"].isObject(queryAttribute) && queryAttribute.comparator) { | ||
if (_underscore["default"].isUndefined(queryAttribute.value) && !['isNull', 'isNotNull'].includes(queryAttribute.comparator)) throw new Error('Value must be supplied for comparator queries'); | ||
switch (queryAttribute.comparator) { | ||
case 'doesNotEqual': | ||
if (objectAttribute === queryAttribute.value) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isGreaterThan': | ||
if (!objectAttribute || objectAttribute <= queryAttribute.value) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isGreaterThanOrEqualTo': | ||
if (!objectAttribute || objectAttribute < queryAttribute.value) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isLessThan': | ||
if (!objectAttribute || objectAttribute >= queryAttribute.value) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isLessThanOrEqualTo': | ||
if (!objectAttribute || objectAttribute > queryAttribute.value) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isBetween': | ||
if (!_underscore["default"].isArray(queryAttribute.value) || queryAttribute.value.length !== 2) { | ||
throw new Error('Value supplied for isBetween comparator must be an array [ min, max ]'); | ||
} | ||
if (!objectAttribute || objectAttribute < queryAttribute.value[0] || objectAttribute > queryAttribute.value[1]) return { | ||
v: false | ||
}; | ||
break; | ||
case 'startsWith': | ||
if (!objectAttribute) return { | ||
v: false | ||
}; | ||
// eslint-disable-next-line max-len | ||
if (!_underscore["default"].isString(objectAttribute) || !_underscore["default"].isString(queryAttribute.value)) throw new Error('For startsWith comparator both the object attribute and the query value must be strings.'); | ||
if (!_startsWith(objectAttribute, queryAttribute.value)) return { | ||
v: false | ||
}; | ||
break; | ||
case 'endsWith': | ||
if (!objectAttribute) return { | ||
v: false | ||
}; | ||
// eslint-disable-next-line max-len | ||
if (!_underscore["default"].isString(objectAttribute) || !_underscore["default"].isString(queryAttribute.value)) throw new Error('For endsWith comparator both the object attribute and the query value must be strings.'); | ||
if (!_endsWith(objectAttribute, queryAttribute.value)) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isNull': | ||
if (!_underscore["default"].isNull(objectAttribute)) return { | ||
v: false | ||
}; | ||
break; | ||
case 'isNotNull': | ||
if (_underscore["default"].isNull(objectAttribute)) return { | ||
v: false | ||
}; | ||
break; | ||
case 'contains': | ||
if (!objectAttribute) return { | ||
v: false | ||
}; | ||
if (_underscore["default"].isString(objectAttribute)) { | ||
return { | ||
v: objectAttribute.includes(queryAttribute.value) | ||
}; | ||
} else if (_underscore["default"].isArray(objectAttribute)) { | ||
return { | ||
v: objectAttribute.some(function (value) { | ||
return _underscore["default"].isEqual(value, queryAttribute.value); | ||
}) | ||
}; | ||
} else { | ||
return { | ||
v: false | ||
}; | ||
} | ||
default: | ||
throw new Error('Invalid comparator ' + queryAttribute.comparator); | ||
} | ||
} else if (_underscore["default"].isArray(queryAttribute)) { | ||
if (!_underscore["default"].contains(queryAttribute, objectAttribute)) return { | ||
v: false | ||
}; | ||
} else { | ||
if (queryAttribute !== objectAttribute) return { | ||
v: false | ||
}; | ||
} | ||
}, | ||
_ret; | ||
for (var attributeName in whereQuery) { | ||
var queryAttribute = whereQuery[attributeName]; | ||
var objectAttribute = object[attributeName]; | ||
if (_underscore["default"].isObject(queryAttribute) && queryAttribute.comparator) { | ||
if (_underscore["default"].isUndefined(queryAttribute.value) && !['isNull', 'isNotNull'].includes(queryAttribute.comparator)) throw new Error('Value must be supplied for comparator queries'); | ||
switch (queryAttribute.comparator) { | ||
case 'doesNotEqual': | ||
if (objectAttribute === queryAttribute.value) return false; | ||
break; | ||
case 'isGreaterThan': | ||
if (!objectAttribute || objectAttribute <= queryAttribute.value) return false; | ||
break; | ||
case 'isGreaterThanOrEqualTo': | ||
if (!objectAttribute || objectAttribute < queryAttribute.value) return false; | ||
break; | ||
case 'isLessThan': | ||
if (!objectAttribute || objectAttribute >= queryAttribute.value) return false; | ||
break; | ||
case 'isLessThanOrEqualTo': | ||
if (!objectAttribute || objectAttribute > queryAttribute.value) return false; | ||
break; | ||
case 'isBetween': | ||
if (!_underscore["default"].isArray(queryAttribute.value) || queryAttribute.value.length !== 2) { | ||
throw new Error('Value supplied for isBetween comparator must be an array [ min, max ]'); | ||
} | ||
if (!objectAttribute || objectAttribute < queryAttribute.value[0] || objectAttribute > queryAttribute.value[1]) return false; | ||
break; | ||
case 'startsWith': | ||
if (!objectAttribute) return false; | ||
// eslint-disable-next-line max-len | ||
if (!_underscore["default"].isString(objectAttribute) || !_underscore["default"].isString(queryAttribute.value)) throw new Error('For startsWith comparator both the object attribute and the query value must be strings.'); | ||
if (!_startsWith(objectAttribute, queryAttribute.value)) return false; | ||
break; | ||
case 'endsWith': | ||
if (!objectAttribute) return false; | ||
// eslint-disable-next-line max-len | ||
if (!_underscore["default"].isString(objectAttribute) || !_underscore["default"].isString(queryAttribute.value)) throw new Error('For endsWith comparator both the object attribute and the query value must be strings.'); | ||
if (!_endsWith(objectAttribute, queryAttribute.value)) return false; | ||
break; | ||
case 'isNull': | ||
if (!_underscore["default"].isNull(objectAttribute)) return false; | ||
break; | ||
case 'isNotNull': | ||
if (_underscore["default"].isNull(objectAttribute)) return false; | ||
break; | ||
case 'contains': | ||
if (!objectAttribute) return false; | ||
if (!_underscore["default"].isString(objectAttribute) && !_underscore["default"].isArray(objectAttribute)) return false; | ||
if (!objectAttribute.includes(queryAttribute.value)) return false; | ||
break; | ||
default: | ||
throw new Error('Invalid comparator ' + queryAttribute.comparator); | ||
} | ||
} else if (_underscore["default"].isArray(queryAttribute)) { | ||
if (!_underscore["default"].contains(queryAttribute, objectAttribute)) return false; | ||
} else { | ||
if (queryAttribute !== objectAttribute) return false; | ||
} | ||
_ret = _loop(); | ||
if (_ret) return _ret.v; | ||
} | ||
@@ -65,0 +112,0 @@ return true; |
{ | ||
"name": "matches-where-query", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"main": "lib/matchesWhereQuery.js", | ||
@@ -21,3 +21,6 @@ "scripts": { | ||
"@babel/preset-env": "^7.9.6", | ||
"@rotundasoftware/eslint-config-rotundasoftware": "^4.1.2", | ||
"chai": "^4.2.0", | ||
"eslint": "^8.33.0", | ||
"eslint-plugin-mocha": "^10.4.3", | ||
"mocha": "^6.2.0" | ||
@@ -24,0 +27,0 @@ }, |
@@ -1,2 +0,1 @@ | ||
var _ = require( 'underscore' ); | ||
var chai = require( 'chai' ); | ||
@@ -7,5 +6,17 @@ var matchesWhereQuery = require( '../lib/matchesWhereQuery' ); | ||
describe( 'Matches where query Test', function() { | ||
var folder; | ||
var person; | ||
beforeEach( function() { | ||
folder = { | ||
childrenElements : [ | ||
{ id : 1, type : 'folder' }, | ||
{ id : 2, type : 'opportunity' } | ||
], | ||
miscArray : [ | ||
[ 1, 2, 3 ], | ||
[ 4, 5, 6 ] | ||
] | ||
}; | ||
person = { | ||
@@ -24,3 +35,3 @@ firstName : 'Martin', | ||
var comparator = { firstName : 'Martin' }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -31,3 +42,3 @@ } ); | ||
var comparator = { firstName : 'Marton' }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.false; | ||
@@ -40,3 +51,3 @@ } ); | ||
var comparator = { firstName : [ 'John', 'Peter', 'Martin' ] }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -55,3 +66,3 @@ } ); | ||
var comparator = { firstName : { comparator : 'doesNotEqual', value : 'Jonh' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -70,3 +81,3 @@ } ); | ||
var comparator = { height : { comparator : 'isGreaterThan', value : 170 } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -85,3 +96,3 @@ } ); | ||
var comparator = { height : { comparator : 'isGreaterThanOrEqualTo', value : 172 } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -99,3 +110,3 @@ } ); | ||
var comparator = { height : { comparator : 'isLessThan', value : 174 } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -113,3 +124,3 @@ } ); | ||
var comparator = { height : { comparator : 'isLessThanOrEqualTo', value : 172 } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -127,3 +138,3 @@ } ); | ||
var comparator = { height : { comparator : 'isBetween', value : [ 171, 176 ] } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -147,3 +158,3 @@ } ); | ||
var comparator = { firstName : { comparator : 'startsWith', value : 'Mar' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -225,3 +236,3 @@ } ); | ||
var comparator = { age : { comparator : 'isNull' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -236,7 +247,7 @@ } ); | ||
} ); | ||
describe( 'isNotNull', function() { | ||
it( 'Match', function() { | ||
var comparator = { firstName : { comparator : 'isNotNull' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
@@ -251,25 +262,29 @@ } ); | ||
} ); | ||
describe( 'isNotNull', function() { | ||
it( 'Match', function() { | ||
var comparator = { firstName : { comparator : 'isNotNull' } }; | ||
describe( 'contains', function() { | ||
it( 'Match - when attribute is string and value is string', function() { | ||
var comparator = { firstName : { comparator : 'contains', value : 'ar' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
} ); | ||
it( 'Not Match', function() { | ||
var comparator = { age : { comparator : 'isNotNull' } }; | ||
it( 'Match - when attribute is array and value is string', function() { | ||
var comparator = { permissions : { comparator : 'contains', value : 'read' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.false; | ||
} ); | ||
} ); | ||
describe( 'contains', function() { | ||
it( 'Match - when string', function() { | ||
var comparator = { firstName : { comparator : 'contains', value : 'ar' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
} ); | ||
it( 'Not Match - when string', function() { | ||
it( 'Match - when attribute is array and value is array', function() { | ||
var comparator = { miscArray : { comparator : 'contains', value : [ 4, 5, 6 ] } }; | ||
expect( matchesWhereQuery( folder, comparator ) ).to.be.true; | ||
} ); | ||
it( 'Match - when attribute is array and value is object', function() { | ||
var comparator = { childrenElements : { comparator : 'contains', value : { id : 1, type : 'folder' } } }; | ||
expect( matchesWhereQuery( folder, comparator ) ).to.be.true; | ||
} ); | ||
it( 'Not Match - when attribute is string and value is string', function() { | ||
var comparator = { firstName : { comparator : 'contains', value : 'no' } }; | ||
@@ -280,9 +295,3 @@ | ||
it( 'Match - when array', function() { | ||
var comparator = { permissions : { comparator : 'contains', value : 'read' } }; | ||
expect( matchesWhereQuery( person, comparator ) ).to.be.true; | ||
} ); | ||
it( 'Not Match - when string', function() { | ||
it( 'Not Match - when attribute is array and value is string', function() { | ||
var comparator = { permissions : { comparator : 'contains', value : 'delete' } }; | ||
@@ -292,3 +301,15 @@ | ||
} ); | ||
it( 'Not Match - when attribute is array and value is array', function() { | ||
var comparator = { miscArray : { comparator : 'contains', value : [ 7, 8, 9 ] } }; | ||
expect( matchesWhereQuery( folder, comparator ) ).to.be.false; | ||
} ); | ||
it( 'Not Match - when attribute is array and value is object', function() { | ||
var comparator = { childrenElements : { comparator : 'contains', value : { id : 3, type : 'folder' } } }; | ||
expect( matchesWhereQuery( folder, comparator ) ).to.be.false; | ||
} ); | ||
} ); | ||
} ); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
22418
351
8
1