Commit 5717cd1d authored by Sven Franck's avatar Sven Franck

updated to latest JIO, fixed AMD check in complex_queries.js so does not break with require

parent 7a6598b6
...@@ -28,6 +28,12 @@ var complex_queries; ...@@ -28,6 +28,12 @@ var complex_queries;
"value": value "value": value
}); });
} }
/**
* Parse a text request to a json query object tree
*
* @param {String} string The string to parse
* @return {Object} The json query tree
*/
function parseStringToObject(string) { function parseStringToObject(string) {
/* /*
...@@ -714,152 +720,308 @@ if ((error_count = __NODEJS_parse(string, error_offsets, error_lookaheads)) > 0) ...@@ -714,152 +720,308 @@ if ((error_count = __NODEJS_parse(string, error_offsets, error_lookaheads)) > 0)
return result; return result;
} // parseStringToObject } // parseStringToObject
_export('parseStringToObject', parseStringToObject);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global _export: true */ /*global _export: true */
/** /**
* Create a class, manage inheritance, static methods, * Escapes regexp special chars from a string.
* protected attributes and can hide methods or/and secure methods
* *
* @param {Class} Class Classes to inherit from (0..n). The last class * @param {String} string The string to escape
* parameter will inherit from the previous one, and so on * @return {String} The escaped string
* @param {Object} option Class option (0..n)
* @param {Boolean} [option.secure_methods=false] Make methods not configurable
* and not writable
* @param {Boolean} [option.hide_methods=false] Make methods not enumerable
* @param {Boolean} [option.secure_static_methods=true] Make static methods not
* configurable and not
* writable
* @param {Boolean} [option.hide_static_methods=false] Make static methods not
* enumerable
* @param {Object} [option.static_methods={}] Object of static methods
* @param {Function} constructor The new class constructor
* @return {Class} The new class
*/ */
function newClass() { function stringEscapeRegexpCharacters(string) {
var j, k, constructors = [], option, new_class; if (typeof string === "string") {
return string.replace(/([\\\.\$\[\]\(\)\{\}\^\?\*\+\-])/g, "\\$1");
for (j = 0; j < arguments.length; j += 1) {
if (typeof arguments[j] === "function") {
constructors.push(arguments[j]);
} else if (typeof arguments[j] === "object") {
option = option || {};
for (k in arguments[j]) {
if (arguments[j].hasOwnProperty(k)) {
option[k] = arguments[j][k];
}
}
}
}
function postObjectCreation(that) {
// modify the object according to 'option'
var key;
if (option) {
for (key in that) {
if (that.hasOwnProperty(key)) {
if (typeof that[key] === "function") {
Object.defineProperty(that, key, {
"configurable": option.secure_methods ? false : true,
"enumerable": option.hide_methods ? false : true,
"writable": option.secure_methods ? false : true,
"value": that[key]
});
} }
throw new TypeError("complex_queries.stringEscapeRegexpCharacters(): " +
"Argument no 1 is not of type 'string'");
}
_export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters);
/**
* Convert metadata values to array of strings. ex:
*
* "a" -> ["a"],
* {"content": "a"} -> ["a"]
*
* @param {Any} value The metadata value
* @return {Array} The value in string array format
*/
function metadataValueToStringArray(value) {
var i, new_value = [];
if (value === undefined) {
return undefined;
} }
if (!Array.isArray(value)) {
value = [value];
} }
for (i = 0; i < value.length; i += 1) {
if (typeof value[i] === 'object') {
new_value[i] = value[i].content;
} else {
new_value[i] = value[i];
} }
} }
return new_value;
}
function postClassCreation(that) { /**
// modify the object according to 'option' * A sort function to sort items by key
var key; *
if (option) { * @param {String} key The key to sort on
for (key in that) { * @param {String} [way="ascending"] 'ascending' or 'descending'
if (that.hasOwnProperty(key)) { * @return {Function} The sort function
if (typeof that[key] === "function") { */
Object.defineProperty(that, key, { function sortFunction(key, way) {
"configurable": option.secure_static_methods === if (way === 'descending') {
false ? true : false, return function (a, b) {
"enumerable": option.hide_static_methods ? false : true, // this comparison is 5 times faster than json comparison
"writable": option.secure_static_methods === false ? true : false, var i, l;
"value": that[key] a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return 1;
}
if (b[i] === undefined) {
return -1;
}
if (a[i] > b[i]) {
return -1;
}
if (a[i] < b[i]) {
return 1;
}
}
return 0;
};
}
if (way === 'ascending') {
return function (a, b) {
// this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return -1;
}
if (b[i] === undefined) {
return 1;
}
if (a[i] > b[i]) {
return 1;
}
if (a[i] < b[i]) {
return -1;
}
}
return 0;
};
}
throw new TypeError("complex_queries.sortFunction(): " +
"Argument 2 must be 'ascending' or 'descending'");
}
/**
* Clones all native object in deep. Managed types: Object, Array, String,
* Number, Boolean, null.
*
* @param {A} object The object to clone
* @return {A} The cloned object
*/
function deepClone(object) {
var i, cloned;
if (Array.isArray(object)) {
cloned = [];
for (i = 0; i < object.length; i += 1) {
cloned[i] = deepClone(object[i]);
}
return cloned;
}
if (typeof object === "object") {
cloned = {};
for (i in object) {
if (object.hasOwnProperty(i)) {
cloned[i] = deepClone(object[i]);
}
}
return cloned;
}
return object;
}
/**
* Inherits the prototype methods from one constructor into another. The
* prototype of `constructor` will be set to a new object created from
* `superConstructor`.
*
* @param {Function} constructor The constructor which inherits the super one
* @param {Function} superConstructor The super constructor
*/
function inherits(constructor, superConstructor) {
constructor.super_ = superConstructor;
constructor.prototype = Object.create(superConstructor.prototype, {
"constructor": {
"configurable": true,
"enumerable": false,
"writable": true,
"value": constructor
}
}); });
}
/**
* Does nothing
*/
function emptyFunction() {}
/**
* Filter a list of items, modifying them to select only wanted keys. If
* `clone` is true, then the method will act on a cloned list.
*
* @param {Array} select_option Key list to keep
* @param {Array} list The item list to filter
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
function select(select_option, list, clone) {
var i, j, new_item;
if (!Array.isArray(select_option)) {
throw new TypeError("complex_queries.select(): " +
"Argument 1 is not of type Array");
}
if (!Array.isArray(list)) {
throw new TypeError("complex_queries.select(): " +
"Argument 2 is not of type Array");
}
if (clone === true) {
list = deepClone(list);
} }
for (i = 0; i < list.length; i += 1) {
new_item = {};
for (j = 0; j < select_option.length; j += 1) {
new_item[select_option[j]] = list[i][select_option[j]];
} }
for (j in new_item) {
if (new_item.hasOwnProperty(j)) {
list[i] = new_item;
break;
} }
} }
} }
return list;
}
new_class = function (spec, my) { _export('select', select);
var i;
spec = spec || {}; /**
my = my || {}; * Sort a list of items, according to keys and directions. If `clone` is true,
// don't use forEach ! * then the method will act on a cloned list.
for (i = 0; i < constructors.length; i += 1) { *
constructors[i].apply(this, [spec, my]); * @param {Array} sort_on_option List of couples [key, direction]
} * @param {Array} list The item list to sort
postObjectCreation(this); * @param {Boolean} [clone=false] If true, modifies a clone of the list
return this; * @return {Array} The filtered list
}; */
option = option || {}; function sortOn(sort_on_option, list, clone) {
option.static_methods = option.static_methods || {}; var sort_index;
for (j in option.static_methods) { if (!Array.isArray(sort_on_option)) {
if (option.static_methods.hasOwnProperty(j)) { throw new TypeError("complex_queries.sortOn(): " +
new_class[j] = option.static_methods[j]; "Argument 1 is not of type 'array'");
} }
if (clone) {
list = deepClone(list);
} }
postClassCreation(new_class); for (sort_index = sort_on_option.length - 1; sort_index >= 0;
return new_class; sort_index -= 1) {
list.sort(sortFunction(
sort_on_option[sort_index][0],
sort_on_option[sort_index][1]
));
}
return list;
} }
_export('sortOn', sortOn);
/** /**
* Escapes regexp special chars from a string. * Limit a list of items, according to index and length. If `clone` is true,
* then the method will act on a cloned list.
* *
* @param {String} string The string to escape * @param {Array} limit_option A couple [from, length]
* @return {String} The escaped string * @param {Array} list The item list to limit
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/ */
function stringEscapeRegexpCharacters(string) { function limit(limit_option, list, clone) {
if (typeof string === "string") { if (!Array.isArray(limit_option)) {
return string.replace(/([\\\.\$\[\]\(\)\{\}\^\?\*\+\-])/g, "\\$1"); throw new TypeError("complex_queries.limit(): " +
"Argument 1 is not of type 'array'");
}
if (!Array.isArray(list)) {
throw new TypeError("complex_queries.limit(): " +
"Argument 2 is not of type 'array'");
} }
if (clone) {
list = deepClone(list);
}
list.splice(0, limit_option[0]);
if (limit_option[1]) {
list.splice(limit_option[1]);
}
return list;
} }
_export("stringEscapeRegexpCharacters", stringEscapeRegexpCharacters); _export('limit', limit);
/** /**
* A sort function to sort items by key * Convert a search text to a regexp.
* *
* @param {String} key The key to sort on * @param {String} string The string to convert
* @param {String} [way="ascending"] 'ascending' or 'descending' * @param {String} [wildcard_character=undefined] The wildcard chararter
* @return {Function} The sort function * @return {RegExp} The search text regexp
*/ */
function sortFunction(key, way) { function convertStringToRegExp(string, wildcard_character) {
if (way === 'descending') { if (typeof string !== 'string') {
return function (a, b) { throw new TypeError("complex_queries.convertStringToRegExp(): " +
return a[key] < b[key] ? 1 : a[key] > b[key] ? -1 : 0; "Argument 1 is not of type 'string'");
};
} }
return function (a, b) { if (wildcard_character === undefined ||
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0; wildcard_character === null || wildcard_character === '') {
}; return new RegExp("^" + stringEscapeRegexpCharacters(string) + "$");
}
if (typeof wildcard_character !== 'string' || wildcard_character.length > 1) {
throw new TypeError("complex_queries.convertStringToRegExp(): " +
"Optional argument 2 must be a string of length <= 1");
}
return new RegExp("^" + stringEscapeRegexpCharacters(string).replace(
new RegExp(stringEscapeRegexpCharacters(wildcard_character), 'g'),
'.*'
) + "$");
} }
_export('convertStringToRegExp', convertStringToRegExp);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global _export: true, ComplexQuery: true, SimpleQuery: true, /*global _export: true, ComplexQuery: true, SimpleQuery: true, Query: true,
newClass: true, Query: true */ parseStringToObject: true */
var query_class_dict = {}, QueryFactory; var query_class_dict = {};
/** /**
* Provides static methods to create Query object * Provides static methods to create Query object
* *
* @class QueryFactory * @class QueryFactory
*/ */
QueryFactory = newClass({ function QueryFactory() {}
"static_methods": {
/** /**
* Creates Query object from a search text string or a serialized version * Creates Query object from a search text string or a serialized version
* of a Query. * of a Query.
* *
...@@ -869,26 +1031,26 @@ QueryFactory = newClass({ ...@@ -869,26 +1031,26 @@ QueryFactory = newClass({
* of a Query * of a Query
* @return {Query} A Query object * @return {Query} A Query object
*/ */
"create": function (object) { QueryFactory.create = function (object) {
if (object === "") { if (object === "") {
return new Query(); return new Query();
} }
if (typeof object === "string") { if (typeof object === "string") {
object = Query.parseStringToObject(object); object = parseStringToObject(object);
} }
if (typeof (object || {}).type === "string" && if (typeof (object || {}).type === "string" &&
query_class_dict[object.type]) { query_class_dict[object.type]) {
return new query_class_dict[object.type](object); return new query_class_dict[object.type](object);
} }
return null; throw new TypeError("QueryFactory.create(): " +
} "Argument 1 is not a search text or a parsable object");
} };
}, function () {});
_export("QueryFactory", QueryFactory); _export("QueryFactory", QueryFactory);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, sortFunction: true, parseStringToObject: true, /*global parseStringToObject: true, emptyFunction: true, sortOn: true, limit:
_export: true, stringEscapeRegexpCharacters: true */ true, select: true, _export: true, stringEscapeRegexpCharacters: true,
deepClone: true */
/** /**
* The query to use to filter a list of objects. * The query to use to filter a list of objects.
...@@ -897,11 +1059,47 @@ _export("QueryFactory", QueryFactory); ...@@ -897,11 +1059,47 @@ _export("QueryFactory", QueryFactory);
* @class Query * @class Query
* @constructor * @constructor
*/ */
var Query = newClass(function () { function Query() {
/**
* Called before parsing the query. Must be overridden!
*
* @method onParseStart
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
this.onParseStart = emptyFunction;
/**
* Called when parsing a simple query. Must be overridden!
*
* @method onParseSimpleQuery
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
this.onParseSimpleQuery = emptyFunction;
var that = this, emptyFunction = function () {}; /**
* Called when parsing a complex query. Must be overridden!
*
* @method onParseComplexQuery
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
this.onParseComplexQuery = emptyFunction;
/** /**
* Called after parsing the query. Must be overridden!
*
* @method onParseEnd
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
this.onParseEnd = emptyFunction;
}
/**
* Filter the item list with matching item only * Filter the item list with matching item only
* *
* @method exec * @method exec
...@@ -914,43 +1112,66 @@ var Query = newClass(function () { ...@@ -914,43 +1112,66 @@ var Query = newClass(function () {
* @param {Array} [option.limit] Couple of integer, first is an index and * @param {Array} [option.limit] Couple of integer, first is an index and
* second is the length. * second is the length.
*/ */
that.exec = function (item_list, option) { Query.prototype.exec = function (item_list, option) {
var i = 0; var i = 0;
if (!Array.isArray(item_list)) {
throw new TypeError("Query().exec(): Argument 1 is not of type 'array'");
}
if (option === undefined) {
option = {};
}
if (typeof option !== 'object') {
throw new TypeError("Query().exec(): " +
"Optional argument 2 is not of type 'object'");
}
if (option.wildcard_character === undefined) {
option.wildcard_character = '%';
}
while (i < item_list.length) { while (i < item_list.length) {
if (!that.match(item_list[i], option.wildcard_character)) { if (!this.match(item_list[i], option.wildcard_character)) {
item_list.splice(i, 1); item_list.splice(i, 1);
} else { } else {
i += 1; i += 1;
} }
} }
if (option.sort_on) { if (option.sort_on) {
Query.sortOn(option.sort_on, item_list); sortOn(option.sort_on, item_list);
} }
if (option.limit) { if (option.limit) {
item_list.splice(0, option.limit[0]); limit(option.limit, item_list);
if (option.limit[1]) {
item_list.splice(option.limit[1]);
}
} }
Query.filterListSelect(option.select_list || [], item_list); select(option.select_list || [], item_list);
}; };
/** /**
* Test if an item matches this query * Test if an item matches this query
* *
* @method match * @method match
* @param {Object} item The object to test * @param {Object} item The object to test
* @return {Boolean} true if match, false otherwise * @return {Boolean} true if match, false otherwise
*/ */
that.match = function (item, wildcard_character) { Query.prototype.match = function (item, wildcard_character) {
return true; return true;
}; };
/**
* Browse the Query in deep calling parser method in each step.
*
* `onParseStart` is called first, on end `onParseEnd` is called.
* It starts from the simple queries at the bottom of the tree calling the
* parser method `onParseSimpleQuery`, and go up calling the
* `onParseComplexQuery` method.
*
* @method parse
* @param {Object} option Any options you want (except 'parsed')
* @return {Any} The parse result
*/
Query.prototype.parse = function (option) {
var that = this, object;
/** /**
* The recursive parser. * The recursive parser.
* *
* @method recParse
* @private
* @param {Object} object The object shared in the parse process * @param {Object} object The object shared in the parse process
* @param {Object} options Some options usable in the parseMethods * @param {Object} options Some options usable in the parseMethods
* @return {Any} The parser result * @return {Any} The parser result
...@@ -969,161 +1190,38 @@ var Query = newClass(function () { ...@@ -969,161 +1190,38 @@ var Query = newClass(function () {
that.onParseSimpleQuery(object, option); that.onParseSimpleQuery(object, option);
} }
} }
/**
* Browse the Query in deep calling parser method in each step.
*
* `onParseStart` is called first, on end `onParseEnd` is called.
* It starts from the simple queries at the bottom of the tree calling the
* parser method `onParseSimpleQuery`, and go up calling the
* `onParseComplexQuery` method.
*
* @method parse
* @param {Object} option Any options you want (except 'parsed')
* @return {Any} The parse result
*/
that.parse = function (option) {
var object;
object = {"parsed": JSON.parse(JSON.stringify(that.serialized()))}; object = {"parsed": JSON.parse(JSON.stringify(that.serialized()))};
that.onParseStart(object, option); that.onParseStart(object, option);
recParse(object, option); recParse(object, option);
that.onParseEnd(object, option); that.onParseEnd(object, option);
return object.parsed; return object.parsed;
}; };
/**
* Called before parsing the query. Must be overridden!
*
* @method onParseStart
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
that.onParseStart = emptyFunction;
/**
* Called when parsing a simple query. Must be overridden!
*
* @method onParseSimpleQuery
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
that.onParseSimpleQuery = emptyFunction;
/**
* Called when parsing a complex query. Must be overridden!
*
* @method onParseComplexQuery
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
that.onParseComplexQuery = emptyFunction;
/**
* Called after parsing the query. Must be overridden!
*
* @method onParseEnd
* @param {Object} object The object shared in the parse process
* @param {Object} option Some option gave in parse()
*/
that.onParseEnd = emptyFunction;
/** /**
* Convert this query to a parsable string. * Convert this query to a parsable string.
* *
* @method toString * @method toString
* @return {String} The string version of this query * @return {String} The string version of this query
*/ */
that.toString = function () { Query.prototype.toString = function () {
return ""; return "";
}; };
/** /**
* Convert this query to an jsonable object in order to be remake thanks to * Convert this query to an jsonable object in order to be remake thanks to
* QueryFactory class. * QueryFactory class.
* *
* @method serialized * @method serialized
* @return {Object} The jsonable object * @return {Object} The jsonable object
*/ */
that.serialized = function () { Query.prototype.serialized = function () {
return undefined; return undefined;
}; };
}, {"static_methods": {
/**
* Filter a list of items, modifying them to select only wanted keys.
*
* @method filterListSelect
* @static
* @param {Array} select_option Key list to keep
* @param {Array} list The item list to filter
*/
"filterListSelect": function (select_option, list) {
var i, j, new_item;
for (i = 0; i < list.length; i += 1) {
new_item = {};
for (j = 0; j < select_option.length; j += 1) {
new_item[select_option[j]] = list[i][select_option[j]];
}
for (j in new_item) {
if (new_item.hasOwnProperty(j)) {
list[i] = new_item;
break;
}
}
}
},
/**
* Sort a list of items, according to keys and directions.
*
* @method sortOn
* @static
* @param {Array} sort_on_option List of couples [key, direction]
* @param {Array} list The item list to sort
*/
"sortOn": function (sort_on_option, list) {
var sort_index;
for (sort_index = sort_on_option.length - 1; sort_index >= 0;
sort_index -= 1) {
list.sort(sortFunction(
sort_on_option[sort_index][0],
sort_on_option[sort_index][1]
));
}
},
/**
* Parse a text request to a json query object tree
*
* @method parseStringToObject
* @static
* @param {String} string The string to parse
* @return {Object} The json query tree
*/
"parseStringToObject": parseStringToObject,
/**
* Convert a search text to a regexp.
*
* @method convertStringToRegExp
* @static
* @param {String} string The string to convert
* @param {String} [wildcard_character=undefined] The wildcard chararter
* @return {RegExp} The search text regexp
*/
"convertStringToRegExp": function (string, wildcard_character) {
return new RegExp("^" + stringEscapeRegexpCharacters(string).replace(
stringEscapeRegexpCharacters(wildcard_character),
'.*'
) + "$");
}
}});
_export("Query", Query); _export("Query", Query);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, Query: true, /*global Query: true, inherits: true, query_class_dict: true, _export: true,
query_class_dict: true, _export: true */ convertStringToRegExp: true */
/** /**
* The SimpleQuery inherits from Query, and compares one metadata value * The SimpleQuery inherits from Query, and compares one metadata value
...@@ -1135,7 +1233,9 @@ _export("Query", Query); ...@@ -1135,7 +1233,9 @@ _export("Query", Query);
* @param {String} spec.key The metadata key * @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare * @param {String} spec.value The value of the metadata to compare
*/ */
var SimpleQuery = newClass(Query, function (spec) { function SimpleQuery(spec) {
Query.call(this);
/** /**
* Operator to use to compare object values * Operator to use to compare object values
* *
...@@ -1162,34 +1262,37 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1162,34 +1262,37 @@ var SimpleQuery = newClass(Query, function (spec) {
*/ */
this.value = spec.value; this.value = spec.value;
/** }
inherits(SimpleQuery, Query);
/**
* #crossLink "Query/match:method" * #crossLink "Query/match:method"
*/ */
this.match = function (item, wildcard_character) { SimpleQuery.prototype.match = function (item, wildcard_character) {
return this[this.operator](item[this.key], this.value, wildcard_character); return this[this.operator](item[this.key], this.value, wildcard_character);
}; };
/** /**
* #crossLink "Query/toString:method" * #crossLink "Query/toString:method"
*/ */
this.toString = function () { SimpleQuery.prototype.toString = function () {
return (this.key ? this.key + ": " : "") + (this.operator || "=") + ' "' + return (this.key ? this.key + ": " : "") + (this.operator || "=") + ' "' +
this.value + '"'; this.value + '"';
}; };
/** /**
* #crossLink "Query/serialized:method" * #crossLink "Query/serialized:method"
*/ */
this.serialized = function () { SimpleQuery.prototype.serialized = function () {
return { return {
"type": "simple", "type": "simple",
"operator": this.operator, "operator": this.operator,
"key": this.key, "key": this.key,
"value": this.value "value": this.value
}; };
}; };
/** /**
* Comparison operator, test if this query value matches the item value * Comparison operator, test if this query value matches the item value
* *
* @method = * @method =
...@@ -1198,15 +1301,39 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1198,15 +1301,39 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {String} wildcard_character The wildcard_character * @param {String} wildcard_character The wildcard_character
* @return {Boolean} true if match, false otherwise * @return {Boolean} true if match, false otherwise
*/ */
this["="] = function (object_value, comparison_value, SimpleQuery.prototype["="] = function (object_value, comparison_value,
wildcard_character) { wildcard_character) {
return Query.convertStringToRegExp( var value, i;
if (!Array.isArray(object_value)) {
object_value = [object_value];
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
value = value.content;
}
if (comparison_value === undefined) {
if (value === undefined) {
return true;
}
return false;
}
if (value === undefined) {
return false;
}
if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character || "%" wildcard_character
).test(object_value.toString()); ).test(value.toString())
}; ) {
return true;
}
}
return false;
};
/** /**
* Comparison operator, test if this query value does not match the item value * Comparison operator, test if this query value does not match the item value
* *
* @method != * @method !=
...@@ -1215,15 +1342,39 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1215,15 +1342,39 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {String} wildcard_character The wildcard_character * @param {String} wildcard_character The wildcard_character
* @return {Boolean} true if not match, false otherwise * @return {Boolean} true if not match, false otherwise
*/ */
this["!="] = function (object_value, comparison_value, SimpleQuery.prototype["!="] = function (object_value, comparison_value,
wildcard_character) { wildcard_character) {
return !Query.convertStringTextToRegExp( var value, i;
if (!Array.isArray(object_value)) {
object_value = [object_value];
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
value = value.content;
}
if (comparison_value === undefined) {
if (value === undefined) {
return false;
}
return true;
}
if (value === undefined) {
return true;
}
if (
convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
wildcard_character || "%" wildcard_character
).test(object_value.toString()); ).test(value.toString())
}; ) {
return false;
}
}
return true;
};
/** /**
* Comparison operator, test if this query value is lower than the item value * Comparison operator, test if this query value is lower than the item value
* *
* @method < * @method <
...@@ -1231,11 +1382,19 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1231,11 +1382,19 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {Number, String} comparison_value The comparison value * @param {Number, String} comparison_value The comparison value
* @return {Boolean} true if lower, false otherwise * @return {Boolean} true if lower, false otherwise
*/ */
this["<"] = function (object_value, comparison_value) { SimpleQuery.prototype["<"] = function (object_value, comparison_value) {
return object_value < comparison_value; var value;
}; if (!Array.isArray(object_value)) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
value = value.content;
}
return value < comparison_value;
};
/** /**
* Comparison operator, test if this query value is equal or lower than the * Comparison operator, test if this query value is equal or lower than the
* item value * item value
* *
...@@ -1244,11 +1403,19 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1244,11 +1403,19 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {Number, String} comparison_value The comparison value * @param {Number, String} comparison_value The comparison value
* @return {Boolean} true if equal or lower, false otherwise * @return {Boolean} true if equal or lower, false otherwise
*/ */
this["<="] = function (object_value, comparison_value) { SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
return object_value <= comparison_value; var value;
}; if (!Array.isArray(object_value)) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
value = value.content;
}
return value <= comparison_value;
};
/** /**
* Comparison operator, test if this query value is greater than the item * Comparison operator, test if this query value is greater than the item
* value * value
* *
...@@ -1257,11 +1424,19 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1257,11 +1424,19 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {Number, String} comparison_value The comparison value * @param {Number, String} comparison_value The comparison value
* @return {Boolean} true if greater, false otherwise * @return {Boolean} true if greater, false otherwise
*/ */
this[">"] = function (object_value, comparison_value) { SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
return object_value > comparison_value; var value;
}; if (!Array.isArray(object_value)) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
value = value.content;
}
return value > comparison_value;
};
/** /**
* Comparison operator, test if this query value is equal or greater than the * Comparison operator, test if this query value is equal or greater than the
* item value * item value
* *
...@@ -1270,16 +1445,23 @@ var SimpleQuery = newClass(Query, function (spec) { ...@@ -1270,16 +1445,23 @@ var SimpleQuery = newClass(Query, function (spec) {
* @param {Number, String} comparison_value The comparison value * @param {Number, String} comparison_value The comparison value
* @return {Boolean} true if equal or greater, false otherwise * @return {Boolean} true if equal or greater, false otherwise
*/ */
this[">="] = function (object_value, comparison_value) { SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
return object_value >= comparison_value; var value;
}; if (!Array.isArray(object_value)) {
}); object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
value = value.content;
}
return value >= comparison_value;
};
query_class_dict.simple = SimpleQuery; query_class_dict.simple = SimpleQuery;
_export("SimpleQuery", SimpleQuery); _export("SimpleQuery", SimpleQuery);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, Query: true, query_class_dict: true, /*global Query: true, query_class_dict: true, inherits: true,
_export: true, QueryFactory: true */ _export: true, QueryFactory: true */
/** /**
...@@ -1293,7 +1475,8 @@ _export("SimpleQuery", SimpleQuery); ...@@ -1293,7 +1475,8 @@ _export("SimpleQuery", SimpleQuery);
* @param {String} spec.key The metadata key * @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare * @param {String} spec.value The value of the metadata to compare
*/ */
var ComplexQuery = newClass(Query, function (spec) { function ComplexQuery(spec) {
Query.call(this);
/** /**
* Logical operator to use to compare object values * Logical operator to use to compare object values
...@@ -1316,17 +1499,20 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1316,17 +1499,20 @@ var ComplexQuery = newClass(Query, function (spec) {
this.query_list = spec.query_list || []; this.query_list = spec.query_list || [];
this.query_list = this.query_list.map(QueryFactory.create); this.query_list = this.query_list.map(QueryFactory.create);
/** }
inherits(ComplexQuery, Query);
/**
* #crossLink "Query/match:method" * #crossLink "Query/match:method"
*/ */
this.match = function (item, wildcard_character) { ComplexQuery.prototype.match = function (item, wildcard_character) {
return this[this.operator](item, wildcard_character); return this[this.operator](item, wildcard_character);
}; };
/** /**
* #crossLink "Query/toString:method" * #crossLink "Query/toString:method"
*/ */
this.toString = function () { ComplexQuery.prototype.toString = function () {
var str_list = ["("], this_operator = this.operator; var str_list = ["("], this_operator = this.operator;
this.query_list.forEach(function (query) { this.query_list.forEach(function (query) {
str_list.push(query.toString()); str_list.push(query.toString());
...@@ -1335,12 +1521,12 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1335,12 +1521,12 @@ var ComplexQuery = newClass(Query, function (spec) {
str_list.pop(); // remove last operator str_list.pop(); // remove last operator
str_list.push(")"); str_list.push(")");
return str_list.join(" "); return str_list.join(" ");
}; };
/** /**
* #crossLink "Query/serialized:method" * #crossLink "Query/serialized:method"
*/ */
this.serialized = function () { ComplexQuery.prototype.serialized = function () {
var s = { var s = {
"type": "complex", "type": "complex",
"operator": this.operator, "operator": this.operator,
...@@ -1350,9 +1536,9 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1350,9 +1536,9 @@ var ComplexQuery = newClass(Query, function (spec) {
s.query_list.push(query.serialized()); s.query_list.push(query.serialized());
}); });
return s; return s;
}; };
/** /**
* Comparison operator, test if all sub queries match the * Comparison operator, test if all sub queries match the
* item value * item value
* *
...@@ -1361,7 +1547,7 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1361,7 +1547,7 @@ var ComplexQuery = newClass(Query, function (spec) {
* @param {String} wildcard_character The wildcard character * @param {String} wildcard_character The wildcard character
* @return {Boolean} true if all match, false otherwise * @return {Boolean} true if all match, false otherwise
*/ */
this.AND = function (item, wildcard_character) { ComplexQuery.prototype.AND = function (item, wildcard_character) {
var i; var i;
for (i = 0; i < this.query_list.length; i += 1) { for (i = 0; i < this.query_list.length; i += 1) {
if (!this.query_list[i].match(item, wildcard_character)) { if (!this.query_list[i].match(item, wildcard_character)) {
...@@ -1369,9 +1555,9 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1369,9 +1555,9 @@ var ComplexQuery = newClass(Query, function (spec) {
} }
} }
return true; return true;
}; };
/** /**
* Comparison operator, test if one of the sub queries matches the * Comparison operator, test if one of the sub queries matches the
* item value * item value
* *
...@@ -1380,7 +1566,7 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1380,7 +1566,7 @@ var ComplexQuery = newClass(Query, function (spec) {
* @param {String} wildcard_character The wildcard character * @param {String} wildcard_character The wildcard character
* @return {Boolean} true if one match, false otherwise * @return {Boolean} true if one match, false otherwise
*/ */
this.OR = function (item, wildcard_character) { ComplexQuery.prototype.OR = function (item, wildcard_character) {
var i; var i;
for (i = 0; i < this.query_list.length; i += 1) { for (i = 0; i < this.query_list.length; i += 1) {
if (this.query_list[i].match(item, wildcard_character)) { if (this.query_list[i].match(item, wildcard_character)) {
...@@ -1388,9 +1574,9 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1388,9 +1574,9 @@ var ComplexQuery = newClass(Query, function (spec) {
} }
} }
return false; return false;
}; };
/** /**
* Comparison operator, test if the sub query does not match the * Comparison operator, test if the sub query does not match the
* item value * item value
* *
...@@ -1399,17 +1585,16 @@ var ComplexQuery = newClass(Query, function (spec) { ...@@ -1399,17 +1585,16 @@ var ComplexQuery = newClass(Query, function (spec) {
* @param {String} wildcard_character The wildcard character * @param {String} wildcard_character The wildcard character
* @return {Boolean} true if one match, false otherwise * @return {Boolean} true if one match, false otherwise
*/ */
this.NOT = function (item, wildcard_character) { ComplexQuery.prototype.NOT = function (item, wildcard_character) {
return !this.query_list[0].match(item, wildcard_character); return !this.query_list[0].match(item, wildcard_character);
}; };
});
query_class_dict.complex = ComplexQuery; query_class_dict.complex = ComplexQuery;
_export("ComplexQuery", ComplexQuery); _export("ComplexQuery", ComplexQuery);
if (typeof define === "function" && define.amd) { if (typeof define === "function" && define.amd) {
// define(to_export); // define(to_export);
Object.defineProperty(window, module_name, { Object.defineProperty(window, module_name, {
configurable: false, configurable: false,
enumerable: true, enumerable: true,
......
...@@ -744,7 +744,7 @@ ...@@ -744,7 +744,7 @@
if (just_check) { if (just_check) {
priv.getIndexDatabase(option, index, function (current_db) { priv.getIndexDatabase(option, index, function (current_db) {
if (db.equals(current_db)) { if (db.equals(current_db)) {
return that.success({"ok": true, "_id": command.getDocId()}); return that.success({"ok": true, "id": command.getDocId()});
} }
return that.error(generateErrorObject( return that.error(generateErrorObject(
"Different Index", "Different Index",
...@@ -754,7 +754,7 @@ ...@@ -754,7 +754,7 @@
}); });
} else { } else {
priv.storeIndexDatabaseList(db_list, {}, function () { priv.storeIndexDatabaseList(db_list, {}, function () {
that.success({"ok": true, "_id": command.getDocId()}); that.success({"ok": true, "id": command.getDocId()});
}); });
} }
}, },
......
...@@ -531,13 +531,13 @@ var command = function (spec, my) { ...@@ -531,13 +531,13 @@ var command = function (spec, my) {
* @param {object} storage The storage. * @param {object} storage The storage.
*/ */
that.validate = function (storage) { that.validate = function (storage) {
if (typeof priv.doc._id === "string" && priv.doc._id.match(" ")) { if (typeof priv.doc._id === "string" && priv.doc._id === "") {
that.error({ that.error({
"status": 21, "status": 21,
"statusText": "Invalid Document Id", "statusText": "Invalid Document Id",
"error": "invalid_document_id", "error": "invalid_document_id",
"message": "The document id is invalid", "message": "The document id is invalid",
"reason": "The document id contains spaces" "reason": "empty"
}); });
return false; return false;
} }
......
...@@ -11,6 +11,37 @@ ...@@ -11,6 +11,37 @@
/** /**
* JIO Local Storage. Type = 'local'. * JIO Local Storage. Type = 'local'.
* Local browser "database" storage. * Local browser "database" storage.
*
* Storage Description:
*
* {
* "type": "local",
* "username": <non empty string>, // to define user space
* "application_name": <string> // default 'untitled'
* }
*
* Document are stored in path
* 'jio/localstorage/username/application_name/document_id' like this:
*
* {
* "_id": "document_id",
* "_attachments": {
* "attachment_name": {
* "length": data_length,
* "digest": "md5-XXX",
* "content_type": "mime/type"
* },
* "attachment_name2": {..}, ...
* },
* "metadata_name": "metadata_value"
* "metadata_name2": ...
* ...
* }
*
* Only "_id" and "_attachments" are specific metadata keys, other one can be
* added without loss.
*
* @class LocalStorage
*/ */
jIO.addStorageType('local', function (spec, my) { jIO.addStorageType('local', function (spec, my) {
...@@ -64,34 +95,6 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -64,34 +95,6 @@ jIO.addStorageType('local', function (spec, my) {
S4() + S4(); S4() + S4();
}; };
/**
* Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting
* with an underscore.
* ex: doc: {key:value1,_key:value2} with
* new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}.
* @param {object} doc The original document object.
* @param {object} new_doc The new document object
*/
priv.documentObjectUpdate = function (doc, new_doc) {
var k;
for (k in doc) {
if (doc.hasOwnProperty(k)) {
if (k[0] !== '_') {
delete doc[k];
}
}
}
for (k in new_doc) {
if (new_doc.hasOwnProperty(k)) {
if (k[0] !== '_') {
doc[k] = new_doc[k];
}
}
}
};
/** /**
* Checks if an object has no enumerable keys * Checks if an object has no enumerable keys
* @method objectIsEmpty * @method objectIsEmpty
...@@ -137,11 +140,11 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -137,11 +140,11 @@ jIO.addStorageType('local', function (spec, my) {
} }
doc = localstorage.getItem(priv.localpath + "/" + doc_id); doc = localstorage.getItem(priv.localpath + "/" + doc_id);
if (doc === null) { if (doc === null) {
// the document does not exist
doc = command.cloneDoc(); doc = command.cloneDoc();
doc._id = doc_id; doc._id = doc_id;
// the document does not exist delete doc._attachments;
localstorage.setItem(priv.localpath + "/" + doc_id, localstorage.setItem(priv.localpath + "/" + doc_id, doc);
doc);
that.success({ that.success({
"ok": true, "ok": true,
"id": doc_id "id": doc_id
...@@ -166,14 +169,17 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -166,14 +169,17 @@ jIO.addStorageType('local', function (spec, my) {
*/ */
that.put = function (command) { that.put = function (command) {
setTimeout(function () { setTimeout(function () {
var doc; var doc, tmp;
doc = localstorage.getItem(priv.localpath + "/" + command.getDocId()); doc = localstorage.getItem(priv.localpath + "/" + command.getDocId());
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
doc = command.cloneDoc(); doc = command.cloneDoc();
delete doc._attachments;
} else { } else {
// the document already exists // the document already exists
priv.documentObjectUpdate(doc, command.cloneDoc()); tmp = command.cloneDoc();
tmp._attachments = doc._attachments;
doc = tmp;
} }
// write // write
localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc); localstorage.setItem(priv.localpath + "/" + command.getDocId(), doc);
......
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