Skip to content

Commit 709837b

Browse files
author
Kevin Delisle
committed
Add DateString type
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.
1 parent bf7ea4c commit 709837b

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

lib/date-string.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
2+
// Node module: loopback-datasource-juggler
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict';
7+
8+
module.exports = DateString;
9+
10+
/**
11+
* A String whose value is a valid representation of a Date.
12+
* Use this type if you need to preserve the format of the value and still
13+
* check if it's valid.
14+
* @param {String} value
15+
* @constructor
16+
*/
17+
function DateString(value) {
18+
if (!(this instanceof DateString)) {
19+
return new DateString(value);
20+
}
21+
22+
if (typeof(value) !== 'string') {
23+
throw new Error('Input must be a string');
24+
}
25+
// Preserve the reference for return via .toDate
26+
this.date = new Date(value);
27+
if (isNaN(this.date.getTime())) {
28+
throw new Error('Invalid date');
29+
}
30+
this.value = value;
31+
};
32+
33+
/**
34+
* Returns the value of DateString in its original form.
35+
* @returns {String} The Date as a String.
36+
*/
37+
DateString.prototype.toString = function() {
38+
return this.value;
39+
};
40+
41+
DateString.prototype.toJSON = function() {
42+
return this.value;
43+
};

lib/types.js

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
4040
};
4141

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

4546
for (var t in Types) {
@@ -63,6 +64,7 @@ module.exports = function(modelTypes) {
6364
modelTypes.registerType(Number);
6465
modelTypes.registerType(Boolean);
6566
modelTypes.registerType(Date);
67+
modelTypes.registerType(DateString);
6668
modelTypes.registerType(Buffer, ['Binary']);
6769
modelTypes.registerType(Array);
6870
modelTypes.registerType(GeoPoint);

test/date-string.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
2+
// Node module: loopback-datasource-juggler
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
/* global describe,it */
7+
/* jshint expr:true */
8+
9+
'use strict';
10+
11+
require('should');
12+
13+
var DateString = require('../lib/date-string');
14+
15+
describe('DateString', function() {
16+
describe('constructor', function() {
17+
it('should support a valid date string', function() {
18+
var theDate = '2015-01-01';
19+
var date = new DateString(theDate);
20+
date.should.not.eql(null);
21+
date.value.should.eql(theDate);
22+
date.toString().should.eql(theDate);
23+
});
24+
25+
it('should throw on invalid input', function() {
26+
var fn = () => {
27+
var date = new DateString('notadate');
28+
};
29+
fn.should.throw(new Error('Invalid date'));
30+
31+
fn = () => {
32+
var date = new DateString(20150101);
33+
};
34+
fn.should.throw(new Error('Input must be a string'));
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)