File tree 3 files changed +82
-0
lines changed
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
40
40
} ;
41
41
42
42
module . exports = function ( modelTypes ) {
43
+ var DateString = require ( './date-string' ) ;
43
44
var GeoPoint = require ( './geo' ) . GeoPoint ;
44
45
45
46
for ( var t in Types ) {
@@ -63,6 +64,7 @@ module.exports = function(modelTypes) {
63
64
modelTypes . registerType ( Number ) ;
64
65
modelTypes . registerType ( Boolean ) ;
65
66
modelTypes . registerType ( Date ) ;
67
+ modelTypes . registerType ( DateString ) ;
66
68
modelTypes . registerType ( Buffer , [ 'Binary' ] ) ;
67
69
modelTypes . registerType ( Array ) ;
68
70
modelTypes . registerType ( GeoPoint ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments