不要怂,就是干,撸起袖子干!

Commit dc181e2d by Dr. Evil

Prettified code, refactored RANGE datatype constructor bits

1 parent ed60554a
...@@ -382,24 +382,21 @@ BLOB.prototype.toSql = function() { ...@@ -382,24 +382,21 @@ BLOB.prototype.toSql = function() {
}; };
/** /**
* Range types are data types representing a range of values of some element type (called the range's subtype). Only available in postgres. * Range types are data types representing a range of values of some element type (called the range's subtype).
* Only available in postgres.
* See {@link http://www.postgresql.org/docs/9.4/static/rangetypes.html|Postgres documentation} for more details * See {@link http://www.postgresql.org/docs/9.4/static/rangetypes.html|Postgres documentation} for more details
* @property RANGE * @property RANGE
*/ */
var RANGE = function (Subtype) { var RANGE = function (subtype) {
var options = {}; var options = _.isPlainObject(subtype) ? subtype : { subtype: subtype };
if(typeof Subtype === 'function') { // if subtype passed - instantiate object of this subtype and return new function
options.subtype = new Subtype(); if (!options.subtype) options.subtype = INTEGER;
return RANGE.bind({}, options);
}
else if(typeof Subtype === 'object' && Subtype.hasOwnProperty('subtype'))
options = Subtype;
if (!(this instanceof RANGE)) return new RANGE(options); if (!(this instanceof RANGE)) return new RANGE(options);
ABSTRACT.apply(this, arguments); ABSTRACT.apply(this, arguments);
this._subtype = options.subtype ? (options.subtype.key || 'INTEGER') : 'INTEGER'; this._subtype = options.subtype.key;
}; };
util.inherits(RANGE, ABSTRACT); util.inherits(RANGE, ABSTRACT);
...@@ -513,7 +510,7 @@ ENUM.prototype.key = ENUM.key = 'ENUM'; ...@@ -513,7 +510,7 @@ ENUM.prototype.key = ENUM.key = 'ENUM';
* @property ARRAY * @property ARRAY
*/ */
var ARRAY = function(type) { var ARRAY = function(type) {
var options = typeof type === "object" && !(type instanceof ABSTRACT) && type || { var options = _.isPlainObject(type) && !(type instanceof ABSTRACT) && type || {
type: type type: type
}; };
if (!(this instanceof ARRAY)) return new ARRAY(options); if (!(this instanceof ARRAY)) return new ARRAY(options);
......
...@@ -7,44 +7,37 @@ module.exports = { ...@@ -7,44 +7,37 @@ module.exports = {
stringify: function (data) { stringify: function (data) {
if (data === null) return null; if (data === null) return null;
if (!Utils._.isArray(data) || data.length !== 2) if (!Utils._.isArray(data) || data.length !== 2) return '';
return '';
if (Utils._.any(data, Utils._.isNull)) if (Utils._.any(data, Utils._.isNull)) return '';
return '';
if (data.hasOwnProperty('inclusive')) { if (data.hasOwnProperty('inclusive')) {
if (!data.inclusive) if (!data.inclusive) data.inclusive = [false, false];
data.inclusive = [false, false]; else if (data.inclusive === true) data.inclusive = [true, true];
else if (data.inclusive === true) } else {
data.inclusive = [true, true];
} else
data.inclusive = [false, false]; data.inclusive = [false, false];
}
Utils._.each(data, function (value, index) { Utils._.each(data, function (value, index) {
if (Utils._.isObject(value)) { if (Utils._.isObject(value)) {
if (value.hasOwnProperty('inclusive')) if (value.hasOwnProperty('inclusive')) data.inclusive[index] = !!value.inclusive;
data.inclusive[index] = !!value.inclusive; if (value.hasOwnProperty('value')) data[index] = value.value;
if (value.hasOwnProperty('value'))
data[index] = value.value;
} }
}); });
return (data.inclusive[0] ? '[' : '(') + JSON.stringify(data[0]) + ',' + JSON.stringify(data[1]) + return (data.inclusive[0] ? '[' : '(') + JSON.stringify(data[0]) + ',' + JSON.stringify(data[1]) + (data.inclusive[1] ? ']' : ')');
(data.inclusive[1] ? ']' : ')');
}, },
parse: function (value, AttributeType) { parse: function (value, AttributeType) {
if (value === null) return null; if (value === null) return null;
if(typeof AttributeType === 'function') AttributeType = new AttributeType(); if(typeof AttributeType === 'function') AttributeType = new AttributeType();
AttributeType = AttributeType || ''; AttributeType = AttributeType || ''; // if attribute is not defined, assign empty string in order to prevent
// AttributeType.toString() to fail with uncaught exception later in the code
var result = value var result = value
.slice(1, -1) .slice(1, -1)
.split(',', 2); .split(',', 2);
if (result.length !== 2) if (result.length !== 2) return value;
return value;
result = result result = result
.map(function (value) { .map(function (value) {
......
...@@ -793,10 +793,10 @@ if (dialect.match(/^postgres/)) { ...@@ -793,10 +793,10 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('should read range correctly from multiple rows', function(done) { it('should read range correctly from multiple rows', function() {
var self = this; var self = this;
self.User return self.User
.create({ username: 'user1', email: ['foo@bar.com'], course_period: [new Date(2015, 0, 1), new Date(2015, 11, 31)]}) .create({ username: 'user1', email: ['foo@bar.com'], course_period: [new Date(2015, 0, 1), new Date(2015, 11, 31)]})
.then(function() { .then(function() {
return self.User.create({ username: 'user2', email: ['foo2@bar.com'], course_period: [new Date(2016, 0, 1), new Date(2016, 11, 31)]}); return self.User.create({ username: 'user2', email: ['foo2@bar.com'], course_period: [new Date(2016, 0, 1), new Date(2016, 11, 31)]});
...@@ -812,8 +812,6 @@ if (dialect.match(/^postgres/)) { ...@@ -812,8 +812,6 @@ if (dialect.match(/^postgres/)) {
expect(users[1].course_period[0].toISOString()).to.equal('2016-01-01T00:00:00.000Z'); // lower bound expect(users[1].course_period[0].toISOString()).to.equal('2016-01-01T00:00:00.000Z'); // lower bound
expect(users[1].course_period[1].toISOString()).to.equal('2016-12-31T00:00:00.000Z'); // upper bound expect(users[1].course_period[1].toISOString()).to.equal('2016-12-31T00:00:00.000Z'); // upper bound
expect(users[1].course_period.inclusive).to.deep.equal([false, false]); // not inclusive expect(users[1].course_period.inclusive).to.deep.equal([false, false]); // not inclusive
done();
}) })
.error(console.log); .error(console.log);
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!