Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

stringification of dotted props with tests

  • Loading branch information...
commit be2de2c2d85523eb09af8c3b050780144122b257 1 parent 704ddd7
@dvv dvv authored
View
4 lib/js-array.js
@@ -58,12 +58,12 @@ exports.operators = {
},
any: filter(function(array, value){
if(typeof value == "function"){
- return array.some(function(i){
+ return array instanceof Array && array.some(function(i){
return value([i]).length;
});
}
else{
- return array.indexOf(value) > -1;
+ return array instanceof Array && array.indexOf(value) > -1;
}
}),
all: filter(function(array, value){
View
22 lib/parser.js
@@ -159,28 +159,6 @@ exports.parseGently = function(){
return terms;
}
-/*
- * reduces query to simple SQL-ish one
- *
- * that is only cond1(args1)&...condN(argsN)&sort(...)&limit(...)&select(...)[&values()] are supported and no hierarchy is supported
- */
-exports.parseSimple = function(){
- var terms;
- try {
- // vanilla parse
- terms = parse.apply(this, arguments);
- // delete any doubled lastSeens
-
- terms = {
- sele: 1
- };
- } catch(err) {
- terms = new exports.Query();
- terms.error = err.message;
- }
- return terms;
-}
-
exports.commonOperatorMap = {
"and" : "&",
"or" : "|",
View
12 lib/query.js
@@ -9,8 +9,8 @@
* // for each object that matches the query
* });
*/
-({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"));}}).
-define(["exports", "./parser"], function(exports, parser){
+({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"), require("./js-array"));}}).
+define(["exports", "./parser", "./js-array"], function(exports, parser, jsarray){
var parseQuery = parser.parseQuery;
try{
@@ -28,7 +28,8 @@ parser.Query = function(seed, params){
return q;
};
exports.Query = parser.Query;
-exports.knownOperators = ["and", "or", "eq", "ne", "le", "lt", "gt", "ge", "sort", "in", "notin", "select", "exclude", "contains", "notcontains", "values","aggregate","distinct","limit","recurse"];
+//exports.knownOperators = ["and", "or", "eq", "ne", "le", "lt", "gt", "ge", "sort", "in", "notin", "select", "exclude", "contains", "notcontains", "values","aggregate","distinct","limit","recurse"];
+exports.knownOperators = Object.keys(jsarray.operators).concat(Object.keys(jsarray.jsOperatorMap));
exports.knownScalarOperators = ["mean", "sum", "min", "max", "count", "first", "one"];
exports.arrayMethods = ["forEach", "reduce", "map", "filter", "indexOf", "some", "every"];
@@ -53,7 +54,10 @@ function queryToString(part) {
return [
part.name,
"(",
- part.args.map(function(arg) {
+ part.args.map(function(arg, pos) {
+ // N.B. args[0] should be joined with '/' if it's an array
+ if (pos === 0 && arg instanceof Array)
+ return arg.map(function(x){return queryToString(x);}).join('/');
return queryToString(arg);
}).join(","),
")"
View
8 test/js-array.js
@@ -32,6 +32,14 @@ exports.testFiltering = function() {
assert.equal(executeQuery("not(all(tags,even))", {}, data).length, 2);
};
+exports.testFiltering1 = function() {
+ var data = [{"path.1":[1,2,3]}, {"path.1":[9,3,7]}];
+ assert.deepEqual(executeQuery("any(path,3)&sort()", {}, data), []); // path is undefined
+ assert.deepEqual(executeQuery("any(path.1,3)&sort()", {}, data), data); // 3 found in both
+ assert.deepEqual(executeQuery("not(any(path.1,3))&sort()", {}, data), []); // 3 found in both
+ assert.deepEqual(executeQuery("not(any(path.1,7))&sort()", {}, data), [data[0]]); // 7 found in second
+};
+
if (require.main === module)
require("patr/runner").run(exports);
View
4 test/query.js
@@ -172,6 +172,10 @@ exports.testStringification = function() {
// string to array and back
var str = 'somefunc(and(1),(a,b),(10,(10,1)),(a,b.c))';
assert.equal(parseQuery(str)+'', str);
+ // arguments with dots
+ assert.equal(parseQuery(Query().eq(['a/b','c'],1)+'')+'', 'eq(a%2Fb/c,1)');
+ var name = ['a/b','c'];
+ assert.deepEqual(parseQuery(Query().eq(name,1)+'').args[0].args[0], name);
};
if (require.main === module)
Please sign in to comment.
Something went wrong with that request. Please try again.