Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sift

Package Overview
Dependencies
Maintainers
2
Versions
155
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sift - npm Package Compare versions

Comparing version 8.5.1 to 9.0.0

278

lib/index.js

@@ -7,6 +7,16 @@ "use strict";

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _typeof =
typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
? function(obj) {
return typeof obj;
}
: function(obj) {
return obj &&
typeof Symbol === "function" &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? "symbol"
: typeof obj;
};
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
exports.default = sift;

@@ -25,3 +35,3 @@ exports.compare = compare;

var typeString = "[object " + type + "]";
return function (value) {
return function(value) {
return Object.prototype.toString.call(value) === typeString;

@@ -42,24 +52,7 @@ };

var nestable = function nestable(validator) {
return function (validateOptions, value, key, valueOwner, nestedResults) {
if (nestedResults) {
return Boolean(nestedResults.find(function (_ref) {
var _ref2 = _slicedToArray(_ref, 3),
value = _ref2[0],
key = _ref2[1],
valueOwner = _ref2[2];
return validator(validateOptions, key, valueOwner);
}));
}
return validator(validateOptions, value, key, valueOwner);
};
};
/**
*/
var or = nestable(function (validator) {
return function (validateOptions, value, key, valueOwner, nestedResults) {
function or(validator) {
return function(validateOptions, value) {
if (!isArray(value) || !value.length) {

@@ -73,3 +66,3 @@ return validator(validateOptions, value);

};
});
}

@@ -80,8 +73,8 @@ /**

function and(validator) {
return function (validateOptions, value, key, valueOwner) {
return function(validateOptions, value) {
if (!isArray(value) || !value.length) {
return validator(validateOptions, value, key, valueOwner);
return validator(validateOptions, value);
}
for (var i = 0, n = value.length; i < n; i++) {
if (!validator(validateOptions, get(value, i), value, valueOwner)) return false;
if (!validator(validateOptions, get(value, i))) return false;
}

@@ -92,4 +85,4 @@ return true;

function _validate(validator, value, key, valueOwner, nestedResults) {
return validator.validate(validator.options, value, key, valueOwner, nestedResults);
function _validate(validator, value, key, valueOwner) {
return validator.validate(validator.options, value, key, valueOwner);
}

@@ -101,3 +94,3 @@

$eq: or(function (test, value) {
$eq: or(function(test, value) {
return test(value);

@@ -109,3 +102,3 @@ }),

$ne: and(function (test, value) {
$ne: and(function(test, value) {
return test(value);

@@ -117,3 +110,3 @@ }),

$gt: or(function (test, value) {
$gt: or(function(test, value) {
return test(value);

@@ -125,3 +118,3 @@ }),

$gte: or(function (test, value) {
$gte: or(function(test, value) {
return test(value);

@@ -133,3 +126,3 @@ }),

$lt: or(function (test, value) {
$lt: or(function(test, value) {
return test(value);

@@ -141,3 +134,3 @@ }),

$lte: or(function (test, value) {
$lte: or(function(test, value) {
return test(value);

@@ -149,3 +142,3 @@ }),

$mod: or(function (test, value) {
$mod: or(function(test, value) {
return test(value);

@@ -161,3 +154,2 @@ }),

/**

@@ -187,4 +179,4 @@ */

$all: function $all(allOptions, value, key, valueOwner, nestedResults) {
return defaultExpressions.$and(allOptions, value, key, valueOwner, nestedResults);
$all: function $all(allOptions, value, key, valueOwner) {
return defaultExpressions.$and(allOptions, value, key, valueOwner);
},

@@ -221,16 +213,7 @@

$and: function $and(validateOptions, value, key, valueOwner, nestedResults) {
if (nestedResults) {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!_validate(get(validateOptions, i), value, key, valueOwner, nestedResults)) {
return false;
}
$and: function $and(validateOptions, value, key, valueOwner) {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!_validate(get(validateOptions, i), value, key, valueOwner)) {
return false;
}
} else {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!_validate(get(validateOptions, i), value, key, valueOwner, nestedResults)) {
return false;
}
}
}

@@ -243,3 +226,3 @@ return true;

$regex: or(function (validateOptions, value) {
$regex: or(function(validateOptions, value) {
return typeof value === "string" && validateOptions.test(value);

@@ -280,8 +263,8 @@ }),

$eq: function $eq(query, queryOwner, _ref3) {
var comparable = _ref3.comparable,
compare = _ref3.compare;
$eq: function $eq(query, queryOwner, _ref) {
var comparable = _ref.comparable,
compare = _ref.compare;
if (query instanceof RegExp) {
return or(function (value) {
return or(function(value) {
return typeof value === "string" && query.test(value);

@@ -293,7 +276,7 @@ });

// Special case of a == []
return or(function (value) {
return or(function(value) {
return isArray(value) && !value.length;
});
} else if (query === null) {
return or(function (value) {
return or(function(value) {
//will match both null and undefined

@@ -303,3 +286,4 @@ return value == null;

}
return or(function (value) {
return or(function(value) {
return compare(comparable(value), comparable(query)) === 0;

@@ -309,7 +293,7 @@ });

$gt: function $gt(query, queryOwner, _ref4) {
var comparable = _ref4.comparable,
compare = _ref4.compare;
$gt: function $gt(query, queryOwner, _ref2) {
var comparable = _ref2.comparable,
compare = _ref2.compare;
return function (value) {
return function(value) {
return compare(comparable(value), comparable(query)) > 0;

@@ -319,7 +303,7 @@ };

$gte: function $gte(query, queryOwner, _ref5) {
var comparable = _ref5.comparable,
compare = _ref5.compare;
$gte: function $gte(query, queryOwner, _ref3) {
var comparable = _ref3.comparable,
compare = _ref3.compare;
return function (value) {
return function(value) {
return compare(comparable(value), comparable(query)) >= 0;

@@ -329,15 +313,15 @@ };

$lt: function $lt(query, queryOwner, _ref6) {
var comparable = _ref6.comparable,
compare = _ref6.compare;
$lt: function $lt(query, queryOwner, _ref4) {
var comparable = _ref4.comparable,
compare = _ref4.compare;
return function (value) {
return function(value) {
return compare(comparable(value), comparable(query)) < 0;
};
},
$lte: function $lte(query, queryOwner, _ref7) {
var comparable = _ref7.comparable,
compare = _ref7.compare;
$lte: function $lte(query, queryOwner, _ref5) {
var comparable = _ref5.comparable,
compare = _ref5.compare;
return function (value) {
return function(value) {
return compare(comparable(value), comparable(query)) <= 0;

@@ -350,5 +334,5 @@ };

return function (value) {
return function(value) {
if (value instanceof Array) {
for (var i = value.length; i--;) {
for (var i = value.length; i--; ) {
if (~query.indexOf(comparable(get(value, i)))) {

@@ -360,5 +344,12 @@ return true;

var comparableValue = comparable(value);
if (comparableValue === value && (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object") {
for (var i = query.length; i--;) {
if (String(query[i]) === String(value) && String(value) !== "[object Object]") {
if (
comparableValue === value &&
(typeof value === "undefined" ? "undefined" : _typeof(value)) ===
"object"
) {
for (var i = query.length; i--; ) {
if (
String(query[i]) === String(value) &&
String(value) !== "[object Object]"
) {
return true;

@@ -374,3 +365,3 @@ }

if (typeof comparableValue == "undefined") {
for (var i = query.length; i--;) {
for (var i = query.length; i--; ) {
if (query[i] == null) {

@@ -385,6 +376,10 @@ return true;

*/
for (var i = query.length; i--;) {
for (var i = query.length; i--; ) {
var validator = createRootValidator(get(query, i), options);
var result = _validate(validator, comparableValue, i, query);
if (result && String(result) !== "[object Object]" && String(comparableValue) !== "[object Object]") {
if (
result &&
String(result) !== "[object Object]" &&
String(comparableValue) !== "[object Object]"
) {
return true;

@@ -403,3 +398,3 @@ }

var eq = prepare.$in(query, queryOwner, options);
return function (validateOptions, value, key, valueOwner) {
return function(validateOptions, value, key, valueOwner) {
return !eq(validateOptions, value, key, valueOwner);

@@ -410,3 +405,3 @@ };

$mod: function $mod(query) {
return function (value) {
return function(value) {
return value % query[0] == query[1];

@@ -421,3 +416,3 @@ };

var eq = prepare.$eq(query, queryOwner, options);
return and(function (validateOptions, value, key, valueOwner) {
return and(function(validateOptions, value, key, valueOwner) {
return !eq(validateOptions, value, key, valueOwner);

@@ -460,3 +455,3 @@ });

var validateOptions = parse(options)(query);
return function (value, key, valueOwner) {
return function(value, key, valueOwner) {
return !_validate(validateOptions, value, key, valueOwner);

@@ -467,4 +462,6 @@ };

$type: function $type(query) {
return function (value, key, valueOwner) {
return value != void 0 ? value instanceof query || value.constructor == query : false;
return function(value, key, valueOwner) {
return value != void 0
? value instanceof query || value.constructor == query
: false;
};

@@ -484,3 +481,5 @@ },

$where: function $where(query) {
return typeof query === "string" ? new Function("obj", "return " + query) : query;
return typeof query === "string"
? new Function("obj", "return " + query)
: query;
},

@@ -527,17 +526,13 @@

function validatedNested(_ref8, value) {
var keyPath = _ref8.keyPath,
child = _ref8.child,
query = _ref8.query;
function validatedNested(_ref6, value) {
var keyPath = _ref6.keyPath,
child = _ref6.child,
query = _ref6.query;
var results = [];
findValues(value, keyPath, 0, value, results);
var values = [];
findValues(value, keyPath, 0, value, values);
if (results.length === 1) {
var _results$ = _slicedToArray(results[0], 3),
_value = _results$[0],
key = _results$[1],
valueOwner = _results$[2];
return _validate(child, _value, key, valueOwner);
if (values.length === 1) {
var first = values[0];
return _validate(child, first[0], first[1], first[2]);
}

@@ -548,21 +543,12 @@

var allValid = inclusive;
var allValues = results.map(function (_ref9) {
var _ref10 = _slicedToArray(_ref9, 1),
value = _ref10[0];
return value;
});
return _validate(child, undefined, undefined, undefined, results);
// for (var i = 0; i < results.length; i++) {
// const [value, key, valueOwner] = results[i];
// var isValid = validate(child, value, key, valueOwner);
// console.log(isValid, value);
// if (inclusive) {
// allValid &= isValid;
// } else {
// allValid |= isValid;
// }
// }
// return allValid;
for (var i = 0; i < values.length; i++) {
var result = values[i];
var isValid = _validate(child, result[0], result[1], result[2]);
if (inclusive) {
allValid &= isValid;
} else {
allValid |= isValid;
}
}
return allValid;
}

@@ -597,3 +583,6 @@

function createNestedValidator(keyPath, child, query) {
return createValidator({ keyPath: keyPath, child: child, query: query }, validatedNested);
return createValidator(
{ keyPath: keyPath, child: child, query: query },
validatedNested
);
}

@@ -606,3 +595,10 @@

function isVanillaObject(value) {
return value && (value.constructor === Object || value.constructor === Array || value.constructor.toString() === "function Object() { [native code] }" || value.constructor.toString() === "function Array() { [native code] }");
return (
value &&
(value.constructor === Object ||
value.constructor === Array ||
value.constructor.toString() === "function Object() { [native code] }" ||
value.constructor.toString() === "function Array() { [native code] }") &&
!value.toJSON
);
}

@@ -612,3 +608,3 @@

var comparable = options.comparable,
expressions = options.expressions;
expressions = options.expressions;

@@ -634,3 +630,4 @@ var wrapQuery = function wrapQuery(query) {

var expression = defaultExpressions[key] || options && expressions && expressions[key];
var expression =
defaultExpressions[key] || (options && expressions && expressions[key]);

@@ -649,7 +646,11 @@ if (expression) {

validators.push(createNestedValidator(keyParts, parseNested(queryValue), queryValue));
validators.push(
createNestedValidator(keyParts, parseNested(queryValue), queryValue)
);
}
}
return validators.length === 1 ? validators[0] : createValidator(validators, defaultExpressions.$and);
return validators.length === 1
? validators[0]
: createValidator(validators, defaultExpressions.$and);
};

@@ -730,3 +731,8 @@

validate: function validate(validateOptions, value, key, valueOwner) {
return _validate(validateOptions, value && options.select(value), key, valueOwner);
return _validate(
validateOptions,
value && options.select(value),
key,
valueOwner
);
}

@@ -742,5 +748,8 @@ };

function sift(query, options) {
options = Object.assign({ compare: compare, comparable: comparable }, options);
options = Object.assign(
{ compare: compare, comparable: comparable },
options
);
var validator = createRootValidator(query, options);
return function (value, key, valueOwner) {
return function(value, key, valueOwner) {
return _validate(validator, value, key, valueOwner);

@@ -755,3 +764,6 @@ };

if (isEqual(a, b)) return 0;
if ((typeof a === "undefined" ? "undefined" : _typeof(a)) === (typeof b === "undefined" ? "undefined" : _typeof(b))) {
if (
(typeof a === "undefined" ? "undefined" : _typeof(a)) ===
(typeof b === "undefined" ? "undefined" : _typeof(b))
) {
if (a > b) {

@@ -758,0 +770,0 @@ return 1;

{
"name": "sift",
"description": "mongodb query style array filtering",
"version": "8.5.1",
"version": "9.0.0",
"repository": "crcn/sift.js",

@@ -6,0 +6,0 @@ "author": {

@@ -345,10 +345,11 @@ !(function(n, t) {

return (
(n &&
((t = n) &&
(t.constructor === Object ||
t.constructor === Array ||
"function Object() { [native code] }" ===
t.constructor.toString() ||
"function Array() { [native code] }" ===
t.constructor.toString()))) ||
(!n ||
(!(t = n) ||
(t.constructor !== Object &&
t.constructor !== Array &&
"function Object() { [native code] }" !==
t.constructor.toString() &&
"function Array() { [native code] }" !==
t.constructor.toString()) ||
t.toJSON)) &&
(n = { $eq: n }),

@@ -355,0 +356,0 @@ n

@@ -27,16 +27,7 @@ /*

const nestable = (validator) => (validateOptions, value, key, valueOwner, nestedResults) => {
if (nestedResults) {
return Boolean(nestedResults.find(([value, key, valueOwner]) => validator(validateOptions, key, valueOwner)));
}
return validator(validateOptions, value, key, valueOwner);
};
/**
*/
const or = nestable((validator) => {
return function(validateOptions, value, key, valueOwner, nestedResults) {
function or(validator) {
return function(validateOptions, value) {
if (!isArray(value) || !value.length) {

@@ -50,3 +41,3 @@ return validator(validateOptions, value);

};
});
}

@@ -57,8 +48,8 @@ /**

function and(validator) {
return function(validateOptions, value, key, valueOwner) {
return function(validateOptions, value) {
if (!isArray(value) || !value.length) {
return validator(validateOptions, value, key, valueOwner);
return validator(validateOptions, value);
}
for (var i = 0, n = value.length; i < n; i++) {
if (!validator(validateOptions, get(value, i), value, valueOwner)) return false;
if (!validator(validateOptions, get(value, i))) return false;
}

@@ -69,4 +60,4 @@ return true;

function validate(validator, value, key, valueOwner, nestedResults) {
return validator.validate(validator.options, value, key, valueOwner, nestedResults);
function validate(validator, value, key, valueOwner) {
return validator.validate(validator.options, value, key, valueOwner);
}

@@ -155,4 +146,4 @@

$all: function(allOptions, value, key, valueOwner, nestedResults) {
return defaultExpressions.$and(allOptions, value, key, valueOwner, nestedResults);
$all: function(allOptions, value, key, valueOwner) {
return defaultExpressions.$and(allOptions, value, key, valueOwner);
},

@@ -189,16 +180,7 @@

$and: function(validateOptions, value, key, valueOwner, nestedResults) {
if (nestedResults) {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!validate(get(validateOptions, i), value, key, valueOwner, nestedResults)) {
return false;
}
$and: function(validateOptions, value, key, valueOwner) {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!validate(get(validateOptions, i), value, key, valueOwner)) {
return false;
}
} else {
for (var i = 0, n = validateOptions.length; i < n; i++) {
if (!validate(get(validateOptions, i), value, key, valueOwner, nestedResults)) {
return false;
}
}
}

@@ -265,2 +247,3 @@ return true;

}
return or(function(value) {

@@ -475,8 +458,8 @@ return compare(comparable(value), comparable(query)) === 0;

function validatedNested({ keyPath, child, query }, value) {
const results = [];
findValues(value, keyPath, 0, value, results);
if (results.length === 1) {
const [value, key, valueOwner] = results[0];
return validate(child, value, key, valueOwner);
var values = [];
findValues(value, keyPath, 0, value, values);
if (values.length === 1) {
var first = values[0];
return validate(child, first[0], first[1], first[2]);
}

@@ -487,16 +470,12 @@

var allValid = inclusive;
const allValues = results.map(([value]) => value);
return validate(child, undefined, undefined, undefined, results);
// for (var i = 0; i < results.length; i++) {
// const [value, key, valueOwner] = results[i];
// var isValid = validate(child, value, key, valueOwner);
// console.log(isValid, value);
// if (inclusive) {
// allValid &= isValid;
// } else {
// allValid |= isValid;
// }
// }
// return allValid;
for (var i = 0; i < values.length; i++) {
var result = values[i];
var isValid = validate(child, result[0], result[1], result[2]);
if (inclusive) {
allValid &= isValid;
} else {
allValid |= isValid;
}
}
return allValid;
}

@@ -544,3 +523,4 @@

value.constructor.toString() === "function Object() { [native code] }" ||
value.constructor.toString() === "function Array() { [native code] }")
value.constructor.toString() === "function Array() { [native code] }") &&
!value.toJSON
);

@@ -547,0 +527,0 @@ }

@@ -280,3 +280,3 @@ import * as assert from "assert";

it("works woth toJSON", () => {
it("works with toJSON", () => {
function ObjectId(value) {

@@ -297,2 +297,17 @@ // primitive implementation

it("Works if prop has toJSON", () => {
function Creator(value) {
// primitive implementation
return {
toJSON: () => value
};
}
const test1 = sift({
creator: Creator("1")
});
assert.equal(test1({ creator: Creator("1") }), true);
});
// https://github.com/crcn/sift.js/issues/159

@@ -314,37 +329,2 @@ it("can sift with a regexp string", () => {

});
// https://github.com/crcn/sift.js/issues/160
it('can work with nested $all', () => {
const items = [{
"order" : {
"id" : "or_0001",
"amount" : 6000,
"items" : [
{
"product" : "poster",
"sku" : "P18x24",
"quantity" : 1,
"amount" : 3000
},
{
"product" : "frame",
"sku" : "P18x24",
"quantity" : 1,
"amount" : 3000
},
{
"product" : "shipping",
"sku" : "shipping",
"quantity" : 1,
"amount" : 5000
}
]
}
}];
const results = items.filter(sift({ 'order.items.product': { $all: ['poster', 'frame'] } }));
assert.equal(results.length, 1);
});
});

@@ -5,4 +5,2 @@ import * as assert from "assert";

describe(__filename + "#", function() {

@@ -9,0 +7,0 @@ [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc