Commit 06c4c331 authored by Marco Mariani's avatar Marco Mariani

JIODate type and tests

parent 23885d21
...@@ -27,6 +27,9 @@ module.exports = function (grunt) { ...@@ -27,6 +27,9 @@ module.exports = function (grunt) {
errorsOnly: true errorsOnly: true
} }
}, },
jiodate: {
src: ['src/jio.date/jiodate.js']
},
tests: { tests: {
src: ['test/**/*.js'], src: ['test/**/*.js'],
options: { options: {
...@@ -82,6 +85,10 @@ module.exports = function (grunt) { ...@@ -82,6 +85,10 @@ module.exports = function (grunt) {
src: 'jio.js', // '<%= pkg.name %>.js' src: 'jio.js', // '<%= pkg.name %>.js'
dest: 'jio.min.js' dest: 'jio.min.js'
}, },
jiodate: {
src: 'src/jio.date/jiodate.js',
dest: 'jiodate.min.js'
},
queries: { queries: {
src: 'complex_queries.js', src: 'complex_queries.js',
dest: 'complex_queries.min.js' dest: 'complex_queries.min.js'
......
This diff is collapsed.
This diff is collapsed.
/*jslint indent: 2, nomen: true */
/*global module, define, exports, window, moment */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(exports, moment);
}
window.jiodate = {};
module(window.jiodate, moment);
}(['exports', 'moment'], function (to_export, moment) {
"use strict";
/**
* Add a secured (write permission denied) property to an object.
*
* @param {Object} object The object to fill
* @param {String} key The object key where to store the property
* @param {Any} value The value to store
*/
function _export(key, value) {
Object.defineProperty(to_export, key, {
"configurable": false,
"enumerable": true,
"writable": false,
"value": value
});
}
var YEAR = 'year',
MONTH = 'month',
DAY = 'day',
HOUR = 'hour',
MIN = 'minute',
SEC = 'second',
MSEC = 'millisecond',
precision_grade = {
'year': 0,
'month': 1,
'day': 2,
'hour': 3,
'minute': 4,
'second': 5,
'millisecond': 6
},
lesserPrecision = function (p1, p2) {
return (precision_grade[p1] < precision_grade[p2]) ? p1 : p2;
},
JIODate = null;
JIODate = function (str) {
// in case of forgotten 'new'
if (!(this instanceof JIODate)) {
return new JIODate(str);
}
if (str instanceof JIODate) {
this.mom = str.mom.clone();
this._precision = str._precision;
return;
}
if (str === undefined) {
this.mom = moment();
this.setPrecision(MSEC);
return;
}
this.mom = null;
// http://www.w3.org/TR/NOTE-datetime
// http://dotat.at/tmp/ISO_8601-2004_E.pdf
// XXX these regexps fail to detect many invalid dates.
if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d/)) {
// ISO, milliseconds
this.mom = moment(str);
this.setPrecision(MSEC);
} else if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/)) {
// ISO, seconds
this.mom = moment(str);
this.setPrecision(SEC);
} else if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d/)) {
// ISO, minutes
this.mom = moment(str);
this.setPrecision(MIN);
} else if (str.match(/\d\d\d\d-\d\d-\d\d \d\d/)) {
this.mom = moment(str);
this.setPrecision(HOUR);
} else if (str.match(/\d\d\d\d-\d\d-\d\d/)) {
this.mom = moment(str);
this.setPrecision(DAY);
} else if (str.match(/\d\d\d\d-\d\d/)) {
this.mom = moment(str);
this.setPrecision(MONTH);
} else if (str.match(/\d\d\d\d/)) {
this.mom = moment(str);
this.setPrecision(YEAR);
}
if (!this.mom) {
throw new Error("Cannot parse: " + str);
}
};
JIODate.prototype.setPrecision = function (prec) {
this._precision = prec;
};
JIODate.prototype.getPrecision = function () {
return this._precision;
};
JIODate.prototype.eq = function (other) {
var p = lesserPrecision(this._precision, other._precision);
return this.mom.isSame(other.mom, p);
};
JIODate.prototype.ne = function (other) {
return (!this.eq(other));
};
JIODate.prototype.gt = function (other) {
var p = lesserPrecision(this._precision, other._precision);
return this.mom.isAfter(other.mom, p);
};
JIODate.prototype.lt = function (other) {
// XXX using the lesser precision cannot change the result (but check with business calendar)
var p = lesserPrecision(this._precision, other._precision);
return this.mom.isBefore(other.mom, p);
};
JIODate.prototype.ge = function (other) {
// XXX using the lesser precision cannot change the result (but check with business calendar)
var m1 = this.mom,
m2 = other.mom,
p = lesserPrecision(this._precision, other._precision);
return m1.isAfter(m2, p) || m1.isSame(m2, p);
};
JIODate.prototype.le = function (other) {
var m1 = this.mom,
m2 = other.mom,
p = lesserPrecision(this._precision, other._precision);
return m1.isBefore(m2, p) || m1.isSame(m2, p);
};
JIODate.prototype.cmp = function (other) {
var m1 = this.mom,
m2 = other.mom,
p = lesserPrecision(this._precision, other._precision);
return m1.isBefore(m2, p) ? -1 : (m1.isSame(m2, p) ? 0 : +1);
};
JIODate.prototype.toPrecisionString = function (precision) {
var fmt;
precision = precision || this._precision;
fmt = {
'millisecond': 'YYYY-MM-DD HH:mm:ss.SSS',
'second': 'YYYY-MM-DD HH:mm:ss',
'minute': 'YYYY-MM-DD HH:mm',
'hour': 'YYYY-MM-DD HH',
'day': 'YYYY-MM-DD',
'month': 'YYYY-MM',
'year': 'YYYY'
}[precision];
if (!fmt) {
throw new TypeError("Unsupported precision value '" + precision + "'");
}
return this.mom.format(fmt);
};
_export('JIODate', JIODate);
_export('YEAR', YEAR);
_export('MONTH', MONTH);
_export('DAY', DAY);
_export('HOUR', HOUR);
_export('MIN', MIN);
_export('SEC', SEC);
_export('MSEC', MSEC);
return to_export;
}));
...@@ -208,6 +208,9 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value, ...@@ -208,6 +208,9 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
if (value === undefined) { if (value === undefined) {
return false; return false;
} }
if (value.eq !== undefined) {
return value.eq(comparison_value);
}
if ( if (
convertStringToRegExp( convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
...@@ -249,6 +252,9 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value, ...@@ -249,6 +252,9 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (value === undefined) { if (value === undefined) {
return true; return true;
} }
if (value.ne !== undefined) {
return value.ne(comparison_value);
}
if ( if (
convertStringToRegExp( convertStringToRegExp(
comparison_value.toString(), comparison_value.toString(),
...@@ -278,6 +284,9 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) { ...@@ -278,6 +284,9 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value.lt !== undefined) {
return value.lt(comparison_value);
}
return value < comparison_value; return value < comparison_value;
}; };
...@@ -299,6 +308,9 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) { ...@@ -299,6 +308,9 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value.le !== undefined) {
return value.le(comparison_value);
}
return value <= comparison_value; return value <= comparison_value;
}; };
...@@ -320,6 +332,9 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) { ...@@ -320,6 +332,9 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value.gt !== undefined) {
return value.gt(comparison_value);
}
return value > comparison_value; return value > comparison_value;
}; };
...@@ -341,6 +356,9 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) { ...@@ -341,6 +356,9 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value.ge !== undefined) {
return value.ge(comparison_value);
}
return value >= comparison_value; return value >= comparison_value;
}; };
......
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, jiodate, window, test, ok,
equal, deepEqual, sinon */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'), require('jiodate'));
}
module(complex_queries, jiodate);
}(['complex_queries', 'jiodate', 'qunit'], function (complex_queries, jiodate) {
"use strict";
module('JIODate with custom keys');
test('Stock comparison operators with year precision', function () {
var doc_list, docList = function () {
return [
{'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'},
{'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'},
{'identifier': 'twenty twelve', 'date': '2012-03-04T08:52:13.746Z'}
];
}, key_schema = {
key_set: {
date: {
read_from: 'date',
cast_to: jiodate.JIODate
}
}
};
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date = 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '!=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date != 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
], 'Match with "date < 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date <= 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date > 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date >= 2011" (query tree form)');
});
}));
This diff is collapsed.
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
}(['complex_queries', 'qunit'], function (complex_queries) { }(['complex_queries', 'qunit'], function (complex_queries) {
"use strict"; "use strict";
module('Checks upon validity of key and key_schema objects'); module('Key and key_schema objects validation');
test('Check the parameters passed to exec() and create()', function () { test('Check the parameters passed to exec() and create()', function () {
try { try {
......
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
<script src="queries/tests.js"></script> <script src="queries/tests.js"></script>
<script src="queries/key-typechecks.tests.js"></script> <script src="queries/key-typechecks.tests.js"></script>
<script src="../lib/moment/moment-2.5.0.js"></script>
<script src="../src/jio.date/jiodate.js"></script>
<script src="queries/jiodate.tests.js"></script>
<script src="../src/jio.storage/localstorage.js"></script> <script src="../src/jio.storage/localstorage.js"></script>
<script src="queries/localstorage-keys.tests.js"></script> <script src="queries/localstorage-keys.tests.js"></script>
<script src="jio.storage/localstorage.tests.js"></script> <script src="jio.storage/localstorage.tests.js"></script>
......
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