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

Commit dc181e2d by Dr. Evil

Prettified code, refactored RANGE datatype constructor bits

1 parent ed60554a
......@@ -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
* @property RANGE
*/
var RANGE = function (Subtype) {
var options = {};
if(typeof Subtype === 'function') { // if subtype passed - instantiate object of this subtype and return new function
options.subtype = new Subtype();
return RANGE.bind({}, options);
}
else if(typeof Subtype === 'object' && Subtype.hasOwnProperty('subtype'))
options = Subtype;
var RANGE = function (subtype) {
var options = _.isPlainObject(subtype) ? subtype : { subtype: subtype };
if (!options.subtype) options.subtype = INTEGER;
if (!(this instanceof RANGE)) return new RANGE(options);
ABSTRACT.apply(this, arguments);
this._subtype = options.subtype ? (options.subtype.key || 'INTEGER') : 'INTEGER';
this._subtype = options.subtype.key;
};
util.inherits(RANGE, ABSTRACT);
......@@ -513,7 +510,7 @@ ENUM.prototype.key = ENUM.key = 'ENUM';
* @property ARRAY
*/
var ARRAY = function(type) {
var options = typeof type === "object" && !(type instanceof ABSTRACT) && type || {
var options = _.isPlainObject(type) && !(type instanceof ABSTRACT) && type || {
type: type
};
if (!(this instanceof ARRAY)) return new ARRAY(options);
......
......@@ -7,44 +7,37 @@ module.exports = {
stringify: function (data) {
if (data === null) return null;
if (!Utils._.isArray(data) || data.length !== 2)
return '';
if (!Utils._.isArray(data) || data.length !== 2) return '';
if (Utils._.any(data, Utils._.isNull))
return '';
if (Utils._.any(data, Utils._.isNull)) return '';
if (data.hasOwnProperty('inclusive')) {
if (!data.inclusive)
data.inclusive = [false, false];
else if (data.inclusive === true)
data.inclusive = [true, true];
} else
if (!data.inclusive) data.inclusive = [false, false];
else if (data.inclusive === true) data.inclusive = [true, true];
} else {
data.inclusive = [false, false];
}
Utils._.each(data, function (value, index) {
if (Utils._.isObject(value)) {
if (value.hasOwnProperty('inclusive'))
data.inclusive[index] = !!value.inclusive;
if (value.hasOwnProperty('value'))
data[index] = value.value;
if (value.hasOwnProperty('inclusive')) data.inclusive[index] = !!value.inclusive;
if (value.hasOwnProperty('value')) data[index] = value.value;
}
});
return (data.inclusive[0] ? '[' : '(') + JSON.stringify(data[0]) + ',' + JSON.stringify(data[1]) +
(data.inclusive[1] ? ']' : ')');
return (data.inclusive[0] ? '[' : '(') + JSON.stringify(data[0]) + ',' + JSON.stringify(data[1]) + (data.inclusive[1] ? ']' : ')');
},
parse: function (value, AttributeType) {
if (value === null) return null;
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
.slice(1, -1)
.split(',', 2);
if (result.length !== 2)
return value;
if (result.length !== 2) return value;
result = result
.map(function (value) {
......
......@@ -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;
self.User
return self.User
.create({ username: 'user1', email: ['foo@bar.com'], course_period: [new Date(2015, 0, 1), new Date(2015, 11, 31)]})
.then(function() {
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/)) {
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.inclusive).to.deep.equal([false, false]); // not inclusive
done();
})
.error(console.log);
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!