Skip to content

Commit

Permalink
refactor: date as extension
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Jul 25, 2016
1 parent bd92342 commit da5f69c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Model.reset = function () {
Model.types = [
'undefined', 'any',
'boolean', 'number', 'string',
'date', 'array', 'object', 'conditional',
'array', 'object', 'conditional',
];

Model.validations = _.cloneDeep(validations);
Expand Down
57 changes: 57 additions & 0 deletions lib/extensions/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var _ = require('lodash');
var assert = require('../assert');

module.exports = function (construct) {
return {
types: ['date'],
formats: {date: ['unix']},
builders: {
date: function () {
return construct({type: 'date'});
},
unixtimestamp: function () {
return construct({type: 'date', format: 'unix'});
},
},
transformations: {
date: {
unix: function (value) {
var asNumber = parseInt(value, 10);
if (!_.isNaN(asNumber)) {
value = asNumber;
}

if (typeof value === 'number') {
return new Date(value * 1000);
} else {
return value;
}
},
__default: function (value) {
if (typeof value === 'string' || typeof value === 'number') {
var date = new Date(value);
var dateWithTz = new Date(value + ' 00:00:00 GMT');

if (_.isNaN(date.getTime())) {
return value;
} else if (_.isNaN(dateWithTz.getTime())) {
return date;
} else {
return dateWithTz;
}
} else {
return value;
}
}
},
},
validations: {
date: {
__default: function (value) {
assert.typeof(value, 'date');
assert.ok(!_.isNaN(value.getTime()), 'invalid date');
}
},
}
};
};
18 changes: 0 additions & 18 deletions lib/transformations.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@ module.exports = {
}
}
},
date: {
__default: function (value) {
if (typeof value === 'string' || typeof value === 'number') {
var date = new Date(value);
var dateWithTz = new Date(value + ' 00:00:00 GMT');

if (_.isNaN(date.getTime())) {
return value;
} else if (_.isNaN(dateWithTz.getTime())) {
return date;
} else {
return dateWithTz;
}
} else {
return value;
}
}
},
object: {
__default: function (value, root, path) {
var children = this.spec.children;
Expand Down
6 changes: 0 additions & 6 deletions lib/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ module.exports = {
checkOptions.call(this, value);
}
},
date: {
__default: function (value) {
assert.typeof(value, 'date');
assert.ok(!_.isNaN(value.getTime()), 'invalid date');
}
},
object: {
__default: function (value) {
assert.typeof(value, 'object');
Expand Down
116 changes: 116 additions & 0 deletions test/extensions/date.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
var _ = require('lodash');
var should = require('chai').should();
var Model = relativeRequire('Model.js');

defineTest('extensions/date.js', function (dateFactory) {
var extension = dateFactory(spec => new Model(spec));

describe('#builders', function () {
var builders = extension.builders;
describe('date', function () {
it('should create a model with type date', function () {
var model = builders.date();
model.should.have.deep.property('spec.type', 'date');
});
});

describe('unix', function () {
it('should create a model with type date and format unix', function () {
Model.formats.date = ['unix'];
var model = builders.unixtimestamp();
model.should.have.deep.property('spec.type', 'date');
model.should.have.deep.property('spec.format', 'unix');
Model.reset();
});
});
});

describe('#transformations', function () {
describe('unix', function () {
var transform = extension.transformations.date.unix;

it('should transform string values', function () {
transform('86400').should.eql(new Date('1970-01-02T00:00:00.000Z'));
transform(String(86400 * 366)).should.eql(new Date('1971-01-02T00:00:00.000Z'));
});

it('should transform number values', function () {
transform(86400).should.eql(new Date('1970-01-02T00:00:00.000Z'));
transform(86400 * 366).should.eql(new Date('1971-01-02T00:00:00.000Z'));
});

it('should directly return a date value', function () {
var d1 = new Date(2016, 04, 03);
var d2 = new Date(1985, 02, 01);
transform(d1).should.equal(d1);
transform(d2).should.equal(d2);
});

it('should directly return all other values', function () {
var obj = {};
var arr = [];

transform('foobar').should.equal('foobar');
transform(obj).should.equal(obj);
transform(arr).should.equal(arr);
should.equal(transform(null), null);
should.equal(transform(undefined), undefined);
});
});

describe('__default', function () {
var transform = extension.transformations.date.__default;

it('should transform string values', function () {
transform('2016-01-04').should.eql(new Date('2016-01-04T00:00:00.000Z'));
transform('1993-02-14T08:00:00.000Z').should.eql(new Date('1993-02-14T08:00:00.000Z'));
});

it('should transform number values', function () {
transform(86400 * 1000).should.eql(new Date('1970-01-02T00:00:00.000Z'));
transform(86400 * 366 * 1000).should.eql(new Date('1971-01-02T00:00:00.000Z'));
});

it('should directly return a date value', function () {
var d1 = new Date(2016, 04, 03);
var d2 = new Date(1985, 02, 01);
transform(d1).should.equal(d1);
transform(d2).should.equal(d2);
});

it('should directly return all other values', function () {
var obj = {};
var arr = [];

transform('foobar').should.equal('foobar');
transform(obj).should.equal(obj);
transform(arr).should.equal(arr);
should.equal(transform(null), null);
should.equal(transform(undefined), undefined);
});
});
});

describe('#validations', function () {
describe('__default', function () {
var validate = extension.validations.date.__default;
it('should pass valid date values', function () {
(function () {
validate(new Date());
validate(new Date('2016-01-02'));
}).should.not.throw();
});

it('should fail invalid values', function () {
[
() => validate(null),
() => validate(undefined),
() => validate(false),
() => validate(1231235),
() => validate('2016-01-12'),
() => validate(new Date('unknown')),
].forEach(f => f.should.throw());
});
});
});
});
32 changes: 0 additions & 32 deletions test/transformations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,6 @@ defineTest('transformations.js', function (transformations) {
});
});

describe('#date', function () {
var transform = transformations.date.__default;

it('should transform string values', function () {
transform('2016-01-04').should.eql(new Date('2016-01-04T00:00:00.000Z'));
transform('1993-02-14T08:00:00.000Z').should.eql(new Date('1993-02-14T08:00:00.000Z'));
});

it('should transform number values', function () {
transform(86400 * 1000).should.eql(new Date('1970-01-02T00:00:00.000Z'));
transform(86400 * 366 * 1000).should.eql(new Date('1971-01-02T00:00:00.000Z'));
});

it('should directly return a date value', function () {
var d1 = new Date(2016, 04, 03);
var d2 = new Date(1985, 02, 01);
transform(d1).should.equal(d1);
transform(d2).should.equal(d2);
});

it('should directly return all other values', function () {
var obj = {};
var arr = [];

transform('foobar').should.equal('foobar');
transform(obj).should.equal(obj);
transform(arr).should.equal(arr);
should.equal(transform(null), null);
should.equal(transform(undefined), undefined);
});
});

describe('#object', function () {
var transform = transformations.object.__default;

Expand Down
12 changes: 0 additions & 12 deletions test/validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ defineTest('validations.js', function (validations) {
});
});

describe('#date', function () {
var validate = validations.date.__default;

it('should pass when date', function () {
testPassingValues([new Date(), new Date('2016-01-12')], validate);
});

it('should fail when not date', function () {
testFailingValues([null, true, 12, 0, {}, '2011-01-01', new Date('foo')], validate);
});
});

describe('#object', function () {
var validate = validations.object.__default;

Expand Down

0 comments on commit da5f69c

Please sign in to comment.