Permalink
Browse files

Update to 0.1.3, able to match on keys of hashes

  • Loading branch information...
deitch committed May 1, 2012
1 parent 73fc3c8 commit 67f2d5477ce51cb76544b133c878977fc6cea48a
Showing with 13 additions and 5 deletions.
  1. +5 −1 README.md
  2. +2 −0 lib/searchjs.js
  3. +1 −1 package.json
  4. +5 −3 test.js
View
@@ -20,6 +20,10 @@ As of version 0.1.1, several enhancements have been added:
3. Combine Record Array with Matcher Array: If both the record field value is an array and the matcher value is an array, then it will return true if any one value in the record array matches any one value in the matcher field. See example 10.
4. Negater: The "_not" negater can be used with Record Array and Matcher Array. See example 11.
+As of version 0.1.3, several additional enhancements:
+
+5. Record Object Field: If the field in the object is itself an object, then the match is done to each key in the array, and returns true if one or more matches. See example 8.
+
Examples
--------
Some examples:
@@ -31,7 +35,7 @@ Some examples:
5. {_not: true, name: "John", age: 30} - all records that have name !== "John" (ignore-case) AND age !== 30
6. {_not: true, _join: "OR", name: "John", age: 30} - all records that have name !== "John" (ignore-case) OR age !== 30
7. {_join: "OR", terms: [{name:"John", age:30},{age:35}]} - all records that have (name === "John" && age === 30) || (age === 35)
-8. {email: "john@foo.com"} - all records that have the email === "john@foo.com", if the record has email as a string, or if email is an array, then each element is checked. Both of the following will match: {email:"john@foo.com"} and {email:["john@foo.com","js@gmail.com"]}
+8. {email: "john@foo.com"} - all records that have the email === "john@foo.com", if the record has email as a string; or if email is an array, then each element is checked; or if email is an object, then the keys are checked. All of the following will match: {email:"john@foo.com"} and {email:["john@foo.com","js@gmail.com"]} and {email:{"john@foo.com":true}}
9. {name:["John","Jill"]} - all records that have name === "John" || name === "Jill"
10. {email:["john@foo.com","jf@gmail.com"]} - all records that have email === "john@foo.com" || email === "jf@gmail.com" OR email in the record is an array, and at least one value in that array is "john@foo.com" or "jf@gmail.com"
11. {_not: true, name:["John","Jill"]} - all records that have name !== "John" && name !== "Jill"
View
@@ -23,6 +23,8 @@
break;
}
}
+ } else if (typeof(field) === "object") {
+ oneMatch = field[s] !== undefined;
}
return(oneMatch);
};
View
@@ -1,7 +1,7 @@
{
"name": "searchjs",
"description": "A library for filtering JavaScript objects based on a json SQL-like language, jsql",
- "version": "0.1.2",
+ "version": "0.1.3",
"url": "http://github.com/deitch/searchjs",
"author": "Avi Deitcher <avi@deitcher.net>",
"engines": ["node >= 0.3"],
View
@@ -3,9 +3,9 @@ var assert = require('assert'), search = require("./lib/searchjs");
var runTest, data, searches;
data = [
- {name:"Alice",age:25, email: "alice@searchjs.com"},
+ {name:"Alice",age:25, email: "alice@searchjs.com",city:{"Montreal":"first","Toronto":"second"}},
{name:"Brian",age:30, email: "brian@searchjs.com"},
- {name:"Carrie",age:30, email: "carrie@searchjs.com"},
+ {name:"Carrie",age:30, email: "carrie@searchjs.com",city:{"Montreal":true,"New York":false}},
{name:"David",age:35, email: "david@searchjs.com"},
{name:"Alice",age:30, email: ["alice@searchjs.com","alice@gmail.com"]}
];
@@ -23,7 +23,9 @@ searches = [
{search: {name: ["Brian","Carrie"]},results: [1,2]},
{search: {email: ["alice@searchjs.com","carrie@searchjs.com"]},results: [0,2,4]},
{search: {_not: true, name: ["Brian","Carrie"]},results: [0,3,4]},
- {search: {_not:true, email: ["alice@searchjs.com","carrie@searchjs.com"]},results: [1,3]}
+ {search: {_not:true, email: ["alice@searchjs.com","carrie@searchjs.com"]},results: [1,3]},
+ {search: {city:"Montreal"},results:[0,2]},
+ {search: {_not:true,city:"Montreal"},results:[1,3,4]}
];

0 comments on commit 67f2d54

Please sign in to comment.