Commit c76af5ed authored by Marco Mariani's avatar Marco Mariani

custom keys: respect naming conventions

parent 749d779e
...@@ -10,17 +10,17 @@ var checkKeySchema = function (key_schema) { ...@@ -10,17 +10,17 @@ var checkKeySchema = function (key_schema) {
throw new TypeError("SimpleQuery().create(): " + throw new TypeError("SimpleQuery().create(): " +
"key_schema is not of type 'object'"); "key_schema is not of type 'object'");
} }
// keys is mandatory // key_set is mandatory
if (key_schema.keys === undefined) { if (key_schema.key_set === undefined) {
throw new TypeError("SimpleQuery().create(): " + throw new TypeError("SimpleQuery().create(): " +
"key_schema has no 'keys' property"); "key_schema has no 'key_set' property");
} }
for (prop in key_schema) { for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) { if (key_schema.hasOwnProperty(prop)) {
switch (prop) { switch (prop) {
case 'keys': case 'key_set':
case 'types': case 'cast_lookup':
case 'comparators': case 'match_lookup':
break; break;
default: default:
throw new TypeError("SimpleQuery().create(): " + throw new TypeError("SimpleQuery().create(): " +
...@@ -83,16 +83,16 @@ inherits(SimpleQuery, Query); ...@@ -83,16 +83,16 @@ inherits(SimpleQuery, Query);
var checkKey = function (key) { var checkKey = function (key) {
var prop; var prop;
if (key.readFrom === undefined) { if (key.read_from === undefined) {
throw new TypeError("Custom key is missing the readFrom property"); throw new TypeError("Custom key is missing the read_from property");
} }
for (prop in key) { for (prop in key) {
if (key.hasOwnProperty(prop)) { if (key.hasOwnProperty(prop)) {
switch (prop) { switch (prop) {
case 'readFrom': case 'read_from':
case 'castTo': case 'cast_to':
case 'defaultMatch': case 'default_match':
break; break;
default: default:
throw new TypeError("Custom key has unknown property '" + throw new TypeError("Custom key has unknown property '" +
...@@ -108,50 +108,50 @@ var checkKey = function (key) { ...@@ -108,50 +108,50 @@ var checkKey = function (key) {
*/ */
SimpleQuery.prototype.match = function (item, wildcard_character) { SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null, var object_value = null,
defaultMatch = null, default_match = null,
castTo = null, cast_to = null,
matchMethod = null, matchMethod = null,
value = null, value = null,
key = this.key; key = this.key;
matchMethod = this[this.operator]; matchMethod = this[this.operator];
if (this._key_schema.keys && this._key_schema.keys[key] !== undefined) { if (this._key_schema.key_set && this._key_schema.key_set[key] !== undefined) {
key = this._key_schema.keys[key]; key = this._key_schema.key_set[key];
} }
if (typeof key === 'object') { if (typeof key === 'object') {
checkKey(key); checkKey(key);
object_value = item[key.readFrom]; object_value = item[key.read_from];
// defaultMatch overrides the default '=' operator // default_match overrides the default '=' operator
defaultMatch = key.defaultMatch; default_match = key.default_match;
// defaultMatch can be a string // default_match can be a string
if (typeof defaultMatch === 'string') { if (typeof default_match === 'string') {
// XXX raise error if defaultMatch not in comparators // XXX raise error if default_match not in match_lookup
defaultMatch = this._key_schema.comparators[defaultMatch]; default_match = this._key_schema.match_lookup[default_match];
} }
// defaultMatch overrides the default '=' operator // default_match overrides the default '=' operator
matchMethod = (defaultMatch || matchMethod); matchMethod = (default_match || matchMethod);
// but an explicit operator: key overrides DefaultMatch // but an explicit operator: key overrides default_match
if (this._spec && this._spec.operator) { if (this._spec && this._spec.operator) {
matchMethod = this[this.operator]; matchMethod = this[this.operator];
} }
value = this.value; value = this.value;
castTo = key.castTo; cast_to = key.cast_to;
if (castTo) { if (cast_to) {
// castTo can be a string // cast_to can be a string
if (typeof castTo === 'string') { if (typeof cast_to === 'string') {
// XXX raise error if castTo not in types // XXX raise error if cast_to not in cast_lookup
castTo = this._key_schema.types[castTo]; cast_to = this._key_schema.cast_lookup[cast_to];
} }
value = castTo(value); value = cast_to(value);
object_value = castTo(object_value); object_value = cast_to(object_value);
} }
} else { } else {
object_value = item[key]; object_value = item[key];
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
/*jslint unparam: true*/ /*jslint unparam: true*/
var key_schema = { var key_schema = {
types: { cast_lookup: {
dateType: function (obj) { dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') { if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone // no need to clone
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
} }
}, },
comparators: { match_lookup: {
sameDay: function (a, b) { sameDay: function (a, b) {
return ( return (
(a.getFullYear() === b.getFullYear()) && (a.getFullYear() === b.getFullYear()) &&
...@@ -55,32 +55,32 @@ ...@@ -55,32 +55,32 @@
equalState: translationEqualityMatcher({'ouvert': 'open'}) equalState: translationEqualityMatcher({'ouvert': 'open'})
}, },
keys: { key_set: {
case_insensitive_identifier: { case_insensitive_identifier: {
readFrom: 'identifier', read_from: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) { default_match: function (object_value, value, wildcard_character) {
// XXX do this with a regexp and wildcard support // XXX do this with a regexp and wildcard support
return (object_value.toLowerCase() === value.toLowerCase()); return (object_value.toLowerCase() === value.toLowerCase());
} }
}, },
date_day: { date_day: {
readFrom: 'date', read_from: 'date',
castTo: 'dateType', cast_to: 'dateType',
defaultMatch: 'sameDay' default_match: 'sameDay'
}, },
date_month: { date_month: {
readFrom: 'date', read_from: 'date',
castTo: 'dateType', cast_to: 'dateType',
defaultMatch: 'sameMonth' default_match: 'sameMonth'
}, },
date_year: { date_year: {
readFrom: 'date', read_from: 'date',
castTo: 'dateType', cast_to: 'dateType',
defaultMatch: 'sameYear' default_match: 'sameYear'
}, },
translated_state: { translated_state: {
readFrom: 'state', read_from: 'state',
defaultMatch: 'equalState' default_match: 'equalState'
} }
} }
}; };
...@@ -176,7 +176,7 @@ ...@@ -176,7 +176,7 @@
{'identifier': '100', 'number': '100'} {'identifier': '100', 'number': '100'}
]; ];
}, key_schema = { }, key_schema = {
types: { cast_lookup: {
intType: function (value) { intType: function (value) {
if (typeof value === 'string') { if (typeof value === 'string') {
return parseInt(value, 10); return parseInt(value, 10);
...@@ -184,10 +184,10 @@ ...@@ -184,10 +184,10 @@
return value; return value;
} }
}, },
keys: { key_set: {
number: { number: {
readFrom: 'number', read_from: 'number',
castTo: 'intType' cast_to: 'intType'
} }
} }
}; };
......
...@@ -60,16 +60,16 @@ ...@@ -60,16 +60,16 @@
try { try {
complex_queries.QueryFactory.create({type: 'simple'}, {}); complex_queries.QueryFactory.create({type: 'simple'}, {});
ok(false, 'key_schema.keys is not checked'); ok(false, 'key_schema.key_set is not checked');
} catch (e) { } catch (e) {
equal(e.name, 'TypeError', 'wrong exception type'); equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message, equal(e.message,
"SimpleQuery().create(): key_schema has no 'keys' property", "SimpleQuery().create(): key_schema has no 'key_set' property",
'wrong exception message'); 'wrong exception message');
} }
try { try {
complex_queries.QueryFactory.create({type: 'simple'}, {keys: {}, foobar: {}}); complex_queries.QueryFactory.create({type: 'simple'}, {key_set: {}, foobar: {}});
ok(false, 'unknown key_schema properties are not checked'); ok(false, 'unknown key_schema properties are not checked');
} catch (e) { } catch (e) {
equal(e.name, 'TypeError', 'wrong exception type'); equal(e.name, 'TypeError', 'wrong exception type');
...@@ -92,11 +92,11 @@ ...@@ -92,11 +92,11 @@
key: {}, key: {},
value: 'a' value: 'a'
}).exec(doc_list); }).exec(doc_list);
ok(false, 'key.readFrom is not checked'); ok(false, 'key.read_from is not checked');
} catch (e) { } catch (e) {
equal(e.name, 'TypeError', 'wrong exception type'); equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message, equal(e.message,
"Custom key is missing the readFrom property", "Custom key is missing the read_from property",
'wrong exception message'); 'wrong exception message');
} }
...@@ -104,7 +104,7 @@ ...@@ -104,7 +104,7 @@
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: { key: {
readFrom: 'identifier', read_from: 'identifier',
foobar: '' foobar: ''
}, },
value: 'a' value: 'a'
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
module('Custom Key Queries'); module('Custom Key Queries');
test('Simple Key with readFrom', function () { test('Simple Key with read_from', function () {
/*jslint unparam: true*/ /*jslint unparam: true*/
var doc_list, docList = function () { var doc_list, docList = function () {
return [ return [
...@@ -27,11 +27,11 @@ ...@@ -27,11 +27,11 @@
]; ];
}, keys = { }, keys = {
title: { title: {
readFrom: 'identifier' read_from: 'identifier'
}, },
case_insensitive_identifier: { case_insensitive_identifier: {
readFrom: 'identifier', read_from: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) { default_match: function (object_value, value, wildcard_character) {
return (object_value.toLowerCase() === value.toLowerCase()); return (object_value.toLowerCase() === value.toLowerCase());
} }
} }
...@@ -103,19 +103,19 @@ ...@@ -103,19 +103,19 @@
var keys = { var keys = {
day: { day: {
readFrom: 'date', read_from: 'date',
castTo: dateCast, cast_to: dateCast,
defaultMatch: sameDay default_match: sameDay
}, },
month: { month: {
readFrom: 'date', read_from: 'date',
castTo: dateCast, cast_to: dateCast,
defaultMatch: sameMonth default_match: sameMonth
}, },
year: { year: {
readFrom: 'date', read_from: 'date',
castTo: dateCast, cast_to: dateCast,
defaultMatch: sameYear default_match: sameYear
} }
}; };
...@@ -173,8 +173,8 @@ ...@@ -173,8 +173,8 @@
]; ];
}, keys = { }, keys = {
mydate: { mydate: {
readFrom: 'date', read_from: 'date',
castTo: dateCast cast_to: dateCast
} }
}; };
...@@ -250,7 +250,7 @@ ...@@ -250,7 +250,7 @@
}); });
test('Simple Key with both defaultMatch and operator attributes', function () { test('Simple Key with both default_match and operator attributes', function () {
var doc_list, docList = function () { var doc_list, docList = function () {
return [ return [
{'identifier': '1', 'date': '2013-01-01'}, {'identifier': '1', 'date': '2013-01-01'},
...@@ -259,9 +259,9 @@ ...@@ -259,9 +259,9 @@
]; ];
}, keys = { }, keys = {
mydate: { mydate: {
readFrom: 'date', read_from: 'date',
castTo: dateCast, cast_to: dateCast,
defaultMatch: function alwaysTrue() { return true; } default_match: function alwaysTrue() { return true; }
} }
}; };
...@@ -315,8 +315,8 @@ ...@@ -315,8 +315,8 @@
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: { key: {
readFrom: 'number', read_from: 'number',
castTo: intType cast_to: intType
}, },
operator: '>', operator: '>',
value: '19' value: '19'
...@@ -329,8 +329,8 @@ ...@@ -329,8 +329,8 @@
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: { key: {
readFrom: 'number', read_from: 'number',
castTo: intType cast_to: intType
}, },
operator: '<', operator: '<',
value: '19' value: '19'
...@@ -346,16 +346,16 @@ ...@@ -346,16 +346,16 @@
query_list: [{ query_list: [{
type: 'simple', type: 'simple',
key: { key: {
readFrom: 'number', read_from: 'number',
castTo: intType cast_to: intType
}, },
operator: '<', operator: '<',
value: '19' value: '19'
}, { }, {
type: 'simple', type: 'simple',
key: { key: {
readFrom: 'number', read_from: 'number',
castTo: intType cast_to: intType
}, },
operator: '=', operator: '=',
value: '19' value: '19'
...@@ -387,8 +387,8 @@ ...@@ -387,8 +387,8 @@
equalState = translationEqualityMatcher({'ouvert': 'open'}), equalState = translationEqualityMatcher({'ouvert': 'open'}),
keys = { keys = {
translated_state: { translated_state: {
readFrom: 'state', read_from: 'state',
defaultMatch: equalState default_match: equalState
} }
}; };
...@@ -479,8 +479,8 @@ ...@@ -479,8 +479,8 @@
]; ];
}, keys = { }, keys = {
identifier: { identifier: {
readFrom: 'identifier', read_from: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) { default_match: function (object_value, value, wildcard_character) {
// XXX todo: regexp & support wildcard_character // XXX todo: regexp & support wildcard_character
return accentFold(object_value) === accentFold(value); return accentFold(object_value) === accentFold(value);
} }
......
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
var key_schema = { var key_schema = {
types: { cast_lookup: {
dateType: function (obj) { dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') { if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone // no need to clone
...@@ -79,10 +79,10 @@ ...@@ -79,10 +79,10 @@
return new Date(obj); return new Date(obj);
} }
}, },
keys: { key_set: {
mydate: { mydate: {
readFrom: 'date', read_from: 'date',
castTo: 'dateType' cast_to: 'dateType'
} }
} }
}; };
......
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