Skip to content

Commit

Permalink
Add DateString type
Browse files Browse the repository at this point in the history
New type that preserves string input as a string, but ensures that
the string is a valid Date.

Additionally, provides a .toDate function to provide the Date
object representation of the string.
  • Loading branch information
Kevin Delisle committed Apr 26, 2017
1 parent bf7ea4c commit eba167a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/date-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

module.exports = DateString;

/**
* A String whose value is a valid representation of a Date.
* Use this type if you need to preserve the format of the value and still
* check if it's valid.
* @param {String} value
* @constructor
*/
function DateString(value) {
if (!(this instanceof DateString)) {
return new DateString(value);
}

if (typeof(value) !== 'string') {
throw new Error('Input must be a string');
}
// Preserve the reference for return via .toDate
this.date = new Date(value);
if (isNaN(this.date.getTime())) {
throw new Error('Invalid date');
}
this.value = value;
};

/**
* Return the equivalent Date object for this DateString.
* @returns {Date} A JavaScript Date object
*/
DateString.prototype.toDate = function() {
return this.date;
};
2 changes: 2 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
};

module.exports = function(modelTypes) {
var DateString = require('./date-string');
var GeoPoint = require('./geo').GeoPoint;

for (var t in Types) {
Expand All @@ -63,6 +64,7 @@ module.exports = function(modelTypes) {
modelTypes.registerType(Number);
modelTypes.registerType(Boolean);
modelTypes.registerType(Date);
modelTypes.registerType(DateString);
modelTypes.registerType(Buffer, ['Binary']);
modelTypes.registerType(Array);
modelTypes.registerType(GeoPoint);
Expand Down
37 changes: 37 additions & 0 deletions test/date-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/* global describe,it */
/* jshint expr:true */

'use strict';

require('should');

var DateString = require('../lib/date-string');

describe('DateString', function() {
describe('constructor', function() {
it('should support a valid date string', function() {
var date = new DateString('2015-01-01');
date.should.not.eql(null);
date.value.should.eql('2015-01-01');
var d = new Date('2015-01-01');
date.toDate().toString().should.eql(d.toString());
});

it('should throw on invalid input', function() {
var fn = () => {
var date = new DateString('notadate');
};
fn.should.throw(new Error('Invalid date'));

fn = () => {
var date = new DateString(20150101);
};
fn.should.throw(new Error('Input must be a string'));
});
});
});

0 comments on commit eba167a

Please sign in to comment.