Commit 7f4bdc7f authored by Marco Mariani's avatar Marco Mariani

support for key_schema.types (castTo) and comparators (defaultMatch)

parent 0dffe5c1
...@@ -15,6 +15,13 @@ ...@@ -15,6 +15,13 @@
function SimpleQuery(spec, key_schema) { function SimpleQuery(spec, key_schema) {
Query.call(this); Query.call(this);
// XXX check for correctness of key_schema:
// XXX 'keys' must exist
// XXX 'types' is optional
// XXX 'comparators' is optional
// XXX anything else is invalid
// XXX each key can have readFrom, castTo, defaultMatch
// (can be checked in the match function)
this._key_schema = key_schema || {}; this._key_schema = key_schema || {};
/** /**
...@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query); ...@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query);
* #crossLink "Query/match:method" * #crossLink "Query/match:method"
*/ */
SimpleQuery.prototype.match = function (item, wildcard_character) { SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null, matchMethod = null, value = null, key = this.key; var object_value = null,
defaultMatch = null,
castTo = null,
matchMethod = null,
value = null,
key = this.key;
matchMethod = this[this.operator]; matchMethod = this[this.operator];
if (this._key_schema[key] !== undefined) { if (this._key_schema.keys && this._key_schema.keys[key] !== undefined) {
key = this._key_schema[key]; key = this._key_schema.keys[key];
} }
if (typeof key === 'object') { if (typeof key === 'object') {
object_value = item[key.readFrom]; object_value = item[key.readFrom];
// defaultMatch overrides the default '=' operator // defaultMatch overrides the default '=' operator
matchMethod = (key.defaultMatch || matchMethod); defaultMatch = key.defaultMatch;
// defaultMatch can be a string
if (typeof defaultMatch === 'string') {
// XXX raise error if defaultMatch not in comparators
defaultMatch = this._key_schema.comparators[defaultMatch];
}
// defaultMatch overrides the default '=' operator
matchMethod = (defaultMatch || matchMethod);
// but an explicit operator: key overrides DefaultMatch // but an explicit operator: key overrides DefaultMatch
if (this._spec && this._spec.operator) { if (this._spec && this._spec.operator) {
...@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) { ...@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
} }
value = this.value; value = this.value;
if (key.castTo) { castTo = key.castTo;
value = key.castTo(value); if (castTo) {
object_value = key.castTo(object_value); // castTo can be a string
if (typeof castTo === 'string') {
// XXX raise error if castTo not in types
castTo = this._key_schema.types[castTo];
}
value = castTo(value);
object_value = castTo(object_value);
} }
} else { } else {
object_value = item[key]; object_value = item[key];
......
...@@ -15,33 +15,6 @@ ...@@ -15,33 +15,6 @@
}(['complex_queries', 'qunit'], function (complex_queries) { }(['complex_queries', 'qunit'], function (complex_queries) {
"use strict"; "use strict";
var dateType = function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
};
var sameDay = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDay() === b.getDay())
);
};
var sameMonth = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
);
};
var sameYear = function (a, b) {
return (a.getFullYear() === b.getFullYear());
};
var translationEqualityMatcher = function (data) { var translationEqualityMatcher = function (data) {
return function (object_value, value) { return function (object_value, value) {
value = data[value]; value = data[value];
...@@ -49,36 +22,66 @@ ...@@ -49,36 +22,66 @@
}; };
}; };
var equalState = translationEqualityMatcher({'ouvert': 'open'});
/*jslint unparam: true*/ /*jslint unparam: true*/
var key_schema = { var key_schema = {
case_insensitive_identifier: { types: {
readFrom: 'identifier', dateType: function (obj) {
defaultMatch: function (object_value, value, wildcard_character) { if (Object.prototype.toString.call(obj) === '[object Date]') {
return (object_value.toLowerCase() === value.toLowerCase()); // no need to clone
return obj;
}
return new Date(obj);
} }
}, },
date_day: {
readFrom: 'date', comparators: {
castTo: dateType, sameDay: function (a, b) {
defaultMatch: sameDay return (
}, (a.getFullYear() === b.getFullYear()) &&
date_month: { (a.getMonth() === b.getMonth()) &&
readFrom: 'date', (a.getDay() === b.getDay())
castTo: dateType, );
defaultMatch: sameMonth },
}, sameMonth: function (a, b) {
date_year: { return (
readFrom: 'date', (a.getFullYear() === b.getFullYear()) &&
castTo: dateType, (a.getMonth() === b.getMonth())
defaultMatch: sameYear );
},
sameYear: function (a, b) {
return (a.getFullYear() === b.getFullYear());
},
equalState: translationEqualityMatcher({'ouvert': 'open'})
}, },
translated_state: {
readFrom: 'state', keys: {
defaultMatch: equalState case_insensitive_identifier: {
readFrom: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) {
// XXX do this with a regexp and wildcard support
return (object_value.toLowerCase() === value.toLowerCase());
}
},
date_day: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameDay'
},
date_month: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameMonth'
},
date_year: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameYear'
},
translated_state: {
readFrom: 'state',
defaultMatch: 'equalState'
}
} }
}; };
/*jslint unparam: false*/ /*jslint unparam: false*/
......
...@@ -69,19 +69,25 @@ ...@@ -69,19 +69,25 @@
} }
var dateType = function (obj) { var key_schema = {
if (Object.prototype.toString.call(obj) === '[object Date]') { types: {
// no need to clone dateType: function (obj) {
return obj; if (Object.prototype.toString.call(obj) === '[object Date]') {
} // no need to clone
return new Date(obj); return obj;
}, key_schema = { }
mydate: { return new Date(obj);
readFrom: 'date', }
castTo: dateType },
keys: {
mydate: {
readFrom: 'date',
castTo: 'dateType'
}
} }
}; };
test("AllDocs", function () { test("AllDocs", function () {
expect(3); expect(3);
var o = {}, jio = jIO.createJIO({ var o = {}, jio = jIO.createJIO({
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment