Skip to content

Commit

Permalink
Support gt and lt in addition to from and to
Browse files Browse the repository at this point in the history
  • Loading branch information
deitch committed Nov 10, 2014
1 parent 8126b2c commit 9bcf86f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/searchjs.js
Expand Up @@ -3,16 +3,19 @@
var matchObj, matchArray, singleMatch;

singleMatch = function(field,s,text,word) {
var oneMatch = false, t, re, j;
var oneMatch = false, t, re, j, from, to;
// for numbers, exact match; for strings, ignore-case match; for anything else, no match
t = typeof(field);
if (field === null) {
oneMatch = s === null;
} else if (field === undefined) {
oneMatch = false;
} else if (t === "number") {
if (typeof(s) === "object" && (s.from !== undefined || s.to !== undefined)) {
oneMatch = (s.from !== undefined ? field >= s.from : true) && (s.to !== undefined ? field <= s.to: true);
if (typeof(s) === "object" && (s.from !== undefined || s.to !== undefined || s.gt !== undefined || s.lt !== undefined)) {
from = s.from || s.gt;
to = s.to || s.lt;
oneMatch = (s.from !== undefined || s.gt !== undefined ? field >= from : true) &&
(s.to !== undefined || s.lt !== undefined ? field <= to: true);
} else {
oneMatch = field === s;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "searchjs",
"description": "A library for filtering JavaScript objects based on a json SQL-like language, jsql",
"version": "0.3.1",
"version": "0.3.2",
"url": "http://github.com/deitch/searchjs",
"author": "Avi Deitcher <avi@deitcher.net>",
"contributors": [
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Expand Up @@ -34,9 +34,13 @@ searches = [
{search: {city:"Montreal"},results:[0,2]},
{search: {_not:true,city:"Montreal"},results:[1,3,4]},
{search: {age:{from:30}},results:[1,2,3,4]},
{search: {age:{gt:30}},results:[1,2,3,4]},
{search: {age:{from:30,to:34}},results:[1,2,4]},
{search: {age:{gt:30,lt:34}},results:[1,2,4]},
{search: {age:{from:25,to:30}},results:[0,1,2,4]},
{search: {age:{gt:25,lt:30}},results:[0,1,2,4]},
{search: {age:{to:29}},results:[0]},
{search: {age:{lt:29}},results:[0]},
{search: {_not:true,age:{to:29}},results:[1,2,3,4]},
{search: {_not:true,age:{from:30,to:34}},results:[0,3]}
];
Expand Down

0 comments on commit 9bcf86f

Please sign in to comment.