Skip to content

Commit

Permalink
Merge pull request #6 from otolab/feature/14689
Browse files Browse the repository at this point in the history
null, 0, undefined周りの修正
  • Loading branch information
akoba authored Jul 12, 2017
2 parents f353299 + 67152bd commit 83559fa
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
31 changes: 20 additions & 11 deletions lib/filtr.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ _eq = function (a, b) {
if (a instanceof Array) {
return a.some(function(v) {return _eq(v, b)})
}
if (!(b instanceof RegExp))
return a == b;
if (a == null)
if (b instanceof RegExp) {
return b.test(a);
}
if (b === null) {
return (a === null || a === undefined) ? true : false
}
if (a === null) {
return false
return b.test(a);
}
return a == b;
}

_in = function (a, b) {
Expand Down Expand Up @@ -139,7 +144,8 @@ Filtr.comparators = {
}

, $exists: function (a, b) {
return !!a == b;
var f = !(a === undefined)
return f === !!b;
}

, $mod: function (a, b) {
Expand Down Expand Up @@ -332,7 +338,7 @@ function getPathValue (parsed, obj) {

function getPathValues (parsed, obj) {

if (obj == null)
if (obj === null)
return [];

var part = parsed[0];
Expand All @@ -349,12 +355,12 @@ function getPathValues (parsed, obj) {
var res = obj[p];

if ( (parsed.length == 1) ) {
if (res != null) {
// if (res != null) {
if(Array.isArray(res))
return res
return [res]
}
return []
// }
// return []
}

if (res != null) {
Expand Down Expand Up @@ -503,7 +509,9 @@ function parseQuery (query) {
|| 'number' == typeof params
|| 'boolean' == typeof params
|| Array.isArray(params)
|| params instanceof RegExp) {
|| params instanceof RegExp
|| params === null
|| params === undefined) {
qry.test = parseFilter({ $eq: params });
qry.path = parsePath(cmd);
} else {
Expand Down Expand Up @@ -539,7 +547,8 @@ function parseFilter (query) {
var p = params[i];
if ('string' == typeof p
|| 'number' == typeof p
|| 'boolean' == typeof p) {
|| 'boolean' == typeof p
|| p === null || p === undefined) {
traverse = false
} else {
var nq = parseQuery(p);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Jake Luer <[email protected]>",
"name": "filtr",
"description": "Array filtering for node.js and the browser inspired by MongoDB.",
"version": "0.3.0",
"version": "0.3.3",
"repository": {
"type": "git",
"url": "git://github.com/logicalparadox/filtr.git"
Expand Down
17 changes: 16 additions & 1 deletion test/comparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ describe('comparator', function () {
it('$lt should work', function () {
comparator.$lt(0,1).should.be.true;
comparator.$lt(1,0).should.be.false;

comparator.$lt(0, 2).should.be.true;
comparator.$lt(1, 2).should.be.true;
comparator.$lt(2, 2).should.be.false;
comparator.$lt(3, 2).should.be.false;
});

it('$lte should work', function () {
comparator.$lte(0,1).should.be.true;
comparator.$lte(1,1).should.be.true;
comparator.$lte(1,0).should.be.false;

comparator.$lte(0, 2).should.be.true;
comparator.$lte(1, 2).should.be.true;
comparator.$lte(2, 2).should.be.true;
comparator.$lte(3, 2).should.be.false;

});

it('$regex should work with string regex pattern', function () {
Expand All @@ -55,13 +66,17 @@ describe('comparator', function () {

it('$exists should work', function () {
var a = undefined
, b = {c: 'hi'};
, b = {c: 'hi', z: 0, n: null};
comparator.$exists(a, false).should.be.true;
comparator.$exists(a, true).should.be.false;
comparator.$exists(b, true).should.be.true;
comparator.$exists(b.c, false).should.be.false;
comparator.$exists(b.a, false).should.be.true;
comparator.$exists('hi', true).should.be.true;
comparator.$exists(b.z, true).should.be.true;
comparator.$exists(b.z, false).should.be.false;
comparator.$exists(b.n, true).should.be.true;
comparator.$exists(b.n, false).should.be.false;
});

it('$mod should work', function () {
Expand Down
63 changes: 63 additions & 0 deletions test/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ describe('Query', function () {
Q.test(11, { type: 'single' }).should.be.false;
});

it('should parse a single query 2', function () {
var query = { $lt: 2 }
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test(0, { type: 'single' }).should.be.true;
Q.test(1, { type: 'single' }).should.be.true;
Q.test(2, { type: 'single' }).should.be.false;
Q.test(3, { type: 'single' }).should.be.false;
});


it('should parse a lengthed query', function () {
var query = { $lt: 10, $gt: 5 }
, Q = filtr(query);
Expand Down Expand Up @@ -159,13 +170,40 @@ describe('Query', function () {
Q.test({ hello: 'universe' }, { type: 'single' }).should.be.true;
});

it('should assume $eq if no comparator provided - string("null")', function () {
var query = { 'hello': 'null' }
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({ hello: 'null' }, { type: 'single' }).should.be.true;
});

it('should assume $eq if no comparator provided - number', function () {
var query = { 'hello': 42 }
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({ hello: 42 }, { type: 'single' }).should.be.true;
});

it('should assume $eq if no comparator provided - number(0)', function () {
var query = { 'hello': 0 }
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({ hello: 0 }, { type: 'single' }).should.be.true;
Q.test({}, { type: 'single' }).should.be.false;
Q.test({ hello: null }, { type: 'single' }).should.be.false;
});

it('should assume $eq if no comparator provided - null', function () {
var query = { 'hello': null }
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({ hello: 0 }, { type: 'single' }).should.be.false;
Q.test({ hello: null }, { type: 'single' }).should.be.true;

// https://docs.mongodb.com/v3.2/tutorial/query-for-null-fields/
Q.test({}, { type: 'single' }).should.be.true;
});

it('should assume $eq if no comparator provided - boolean', function () {
var query = { 'hello': true }
, Q = filtr(query);
Expand Down Expand Up @@ -268,6 +306,31 @@ describe('Query', function () {
Q.test({sets: ['Firefox', 'abc']}, {type: 'single'}).should.be.false;
});

it('should regexp $ne work', function () {
var query = {'sets': {$ne: /Chrome/}}
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({sets: ['Chrome', 'abc']}, {type: 'single'}).should.be.false;
Q.test({sets: ['Firefox', 'abc']}, {type: 'single'}).should.be.true;
});

it('should regexp $eq work', function () {
var query = {'sets': {$eq: /Chrome/}}
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({sets: ['Chrome', 'abc']}, {type: 'single'}).should.be.true;
Q.test({sets: ['Firefox', 'abc']}, {type: 'single'}).should.be.false;
});

it('should regexp $eq work', function () {
var query = {'sets': /Chrome/}
, Q = filtr(query);
Q.stack.should.have.length(1);
Q.test({sets: ['Chrome', 'abc']}, {type: 'single'}).should.be.true;
Q.test({sets: ['Firefox', 'abc']}, {type: 'single'}).should.be.false;
});


});

// TODO: All nesting options.
Expand Down

0 comments on commit 83559fa

Please sign in to comment.