-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
78 additions
and
0 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
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; | ||
}; |
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,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')); | ||
}); | ||
}); | ||
}); |