-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd92342
commit da5f69c
Showing
7 changed files
with
174 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}, | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters