Comparing version 8.3.2 to 8.3.3
@@ -0,1 +1,5 @@ | ||
## 8.3.2 | ||
- Query _properties_ now excpect exact object shape (based on https://github.com/crcn/sift.js/issues/152). E.g: `[{a: { b: 1}}, {a: { b: 1, c: 2}}]].filter(sift({ a: { b: 1} })) === [{a: {b: 1}]`, and `[{a: 1, b: 1}, {a: 1}]].filter(sift({ a: 1 })) === [{a: 1, b: 1}, {a: 1}]`. | ||
## 8.0.0 | ||
@@ -2,0 +6,0 @@ |
112
lib/index.js
@@ -7,15 +7,3 @@ "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; }; | ||
@@ -34,3 +22,3 @@ exports.default = sift; | ||
var typeString = "[object " + type + "]"; | ||
return function(value) { | ||
return function (value) { | ||
return Object.prototype.toString.call(value) === typeString; | ||
@@ -70,3 +58,3 @@ }; | ||
function or(validator) { | ||
return function(a, b) { | ||
return function (a, b) { | ||
if (!isArray(b) || !b.length) { | ||
@@ -86,3 +74,3 @@ return validator(a, b); | ||
function and(validator) { | ||
return function(a, b) { | ||
return function (a, b) { | ||
if (!isArray(b) || !b.length) { | ||
@@ -106,3 +94,3 @@ return validator(a, b); | ||
$eq: or(function(a, b) { | ||
$eq: or(function (a, b) { | ||
return a(b); | ||
@@ -114,3 +102,3 @@ }), | ||
$ne: and(function(a, b) { | ||
$ne: and(function (a, b) { | ||
return !a(b); | ||
@@ -122,3 +110,3 @@ }), | ||
$gt: or(function(a, b) { | ||
$gt: or(function (a, b) { | ||
return compare(comparable(b), a) > 0; | ||
@@ -130,3 +118,3 @@ }), | ||
$gte: or(function(a, b) { | ||
$gte: or(function (a, b) { | ||
return compare(comparable(b), a) >= 0; | ||
@@ -138,3 +126,3 @@ }), | ||
$lt: or(function(a, b) { | ||
$lt: or(function (a, b) { | ||
return compare(comparable(b), a) < 0; | ||
@@ -146,3 +134,3 @@ }), | ||
$lte: or(function(a, b) { | ||
$lte: or(function (a, b) { | ||
return compare(comparable(b), a) <= 0; | ||
@@ -154,3 +142,3 @@ }), | ||
$mod: or(function(a, b) { | ||
$mod: or(function (a, b) { | ||
return b % a[0] == a[1]; | ||
@@ -164,3 +152,3 @@ }), | ||
if (b instanceof Array) { | ||
for (var i = b.length; i--; ) { | ||
for (var i = b.length; i--;) { | ||
if (~a.indexOf(comparable(get(b, i)))) { | ||
@@ -172,7 +160,4 @@ return true; | ||
var comparableB = comparable(b); | ||
if ( | ||
comparableB === b && | ||
(typeof b === "undefined" ? "undefined" : _typeof(b)) === "object" | ||
) { | ||
for (var i = a.length; i--; ) { | ||
if (comparableB === b && (typeof b === "undefined" ? "undefined" : _typeof(b)) === "object") { | ||
for (var i = a.length; i--;) { | ||
if (String(a[i]) === String(b) && String(b) !== "[object Object]") { | ||
@@ -189,3 +174,3 @@ return true; | ||
if (typeof comparableB == "undefined") { | ||
for (var i = a.length; i--; ) { | ||
for (var i = a.length; i--;) { | ||
if (a[i] == null) { | ||
@@ -200,10 +185,6 @@ return true; | ||
*/ | ||
for (var i = a.length; i--; ) { | ||
for (var i = a.length; i--;) { | ||
var validator = createRootValidator(get(a, i), undefined); | ||
var result = validate(validator, b, i, a); | ||
if ( | ||
result && | ||
String(result) !== "[object Object]" && | ||
String(b) !== "[object Object]" | ||
) { | ||
if (result && String(result) !== "[object Object]" && String(b) !== "[object Object]") { | ||
return true; | ||
@@ -259,3 +240,5 @@ } | ||
for (var i = 0, n = a.length; i < n; i++) { | ||
if (validate(get(a, i), b, k, o)) return true; | ||
if (validate(get(a, i), b, k, o)) { | ||
return true; | ||
} | ||
} | ||
@@ -287,3 +270,3 @@ return false; | ||
$regex: or(function(a, b) { | ||
$regex: or(function (a, b) { | ||
return typeof b === "string" && a.test(b); | ||
@@ -326,3 +309,3 @@ }), | ||
if (a instanceof RegExp) { | ||
return function(b) { | ||
return function (b) { | ||
return typeof b === "string" && a.test(b); | ||
@@ -334,7 +317,7 @@ }; | ||
// Special case of a == [] | ||
return function(b) { | ||
return function (b) { | ||
return isArray(b) && !b.length; | ||
}; | ||
} else if (a === null) { | ||
return function(b) { | ||
return function (b) { | ||
//will match both null and undefined | ||
@@ -344,3 +327,3 @@ return b == null; | ||
} | ||
return function(b) { | ||
return function (b) { | ||
return compare(comparable(b), comparable(a)) === 0; | ||
@@ -508,15 +491,11 @@ }; | ||
function parse(options) { | ||
var parseSub = function parseSub(query) { | ||
var useExact = | ||
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
query = comparable(query); | ||
var wrapQuery = function wrapQuery(query) { | ||
if (!query || !isVanillaObject(query)) { | ||
query = { $eq: query }; | ||
} | ||
return query; | ||
}; | ||
if (isExactObject(query) && useExact !== false) { | ||
return createValidator(query, isEqual); | ||
} | ||
var parseQuery = function parseQuery(query) { | ||
query = comparable(query); | ||
@@ -532,5 +511,3 @@ var validators = []; | ||
var expression = | ||
expressions[key] || | ||
(options && options.expressions && options.expressions[key]); | ||
var expression = expressions[key] || options && options.expressions && options.expressions[key]; | ||
@@ -549,12 +526,22 @@ if (expression) { | ||
validators.push(createNestedValidator(keyParts, parseSub(a, true), a)); | ||
validators.push(createNestedValidator(keyParts, parseNested(a), a)); | ||
} | ||
} | ||
return validators.length === 1 | ||
? validators[0] | ||
: createValidator(validators, expressions.$and); | ||
return validators.length === 1 ? validators[0] : createValidator(validators, expressions.$and); | ||
}; | ||
return parseSub; | ||
var parseNested = function parseNested(query) { | ||
query = wrapQuery(query); | ||
if (isExactObject(query)) { | ||
return createValidator(query, isEqual); | ||
} | ||
return parseQuery(query); | ||
}; | ||
var parseRoot = function parseRoot(query) { | ||
return parseQuery(wrapQuery(query)); | ||
}; | ||
return parseRoot; | ||
} | ||
@@ -632,3 +619,3 @@ | ||
var validator = createRootValidator(query, options); | ||
return function(b, k, o) { | ||
return function (b, k, o) { | ||
return validate(validator, b, k, o); | ||
@@ -643,6 +630,3 @@ }; | ||
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) { | ||
@@ -649,0 +633,0 @@ return 1; |
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "8.3.2", | ||
"version": "8.3.3", | ||
"repository": "crcn/sift.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -429,1 +429,21 @@ ## validate objects & filter arrays with mongodb queries | ||
``` | ||
## MongoDB behavior differences | ||
There are some cases where Sift behaves a bit differently than Mongodb. | ||
#### Date comparison | ||
Mongodb allows you to do date comparisons like so: | ||
```javascript | ||
db.collection.find({ createdAt: { $gte: "2018-03-22T06:00:00Z" } }); | ||
``` | ||
In Sift, you'll need to specify a Date object: | ||
```javascript | ||
collection.find( | ||
sift({ createdAt: { $gte: new Date("2018-03-22T06:00:00Z") } }) | ||
); | ||
``` |
@@ -301,12 +301,34 @@ !(function(n, t) { | ||
function d(n) { | ||
var t = function(r, e = !1) { | ||
if ( | ||
(((r = f(r)) && | ||
(function(n) { | ||
return ( | ||
n && (n.constructor === Object || n.constructor === Array) | ||
); | ||
})(r)) || | ||
(r = { $eq: r }), | ||
(function(n) { | ||
var t = function(n) { | ||
return ( | ||
(n && | ||
(function(n) { | ||
return ( | ||
n && (n.constructor === Object || n.constructor === Array) | ||
); | ||
})(n)) || | ||
(n = { $eq: n }), | ||
n | ||
); | ||
}, | ||
r = function(t) { | ||
t = f(t); | ||
var r = []; | ||
for (var u in t) { | ||
var o = t[u]; | ||
if ("$options" !== u) { | ||
var i = s[u] || (n && n.expressions && n.expressions[u]); | ||
if (i) p[u] && (o = p[u](o, t, n)), r.push(g(f(o), i)); | ||
else { | ||
if (36 === u.charCodeAt(0)) | ||
throw new Error("Unknown operation " + u); | ||
var c = u.split("."); | ||
r.push($(c, e(o), o)); | ||
} | ||
} | ||
} | ||
return 1 === r.length ? r[0] : g(r, s.$and); | ||
}, | ||
e = function(n) { | ||
return (function(n) { | ||
return ( | ||
@@ -322,22 +344,9 @@ -1 === | ||
); | ||
})(r) && !1 !== e) | ||
) | ||
return g(r, h); | ||
var u = []; | ||
for (var i in r) { | ||
var c = r[i]; | ||
if ("$options" !== i) { | ||
var a = s[i] || (n && n.expressions && n.expressions[i]); | ||
if (a) p[i] && (c = p[i](c, r, n)), u.push(g(f(c), a)); | ||
else { | ||
if (36 === i.charCodeAt(0)) | ||
throw new Error("Unknown operation " + i); | ||
var l = i.split("."); | ||
u.push($(l, t(c, !0), c)); | ||
} | ||
} | ||
} | ||
return 1 === u.length ? u[0] : g(u, s.$and); | ||
})((n = t(n))) | ||
? g(n, h) | ||
: r(n); | ||
}; | ||
return function(n) { | ||
return r(t(n)); | ||
}; | ||
return t; | ||
} | ||
@@ -384,3 +393,3 @@ function h(n, t) { | ||
function j(n, t) { | ||
if (n === t) return 0; | ||
if (h(n, t)) return 0; | ||
if (typeof n == typeof t) { | ||
@@ -387,0 +396,0 @@ if (n > t) return 1; |
@@ -218,4 +218,7 @@ /* | ||
$or: function(a, b, k, o) { | ||
for (var i = 0, n = a.length; i < n; i++) | ||
if (validate(get(a, i), b, k, o)) return true; | ||
for (var i = 0, n = a.length; i < n; i++) { | ||
if (validate(get(a, i), b, k, o)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
@@ -463,12 +466,11 @@ }, | ||
function parse(options) { | ||
var parseSub = function(query, useExact = false) { | ||
query = comparable(query); | ||
var wrapQuery = function(query) { | ||
if (!query || !isVanillaObject(query)) { | ||
query = { $eq: query }; | ||
} | ||
return query; | ||
}; | ||
if (isExactObject(query) && useExact !== false) { | ||
return createValidator(query, isEqual); | ||
} | ||
var parseQuery = function(query) { | ||
query = comparable(query); | ||
@@ -500,3 +502,3 @@ var validators = []; | ||
validators.push(createNestedValidator(keyParts, parseSub(a, true), a)); | ||
validators.push(createNestedValidator(keyParts, parseNested(a), a)); | ||
} | ||
@@ -510,3 +512,15 @@ } | ||
return parseSub; | ||
var parseNested = function(query) { | ||
query = wrapQuery(query); | ||
if (isExactObject(query)) { | ||
return createValidator(query, isEqual); | ||
} | ||
return parseQuery(query); | ||
}; | ||
var parseRoot = function(query) { | ||
return parseQuery(wrapQuery(query)); | ||
}; | ||
return parseRoot; | ||
} | ||
@@ -513,0 +527,0 @@ |
@@ -76,2 +76,36 @@ import * as assert from "assert"; | ||
], | ||
[ | ||
{ | ||
educations: { | ||
$elemMatch: { | ||
$or: [ | ||
{ | ||
value: "refa", | ||
$or: [{ unfinished: true }] | ||
}, | ||
{ | ||
value: "reno", | ||
$or: [{ unfinished: true }] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
[ | ||
{ | ||
educations: [ | ||
{ value: "refa", unfinished: true }, | ||
{ value: "reno", unfinished: true } | ||
] | ||
} | ||
], | ||
[ | ||
{ | ||
educations: [ | ||
{ value: "refa", unfinished: true }, | ||
{ value: "reno", unfinished: true } | ||
] | ||
} | ||
] | ||
], | ||
// $ne | ||
@@ -141,2 +175,3 @@ [{ $ne: 5 }, [5, "5", 6], ["5", 6]], | ||
], | ||
[ | ||
@@ -244,3 +279,7 @@ { field: { $exists: false } }, | ||
[{ $or: [{ $ne: 1 }, 2] }, [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6]], | ||
[{ $or: [{ a: 1 }, { b: 1 }] }, [{ a: 1, b: 2 }, { a: 1 }], [{ a: 1 }]], | ||
[ | ||
{ $or: [{ a: 1 }, { b: 1 }] }, | ||
[{ a: 1, b: 2 }, { a: 1 }], | ||
[{ a: 1, b: 2 }, { a: 1 }] | ||
], | ||
@@ -252,4 +291,4 @@ // $nor | ||
{ $nor: [{ a: 1 }, { b: 1 }] }, | ||
[{ a: 1, b: 2 }, { a: 1 }], | ||
[{ a: 1, b: 2 }] | ||
[{ a: 1, b: 2 }, { a: 1 }, { c: 1 }], | ||
[{ c: 1 }] | ||
], | ||
@@ -256,0 +295,0 @@ |
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
249561
2783
449