Skip to content

Commit 5650d82

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 5650d82

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

lib/date-string.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
var inspect = require('util').inspect;
9+
10+
module.exports = DateString;
11+
12+
/**
13+
* A String whose value is a valid representation of a Date.
14+
* Use this type if you need to preserve the format of the value and still
15+
* check if it's valid.
16+
* @param {String} value
17+
* @constructor
18+
*/
19+
function DateString(value) {
20+
if (!(this instanceof DateString)) {
21+
return new DateString(value);
22+
}
23+
24+
if (typeof(value) !== 'string') {
25+
throw new Error('Input must be a string');
26+
}
27+
28+
Object.defineProperty(this, 'value', {
29+
get: () => { return this._value; },
30+
set: (val) => {
31+
var d = new Date(val);
32+
if (isNaN(d.getTime())) {
33+
throw new Error('Invalid date');
34+
} else {
35+
this._value = val;
36+
this._date = d;
37+
}
38+
},
39+
});
40+
41+
this.value = value;
42+
};
43+
44+
/**
45+
* Returns the value of DateString in its original form.
46+
* @returns {String} The Date as a String.
47+
*/
48+
DateString.prototype.toString = function() {
49+
return this.value;
50+
};
51+
52+
DateString.prototype.toJSON = function() {
53+
return this.value;
54+
};
55+
56+
DateString.prototype.inspect = function(depth, options) {
57+
return 'DateString ' + inspect({
58+
value: this.value,
59+
_date: this._date,
60+
});
61+
};

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

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
var fmt = require('util').format;
15+
var inspect = require('util').inspect;
16+
var os = require('os');
17+
18+
describe('DateString', function() {
19+
describe('constructor', function() {
20+
it('should support a valid date string', function() {
21+
var theDate = '2015-01-01';
22+
var date = new DateString(theDate);
23+
date.should.not.eql(null);
24+
date.value.should.eql(theDate);
25+
date.toString().should.eql(theDate);
26+
});
27+
28+
it('should throw on invalid input', function() {
29+
var fn = () => {
30+
var date = new DateString('notadate');
31+
};
32+
fn.should.throw(new Error('Invalid date'));
33+
34+
fn = () => {
35+
var date = new DateString('2015-01-01 25:00:00');
36+
};
37+
fn.should.throw(new Error('Invalid date'));
38+
39+
fn = () => {
40+
var date = new DateString(20150101);
41+
};
42+
fn.should.throw(new Error('Input must be a string'));
43+
});
44+
45+
it('should update internal date on set', function() {
46+
var date = new DateString('2015-01-01');
47+
date.value = '2016-01-01';
48+
date.value.should.eql('2016-01-01');
49+
var d = new Date('2016-01-01');
50+
// The internal date representation should also be updated!
51+
date._date.toString().should.eql(d.toString());
52+
});
53+
54+
it('should allow date with time', function() {
55+
var theDate = '2015-01-01 02:00:00';
56+
var date = new DateString(theDate);
57+
date.value.should.eql(theDate);
58+
var d = new Date(theDate);
59+
date._date.toString().should.eql(d.toString());
60+
});
61+
62+
it('should return custom inspect output', function() {
63+
var date = new DateString('2015-01-01');
64+
var result = inspect(date);
65+
result.should.not.eql(null);
66+
result.should.eql(fmt('DateString ' + inspect({
67+
value: date.value,
68+
_date: date._date,
69+
})));
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)