Now with support for regular expressions

pull/2/head
Louis Chatriot 12 years ago
parent 066b55df10
commit f1b7c96686
  1. 13
      lib/model.js
  2. 6466
      out.js
  3. 2
      package.json
  4. 27
      test/model.test.js

@ -415,6 +415,15 @@ function getDotValue (obj, field) {
function areThingsEqual (a, b) {
var aKeys , bKeys , i;
// String against regexp
if (util.isRegExp(b)) {
if (typeof a !== 'string') {
return false
} else {
return b.test(a);
}
}
// Strings, booleans, numbers, null
if (a === null || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'number' ||
b === null || typeof b === 'string' || typeof b === 'boolean' || typeof b === 'number') { return a === b; }
@ -426,7 +435,7 @@ function areThingsEqual (a, b) {
// undefined (no match since they mean field doesn't exist and can't be serialized)
if (util.isArray(a) || util.isArray(b) || a === undefined || b === undefined) { return false; }
// Objects (check for deep equality)
// General objects (check for deep equality)
// a and b should be objects at this point
try {
aKeys = Object.keys(a);
@ -609,7 +618,7 @@ function matchQueryPart (obj, queryKey, queryValue) {
// queryValue is an actual object. Determine whether it contains comparison operators
// or only normal fields. Mixed objects are not allowed
if (queryValue !== null && typeof queryValue === 'object') {
if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) {
keys = Object.keys(queryValue);
firstChars = _.map(keys, function (item) { return item[0]; });
dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; });

6466
out.js

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{
"name": "nedb",
"version": "0.7.14",
"version": "0.7.15",
"author": {
"name": "tldr.io",
"email": "hello@tldr.io"

@ -729,6 +729,33 @@ describe('Model', function () {
});
describe('Regular expression matching', function () {
it('Matching a non-string to a regular expression always yields false', function () {
var d = new Date()
, r = new RegExp(d.getTime());
model.match({ test: true }, { test: /true/ }).should.equal(false);
model.match({ test: null }, { test: /null/ }).should.equal(false);
model.match({ test: 42 }, { test: /42/ }).should.equal(false);
model.match({ test: d }, { test: r }).should.equal(false);
});
it('Can match strings', function () {
model.match({ test: 'true' }, { test: /true/ }).should.equal(true);
model.match({ test: 'babaaaar' }, { test: /aba+r/ }).should.equal(true);
model.match({ test: 'babaaaar' }, { test: /^aba+r/ }).should.equal(false);
model.match({ test: 'true' }, { test: /t[ru]e/ }).should.equal(false);
});
it('Can use dot-notation', function () {
model.match({ test: { nested: 'true' } }, { 'test.nested': /true/ }).should.equal(true);
model.match({ test: { nested: 'babaaaar' } }, { 'test.nested': /^aba+r/ }).should.equal(false);
});
});
describe('$lt', function () {
it('Cannot compare a field to an object, an array, null or a boolean, it will return false', function () {

Loading…
Cancel
Save