Permalink
Browse files

- renamed `Date.toDate` to `Date.coerce` to be consistent with other …

…libs — `Date.toDate` maps to `Date.coerce` to avoid breaking anything

- all references to `Date` changed to `Type` to be inline with sandboxing model
- fixed issue with `Date.prototype.diff` not setting props correctly when using current `Date` as the `now` param
- added `Date.valid` and `Date.prototype.valid`
  • Loading branch information...
constantology committed Nov 21, 2012
1 parent a76af59 commit 18f3161059f4c47438f382b919c55bf2af11ce1d
Showing with 27 additions and 21 deletions.
  1. +1 −1 .catn8
  2. +4 −4 src/{toDate.js → coerce.js}
  3. +11 −11 src/diff.js
  4. +9 −5 src/expose.js
  5. +2 −0 src/fns.js
View
2 .catn8
@@ -1,7 +1,7 @@
{
"source" : {
"dir" : "./src",
- "files" : ["utils", "fns", "format", "toDate", "diff", "vars", "expose"]
+ "files" : ["utils", "coerce", "diff", "display", "fns", "format", "vars", "expose"]
},
"target" : {
"dir" : "{pwd}"
@@ -1,3 +1,7 @@
+ function coerce( date_str, date_format ) {
+ return buildParser( date_format )( date_str );
+ }
+
function buildParser( date_format ) {
if ( cache_parse[date_format] ) return cache_parse[date_format];
var parsers = {}, keys = [], i = -1, part,
@@ -95,7 +99,3 @@
function parse_setTimezoneOffset( date, tzoffset ) {
!between_equalto( tzoffset, -43200, 50400 ) || date.adjust( Type.SECOND, ( -date.getTimezoneOffset() * 60 ) - tzoffset );
}
-
- function toDate( date_str, date_format ) {
- return buildParser( date_format )( date_str );
- }
View
@@ -1,22 +1,22 @@
function diff( now, props ) { //noinspection FallthroughInSwitchStatementJS
switch ( util.ntype( now ) ) {
case 'string' :
- if ( isNaN( +( new Date( now ) ) ) ) {
- if ( props ) {
- now = Date.now();
- break;
- }
- }
+ if ( valid( new Type( now ) ) )
+ now = new Type( now );
else {
- now = new Date( now );
+ if ( !props )
+ props = now;
+
+ now = new Type( now );
+
break;
- } // allow [specific] fall-through
+ } // allow [specific] fall-through
case 'array' : case 'object' :
props = now;
- now = Date.now();
+ now = Type.now();
break;
- case 'date' : if ( !isNaN( +( new Date( now ) ) ) ) break; // allow fall-through if not a valid date
- default : now = Date.now();
+ case 'date' : if ( valid( new Type( now ) ) ) break; // allow fall-through if not a valid date
+ default : now = Type.now();
}
View
@@ -6,17 +6,21 @@
getISODay : getISODay, getISODaysInYear : getISODaysInYear, getISOFirstMondayOfYear : getISOFirstMondayOfYear,
getISOWeek : getISOWeek, getISOWeeksInYear : getISOWeeksInYear, getLastOfTheMonth : getLastOfTheMonth,
getWeek : getWeek, isDST : isDST, isLeapYear : isLeapYear,
- setWeek : setWeek, timezone : timezone
+ setWeek : setWeek, timezone : timezone, valid : function() { return Type.valid( this ); }
}, 'r' );
// static methods & properties
util.defs( Type, {
// constants used by Date.prototype.adjust
- DAY : DAY, HOUR : 'hr', MINUTE : MINUTE.substring( 0, 3 ), MILLISECOND : MILLISECOND, MONTH : MONTH, SECOND : SECOND.substring( 0, 3 ), WEEK : WEEK, YEAR : YEAR,
+ DAY : DAY, HOUR : 'hr', MINUTE : MINUTE.substring( 0, 3 ),
+ MILLISECOND : MILLISECOND, MONTH : MONTH, SECOND : SECOND.substring( 0, 3 ),
+ WEEK : WEEK, YEAR : YEAR,
// constants defining milliseconds for different times
- MS_DAY : MS_DAY, MS_HOUR : MS_HOUR, MS_MINUTE : MS_MINUTE, MS_MONTH : MS_MONTH, MS_SECOND : MS_SECOND, MS_WEEK : MS_WEEK, MS_YEAR : MS_YEAR,
+ MS_DAY : MS_DAY, MS_HOUR : MS_HOUR, MS_MINUTE : MS_MINUTE, MS_MONTH : MS_MONTH,
+ MS_SECOND : MS_SECOND, MS_WEEK : MS_WEEK, MS_YEAR : MS_YEAR,
// filters and formats
- filters : { value : filter }, formats : { value : formats }, parsers : { value : parser },
+ filters : { value : filter }, formats : { value : formats }, parsers : { value : parser },
// static methods
- getOrdinal : LOCALE.getOrdinal, isLeapYear : LOCALE.isLeapYear, setLeapYear : LOCALE.setLeapYear, toDate : toDate
+ coerce : coerce, getOrdinal : LOCALE.getOrdinal, isLeapYear : LOCALE.isLeapYear,
+ setLeapYear : LOCALE.setLeapYear, toDate : coerce, valid : valid
}, 'r' );
View
@@ -102,3 +102,5 @@
var s = this.toString().split( ' ' );
return s.splice( 4, s.length ).join( ' ' ).replace( re_tz, '$1' ).replace( re_tz_abbr, '' );
}
+
+ function valid( date ) { return util.ntype( date ) == 'date' && !isNaN( +date ); }

0 comments on commit 18f3161

Please sign in to comment.