Comparing version 0.3.8 to 0.4.0
843
d8.js
@@ -8,5 +8,4 @@ ;!function( util ) { | ||
function _indexOf( o, k ) { var i = o.indexOf( k ); return i == -1 ? null : i; } | ||
function _lc( o ) { return o.toLowerCase(); } | ||
function _substr( s ) { return s.substring( 0, 3 ); } | ||
function _uc( o ) { return o.toUpperCase(); } | ||
function _lc( o ) { return o.toLocaleLowerCase(); } | ||
function _uc( o ) { return o.toLocaleUpperCase(); } | ||
function associate( o, k ) { return o.reduce( function( res, v, i ) { res[k[i]] = v; return res; }, {} ); } | ||
@@ -20,144 +19,49 @@ function between_equalto( v, l, h ) { return l <= v && v <= h; } | ||
} | ||
function pluck( a, k ) { return a.reduce( function( v, o ) { !( k in o ) || v.push( o[k] ); return v; }, [] ); } | ||
function sum( v, i ) { return v + i; } | ||
/*~ src/fns.js ~*/ | ||
// private methods | ||
function _24hrTime( o, res ) { return ( o = Number( o ) ) < 12 && _lc( res.ampm ) == _lc( LOCALE.PM ) ? o += 12 : o; } | ||
function _adjust( d, v, k ) { return d.adjust( k, v ); } | ||
function _adjust_toobj( a ) { | ||
return adjust_order.reduce( function( v, k, i ) { | ||
var delta = parseFloat( a[i] ); | ||
/*~ src/vars.js ~*/ | ||
var U, | ||
// DAY_OFFSETS is the amount of days from the current day to the Monday of the week it belongs to | ||
DAY_OFFSETS = [9, 1, 0, -1, -2, 4, 3], MS_DAY = 864e5, MS_HOUR = 3600000, MS_MINUTE = 60000, | ||
MS_MONTH = 2592e6, MS_SECOND = 1000, MS_WEEK = 6048e5, MS_YEAR = 31536e6, | ||
// parser keys | ||
AMPM = 'ampm', DAY = 'day', DAYWEEK = 'dayweek', DAYYEAR = 'dayyear', HOUR = 'hour', | ||
MILLISECOND = 'ms', MINUTE = 'minute', MONTH = 'month', SECOND = 'second', TIMEZONE = 'timezone', | ||
UNIX = 'unix', WEEK = 'week', YEAR = 'year', | ||
// used by Date.prototype.format && Date.toDate to replace escaped chars | ||
NOREPLACE = 'NOREPLACE', NOREPLACE_RB = '<' + NOREPLACE + '<', NOREPLACE_RE = '>END' + NOREPLACE + '>', | ||
adjust_by = { day : ['getDate', 'setDate'], hr : ['getHours', 'setHours'], min : ['getMinutes', 'setMinutes'], month : ['getMonth', 'setMonth'], ms : ['getMilliseconds', 'setMilliseconds'], sec : ['getSeconds', 'setSeconds'], week : ['getWeek', 'setWeek'], year : ['getFullYear', 'setFullYear'] }, | ||
adjust_order = [YEAR, MONTH, WEEK, DAY, 'hr', MINUTE.substring( 0, 3 ), SECOND.substring( 0, 3 ), MILLISECOND], | ||
// cache objects | ||
cache_format = util.obj(), cache_parse = util.obj(), date_members = [DAY, DAYWEEK, DAYYEAR, MONTH, WEEK, YEAR], | ||
filter, filter_chars, formats, lexicon = util.obj(), locales = util.obj(), m, parser, | ||
re_ampm = '(am|pm)', re_add_enr = />/g, re_add_nr = /</g, re_compile, | ||
re_d1_2 = '([0-9]{1,2})', re_d2 = '([0-9]{2})', re_d4 = '([0-9]{4})', | ||
re_space = /\s{2,}/g, re_split = /[<>]/, re_tz = /[^\(]*\(([^\)]+)\)/g, | ||
re_tz_abbr = /[^A-Z]+/g, re_tz_off = /[\+-]?([0-9]{2}):?([0-9]{2})/, | ||
time_map = [ // the order of this Array is important as it is the remainder of the larger | ||
[YEAR + 's', MS_YEAR], // time unit that gets passed to the following time unit — as such we want | ||
[MONTH + 's', MS_MONTH], // to keep the order in case we want to exclude time units from the diff | ||
[WEEK + 's', MS_WEEK], | ||
[DAY + 's', MS_DAY], | ||
[HOUR + 's', MS_HOUR], | ||
[MINUTE + 's', MS_MINUTE], | ||
[SECOND + 's', MS_SECOND], | ||
[MILLISECOND, 1] | ||
], | ||
time_props = time_map.pluck( 0 ); | ||
if ( !isNaN( delta ) && delta !== 0 ) | ||
v[k] = delta; | ||
return v; | ||
}, util.obj() ); | ||
/*~ src/coerce.js ~*/ | ||
function coerce( date_str, date_format ) { | ||
return buildParser( date_format )( date_str ); | ||
} | ||
function _dayOffset( d ) { return Math.floor( ( d - getISOFirstMondayOfYear.call( d ) ) / MS_DAY ); } | ||
function _timezoneOffset( o ) { | ||
var t = !!o.indexOf( '-' ), | ||
m = o.match( re_tz_off ), | ||
v = ( Number( m[1] ) + ( m[2] / 60 ) ) * 3600; | ||
return t ? v : -v; | ||
} | ||
function _weekOffset( d ) { return Math.floor( Math.abs( _dayOffset( d ) / 7 ) ); } | ||
function _zeroIndexedInt( o, k ) { return !isNaN( k ) ? k == o ? 0 : Number( k ) : Number( o ) - 1; } | ||
// public methods | ||
function buildParser( date_format ) { | ||
var LID = Type.locale.id, i, keys, l, parsers, part, parts, re; | ||
function adjust( o, v ) { | ||
var date = this, day, fn, weekday; // noinspection FallthroughInSwitchStatementJS | ||
switch ( util.ntype( o ) ) { | ||
case 'number' : o = _adjust_toobj( arguments ); // allow fall-through | ||
case 'object' : Object.reduce( o, _adjust, date ); break; | ||
case 'string' : | ||
fn = adjust_by[o.toLowerCase()]; | ||
if ( fn && v !== 0 ) { | ||
LOCALE.setLeapYear( date ); | ||
if ( cache_parse[LID][date_format] ) return cache_parse[LID][date_format]; | ||
if ( fn == adjust_by.month ) { | ||
day = date.getDate(); | ||
day < 28 || date.setDate( Math.min( day, getLastOfTheMonth.call( getFirstOfTheMonth.call( date ).adjust( Type.MONTH, v ) ).getDate() ) ); | ||
} | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ); | ||
keys = []; i = -1; l = parts.length; parsers = {}; re = []; | ||
fn != adjust_by.week || ( weekday = date.getDay() ); | ||
date[fn[1]]( date[fn[0]]() + v ); | ||
!weekday || date.setDate( date.getDate() + weekday ); | ||
} | ||
} | ||
return date; | ||
} | ||
function between( l, h ) { return this >= l && this <= h; } | ||
function clearTime() { | ||
this.setHours( 0 ); this.setMinutes( 0 ); this.setSeconds( 0 ); this.setMilliseconds( 0 ); | ||
return this; | ||
} | ||
function clone() { return new Type( this.getTime() ); } | ||
function getDayOfYear() { | ||
LOCALE.setLeapYear( this ); | ||
return LOCALE.day_count.slice( 0, this.getMonth() ).reduce( sum, 0 ) + this.getDate() - 1; | ||
} | ||
function getFirstOfTheMonth() { return new Type( this.getFullYear(), this.getMonth(), 1 ); } | ||
function getGMTOffset( colon ) { | ||
var tz = this.getTimezoneOffset(); | ||
return [( tz > 0 ? '-' : '+' ), pad( Math.floor( Math.abs( tz ) / 60 ), 2 ), ( colon ? ':' : '' ), pad( Math.abs( tz % 60 ), 2 )].join( '' ); | ||
} | ||
function getISODay() { return this.getDay() || 7; } | ||
function getISODaysInYear() { return Math.ceil( ( getISOFirstMondayOfYear.call( new Type( this.getFullYear() + 1, 0, 1 ) ) - getISOFirstMondayOfYear.call( this ) ) / MS_DAY ); } | ||
function getISOFirstMondayOfYear() { | ||
var y = this.getFullYear(); | ||
return new Type( y, 0, DAY_OFFSETS[new Type( y, 0, 1 ).getDay()] ); | ||
} | ||
function getISOWeek() { | ||
var w, y = this.getFullYear(); | ||
if ( this >= getISOFirstMondayOfYear.call( new Type( y + 1, 0, 1 ) ) ) return 1; | ||
w = Math.floor( ( getDayOfYear.call( this ) - getISODay.call( this ) + 10 ) / 7 ); | ||
return w == 0 ? getISOWeeksInYear.call( new Type( y - 1, 0, 1 ) ) - _weekOffset( this ) : w; | ||
} | ||
function getISOWeeksInYear() { return Math.round( ( getISOFirstMondayOfYear.call( new Type( this.getFullYear() + 1, 0, 1 ) ) - getISOFirstMondayOfYear.call( this ) ) / MS_WEEK ); } | ||
function getLastOfTheMonth() { | ||
var m = this.getMonth(); LOCALE.setLeapYear( this ); | ||
return new Type( this.getFullYear(), m, LOCALE.day_count[m] ); | ||
} | ||
function getWeek() { return Math.floor( getDayOfYear.call( this ) / 7 ); } | ||
function isDST() { return new Type( this.getFullYear(), 0, 1 ).getTimezoneOffset() != this.getTimezoneOffset(); } | ||
function isLeapYear() { return LOCALE.isLeapYear( this.getFullYear() ); } | ||
function setWeek( v ) { this.setMonth( 0 ); this.setDate( 1 ); return ( this.adjust( Type.DAY, v * 7 ) ).getTime(); } | ||
function timezone() { | ||
var s = this.toString().split( ' ' ); | ||
return s.splice( 4, s.length ).join( ' ' ).replace( re_tz, '$1' ).replace( re_tz_abbr, '' ); | ||
} | ||
/*~ src/format.js ~*/ | ||
function buildTemplate( date_format ) { | ||
if ( cache_format[date_format] ) return cache_format[date_format]; | ||
var fn = ['var out=[];'], i = -1, part, | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ), | ||
re_invalid = /^[^A-Za-z]*$/g, l = parts.length; | ||
while( ++i < l ) { | ||
part = parts[i]; | ||
part == NOREPLACE ? ( fn.push( tplOut( parts[++i] ) ), ++i ) | ||
: re_invalid.test( part ) | ||
? fn.push( tplOut( part ) ) | ||
: fn.push( compileTplStr( part ) ); | ||
} | ||
fn.push( 'return out.join( "" );\n//@ sourceURL=d8/format/' + date_format ); | ||
return cache_format[date_format] = new Function( 'filter', 'date', fn.join( '\n' ) ); | ||
} | ||
function format( f ) { return buildTemplate( f )( filter, this ); } | ||
function compileTplStr( o ) { return o.replace( re_compile, function( m, p0, p1, p2 ) { return tplOut( p0 + '\', filter.' + p1 + '( date ), \'' + p2 ); } ); } | ||
function tplOut( s ) { return 'out.push( \'' + s + '\' );'; } | ||
/*~ src/toDate.js ~*/ | ||
function buildParser( date_format ) { | ||
if ( cache_parse[date_format] ) return cache_parse[date_format]; | ||
var parsers = {}, keys = [], i = -1, part, | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ), | ||
l = parts.length, re = []; | ||
while ( ++i < l ) { | ||
@@ -177,4 +81,4 @@ part = parts[i]; | ||
if ( _p.combo ) { | ||
_k = pluck( _p.combo, 'k' ); | ||
_fn = associate( pluck( _p.combo, 'fn' ), _k ); | ||
_k = _p.combo.pluck( 'k' ); | ||
_fn = associate( _p.combo.pluck( 'fn' ), _k ); | ||
keys.push.apply( keys, _k ); | ||
@@ -186,3 +90,3 @@ util.copy( parsers, _fn, true ); | ||
} | ||
return cache_parse[date_format] = parse.bind( null, new RegExp( re.join( '' ) ), keys, parsers ); | ||
return cache_parse[LID][date_format] = parse.bind( null, new RegExp( re.join( '' ) ), keys, parsers ); | ||
} | ||
@@ -211,3 +115,3 @@ | ||
function parse_setDate( date, parsers ) { | ||
var dayweek, i = -1, l, leapyr, ordinal; | ||
var L = Type.locale, dayweek, i = -1, l, leapyr, ordinal; | ||
@@ -219,4 +123,4 @@ if ( date_members.every( util.has.bind( null, parsers ) ) ) return; // only set the date if there's one to set (i.e. the format is not just for time) | ||
if ( isNaN( parsers[MONTH] ) ) { | ||
leapyr = LOCALE.isLeapYear( parsers[YEAR] ) ? 1 : 0; | ||
ordinal = LOCALE.ordinal_day_count[leapyr]; | ||
leapyr = L.isLeapYear( parsers[YEAR] ) ? 1 : 0; | ||
ordinal = L.ordinal_day_count[leapyr]; | ||
l = ordinal.length; | ||
@@ -259,82 +163,80 @@ parsers[MONTH] = 0; | ||
function toDate( date_str, date_format ) { | ||
return buildParser( date_format )( date_str ); | ||
} | ||
/*~ src/diff.js ~*/ | ||
function diff( now, props ) { //noinspection FallthroughInSwitchStatementJS | ||
switch ( util.ntype( now ) ) { | ||
case 'string' : | ||
if ( isNaN( +( new Date( now ) ) ) ) { | ||
if ( props ) { | ||
now = Date.now(); | ||
break; | ||
} | ||
} | ||
case 'number' : case 'string' : | ||
if ( valid( new Type( now ) ) ) | ||
now = new Type( now ); | ||
else { | ||
now = new Date( now ); | ||
if ( !props ) props = now; | ||
now = 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 [conditional] fall-through if not a valid date | ||
default : now = Type.now(); | ||
} | ||
var ddiiff, | ||
ms = now - ( +this ), | ||
var diff, | ||
ms = +now - +this, | ||
tense = ms < 0 ? 1 : ms > 0 ? -1 : 0; | ||
ddiiff = tense === 0 ? util.obj() : diff_get( Math.abs( ms ), diff_get_exclusions( props ) ); | ||
if ( !tense ) { | ||
diff = util.obj(); | ||
diff.value = 0; | ||
} | ||
else | ||
diff = diff_get( Math.abs( ms ), diff_get_exclusions( props ) ); | ||
ddiiff.tense = tense; | ||
diff.tense = tense; | ||
return ddiiff; | ||
return diff; | ||
} | ||
function diff_eval( ddiiff, calc, i, calcs ) { | ||
function diff_eval( diff, calc, i, calcs ) { | ||
var time; | ||
if ( ddiiff.__ms__ ) { | ||
if ( !ddiiff.excl[calc[0]] ) { | ||
if ( ddiiff.__ms__ >= calc[1] ) { | ||
if ( diff.__ms__ ) { | ||
if ( !diff.excl[calc[0]] ) { | ||
if ( diff.__ms__ >= calc[1] ) { | ||
time = ddiiff.__ms__ / calc[1]; | ||
time = diff.__ms__ / calc[1]; | ||
if ( !( calc[0] in ddiiff.val ) ) { | ||
ddiiff.__ms__ = ( time % 1 ) * calc[1]; | ||
ddiiff.val[calc[0]] = Math.floor( time ); | ||
if ( !( calc[0] in diff.val ) ) { | ||
diff.__ms__ = ( time % 1 ) * calc[1]; | ||
diff.val[calc[0]] = Math.floor( time ); | ||
} | ||
else { | ||
time = Math.floor( time ); | ||
ddiiff.__ms__ -= time * calc[1]; | ||
ddiiff.val[calc[0]] += time; | ||
diff.__ms__ -= time * calc[1]; | ||
diff.val[calc[0]] += time; | ||
} | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
// round up or down depending on what's available | ||
if ( ( !calcs[i + 1] || ddiiff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) { | ||
time = ddiiff.__ms__ / calc[1]; | ||
ddiiff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( ddiiff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] ); | ||
return diff_eval( ddiiff, calc, i - 1, [] ); | ||
if ( ( !calcs[i + 1] || diff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) { | ||
time = diff.__ms__ / calc[1]; | ||
diff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( diff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] ); | ||
return diff_eval( diff, calc, i - 1, [] ); | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
function diff_get( ms, excl ) { | ||
var ddiiff = diff_calc.reduce( diff_eval, { | ||
__ms__ : ms, | ||
excl : excl, | ||
val : util.obj() | ||
var diff = time_map.reduce( diff_eval, { | ||
__ms__ : ms, excl : excl, val : util.obj() | ||
} ).val; | ||
ddiiff.value = ms; | ||
diff.value = ms; | ||
return ddiiff; | ||
return diff; | ||
} | ||
@@ -354,3 +256,3 @@ | ||
diff_props.map( function( prop ) { | ||
time_props.map( function( prop ) { | ||
if ( !( prop in this ) ) | ||
@@ -370,6 +272,6 @@ this[prop] = !incl_remaining; | ||
case '>' : | ||
diff_calc.map( diff_excl_iter, { excl : excl, prop : prop, val : true } ); | ||
time_map.map( diff_excl_iter, { excl : excl, prop : prop, val : true } ); | ||
break; | ||
case '<' : | ||
diff_calc.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } ); | ||
time_map.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } ); | ||
break; | ||
@@ -389,150 +291,453 @@ default : excl[val] = false; | ||
/*~ src/vars.js ~*/ | ||
var LOCALE = Type.locale, U, | ||
// DAY_OFFSETS is the amount of days from the current day to the Monday of the week it belongs to | ||
DAY_OFFSETS = [9, 1, 0, -1, -2, 4, 3], MS_DAY = 864e5, MS_HOUR = 3600000, MS_MINUTE = 60000, | ||
MS_MONTH = 2592e6, MS_SECOND = 1000, MS_WEEK = 6048e5, MS_YEAR = 31536e6, | ||
SHORT_DAYS = LOCALE.days.map( _substr ), SHORT_MONTHS = LOCALE.months.map( _substr ), | ||
// parser keys | ||
AMPM = 'ampm', DAY = 'day', DAYWEEK = 'dayweek', DAYYEAR = 'dayyear', HOUR = 'hour', MILLISECOND = 'ms', MINUTE ='minute', | ||
MONTH = 'month', SECOND = 'second', TIMEZONE = 'timezone', UNIX = 'unix', WEEK = 'week', YEAR = 'year', | ||
// used by Date.prototype.format && Date.toDate to replace escaped chars | ||
NOREPLACE = 'NOREPLACE', NOREPLACE_RB = '<' + NOREPLACE + '<', NOREPLACE_RE = '>END' + NOREPLACE + '>', | ||
adjust_by = { day : ['getDate', 'setDate'], hr : ['getHours', 'setHours'], min : ['getMinutes', 'setMinutes'], month : ['getMonth', 'setMonth'], ms : ['getMilliseconds', 'setMilliseconds'], sec : ['getSeconds', 'setSeconds'], week : ['getWeek', 'setWeek'], year : ['getFullYear', 'setFullYear'] }, | ||
adjust_order = [YEAR, MONTH, WEEK, DAY, 'hr', MINUTE.substring( 0, 3 ), SECOND.substring( 0, 3 ), MILLISECOND], | ||
// cache objects | ||
cache_format = {}, cache_parse = {}, date_members = [DAY, DAYWEEK, DAYYEAR, MONTH, WEEK, YEAR], | ||
diff_calc = [ // the order of this Array is important as it is the remainder of the larger | ||
[YEAR + 's', MS_YEAR], // time unit that gets passed to the following time unit — as such we want | ||
[MONTH + 's', MS_MONTH], // to keep the order in case we want to exclude time units from the diff | ||
[WEEK + 's', MS_WEEK], | ||
[DAY + 's', MS_DAY], | ||
[HOUR + 's', MS_HOUR], | ||
[MINUTE + 's', MS_MINUTE], | ||
[SECOND + 's', MS_SECOND], | ||
[MILLISECOND, 1] | ||
], | ||
diff_props = diff_calc.map( function( calc ) { return calc[0]; } ), | ||
filter = { | ||
// this ensures a diff's keys are always in descending order of | ||
// number of milliseconds per unit of time, i.e. year, ..., millisecond | ||
function diff_keys( diff ) { | ||
diff = util.copy( diff ); util.remove( diff, 'tense', 'value' ); | ||
// while this may seem like overkill, only having to run `indexOf` once for each sort item means that | ||
// the overall performance is dramatically improved | ||
return Object.keys( diff ).map( function( k ) { | ||
return [time_props.indexOf( k ), k]; | ||
} ).sort( function( a, b ) { | ||
a = a[0]; b = b[0]; | ||
return a > b ? 1 : -1; // skipping `===` check as we know all indexes are unique | ||
} ).pluck( 1 ); | ||
} | ||
/*~ src/fns.js ~*/ | ||
// private methods | ||
function _24hrTime( o, res ) { return ( o = Number( o ) ) < 12 && _lc( res.ampm ) == _lc( Type.locale.PM ) ? o += 12 : o; } | ||
function _adjust( d, v, k ) { return d.adjust( k, v ); } | ||
function _adjust_toobj( a ) { | ||
return adjust_order.reduce( function( v, k, i ) { | ||
var delta = parseFloat( a[i] ); | ||
if ( !isNaN( delta ) && delta !== 0 ) | ||
v[k] = delta; | ||
return v; | ||
}, util.obj() ); | ||
} | ||
function _dayOffset( d ) { return Math.floor( ( d - getISOFirstMondayOfYear.call( d ) ) / MS_DAY ); } | ||
function _timezoneOffset( o ) { | ||
var t = !!o.indexOf( '-' ), | ||
m = o.match( re_tz_off ), | ||
v = ( Number( m[1] ) + ( m[2] / 60 ) ) * 3600; | ||
return t ? v : -v; | ||
} | ||
function _weekOffset( d ) { return Math.floor( Math.abs( _dayOffset( d ) / 7 ) ); } | ||
function _zeroIndexedInt( o, k ) { return !isNaN( k ) ? k == o ? 0 : Number( k ) : Number( o ) - 1; } | ||
// public methods | ||
function adjust( o, v ) { | ||
var date = this, day, fn, weekday; // noinspection FallthroughInSwitchStatementJS | ||
switch ( util.ntype( o ) ) { | ||
case 'number' : o = arguments; // allow fall-through | ||
case 'array' : o = _adjust_toobj( o ); // allow fall-through | ||
case 'object' : Object.reduce( o, _adjust, date ); break; | ||
case 'string' : | ||
fn = adjust_by[o.toLowerCase()]; | ||
if ( fn && v !== 0 ) { | ||
Type.locale.setLeapYear( date ); | ||
if ( fn == adjust_by.month ) { | ||
day = date.getDate(); | ||
day < 28 || date.setDate( Math.min( day, getLastOfTheMonth.call( getFirstOfTheMonth.call( date ).adjust( Type.MONTH, v ) ).getDate() ) ); | ||
} | ||
fn != adjust_by.week || ( weekday = date.getDay() ); | ||
date[fn[1]]( date[fn[0]]() + v ); | ||
!weekday || date.setDate( date.getDate() + weekday ); | ||
} | ||
} | ||
return date; | ||
} | ||
function between( l, h ) { return this >= l && this <= h; } | ||
function clearTime() { | ||
this.setHours( 0 ); this.setMinutes( 0 ); this.setSeconds( 0 ); this.setMilliseconds( 0 ); | ||
return this; | ||
} | ||
function clone() { return new Type( this.getTime() ); } | ||
function getDayOfYear() { | ||
var L = Type.locale; | ||
L.setLeapYear( this ); | ||
return L.day_count.slice( 0, this.getMonth() ).reduce( sum, 0 ) + this.getDate() - 1; | ||
} | ||
function getFirstOfTheMonth() { return new Type( this.getFullYear(), this.getMonth(), 1 ); } | ||
function getGMTOffset( colon ) { | ||
var tz = this.getTimezoneOffset(); | ||
return [( tz > 0 ? '-' : '+' ), pad( Math.floor( Math.abs( tz ) / 60 ), 2 ), ( colon ? ':' : '' ), pad( Math.abs( tz % 60 ), 2 )].join( '' ); | ||
} | ||
function getISODay() { return this.getDay() || 7; } | ||
function getISODaysInYear() { return Math.ceil( ( getISOFirstMondayOfYear.call( new Type( this.getFullYear() + 1, 0, 1 ) ) - getISOFirstMondayOfYear.call( this ) ) / MS_DAY ); } | ||
function getISOFirstMondayOfYear() { | ||
var y = this.getFullYear(); | ||
return new Type( y, 0, DAY_OFFSETS[new Type( y, 0, 1 ).getDay()] ); | ||
} | ||
function getISOWeek() { | ||
var w, y = this.getFullYear(); | ||
if ( this >= getISOFirstMondayOfYear.call( new Type( y + 1, 0, 1 ) ) ) return 1; | ||
w = Math.floor( ( getDayOfYear.call( this ) - getISODay.call( this ) + 10 ) / 7 ); | ||
return w == 0 ? getISOWeeksInYear.call( new Type( y - 1, 0, 1 ) ) - _weekOffset( this ) : w; | ||
} | ||
function getISOWeeksInYear() { return Math.round( ( getISOFirstMondayOfYear.call( new Type( this.getFullYear() + 1, 0, 1 ) ) - getISOFirstMondayOfYear.call( this ) ) / MS_WEEK ); } | ||
function getLastOfTheMonth() { | ||
var L = Type.locale, m = this.getMonth(); L.setLeapYear( this ); | ||
return new Type( this.getFullYear(), m, L.day_count[m] ); | ||
} | ||
function getWeek() { return Math.floor( getDayOfYear.call( this ) / 7 ); } | ||
function isDST() { return new Type( this.getFullYear(), 0, 1 ).getTimezoneOffset() != this.getTimezoneOffset(); } | ||
function isLeapYear() { return Type.locale.isLeapYear( this.getFullYear() ); } | ||
function setWeek( v ) { this.setMonth( 0 ); this.setDate( 1 ); return ( this.adjust( Type.DAY, v * 7 ) ).getTime(); } | ||
function timezone() { | ||
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 ); } | ||
/*~ src/format.js ~*/ | ||
function buildTemplate( date_format ) { | ||
var LID = Type.locale.id, fn, i, l, part, parts, re_invalid; | ||
if ( cache_format[LID][date_format] ) return cache_format[LID][date_format]; | ||
fn = ['\tvar out=[];']; | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ), | ||
re_invalid = /^[^A-Za-z]*$/g; | ||
i = -1; l = parts.length; | ||
while( ++i < l ) { | ||
part = parts[i]; | ||
part == NOREPLACE ? ( fn.push( tplOut( parts[++i] ) ), ++i ) | ||
: re_invalid.test( part ) | ||
? fn.push( tplOut( part ) ) | ||
: fn.push( compileTplStr( part ) ); | ||
} | ||
fn.push( 'return out.join( "" );\n\t//@ sourceURL=d8/format/' + LID + '/' + date_format ); | ||
return cache_format[LID][date_format] = new Function( 'filter', 'date', fn.join( '\n\n\t' ) ); | ||
} | ||
function format( f ) { return buildTemplate( f )( filter, this ); } | ||
function compileTplStr( o ) { return o.replace( re_compile, function( m, p0, p1, p2 ) { return tplOut( p0 + '\', filter.' + p1 + '( date ), \'' + p2 ); } ); } | ||
function tplOut( s ) { return 'out.push( \'' + s + '\' );'; } | ||
/*~ src/lexicalize.js ~*/ | ||
function lexicalize( now, precision ) { | ||
if ( !valid( now ) ) { | ||
if ( valid( new Type( now ) ) ) | ||
now = new Type( now ); | ||
else { | ||
precision = now; | ||
now = Type.now(); | ||
} | ||
} | ||
var LEX = Type.locale.lexicon; | ||
if ( typeof lexicon[precision = String( precision ).toLowerCase()] != 'function' ) | ||
precision = LEX.DEFAULT; | ||
return !( +now - +this ) ? LEX.just_now : lexicon[precision].call( LEX, this, now ).replace( re_space, ' ' ); | ||
} | ||
function lexicalize_approx( parts, diff ) { | ||
return parts.join( ' ' ); | ||
} | ||
function lexicalize_exact( parts, diff ) { | ||
var last = parts.pop(); | ||
return ( parts.length ? parts.join( this.delim ) + ' ' + this.and + ' ' + last : last ) + ' ' + this[diff.tense < 1 ? 'ago' : 'from_now']; | ||
} | ||
lexicon.approx = function( date, now ) { | ||
var adverb, bal, determiner = this.a, | ||
diff = date.diff( now ), | ||
dkeys = Type.diffKeys( diff ), index, parts, tense, | ||
tm = Type.time_map, tu = this.time_units, use_noun; | ||
if ( diff.value < Type.MS_MINUTE ) | ||
return this.just_now; | ||
switch ( dkeys[0] ) { | ||
case 'years' : index = 0; break; | ||
case 'months' : index = 1; break; | ||
case 'weeks' : index = 2; break; | ||
case 'days' : use_noun = diff.days === 1 && ( dkeys[1] != 'hours' || diff.hours / 24 < .25 ); | ||
index = 3; break; | ||
case 'hours' : use_noun = diff.hours / 24 >= .75; | ||
determiner = this.an; | ||
index = 4; break; | ||
case 'minutes' : index = 5; break; | ||
} | ||
bal = ( diff.value - tm[index][1] * diff[dkeys[0]] ) / tm[index][1]; | ||
if ( use_noun ) | ||
return diff.tense > 0 ? this.tomorrow : index === 4 ? this.today : this.yesterday; | ||
parts = []; | ||
tense = diff.tense > 0 ? this.from_now : this.ago; | ||
if ( bal < .1 ) { //noinspection FallthroughInSwitchStatementJS | ||
switch ( dkeys[0] ) { | ||
case 'years' : case 'months' : case 'weeks' : | ||
if ( diff[dkeys[0]] === 1 ) { | ||
parts.push( ( diff.tense < 1 ? this.last : this.next ), tu[index][0] ); | ||
break; | ||
} // allow [conditional] fall-through | ||
default : | ||
!bal || parts.push( this.about ); | ||
parts.push( diff[dkeys[0]], tu[index][diff[dkeys[0]] > 1 ? 1 : 0], tense ); | ||
} | ||
} | ||
else { | ||
if ( bal < .74 ) { | ||
if ( bal < .24 ) | ||
adverb = this.just_over; | ||
else { | ||
adverb = ( bal > .24 && bal < .4 ) ? this.almost : this.about; | ||
parts.push( this.and, this.a, this.half ); | ||
} | ||
} | ||
else | ||
parts.push( this.almost, ( diff[dkeys[0]] + 1 ), tu[index][1], tense ); | ||
} | ||
if ( adverb ) { | ||
parts.push( tu[index][diff[dkeys[0]] > 1 || parts.length ? 1 : 0], tense ); | ||
parts.unshift( adverb, diff[dkeys[0]] ); | ||
} | ||
return typeof this.approx == 'function' ? this.approx.call( this, parts, diff ) : lexicalize_approx.call( this, parts, diff ); | ||
}; | ||
lexicon.exact = function( date, now ) { | ||
var diff = date.diff( now ), parts, tu = this.time_units; | ||
parts = Type.time_map.reduce( function( val, unit, i ) { | ||
var v = diff[unit[0]]; | ||
!v || !tu[i] || val.push( v + ' ' + tu[i][v > 1 ? 1 : 0] ); | ||
return val; | ||
}, [] ); | ||
if ( !parts.length ) | ||
return this.just_now; | ||
return typeof this.exact == 'function' ? this.exact.call( this, parts, diff ) : lexicalize_exact.call( this, parts, diff ); | ||
}; | ||
/*~ src/localize.js ~*/ | ||
function localize( locale ) { //noinspection FallthroughInSwitchStatementJS | ||
switch ( util.ntype( locale ) ) { | ||
case 'object' : | ||
if ( locale.id ) { | ||
locales[locale.id] = locale; | ||
break; | ||
} // allow [conditional] fall-through | ||
case 'string' : | ||
if ( locale in locales ) { | ||
locale = locales[locale]; | ||
break; | ||
} // allow [conditional] fall-through | ||
default : locale = null; | ||
} | ||
if ( util.ntype( locale ) == 'object' ) { | ||
util.defs( Type, { | ||
locale : { value : locale }, | ||
getOrdinal : locale.getOrdinal, | ||
isLeapYear : locale.isLeapYear, | ||
setLeapYear : locale.setLeapYear | ||
}, 'w', true ); | ||
if ( !( locale.id in cache_format ) ) | ||
cache_format[locale.id] = util.obj(); | ||
if ( !( locale.id in cache_parse ) ) | ||
cache_parse[locale.id] = util.obj(); | ||
filter = localize_filters( locale ); | ||
formats = localize_formats( locale ); | ||
parser = localize_parsers( locale ); | ||
} | ||
return Type; | ||
} | ||
/*~ src/filters.js ~*/ | ||
function localize_filters( L ) { | ||
var F = { | ||
// day | ||
d : function( d ) { return pad( d.getDate(), 2 ); }, // Day of the month, 2 digits with leading zeros | ||
D : function( d ) { return LOCALE.days[d.getDay()].substring( 0, 3 ); }, // A textual representation of a day, three letters | ||
j : function( d ) { return d.getDate(); }, // Day of the month without leading zeros | ||
l : function( d ) { return LOCALE.days[d.getDay()]; }, // A full textual representation of the day of the week | ||
N : function( d ) { return getISODay.call( d ); }, // ISO-8601 numeric representation of the day of the week | ||
S : function( d ) { return LOCALE.getOrdinal( d.getDate() ); }, // English ordinal suffix for the day of the month, 2 characters | ||
w : function( d ) { return d.getDay(); }, // Numeric representation of the day of the week | ||
z : function( d ) { return d.getDayOfYear(); }, // The day of the year (starting from 0) | ||
d : function( d ) { return pad( d.getDate(), 2 ); }, // Day of the month, 2 digits with leading zeros | ||
D : function( d ) { return L.days[d.getDay()].substring( 0, 3 ); }, // A textual representation of a day, three letters | ||
j : function( d ) { return d.getDate(); }, // Day of the month without leading zeros | ||
l : function( d ) { return L.days[d.getDay()]; }, // A full textual representation of the day of the week | ||
N : function( d ) { return getISODay.call( d ); }, // ISO-8601 numeric representation of the day of the week | ||
S : function( d ) { return L.getOrdinal( d.getDate() ); }, // English ordinal suffix for the day of the month, 2 characters | ||
w : function( d ) { return d.getDay(); }, // Numeric representation of the day of the week | ||
z : function( d ) { return d.getDayOfYear(); }, // The day of the year (starting from 0) | ||
// week | ||
W : function( d ) { return getISOWeek.call( d ); }, // ISO-8601 week number of year, weeks starting on Monday | ||
W : function( d ) { return getISOWeek.call( d ); }, // ISO-8601 week number of year, weeks starting on Monday | ||
// month | ||
F : function( d ) { return LOCALE.months[d.getMonth()]; }, // A full textual representation of a month | ||
m : function( d ) { return pad( ( d.getMonth() + 1 ), 2 ); }, // Numeric representation of a month, with leading zeros | ||
M : function( d ) { return LOCALE.months[d.getMonth()].substring( 0, 3 ); }, // A short textual representation of a month, three letters | ||
n : function( d ) { return d.getMonth() + 1; }, // Numeric representation of a month, without leading zeros | ||
t : function( d ) { LOCALE.setLeapYear( d ); return LOCALE.day_count[d.getMonth()]; }, // Number of days in the given month | ||
F : function( d ) { return L.months[d.getMonth()]; }, // A full textual representation of a month | ||
m : function( d ) { return pad( ( d.getMonth() + 1 ), 2 ); }, // Numeric representation of a month, with leading zeros | ||
M : function( d ) { return L.months[d.getMonth()].substring( 0, 3 ); }, // A short textual representation of a month, three letters | ||
n : function( d ) { return d.getMonth() + 1; }, // Numeric representation of a month, without leading zeros | ||
t : function( d ) { // Number of days in the given month | ||
L.setLeapYear( d ); | ||
return L.day_count[d.getMonth()]; | ||
}, | ||
// year | ||
L : function( d ) { return d.isLeapYear() ? 1 : 0; }, // Whether it's a leap year | ||
o : function( d ) { // ISO-8601 year number. This has the same value as Y, except that if the ISO | ||
var m = d.getMonth(), w = getISOWeek.call( d ); // week number (W) belongs to the previous or next year, that year is used instead. | ||
L : function( d ) { return d.isLeapYear() ? 1 : 0; }, // Whether it's a leap year | ||
o : function( d ) { // ISO-8601 year number. This has the same value as Y, except that if the ISO | ||
var m = d.getMonth(), w = getISOWeek.call( d ); // week number (W) belongs to the previous or next year, that year is used instead. | ||
return ( d.getFullYear() + ( w == 1 && m > 0 ? 1 : ( w >= 52 && m < 11 ? -1 : 0 ) ) ); | ||
}, | ||
Y : function( d ) { return d.getFullYear(); }, // A full numeric representation of a year, 4 digits | ||
y : function( d ) { return String( d.getFullYear() ).substring( 2, 4 ); }, // A two digit representation of a year | ||
Y : function( d ) { return d.getFullYear(); }, // A full numeric representation of a year, 4 digits | ||
y : function( d ) { return String( d.getFullYear() ).substring( 2, 4 ); }, // A two digit representation of a year | ||
// time | ||
a : function( d ) { return _lc( d.getHours() < 12 ? LOCALE.AM : LOCALE.PM ); }, // Lowercase Ante meridiem and Post meridiem | ||
A : function( d ) { return _uc( d.getHours() < 12 ? LOCALE.AM : LOCALE.PM ); }, // Uppercase Ante meridiem and Post meridiem | ||
g : function( d ) { return d.getHours() % 12 || 12; }, // 12-hour format of an hour without leading zeros | ||
G : function( d ) { return d.getHours(); }, // 24-hour format of an hour without leading zeros | ||
h : function( d ) { return pad( filter.g( d ), 2 ); }, // 12-hour format of an hour with leading zeros | ||
H : function( d ) { return pad( filter.G( d ), 2 ); }, // 24-hour format of an hour with leading zeros | ||
i : function( d ) { return pad( d.getMinutes(), 2 ); }, // Minutes with leading zeros | ||
s : function( d ) { return pad( d.getSeconds(), 2 ); }, // Seconds, with leading zeros | ||
u : function( d ) { return pad( d.getMilliseconds(), 3 ); }, // Milliseconds | ||
a : function( d ) { return _lc( d.getHours() < 12 ? L.AM : L.PM ); }, // Lowercase Ante meridiem and Post meridiem | ||
A : function( d ) { return _uc( d.getHours() < 12 ? L.AM : L.PM ); }, // Uppercase Ante meridiem and Post meridiem | ||
g : function( d ) { return d.getHours() % 12 || 12; }, // 12-hour format of an hour without leading zeros | ||
G : function( d ) { return d.getHours(); }, // 24-hour format of an hour without leading zeros | ||
h : function( d ) { return pad( filter.g( d ), 2 ); }, // 12-hour format of an hour with leading zeros | ||
H : function( d ) { return pad( filter.G( d ), 2 ); }, // 24-hour format of an hour with leading zeros | ||
i : function( d ) { return pad( d.getMinutes(), 2 ); }, // Minutes with leading zeros | ||
s : function( d ) { return pad( d.getSeconds(), 2 ); }, // Seconds, with leading zeros | ||
u : function( d ) { return pad( d.getMilliseconds(), 3 ); }, // Milliseconds | ||
// timezone | ||
O : function( d ) { return getGMTOffset.call( d ); }, // Difference to Greenwich time (GMT) in hours | ||
P : function( d ) { return getGMTOffset.call( d, true ); }, // Difference to Greenwich time (GMT) with colon between hours and minutes | ||
T : function( d ) { return timezone.call( d ); }, // Timezone abbreviation | ||
Z : function( d ) { return d.getTimezoneOffset() * -60; }, // Timezone offset in seconds. The offset for timezones west of UTC | ||
// is always negative, and for those east of UTC is always positive. | ||
O : function( d ) { return getGMTOffset.call( d ); }, // Difference to Greenwich time (GMT) in hours | ||
P : function( d ) { return getGMTOffset.call( d, true ); }, // Difference to Greenwich time (GMT) with colon between hours and minutes | ||
T : function( d ) { return timezone.call( d ); }, // Timezone abbreviation | ||
Z : function( d ) { return d.getTimezoneOffset() * -60; }, // Timezone offset in seconds. The offset for timezones west of UTC | ||
// is always negative, and for those east of UTC is always positive. | ||
// full date/time | ||
c : function( d ) { return format.call( d, formats.ISO_8601 ); }, // ISO 8601 date | ||
r : function( d ) { return format.call( d, formats.RFC_2822 ); }, // RFC 2822 formatted date | ||
U : function( d ) { return d.getTime(); } // Seconds since the Unix Epoch January 1 1970 00:00:00 GMT | ||
}, | ||
filter_chars = Object.keys( filter ).sort().join( '' ), | ||
formats = util.copy( { | ||
c : function( d ) { return format.call( d, formats.ISO_8601 ); }, // ISO 8601 date | ||
r : function( d ) { return format.call( d, formats.RFC_2822 ); }, // RFC 2822 formatted date | ||
U : function( d ) { return d.getTime(); }, // Seconds since the Unix Epoch January 1 1970 00:00:00 GMT | ||
// custom | ||
e : function( d ) { return d.lexicalize( 'exact' ); }, // these are either self explanatory or you need serious help! | ||
x : function( d ) { return d.lexicalize( 'approx' ); } // t(om )hanks you. | ||
}; | ||
filter_chars = Object.keys( F ).sort().join( '' ); | ||
re_compile = new RegExp( '([^' + filter_chars + ']*)([' + filter_chars + '])([^' + filter_chars + ']*)', 'g' ); | ||
util.def( Type, 'filters', { value : filter = F }, 'w', true ); | ||
return F; | ||
} | ||
/*~ src/formats.js ~*/ | ||
function localize_formats( L ) { | ||
var F = util.copy( { | ||
ISO_8601 : 'Y-m-d<T>H:i:s.u<Z>', ISO_8601_SHORT : 'Y-m-d', | ||
RFC_850 : 'l, d-M-y H:i:s T', RFC_2822 : 'D, d M Y H:i:s O', | ||
sortable : 'Y-m-d H:i:sO' | ||
}, LOCALE.formats ), | ||
m, | ||
re_ampm = '(am|pm)', re_add_enr = />/g, re_add_nr = /</g, | ||
re_compile = new RegExp( '([^' + filter_chars + ']*)([' + filter_chars + '])([^' + filter_chars + ']*)', 'g' ), | ||
re_d1_2 = '([0-9]{1,2})', re_d2 = '([0-9]{2})', re_d4 = '([0-9]{4})', | ||
re_split = /[<>]/, re_tz = /[^\(]*\(([^\)]+)\)/g, re_tz_abbr = /[^A-Z]+/g, | ||
re_tz_off = /[\+-]?([0-9]{2}):?([0-9]{2})/, | ||
parser = { | ||
// day | ||
}, L.formats ); | ||
F.atom = F.ISO_8601; F.cookie = F.RFC_850; F.rss = F.RFC_2822; | ||
util.def( Type, 'formats', { value : formats = F }, 'w', true ); | ||
return F; | ||
} | ||
/*~ src/parsers.js ~*/ | ||
function localize_parsers( L ) { | ||
var P = { | ||
// day | ||
d : { k : DAY, fn : Number, re : re_d2 }, | ||
D : { k : DAYWEEK, fn : _indexOf.bind( null, SHORT_DAYS ), re : '(' + SHORT_DAYS.join( '|' ) + ')' }, | ||
D : { k : DAYWEEK, fn : _indexOf.bind( null, L.days_short ), re : '(' + L.days_short.join( '|' ) + ')' }, | ||
j : { k : DAY, fn : Number, re : re_d1_2 }, | ||
l : { k : DAYWEEK, fn : _indexOf.bind( null, LOCALE.days ), re : '(' + LOCALE.days.join( '|' ) + ')' }, | ||
l : { k : DAYWEEK, fn : _indexOf.bind( null, L.days ), re : '(' + L.days.join( '|' ) + ')' }, | ||
N : { k : DAYWEEK, fn : _zeroIndexedInt.bind( null, 7 ), re : '([1-7])' }, | ||
S : { re : '(?:' + LOCALE.ordinal.join( '|' ) + ')' }, | ||
w : { k : DAYWEEK, fn : Number, re : '([0-6])' }, | ||
z : { k : DAYYEAR, fn : Number, re : '([0-9]{1,3})' }, | ||
// week | ||
W : { k : WEEK, fn : Number, re : re_d2 }, | ||
// month | ||
F : { k : MONTH, fn : _indexOf.bind( null, LOCALE.months ), re : '(' + LOCALE.months.join( '|' ) + ')' }, | ||
m : { k : MONTH, fn : _zeroIndexedInt, re : re_d2 }, | ||
M : { k : MONTH, fn : _indexOf.bind( null, SHORT_MONTHS ), re : '(' + SHORT_MONTHS.join( '|' ) + ')' }, | ||
n : { k : MONTH, fn : _zeroIndexedInt, re : re_d1_2 }, | ||
S : { re : '(?:' + L.ordinal.join( '|' ) + ')' }, | ||
w : { k : DAYWEEK, fn : Number, re : '([0-6])' }, | ||
z : { k : DAYYEAR, fn : Number, re : '([0-9]{1,3})' }, | ||
// week | ||
W : { k : WEEK, fn : Number, re : re_d2 }, | ||
// month | ||
F : { k : MONTH, fn : _indexOf.bind( null, L.months ), re : '(' + L.months.join( '|' ) + ')' }, | ||
m : { k : MONTH, fn : _zeroIndexedInt, re : re_d2 }, | ||
M : { k : MONTH, fn : _indexOf.bind( null, L.months_short ), re : '(' + L.months_short.join( '|' ) + ')' }, | ||
n : { k : MONTH, fn : _zeroIndexedInt, re : re_d1_2 }, | ||
t : { re : '[0-9]{2}' }, | ||
// year | ||
// year | ||
L : { re : '(?:0|1)' }, | ||
o : { k : YEAR, fn : Number, re : re_d4 }, | ||
Y : { k : YEAR, fn : Number, re : re_d4 }, | ||
y : { k : YEAR, fn : function( o ) { o = Number( o ); return o += ( o < 30 ? 2000 : 1900 ); }, re : re_d2 }, | ||
// time | ||
a : { k : AMPM, fn : util, re : re_ampm }, | ||
A : { k : AMPM, fn : _lc, re : _uc( re_ampm ) }, | ||
g : { k : HOUR, fn : _24hrTime, re : re_d1_2 }, | ||
G : { k : HOUR, fn : Number, re : re_d1_2 }, | ||
h : { k : HOUR, fn : _24hrTime, re : re_d2 }, | ||
H : { k : HOUR, fn : Number, re : re_d2 }, | ||
i : { k : MINUTE, fn : Number, re : re_d2 }, | ||
s : { k : SECOND, fn : Number, re : re_d2 }, | ||
u : { k : MILLISECOND, fn : Number, re : '([0-9]{1,})' }, | ||
// timezone | ||
O : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{4})' }, | ||
P : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{2}:[0-9]{2})' }, | ||
o : { k : YEAR, fn : Number, re : re_d4 }, | ||
Y : { k : YEAR, fn : Number, re : re_d4 }, | ||
y : { k : YEAR, fn : function( o ) { | ||
o = Number( o ); | ||
return o += ( o < 30 ? 2000 : 1900 ); | ||
}, re : re_d2 }, | ||
// time | ||
a : { k : AMPM, fn : util, re : re_ampm }, | ||
A : { k : AMPM, fn : _lc, re : _uc( re_ampm ) }, | ||
g : { k : HOUR, fn : _24hrTime, re : re_d1_2 }, | ||
G : { k : HOUR, fn : Number, re : re_d1_2 }, | ||
h : { k : HOUR, fn : _24hrTime, re : re_d2 }, | ||
H : { k : HOUR, fn : Number, re : re_d2 }, | ||
i : { k : MINUTE, fn : Number, re : re_d2 }, | ||
s : { k : SECOND, fn : Number, re : re_d2 }, | ||
u : { k : MILLISECOND, fn : Number, re : '([0-9]{1,})' }, | ||
// timezone | ||
O : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{4})' }, | ||
P : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{2}:[0-9]{2})' }, | ||
T : { re : '[A-Z]{1,4}' }, | ||
Z : { k : TIMEZONE, fn : Number, re : '([\\+-]?[0-9]{5})' }, | ||
// full date/time | ||
U : { k : UNIX, fn : Number, re : '(-?[0-9]{1,})' } | ||
Z : { k : TIMEZONE, fn : Number, re : '([\\+-]?[0-9]{5})' }, | ||
// full date/time | ||
U : { k : UNIX, fn : Number, re : '(-?[0-9]{1,})' } | ||
}; | ||
formats.atom = formats.ISO_8601; formats.cookie = formats.RFC_850; formats.rss = formats.RFC_2822; | ||
P.c = { | ||
combo : [P.Y, P.m, P.d, P.H, P.i, P.s, P.u, P.P], | ||
re : [P.Y.re, '-', P.m.re, '-', P.d.re, 'T', P.H.re, ':', P.i.re, ':', P.s.re, '(?:\\.', P.u.re, '){0,1}', P.P.re, '{0,1}'].join( '' ) | ||
}; | ||
P.r = { | ||
combo : [P.D, P.d, P.M, P.Y, P.H, P.i, P.s, P.O], | ||
re : [P.D.re, ', ', P.d.re, ' ', P.M.re, ' ', P.Y.re, ' ', P.H.re, ':', P.i.re, ':', P.s.re, ' ', P.O.re].join( '' ) | ||
}; | ||
parser.c = { | ||
combo : [parser.Y, parser.m, parser.d, parser.H, parser.i, parser.s, parser.u, parser.P], | ||
re : [parser.Y.re, '-', parser.m.re, '-', parser.d.re, 'T', parser.H.re, ':', parser.i.re, ':', parser.s.re, '(?:\\.', parser.u.re, '){0,1}', parser.P.re, '{0,1}'].join( '' ) | ||
}; | ||
parser.r = { | ||
combo : [parser.D, parser.d, parser.M, parser.Y, parser.H, parser.i, parser.s, parser.O], | ||
re : [parser.D.re, ', ', parser.d.re, ' ', parser.M.re, ' ', parser.Y.re, ' ', parser.H.re, ':', parser.i.re, ':', parser.s.re, ' ', parser.O.re].join( '' ) | ||
}; | ||
util.def( Type, 'parsers', { value : parser = P }, 'w', true ); | ||
return P; | ||
} | ||
/*~ src/expose.js ~*/ | ||
// instance methods | ||
util.defs( Type.prototype, { | ||
adjust : adjust, between : between, clearTime : clearTime, | ||
clone : clone, diff : diff, format : format, | ||
getDayOfYear : getDayOfYear, getFirstOfTheMonth : getFirstOfTheMonth, getGMTOffset : getGMTOffset, | ||
getISODay : getISODay, getISODaysInYear : getISODaysInYear, getISOFirstMondayOfYear : getISOFirstMondayOfYear, | ||
getISOWeek : getISOWeek, getISOWeeksInYear : getISOWeeksInYear, getLastOfTheMonth : getLastOfTheMonth, | ||
getWeek : getWeek, isDST : isDST, isLeapYear : isLeapYear, | ||
setWeek : setWeek, timezone : timezone | ||
adjust : adjust, between : between, clearTime : clearTime, | ||
clone : clone, diff : diff, format : format, | ||
getDayOfYear : getDayOfYear, getFirstOfTheMonth : getFirstOfTheMonth, getGMTOffset : getGMTOffset, | ||
getISODay : getISODay, getISODaysInYear : getISODaysInYear, getISOFirstMondayOfYear : getISOFirstMondayOfYear, | ||
getISOWeek : getISOWeek, getISOWeeksInYear : getISOWeeksInYear, getLastOfTheMonth : getLastOfTheMonth, | ||
getWeek : getWeek, isDST : isDST, isLeapYear : isLeapYear, | ||
lexicalize : lexicalize, setWeek : setWeek, timezone : timezone, | ||
valid : function() { return Type.valid( this ); } | ||
}, 'r' ); | ||
@@ -543,9 +748,13 @@ | ||
// 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 }, | ||
lexicon : { value : lexicon }, time_map : { value : time_map }, time_props : { value : time_props }, | ||
// static methods | ||
getOrdinal : LOCALE.getOrdinal, isLeapYear : LOCALE.isLeapYear, setLeapYear : LOCALE.setLeapYear, toDate : toDate | ||
coerce : coerce, diffKeys : diff_keys, localize : localize, | ||
toDate : coerce, valid : valid | ||
}, 'r' ); | ||
@@ -552,0 +761,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e){"use strict";e.x.cache("Date",function(t){function n(e,t){var n=e.indexOf(t);return n==-1?null:n}function r(e){return e.toLowerCase()}function i(e){return e.substring(0,3)}function s(e){return e.toUpperCase()}function o(e,t){return e.reduce(function(e,n,r){return e[t[r]]=n,e},{})}function u(e,t,n){return t<=e&&e<=n}function a(e,t,n){var r=-1,i=e.toString(n||10);t-=i.length;while(++r<t)i="0"+i;return i}function f(e,t){return e.reduce(function(e,n){return!(t in n)||e.push(n[t]),e},[])}function l(e,t){return e+t}function c(e,t){return(e=Number(e))<12&&r(t.ampm)==r(Y.PM)?e+=12:e}function h(e,t,n){return e.adjust(n,t)}function p(t){return kt.reduce(function(e,n,r){var i=parseFloat(t[r]);return!isNaN(i)&&i!==0&&(e[n]=i),e},e.obj())}function d(e){return Math.floor((e-k.call(e))/tt)}function v(e){var t=!!e.indexOf("-"),n=e.match($t),r=(Number(n[1])+n[2]/60)*3600;return t?r:-r}function m(e){return Math.floor(Math.abs(d(e)/7))}function g(e,t){return isNaN(t)?Number(e)-1:t==e?0:Number(t)}function y(n,r){var i=this,s,o,u;switch(e.ntype(n)){case"number":n=p(arguments);case"object":Object.reduce(n,h,i);break;case"string":o=Ct[n.toLowerCase()],o&&r!==0&&(Y.setLeapYear(i),o==Ct.month&&(s=i.getDate(),s<28||i.setDate(Math.min(s,O.call(x.call(i).adjust(t.MONTH,r)).getDate()))),o!=Ct.week||(u=i.getDay()),i[o[1]](i[o[0]]()+r),!u||i.setDate(i.getDate()+u))}return i}function b(e,t){return this>=e&&this<=t}function w(){return this.setHours(0),this.setMinutes(0),this.setSeconds(0),this.setMilliseconds(0),this}function E(){return new t(this.getTime())}function S(){return Y.setLeapYear(this),Y.day_count.slice(0,this.getMonth()).reduce(l,0)+this.getDate()-1}function x(){return new t(this.getFullYear(),this.getMonth(),1)}function T(e){var t=this.getTimezoneOffset();return[t>0?"-":"+",a(Math.floor(Math.abs(t)/60),2),e?":":"",a(Math.abs(t%60),2)].join("")}function N(){return this.getDay()||7}function C(){return Math.ceil((k.call(new t(this.getFullYear()+1,0,1))-k.call(this))/tt)}function k(){var e=this.getFullYear();return new t(e,0,et[(new t(e,0,1)).getDay()])}function L(){var e,n=this.getFullYear();return this>=k.call(new t(n+1,0,1))?1:(e=Math.floor((S.call(this)-N.call(this)+10)/7),e==0?A.call(new t(n-1,0,1))-m(this):e)}function A(){return Math.round((k.call(new t(this.getFullYear()+1,0,1))-k.call(this))/ot)}function O(){var e=this.getMonth();return Y.setLeapYear(this),new t(this.getFullYear(),e,Y.day_count[e])}function M(){return Math.floor(S.call(this)/7)}function _(){return(new t(this.getFullYear(),0,1)).getTimezoneOffset()!=this.getTimezoneOffset()}function D(){return Y.isLeapYear(this.getFullYear())}function P(e){return this.setMonth(0),this.setDate(1),this.adjust(t.DAY,e*7).getTime()}function H(){var e=this.toString().split(" ");return e.splice(4,e.length).join(" ").replace(Xt,"$1").replace(Vt,"")}function B(e){if(Lt[e])return Lt[e];var t=["var out=[];"],n=-1,r,i=e.replace(It,Tt).replace(Ft,Nt).split(Wt),s=/^[^A-Za-z]*$/g,o=i.length;while(++n<o)r=i[n],r==xt?(t.push(I(i[++n])),++n):s.test(r)?t.push(I(r)):t.push(F(r));return t.push('return out.join( "" );\n//@ sourceURL=d8/format/'+e),Lt[e]=new Function("filter","date",t.join("\n"))}function j(e){return B(e)(Dt,this)}function F(e){return e.replace(qt,function(e,t,n,r){return I(t+"', filter."+n+"( date ), '"+r)})}function I(e){return"out.push( '"+e+"' );"}function q(t){if(At[t])return At[t];var n={},r=[],i=-1,s,u=t.replace(It,Tt).replace(Ft,Nt).split(Wt),a=u.length,l=[];while(++i<a){s=u[i];if(s==xt){l.push(u[++i]),++i;continue}s.replace(qt,function(t,i,s,u){var a,c,h;if(!(h=Jt[s]))return;h.k&&(r.push(h.k),h.fn&&(n[h.k]=h.fn)),h.combo&&(c=f(h.combo,"k"),a=o(f(h.combo,"fn"),c),r.push.apply(r,c),e.copy(n,a,!0)),h.re&&l.push(i,h.re,u)})}return At[t]=R.bind(null,new RegExp(l.join("")),r,n)}function R(e,n,r,i){var s=new t,u=i.match(e),a=o(u.slice(1),n);return Object.reduce(a,function(e,t,n){return typeof t=="string"&&r[n]&&(a[n]=r[n](t,a)),e},null),isNaN(a[wt])?(z(s,a[dt],a[mt],a[yt],a[vt]),U(s,a),W(s,a[bt])):s.setTime(a[wt]),s}function U(t,n){var r,i=-1,s,o,a;if(Ot.every(e.has.bind(null,n)))return;isNaN(n[St])&&(n[St]=t.getFullYear());if(isNaN(n[gt])){o=Y.isLeapYear(n[St])?1:0,a=Y.ordinal_day_count[o],s=a.length,n[gt]=0,n[Et]&&!n[pt]&&(r=n[ht],r=isNaN(r)?0:r?r:7,n[pt]=n[Et]*7-(4-r));if(!isNaN(n[pt])){n[pt]>a[a.length-1]&&(n[pt]-=a[a.length-1],++n[St]);while(++i<s)if(u(n[pt],a[i],a[i+1])){n[gt]=i,n[ct]=a[i]==0?n[pt]:n[pt]-a[i];break}}}isNaN(n[ct])&&(n[ct]=1),t.setYear(n[St]),t.setMonth(n[gt]),t.setDate(n[ct])}function z(e,t,n,r,i){e.setHours(t||0),e.setMinutes(n||0),e.setSeconds(r||0),e.setMilliseconds(i||0)}function W(e,n){!u(n,-43200,50400)||e.adjust(t.SECOND,-e.getTimezoneOffset()*60-n)}function X(e,t){return q(t)(e)}function V(t,n){switch(e.ntype(t)){case"string":if(!isNaN(+(new Date(t)))){t=new Date(t);break}if(n){t=Date.now();break};case"array":case"object":n=t,t=Date.now();break;case"date":if(!isNaN(+(new Date(t))))break;default:t=Date.now()}var r,i=t- +this,s=i<0?1:i>0?-1:0;return r=s===0?e.obj():J(Math.abs(i),K(n)),r.tense=s,r}function $(e,t,n,r){var i;return e.__ms__?e.excl[t[0]]?(!r[n+1]||e.excl[r[n+1][0]])&&(t=r[n-1])?(i=e.__ms__/t[1],e.__ms__=Math.round(i)*t[1]+e.__ms__/r[n][1]%1*r[n][1],$(e,t,n-1,[])):e:(e.__ms__>=t[1]&&(i=e.__ms__/t[1],t[0]in e.val?(i=Math.floor(i),e.__ms__-=i*t[1],e.val[t[0]]+=i):(e.__ms__=i%1*t[1],e.val[t[0]]=Math.floor(i))),e):e}function J(t,n){var r=Mt.reduce($,{__ms__:t,excl:n,val:e.obj()}).val;return r.value=t,r}function K(t){var n=e.obj(),r=!0;if(t)switch(e.ntype(t)){case"object":r=!1;break;case"string":t=t.split(" ");case"array":t=t.reduce(Q,n),r=!!e.len(n)}return _t.map(function(e){e in this||(this[e]=!r)},n),n}function Q(e,t){var n=(t=String(t).toLowerCase()).substring(1);switch(t.charAt(0)){case"-":e[n]=!0;break;case"+":e[n]=!1;break;case">":Mt.map(G,{excl:e,prop:n,val:!0});break;case"<":Mt.slice().reverse().map(G,{excl:e,prop:n,val:!1});break;default:e[t]=!1}return e}function G(e){e[0]===this.prop&&(this.SET_VALID=!0),this.SET_VALID&&(this.excl[e[0]]=this.val)}var Y=t.locale,Z,et=[9,1,0,-1,-2,4,3],tt=864e5,nt=36e5,rt=6e4,it=2592e6,st=1e3,ot=6048e5,ut=31536e6,at=Y.days.map(i),ft=Y.months.map(i),lt="ampm",ct="day",ht="dayweek",pt="dayyear",dt="hour",vt="ms",mt="minute",gt="month",yt="second",bt="timezone",wt="unix",Et="week",St="year",xt="NOREPLACE",Tt="<"+xt+"<",Nt=">END"+xt+">",Ct={day:["getDate","setDate"],hr:["getHours","setHours"],min:["getMinutes","setMinutes"],month:["getMonth","setMonth"],ms:["getMilliseconds","setMilliseconds"],sec:["getSeconds","setSeconds"],week:["getWeek","setWeek"],year:["getFullYear","setFullYear"]},kt=[St,gt,Et,ct,"hr",mt.substring(0,3),yt.substring(0,3),vt],Lt={},At={},Ot=[ct,ht,pt,gt,Et,St],Mt=[[St+"s",ut],[gt+"s",it],[Et+"s",ot],[ct+"s",tt],[dt+"s",nt],[mt+"s",rt],[yt+"s",st],[vt,1]],_t=Mt.map(function(e){return e[0]}),Dt={d:function(e){return a(e.getDate(),2)},D:function(e){return Y.days[e.getDay()].substring(0,3)},j:function(e){return e.getDate()},l:function(e){return Y.days[e.getDay()]},N:function(e){return N.call(e)},S:function(e){return Y.getOrdinal(e.getDate())},w:function(e){return e.getDay()},z:function(e){return e.getDayOfYear()},W:function(e){return L.call(e)},F:function(e){return Y.months[e.getMonth()]},m:function(e){return a(e.getMonth()+1,2)},M:function(e){return Y.months[e.getMonth()].substring(0,3)},n:function(e){return e.getMonth()+1},t:function(e){return Y.setLeapYear(e),Y.day_count[e.getMonth()]},L:function(e){return e.isLeapYear()?1:0},o:function(e){var t=e.getMonth(),n=L.call(e);return e.getFullYear()+(n==1&&t>0?1:n>=52&&t<11?-1:0)},Y:function(e){return e.getFullYear()},y:function(e){return String(e.getFullYear()).substring(2,4)},a:function(e){return r(e.getHours()<12?Y.AM:Y.PM)},A:function(e){return s(e.getHours()<12?Y.AM:Y.PM)},g:function(e){return e.getHours()%12||12},G:function(e){return e.getHours()},h:function(e){return a(Dt.g(e),2)},H:function(e){return a(Dt.G(e),2)},i:function(e){return a(e.getMinutes(),2)},s:function(e){return a(e.getSeconds(),2)},u:function(e){return a(e.getMilliseconds(),3)},O:function(e){return T.call(e)},P:function(e){return T.call(e,!0)},T:function(e){return H.call(e)},Z:function(e){return e.getTimezoneOffset()*-60},c:function(e){return j.call(e,Ht.ISO_8601)},r:function(e){return j.call(e,Ht.RFC_2822)},U:function(e){return e.getTime()}},Pt=Object.keys(Dt).sort().join(""),Ht=e.copy({ISO_8601:"Y-m-d<T>H:i:s.u<Z>",ISO_8601_SHORT:"Y-m-d",RFC_850:"l, d-M-y H:i:s T",RFC_2822:"D, d M Y H:i:s O",sortable:"Y-m-d H:i:sO"},Y.formats),Bt,jt="(am|pm)",Ft=/>/g,It=/</g,qt=new RegExp("([^"+Pt+"]*)(["+Pt+"])([^"+Pt+"]*)","g"),Rt="([0-9]{1,2})",Ut="([0-9]{2})",zt="([0-9]{4})",Wt=/[<>]/,Xt=/[^\(]*\(([^\)]+)\)/g,Vt=/[^A-Z]+/g,$t=/[\+-]?([0-9]{2}):?([0-9]{2})/,Jt={d:{k:ct,fn:Number,re:Ut},D:{k:ht,fn:n.bind(null,at),re:"("+at.join("|")+")"},j:{k:ct,fn:Number,re:Rt},l:{k:ht,fn:n.bind(null,Y.days),re:"("+Y.days.join("|")+")"},N:{k:ht,fn:g.bind(null,7),re:"([1-7])"},S:{re:"(?:"+Y.ordinal.join("|")+")"},w:{k:ht,fn:Number,re:"([0-6])"},z:{k:pt,fn:Number,re:"([0-9]{1,3})"},W:{k:Et,fn:Number,re:Ut},F:{k:gt,fn:n.bind(null,Y.months),re:"("+Y.months.join("|")+")"},m:{k:gt,fn:g,re:Ut},M:{k:gt,fn:n.bind(null,ft),re:"("+ft.join("|")+")"},n:{k:gt,fn:g,re:Rt},t:{re:"[0-9]{2}"},L:{re:"(?:0|1)"},o:{k:St,fn:Number,re:zt},Y:{k:St,fn:Number,re:zt},y:{k:St,fn:function(e){return e=Number(e),e+=e<30?2e3:1900},re:Ut},a:{k:lt,fn:e,re:jt},A:{k:lt,fn:r,re:s(jt)},g:{k:dt,fn:c,re:Rt},G:{k:dt,fn:Number,re:Rt},h:{k:dt,fn:c,re:Ut},H:{k:dt,fn:Number,re:Ut},i:{k:mt,fn:Number,re:Ut},s:{k:yt,fn:Number,re:Ut},u:{k:vt,fn:Number,re:"([0-9]{1,})"},O:{k:bt,fn:v,re:"([\\+-][0-9]{4})"},P:{k:bt,fn:v,re:"([\\+-][0-9]{2}:[0-9]{2})"},T:{re:"[A-Z]{1,4}"},Z:{k:bt,fn:Number,re:"([\\+-]?[0-9]{5})"},U:{k:wt,fn:Number,re:"(-?[0-9]{1,})"}};Ht.atom=Ht.ISO_8601,Ht.cookie=Ht.RFC_850,Ht.rss=Ht.RFC_2822,Jt.c={combo:[Jt.Y,Jt.m,Jt.d,Jt.H,Jt.i,Jt.s,Jt.u,Jt.P],re:[Jt.Y.re,"-",Jt.m.re,"-",Jt.d.re,"T",Jt.H.re,":",Jt.i.re,":",Jt.s.re,"(?:\\.",Jt.u.re,"){0,1}",Jt.P.re,"{0,1}"].join("")},Jt.r={combo:[Jt.D,Jt.d,Jt.M,Jt.Y,Jt.H,Jt.i,Jt.s,Jt.O],re:[Jt.D.re,", ",Jt.d.re," ",Jt.M.re," ",Jt.Y.re," ",Jt.H.re,":",Jt.i.re,":",Jt.s.re," ",Jt.O.re].join("")},e.defs(t.prototype,{adjust:y,between:b,clearTime:w,clone:E,diff:V,format:j,getDayOfYear:S,getFirstOfTheMonth:x,getGMTOffset:T,getISODay:N,getISODaysInYear:C,getISOFirstMondayOfYear:k,getISOWeek:L,getISOWeeksInYear:A,getLastOfTheMonth:O,getWeek:M,isDST:_,isLeapYear:D,setWeek:P,timezone:H},"r"),e.defs(t,{DAY:ct,HOUR:"hr",MINUTE:mt.substring(0,3),MILLISECOND:vt,MONTH:gt,SECOND:yt.substring(0,3),WEEK:Et,YEAR:St,MS_DAY:tt,MS_HOUR:nt,MS_MINUTE:rt,MS_MONTH:it,MS_SECOND:st,MS_WEEK:ot,MS_YEAR:ut,filters:{value:Dt},formats:{value:Ht},parsers:{value:Jt},getOrdinal:Y.getOrdinal,isLeapYear:Y.isLeapYear,setLeapYear:Y.setLeapYear,toDate:X},"r")}).x(Date)}(typeof m8!="undefined"?m8:typeof require!="undefined"?require("m8"):null) | ||
!function(e){"use strict";e.x.cache("Date",function(t){function n(e,t){var n=e.indexOf(t);return-1==n?null:n}function r(e){return e.toLocaleLowerCase()}function i(e){return e.toLocaleUpperCase()}function u(e,t){return e.reduce(function(e,n,r){return e[t[r]]=n,e},{})}function s(e,t,n){return e>=t&&n>=e}function a(e,t,n){var r=-1,i=e.toString(n||10);for(t-=i.length;t>++r;)i="0"+i;return i}function o(e,t){return e+t}function c(e,t){return l(t)(e)}function l(n){var r,i,s,a,o,c,l,h=t.locale.id;if(At[h][n])return At[h][n];for(c=n.replace(Pt,Lt).replace(Wt,Ft).split(Vt),i=[],r=-1,s=c.length,a={},l=[];s>++r;)o=c[r],o!=Tt?o.replace(ct,function(t,n,r,s){var o,c,f;(f=ot[r])&&(f.k&&(i.push(f.k),f.fn&&(a[f.k]=f.fn)),f.combo&&(c=f.combo.pluck("k"),o=u(f.combo.pluck("fn"),c),i.push.apply(i,c),e.copy(a,o,!0)),f.re&&l.push(n,f.re,s))}):(l.push(c[++r]),++r);return At[h][n]=f.bind(null,RegExp(l.join("")),i,a)}function f(e,n,r,i){var s=new t,a=i.match(e),o=u(a.slice(1),n);return Object.reduce(o,function(e,t,n){return"string"==typeof t&&r[n]&&(o[n]=r[n](t,o)),e},null),isNaN(o[Dt])?(g(s,o[vt],o[wt],o[Yt],o[Nt]),h(s,o),d(s,o[jt])):s.setTime(o[Dt]),s}function h(n,r){var i,u,a,o,c=t.locale,l=-1;if(!Ct.every(e.has.bind(null,r))){if(isNaN(r[xt])&&(r[xt]=n.getFullYear()),isNaN(r[Ot])&&(a=c.isLeapYear(r[xt])?1:0,o=c.ordinal_day_count[a],u=o.length,r[Ot]=0,r[St]&&!r[yt]&&(i=r[Mt],i=isNaN(i)?0:i?i:7,r[yt]=7*r[St]-(4-i)),!isNaN(r[yt])))for(r[yt]>o[o.length-1]&&(r[yt]-=o[o.length-1],++r[xt]);u>++l;)if(s(r[yt],o[l],o[l+1])){r[Ot]=l,r[kt]=0==o[l]?r[yt]:r[yt]-o[l];break}isNaN(r[kt])&&(r[kt]=1),n.setYear(r[xt]),n.setMonth(r[Ot]),n.setDate(r[kt])}}function g(e,t,n,r,i){e.setHours(t||0),e.setMinutes(n||0),e.setSeconds(r||0),e.setMilliseconds(i||0)}function d(e,n){!s(n,-43200,50400)||e.adjust(t.SECOND,60*-e.getTimezoneOffset()-n)}function m(n,r){switch(e.ntype(n)){case"number":case"string":if(!q(new t(n))){r||(r=n),n=t.now();break}n=new t(n);case"array":case"object":r=n,n=t.now();break;case"date":if(q(new t(+n)))break;default:n=t.now()}var i,u=+n-+this,s=0>u?1:u>0?-1:0;return s?i=b(Math.abs(u),_(r)):(i=e.obj(),i.value=0),i.tense=s,i}function p(e,t,n,r){var i;return e.__ms__?e.excl[t[0]]?r[n+1]&&!e.excl[r[n+1][0]]||!(t=r[n-1])?e:(i=e.__ms__/t[1],e.__ms__=Math.round(i)*t[1]+e.__ms__/r[n][1]%1*r[n][1],p(e,t,n-1,[])):(e.__ms__>=t[1]&&(i=e.__ms__/t[1],t[0]in e.val?(i=Math.floor(i),e.__ms__-=i*t[1],e.val[t[0]]+=i):(e.__ms__=i%1*t[1],e.val[t[0]]=Math.floor(i))),e):e}function b(t,n){var r=Qt.reduce(p,{__ms__:t,excl:n,val:e.obj()}).val;return r.value=t,r}function _(t){var n=e.obj(),r=!0;if(t)switch(e.ntype(t)){case"object":r=!1;break;case"string":t=t.split(" ");case"array":t=t.reduce(k,n),r=!!e.len(n)}return Xt.map(function(e){e in this||(this[e]=!r)},n),n}function k(e,t){var n=(t=(t+"").toLowerCase()).substring(1);switch(t.charAt(0)){case"-":e[n]=!0;break;case"+":e[n]=!1;break;case">":Qt.map(M,{excl:e,prop:n,val:!0});break;case"<":Qt.slice().reverse().map(M,{excl:e,prop:n,val:!1});break;default:e[t]=!1}return e}function M(e){e[0]===this.prop&&(this.SET_VALID=!0),this.SET_VALID&&(this.excl[e[0]]=this.val)}function y(t){return t=e.copy(t),e.remove(t,"tense","value"),Object.keys(t).map(function(e){return[Xt.indexOf(e),e]}).sort(function(e,t){return e=e[0],t=t[0],e>t?1:-1}).pluck(1)}function v(e,n){return 12>(e=Number(e))&&r(n.ampm)==r(t.locale.PM)?e+=12:e}function N(e,t,n){return e.adjust(n,t)}function w(t){return Et.reduce(function(e,n,r){var i=parseFloat(t[r]);return isNaN(i)||0===i||(e[n]=i),e},e.obj())}function O(e){return Math.floor((e-C.call(e))/ft)}function Y(e){var t=!!e.indexOf("-"),n=e.match(Jt),r=3600*(Number(n[1])+n[2]/60);return t?r:-r}function j(e){return Math.floor(Math.abs(O(e)/7))}function D(e,t){return isNaN(t)?Number(e)-1:t==e?0:Number(t)}function S(n,r){var i,u,s,a=this;switch(e.ntype(n)){case"number":n=arguments;case"array":n=w(n);case"object":Object.reduce(n,N,a);break;case"string":u=Ht[n.toLowerCase()],u&&0!==r&&(t.locale.setLeapYear(a),u==Ht.month&&(i=a.getDate(),28>i||a.setDate(Math.min(i,U.call(H.call(a).adjust(t.MONTH,r)).getDate()))),u!=Ht.week||(s=a.getDay()),a[u[1]](a[u[0]]()+r),!s||a.setDate(a.getDate()+s))}return a}function x(e,t){return this>=e&&t>=this}function T(){return this.setHours(0),this.setMinutes(0),this.setSeconds(0),this.setMilliseconds(0),this}function L(){return new t(this.getTime())}function F(){var e=t.locale;return e.setLeapYear(this),e.day_count.slice(0,this.getMonth()).reduce(o,0)+this.getDate()-1}function H(){return new t(this.getFullYear(),this.getMonth(),1)}function E(e){var t=this.getTimezoneOffset();return[t>0?"-":"+",a(Math.floor(Math.abs(t)/60),2),e?":":"",a(Math.abs(t%60),2)].join("")}function I(){return this.getDay()||7}function A(){return Math.ceil((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/ft)}function C(){var e=this.getFullYear();return new t(e,0,lt[new t(e,0,1).getDay()])}function z(){var e,n=this.getFullYear();return this>=C.call(new t(n+1,0,1))?1:(e=Math.floor((F.call(this)-I.call(this)+10)/7),0==e?R.call(new t(n-1,0,1))-j(this):e)}function R(){return Math.round((C.call(new t(this.getFullYear()+1,0,1))-C.call(this))/pt)}function U(){var e=t.locale,n=this.getMonth();return e.setLeapYear(this),new t(this.getFullYear(),n,e.day_count[n])}function W(){return Math.floor(F.call(this)/7)}function P(){return new t(this.getFullYear(),0,1).getTimezoneOffset()!=this.getTimezoneOffset()}function Z(){return t.locale.isLeapYear(this.getFullYear())}function G(e){return this.setMonth(0),this.setDate(1),this.adjust(t.DAY,7*e).getTime()}function K(){var e=(""+this).split(" ");return e.splice(4,e.length).join(" ").replace($t,"$1").replace(Bt,"")}function q(t){return"date"==e.ntype(t)&&!isNaN(+t)}function V(e){var n,r,i,u,s,a,o=t.locale.id;if(It[o][e])return It[o][e];for(n=[" var out=[];"],s=e.replace(Pt,Lt).replace(Wt,Ft).split(Vt),a=/^[^A-Za-z]*$/g,r=-1,i=s.length;i>++r;)u=s[r],u==Tt?(n.push(J(s[++r])),++r):a.test(u)?n.push(J(u)):n.push(B(u));return n.push('return out.join( "" );\n //@ sourceURL=d8/format/'+o+"/"+e),It[o][e]=Function("filter","date",n.join("\n\n "))}function $(e){return V(e)(ut,this)}function B(e){return e.replace(ct,function(e,t,n,r){return J(t+"', filter."+n+"( date ), '"+r)})}function J(e){return"out.push( '"+e+"' );"}function Q(e,n){q(e)||(q(new t(e))?e=new t(e):(n=e,e=t.now()));var r=t.locale.lexicon;return"function"!=typeof zt[n=(n+"").toLowerCase()]&&(n=r.DEFAULT),+e-+this?zt[n].call(r,this,e).replace(qt," "):r.just_now}function X(e){return e.join(" ")}function et(e,t){var n=e.pop();return(e.length?e.join(this.delim)+" "+this.and+" "+n:n)+" "+this[1>t.tense?"ago":"from_now"]}function tt(n){switch(e.ntype(n)){case"object":if(n.id){Rt[n.id]=n;break}case"string":if(n in Rt){n=Rt[n];break}default:n=null}return"object"==e.ntype(n)&&(e.defs(t,{locale:{value:n},getOrdinal:n.getOrdinal,isLeapYear:n.isLeapYear,setLeapYear:n.setLeapYear},"w",!0),n.id in It||(It[n.id]=e.obj()),n.id in At||(At[n.id]=e.obj()),ut=nt(n),at=rt(n),ot=it(n)),t}function nt(n){var u={d:function(e){return a(e.getDate(),2)},D:function(e){return n.days[e.getDay()].substring(0,3)},j:function(e){return e.getDate()},l:function(e){return n.days[e.getDay()]},N:function(e){return I.call(e)},S:function(e){return n.getOrdinal(e.getDate())},w:function(e){return e.getDay()},z:function(e){return e.getDayOfYear()},W:function(e){return z.call(e)},F:function(e){return n.months[e.getMonth()]},m:function(e){return a(e.getMonth()+1,2)},M:function(e){return n.months[e.getMonth()].substring(0,3)},n:function(e){return e.getMonth()+1},t:function(e){return n.setLeapYear(e),n.day_count[e.getMonth()]},L:function(e){return e.isLeapYear()?1:0},o:function(e){var t=e.getMonth(),n=z.call(e);return e.getFullYear()+(1==n&&t>0?1:n>=52&&11>t?-1:0)},Y:function(e){return e.getFullYear()},y:function(e){return(e.getFullYear()+"").substring(2,4)},a:function(e){return r(12>e.getHours()?n.AM:n.PM)},A:function(e){return i(12>e.getHours()?n.AM:n.PM)},g:function(e){return e.getHours()%12||12},G:function(e){return e.getHours()},h:function(e){return a(ut.g(e),2)},H:function(e){return a(ut.G(e),2)},i:function(e){return a(e.getMinutes(),2)},s:function(e){return a(e.getSeconds(),2)},u:function(e){return a(e.getMilliseconds(),3)},O:function(e){return E.call(e)},P:function(e){return E.call(e,!0)},T:function(e){return K.call(e)},Z:function(e){return-60*e.getTimezoneOffset()},c:function(e){return $.call(e,at.ISO_8601)},r:function(e){return $.call(e,at.RFC_2822)},U:function(e){return e.getTime()},e:function(e){return e.lexicalize("exact")},x:function(e){return e.lexicalize("approx")}};return st=Object.keys(u).sort().join(""),ct=RegExp("([^"+st+"]*)(["+st+"])([^"+st+"]*)","g"),e.def(t,"filters",{value:ut=u},"w",!0),u}function rt(n){var r=e.copy({ISO_8601:"Y-m-d<T>H:i:s.u<Z>",ISO_8601_SHORT:"Y-m-d",RFC_850:"l, d-M-y H:i:s T",RFC_2822:"D, d M Y H:i:s O",sortable:"Y-m-d H:i:sO"},n.formats);return r.atom=r.ISO_8601,r.cookie=r.RFC_850,r.rss=r.RFC_2822,e.def(t,"formats",{value:at=r},"w",!0),r}function it(u){var s={d:{k:kt,fn:Number,re:Gt},D:{k:Mt,fn:n.bind(null,u.days_short),re:"("+u.days_short.join("|")+")"},j:{k:kt,fn:Number,re:Zt},l:{k:Mt,fn:n.bind(null,u.days),re:"("+u.days.join("|")+")"},N:{k:Mt,fn:D.bind(null,7),re:"([1-7])"},S:{re:"(?:"+u.ordinal.join("|")+")"},w:{k:Mt,fn:Number,re:"([0-6])"},z:{k:yt,fn:Number,re:"([0-9]{1,3})"},W:{k:St,fn:Number,re:Gt},F:{k:Ot,fn:n.bind(null,u.months),re:"("+u.months.join("|")+")"},m:{k:Ot,fn:D,re:Gt},M:{k:Ot,fn:n.bind(null,u.months_short),re:"("+u.months_short.join("|")+")"},n:{k:Ot,fn:D,re:Zt},t:{re:"[0-9]{2}"},L:{re:"(?:0|1)"},o:{k:xt,fn:Number,re:Kt},Y:{k:xt,fn:Number,re:Kt},y:{k:xt,fn:function(e){return e=Number(e),e+=30>e?2e3:1900},re:Gt},a:{k:_t,fn:e,re:Ut},A:{k:_t,fn:r,re:i(Ut)},g:{k:vt,fn:v,re:Zt},G:{k:vt,fn:Number,re:Zt},h:{k:vt,fn:v,re:Gt},H:{k:vt,fn:Number,re:Gt},i:{k:wt,fn:Number,re:Gt},s:{k:Yt,fn:Number,re:Gt},u:{k:Nt,fn:Number,re:"([0-9]{1,})"},O:{k:jt,fn:Y,re:"([\\+-][0-9]{4})"},P:{k:jt,fn:Y,re:"([\\+-][0-9]{2}:[0-9]{2})"},T:{re:"[A-Z]{1,4}"},Z:{k:jt,fn:Number,re:"([\\+-]?[0-9]{5})"},U:{k:Dt,fn:Number,re:"(-?[0-9]{1,})"}};return s.c={combo:[s.Y,s.m,s.d,s.H,s.i,s.s,s.u,s.P],re:[s.Y.re,"-",s.m.re,"-",s.d.re,"T",s.H.re,":",s.i.re,":",s.s.re,"(?:\\.",s.u.re,"){0,1}",s.P.re,"{0,1}"].join("")},s.r={combo:[s.D,s.d,s.M,s.Y,s.H,s.i,s.s,s.O],re:[s.D.re,", ",s.d.re," ",s.M.re," ",s.Y.re," ",s.H.re,":",s.i.re,":",s.s.re," ",s.O.re].join("")},e.def(t,"parsers",{value:ot=s},"w",!0),s}var ut,st,at,ot,ct,lt=[9,1,0,-1,-2,4,3],ft=864e5,ht=36e5,gt=6e4,dt=2592e6,mt=1e3,pt=6048e5,bt=31536e6,_t="ampm",kt="day",Mt="dayweek",yt="dayyear",vt="hour",Nt="ms",wt="minute",Ot="month",Yt="second",jt="timezone",Dt="unix",St="week",xt="year",Tt="NOREPLACE",Lt="<"+Tt+"<",Ft=">END"+Tt+">",Ht={day:["getDate","setDate"],hr:["getHours","setHours"],min:["getMinutes","setMinutes"],month:["getMonth","setMonth"],ms:["getMilliseconds","setMilliseconds"],sec:["getSeconds","setSeconds"],week:["getWeek","setWeek"],year:["getFullYear","setFullYear"]},Et=[xt,Ot,St,kt,"hr",wt.substring(0,3),Yt.substring(0,3),Nt],It=e.obj(),At=e.obj(),Ct=[kt,Mt,yt,Ot,St,xt],zt=e.obj(),Rt=e.obj(),Ut="(am|pm)",Wt=/>/g,Pt=/</g,Zt="([0-9]{1,2})",Gt="([0-9]{2})",Kt="([0-9]{4})",qt=/\s{2,}/g,Vt=/[<>]/,$t=/[^\(]*\(([^\)]+)\)/g,Bt=/[^A-Z]+/g,Jt=/[\+-]?([0-9]{2}):?([0-9]{2})/,Qt=[[xt+"s",bt],[Ot+"s",dt],[St+"s",pt],[kt+"s",ft],[vt+"s",ht],[wt+"s",gt],[Yt+"s",mt],[Nt,1]],Xt=Qt.pluck(0);zt.approx=function(e,n){var r,i,u,s,a,o,c=this.a,l=e.diff(n),f=t.diffKeys(l),h=t.time_map,g=this.time_units;if(l.value<t.MS_MINUTE)return this.just_now;switch(f[0]){case"years":u=0;break;case"months":u=1;break;case"weeks":u=2;break;case"days":o=1===l.days&&("hours"!=f[1]||.25>l.hours/24),u=3;break;case"hours":o=l.hours/24>=.75,c=this.an,u=4;break;case"minutes":u=5}if(i=(l.value-h[u][1]*l[f[0]])/h[u][1],o)return l.tense>0?this.tomorrow:4===u?this.today:this.yesterday;if(s=[],a=l.tense>0?this.from_now:this.ago,.1>i)switch(f[0]){case"years":case"months":case"weeks":if(1===l[f[0]]){s.push(1>l.tense?this.last:this.next,g[u][0]);break}default:!i||s.push(this.about),s.push(l[f[0]],g[u][l[f[0]]>1?1:0],a)}else.74>i?.24>i?r=this.just_over:(r=i>.24&&.4>i?this.almost:this.about,s.push(this.and,this.a,this.half)):s.push(this.almost,l[f[0]]+1,g[u][1],a);return r&&(s.push(g[u][l[f[0]]>1||s.length?1:0],a),s.unshift(r,l[f[0]])),"function"==typeof this.approx?this.approx.call(this,s,l):X.call(this,s,l)},zt.exact=function(e,n){var r,i=e.diff(n),u=this.time_units;return r=t.time_map.reduce(function(e,t,n){var r=i[t[0]];return!r||!u[n]||e.push(r+" "+u[n][r>1?1:0]),e},[]),r.length?"function"==typeof this.exact?this.exact.call(this,r,i):et.call(this,r,i):this.just_now},e.defs(t.prototype,{adjust:S,between:x,clearTime:T,clone:L,diff:m,format:$,getDayOfYear:F,getFirstOfTheMonth:H,getGMTOffset:E,getISODay:I,getISODaysInYear:A,getISOFirstMondayOfYear:C,getISOWeek:z,getISOWeeksInYear:R,getLastOfTheMonth:U,getWeek:W,isDST:P,isLeapYear:Z,lexicalize:Q,setWeek:G,timezone:K,valid:function(){return t.valid(this)}},"r"),e.defs(t,{DAY:kt,HOUR:"hr",MINUTE:wt.substring(0,3),MILLISECOND:Nt,MONTH:Ot,SECOND:Yt.substring(0,3),WEEK:St,YEAR:xt,MS_DAY:ft,MS_HOUR:ht,MS_MINUTE:gt,MS_MONTH:dt,MS_SECOND:mt,MS_WEEK:pt,MS_YEAR:bt,lexicon:{value:zt},time_map:{value:Qt},time_props:{value:Xt},coerce:c,diffKeys:y,localize:tt,toDate:c,valid:q},"r")}).x(Date)}("undefined"!=typeof m8?m8:"undefined"!=typeof require?require("m8"):null); |
@@ -1,25 +0,55 @@ | ||
( typeof m8 != 'undefined' ? m8 : require( 'm8' ) ).x.cache( 'Date', function( Type, util ) { | ||
util.def( Type, 'locale', util.describe( { value : { | ||
id : 'en-GB', | ||
AM : 'am', | ||
PM : 'pm', | ||
days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
day_count : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
formats : { | ||
server_date : 'Y-m-d', | ||
server_datetime : 'Y-m-d<T>H:i:sP', | ||
server_time : 'H:i:s', | ||
short_date : 'd/m/Y', | ||
short_time : 'h:ia' | ||
}, | ||
months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
ordinal : ['th', 'st', 'nd', 'rd', 'th'], | ||
ordinal_day_count : [ | ||
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365], | ||
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] | ||
], | ||
getOrdinal : function( d ) { return ( d > 3 && d < 21 ) ? Type.locale.ordinal[0] : Type.locale.ordinal[Math.min( d % 10, 4 )]; }, | ||
isLeapYear : function( y ) { return !!( y && ( ( new Type( y, 1, 29 ) ).getDate() == 29 && ( y % 100 || y % 400 == 0 ) ) ); }, | ||
setLeapYear : function( d ) { Type.locale.day_count[1] = Type.locale.isLeapYear( d.getFullYear() ) ? 29 : 28; } | ||
} }, 'w' ) ); | ||
Date.localize( { | ||
id : 'en-GB', | ||
AM : 'am', | ||
PM : 'pm', | ||
days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
days_short : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
day_count : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
lexicon : { | ||
DEFAULT : 'approx', | ||
a : 'a', | ||
about : 'about', | ||
ago : 'ago', | ||
almost : 'almost', | ||
an : 'an', | ||
and : 'and', | ||
delim : ', ', | ||
from_now : 'from now', | ||
half : 'half', | ||
just_now : 'just now', | ||
just_over : 'just over', | ||
last : 'last', | ||
next : 'next', | ||
now : 'now', | ||
today : 'today', | ||
tomorrow : 'tomorrow', | ||
yesterday : 'yesterday', | ||
time_units : [ // the descending order of these is important! | ||
['year', 'years'], ['month', 'months'], ['week', 'weeks'], ['day', 'days'], | ||
['hour', 'hours'], ['minute', 'minutes'], ['second', 'seconds'] | ||
] | ||
}, | ||
formats : { | ||
server_date : 'Y-m-d', | ||
server_datetime : 'Y-m-d<T>H:i:sP', | ||
server_time : 'H:i:s', | ||
short_date : 'd/m/Y', | ||
short_time : 'h:ia' | ||
}, | ||
months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
months_short : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
ordinal : ['th', 'st', 'nd', 'rd', 'th'], | ||
ordinal_day_count : [ | ||
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365], | ||
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] | ||
], | ||
getOrdinal : function( d ) { | ||
return ( d > 3 && d < 21 ) ? Date.locale.ordinal[0] : Date.locale.ordinal[Math.min( d % 10, 4 )]; | ||
}, | ||
isLeapYear : function( y ) { | ||
return !!( y && ( ( new Date( y, 1, 29 ) ).getDate() == 29 && ( y % 100 || y % 400 == 0 ) ) ); | ||
}, | ||
setLeapYear : function( d ) { | ||
Date.locale.day_count[1] = Date.locale.isLeapYear( d.getFullYear() ) ? 29 : 28; | ||
} | ||
} ); |
@@ -1,25 +0,55 @@ | ||
( typeof m8 != 'undefined' ? m8 : require( 'm8' ) ).x.cache( 'Date', function( Type, util ) { | ||
util.def( Type, 'locale', util.describe( { value : { | ||
id : 'en-US', | ||
AM : 'am', | ||
PM : 'pm', | ||
days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
day_count : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
formats : { | ||
server_date : 'Y-m-d', | ||
server_datetime : 'Y-m-d<T>H:i:sP', | ||
server_time : 'H:i:s', | ||
short_date : 'm/d/Y', | ||
short_time : 'h:ia' | ||
}, | ||
months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
ordinal : ['th', 'st', 'nd', 'rd', 'th'], | ||
ordinal_day_count : [ | ||
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365], | ||
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] | ||
], | ||
getOrdinal : function( d ) { return ( d > 3 && d < 21 ) ? Type.locale.ordinal[0] : Type.locale.ordinal[Math.min( d % 10, 4 )]; }, | ||
isLeapYear : function( y ) { return !!( y && ( ( new Type( y, 1, 29 ) ).getDate() == 29 && ( y % 100 || y % 400 == 0 ) ) ); }, | ||
setLeapYear : function( d ) { Type.locale.day_count[1] = Type.locale.isLeapYear( d.getFullYear() ) ? 29 : 28; } | ||
} }, 'w' ) ); | ||
Date.localize( { | ||
id : 'en-US', | ||
AM : 'am', | ||
PM : 'pm', | ||
days : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | ||
days_short : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
day_count : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
lexicon : { | ||
DEFAULT : 'approx', | ||
a : 'a', | ||
about : 'about', | ||
ago : 'ago', | ||
almost : 'almost', | ||
an : 'an', | ||
and : 'and', | ||
delim : ', ', | ||
from_now : 'from now', | ||
half : 'half', | ||
just_now : 'just now', | ||
just_over : 'just over', | ||
last : 'last', | ||
next : 'next', | ||
now : 'now', | ||
today : 'today', | ||
tomorrow : 'tomorrow', | ||
yesterday : 'yesterday', | ||
time_units : [ // the descending order of these is important! | ||
['year', 'years'], ['month', 'months'], ['week', 'weeks'], ['day', 'days'], | ||
['hour', 'hours'], ['minute', 'minutes'], ['second', 'seconds'] | ||
] | ||
}, | ||
formats : { | ||
server_date : 'Y-m-d', | ||
server_datetime : 'Y-m-d<T>H:i:sP', | ||
server_time : 'H:i:s', | ||
short_date : 'm/d/Y', | ||
short_time : 'h:ia' | ||
}, | ||
months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
months_short : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
ordinal : ['th', 'st', 'nd', 'rd', 'th'], | ||
ordinal_day_count : [ | ||
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365], | ||
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] | ||
], | ||
getOrdinal : function( d ) { | ||
return ( d > 3 && d < 21 ) ? Date.locale.ordinal[0] : Date.locale.ordinal[Math.min( d % 10, 4 )]; | ||
}, | ||
isLeapYear : function( y ) { | ||
return !!( y && ( ( new Date( y, 1, 29 ) ).getDate() == 29 && ( y % 100 || y % 400 == 0 ) ) ); | ||
}, | ||
setLeapYear : function( d ) { | ||
Date.locale.day_count[1] = Date.locale.isLeapYear( d.getFullYear() ) ? 29 : 28; | ||
} | ||
} ); |
@@ -9,3 +9,3 @@ { | ||
"dependencies" : { | ||
"m8" : ">= 0.3.1" | ||
"m8" : ">= 0.3.4" | ||
}, | ||
@@ -34,3 +34,3 @@ "devDependencies" : { | ||
}, | ||
"version" : "0.3.8" | ||
"version" : "0.4.0" | ||
} |
@@ -13,5 +13,11 @@ # d8.js [![build status](https://secure.travis-ci.org/constantology/d8.png)](http://travis-ci.org/constantology/d8) | ||
- en-US (0.9kb gzipped) | ||
- GR (1.1kb gzipped) **this still needs some work as my Greek is — how you say — "hella-rusty"** | ||
but feel free to create a locale for your specific nationality and submit a pull request! :D | ||
## File size | ||
- d8.js ≅ 8.8kb (gzipped) | ||
- d8.min.js ≅ 5.2kb (minzipped) | ||
## Dependencies | ||
@@ -32,5 +38,5 @@ | ||
<!-- IMPORTANT: The correct locale must be loaded before d8! --> | ||
<script src="/path/to/d8/d8.min.js" type="text/javascript"></script> | ||
<!-- This should now come after the actual library, since it is now possible to have use locales at once --> | ||
<script src="/path/to/d8/locale/en-GB.js" type="text/javascript"></script> | ||
<script src="/path/to/d8/d8.min.js" type="text/javascript"></script> | ||
@@ -43,7 +49,7 @@ ``` | ||
require( 'd8/locale/en-GB' ); // IMPORTANT: The correct locale must be loaded before d8!! | ||
require( 'd8' ); | ||
require( 'd8/locale/en-GB' ); // NOTE: This should now come after the actual library, since it is now possible to have use locales at once | ||
// if running in a sandboxed environment remember to: | ||
require( 'm8' ).x( Date ); // and/ or any other Types that require extending. | ||
require( 'm8' ).x( Date/*[, Object, Array, Boolean Function]*/ ); // and/ or any other Types that require extending. | ||
@@ -56,3 +62,3 @@ ``` | ||
Tested to work with nodejs, FF4+, Safari 5+, Chrome 7+, IE9+. Should technically work in any browser that supports [ecma 5]( http://kangax.github.com/es5-compat-table/) without throwing any JavaScript errors. | ||
Tested to work with nodejs, FF4+, Safari 5+, Chrome 7+, IE9+ and Opera — with one exception: `( new Date( [] ) ).valid() )` returns `true` in Opera and false in every other browser — technically **d8** should work in any JavaScript parser that supports [ecma 5]( http://kangax.github.com/es5-compat-table/) without throwing any JavaScript errors. | ||
@@ -174,6 +180,6 @@ ## API | ||
<dt><code>-1</code></dt><dd>The Date instance is less than now or the passed Date, i.e. in the past</dd> | ||
<dt><code>0</code></dt><dd>The Date instance is euqal to now or the passed Date, i.e. in the present.<br /><strong>NOTE:</strong> If <code>tense</code> is <code>0</code> then the Object will most probably have no other properties.</dd> | ||
<dt><code>0</code></dt><dd>The Date instance is equal to now or the passed Date, i.e. in the present.<br /><strong>NOTE:</strong> If <code>tense</code> is <code>0</code> then the Object will most probably have no other properties, except <code>value</code>, which will be zero.</dd> | ||
<dt><code>1</code></dt><dd>The Date instance is greater than now or the passed Date, i.e. in the future</dd> | ||
</dl> | ||
<strong>NOTE:</strong> All other properties Numbers will be positive Numbers as you will know if the <code>diff</code> is in the past, present or future by using this property. | ||
<strong>NOTE:</strong> To make the <code>diff</code> Object's values easier to work with all other properties will be positive Numbers. You should use the <code>tense</code> property as your reference for the <code>diff</code> being in the past, present or future. | ||
</td></tr> | ||
@@ -192,5 +198,4 @@ <tr><td width="48"><code>value</code></td><td width="48">Number</td><td>The — absolute — number of milliseconds difference between the two Dates.</td></tr> | ||
**NOTE:** If any of the properties — other than `tense` — is zero it will be removed from the `diff` Object. | ||
**NOTE:** If any property — other than `tense` & `value` — is zero it will be omitted from the `diff` Object. | ||
**NOTE:** You can supply a **space delimited** a String defining which properties you want to exclude from the result and `diff` will try and round off to the nearest time frame. | ||
@@ -203,12 +208,29 @@ ##### Example: | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2012, 0, 2 ) ) // returns => { tense : -1, days : 1 } | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2012, 0, 2 ) ) // returns => { tense : -1, value : 86400000, days : 1 } | ||
( new Date( 2012, 0, 2 ) ).diff( new Date( 2012, 0, 1 ) ) // returns => { tense : 1, days : 1 } | ||
( new Date( 2012, 0, 2 ) ).diff( new Date( 2012, 0, 1 ) ) // returns => { tense : 1, value : 86400000, days : 1 } | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2010, 9, 8, 7, 6, 5, 4 ) ) // returns => { tense : 1, years : 1, months : 9, weeks : 2, days : 1, hours : 17, minutes : 6, seconds : 54, ms : 4 } | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2010, 9, 8, 7, 6, 5, 4 ) ) // returns => { tense : 1, value : 38858034996, years : 1, months : 2, weeks : 3, days : 3, hours : 17, minutes : 53, seconds : 54, ms : 995 } | ||
``` | ||
**NOTE:** You can supply a **space delimited** String defining which properties you want to exclude from the result and `diff` will either pass the current calculation to the next time unit or, if there are none will round off — up if over .5 or down if less, uses `Math.round` to figure this out — to the previous time unit. | ||
Exclusion codes: | ||
- `-` will exclude the time unit from the `diff` Object. | ||
- `+` will include the time unit in the `diff` Object. **Note:** this is the same as not including the time unit in the `exclusions` String. | ||
- `>` will exclude all time units from this time unit down from the `diff` Object. | ||
##### Example with exclusions: | ||
```javascript | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2012, 0, 2 ), '-days' ) // returns => { tense : -1, value : 86400000, hours : 24 } | ||
( new Date( 2012, 0, 2 ) ).diff( new Date( 2012, 0, 1 ), '-days' ) // returns => { tense : 1, value : 86400000, hours : 24 } | ||
( new Date( 2012, 0, 1 ) ).diff( new Date( 2010, 9, 8, 7, 6, 5, 4 ), '-years -weeks >minutes' ) // returns => { tense : 1, value : 38858034996, months : 14, days : 29, hours : 18 } | ||
``` | ||
#### format( format:String ):String | ||
@@ -287,2 +309,27 @@ Returns a string representation of the Date instance, based on the passed format. See the [Date formatting and parsing options](#date-formatting-and-parsing-options) below. | ||
#### lexicalize( [now:Date, format:String] ):String | ||
Returns a String representation of the difference between the date instance and now, or the passed `Date`. | ||
#### Available formats | ||
The default format is `approx`, however this can be over-written by changing the **locale** file and/ or by passing in the desired format to the method. | ||
<table border="0" cellpadding="0" cellspacing="0" width="100%"> | ||
<tr><td width="96">approx</td><td>Will return an approximate difference. e.g. about 2 days ago; almost 1 and a half years from now.</td> | ||
<tr><td width="96">exact</td><td>Will return the exact difference, e.g. 2 days 3 hours and 5 minutes ago; 1 year, 4 months, 2 weeks, 1 day, 5 hours, 3 minutes and 7 seconds from now.</td> | ||
</table> | ||
##### Example: | ||
```javascript | ||
var date = new Date( 2012, 0, 1 ); | ||
date.clone().adjust( { hr : -3, day : -2 } ).lexicalize( date, 'approx' ); // returns => "just over 2 days ago" | ||
date.clone().adjust( { hr : -3, day : -2 } ).lexicalize( date, 'exact' ); // returns => "2 days and 3 hours ago" | ||
date.lexicalize( date.clone().adjust( { hr : -6, day : -2 } ), 'approx' ); // returns => "almost 2 and a half days from now" | ||
date.lexicalize( date.clone().adjust( { hr : -6, day : -2 } ), 'exact' ); // returns => "2 days and 6 hours from now" | ||
``` | ||
#### setWeek():Number(UnixTimeStamp) | ||
@@ -375,8 +422,8 @@ Sets the week of the year from the 1st January. | ||
</table> | ||
### custom | ||
<table border="0" cellpadding="0" cellspacing="0" width="100%"> | ||
<tr><td width="32">e</td><td>this is a convenience for `date.lexicalize( 'exact' );`</td></tr> | ||
<tr><td width="32">x</td><td>this is a convenience for `date.lexicalize( 'approx' );`</td></tr> | ||
</table> | ||
## File size | ||
- d8.js ≅ 6.8kb (gzipped) | ||
- d8.min.js ≅ 3.9kb (minzipped) | ||
## License | ||
@@ -383,0 +430,0 @@ |
102
src/diff.js
function diff( now, props ) { //noinspection FallthroughInSwitchStatementJS | ||
switch ( util.ntype( now ) ) { | ||
case 'string' : | ||
if ( isNaN( +( new Date( now ) ) ) ) { | ||
if ( props ) { | ||
now = Date.now(); | ||
break; | ||
} | ||
} | ||
case 'number' : case 'string' : | ||
if ( valid( new Type( now ) ) ) | ||
now = new Type( now ); | ||
else { | ||
now = new Date( now ); | ||
if ( !props ) props = now; | ||
now = 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 [conditional] fall-through if not a valid date | ||
default : now = Type.now(); | ||
} | ||
var ddiiff, | ||
ms = now - ( +this ), | ||
var diff, | ||
ms = +now - +this, | ||
tense = ms < 0 ? 1 : ms > 0 ? -1 : 0; | ||
ddiiff = tense === 0 ? util.obj() : diff_get( Math.abs( ms ), diff_get_exclusions( props ) ); | ||
if ( !tense ) { | ||
diff = util.obj(); | ||
diff.value = 0; | ||
} | ||
else | ||
diff = diff_get( Math.abs( ms ), diff_get_exclusions( props ) ); | ||
ddiiff.tense = tense; | ||
diff.tense = tense; | ||
return ddiiff; | ||
return diff; | ||
} | ||
function diff_eval( ddiiff, calc, i, calcs ) { | ||
function diff_eval( diff, calc, i, calcs ) { | ||
var time; | ||
if ( ddiiff.__ms__ ) { | ||
if ( !ddiiff.excl[calc[0]] ) { | ||
if ( ddiiff.__ms__ >= calc[1] ) { | ||
if ( diff.__ms__ ) { | ||
if ( !diff.excl[calc[0]] ) { | ||
if ( diff.__ms__ >= calc[1] ) { | ||
time = ddiiff.__ms__ / calc[1]; | ||
time = diff.__ms__ / calc[1]; | ||
if ( !( calc[0] in ddiiff.val ) ) { | ||
ddiiff.__ms__ = ( time % 1 ) * calc[1]; | ||
ddiiff.val[calc[0]] = Math.floor( time ); | ||
if ( !( calc[0] in diff.val ) ) { | ||
diff.__ms__ = ( time % 1 ) * calc[1]; | ||
diff.val[calc[0]] = Math.floor( time ); | ||
} | ||
else { | ||
time = Math.floor( time ); | ||
ddiiff.__ms__ -= time * calc[1]; | ||
ddiiff.val[calc[0]] += time; | ||
diff.__ms__ -= time * calc[1]; | ||
diff.val[calc[0]] += time; | ||
} | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
// round up or down depending on what's available | ||
if ( ( !calcs[i + 1] || ddiiff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) { | ||
time = ddiiff.__ms__ / calc[1]; | ||
ddiiff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( ddiiff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] ); | ||
return diff_eval( ddiiff, calc, i - 1, [] ); | ||
if ( ( !calcs[i + 1] || diff.excl[calcs[i + 1][0]] ) && ( calc = calcs[i - 1] ) ) { | ||
time = diff.__ms__ / calc[1]; | ||
diff.__ms__ = ( Math.round( time ) * calc[1] ) + ( ( ( diff.__ms__ / calcs[i][1] ) % 1 ) * calcs[i][1] ); | ||
return diff_eval( diff, calc, i - 1, [] ); | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
return ddiiff; | ||
return diff; | ||
} | ||
function diff_get( ms, excl ) { | ||
var ddiiff = diff_calc.reduce( diff_eval, { | ||
__ms__ : ms, | ||
excl : excl, | ||
val : util.obj() | ||
var diff = time_map.reduce( diff_eval, { | ||
__ms__ : ms, excl : excl, val : util.obj() | ||
} ).val; | ||
ddiiff.value = ms; | ||
diff.value = ms; | ||
return ddiiff; | ||
return diff; | ||
} | ||
@@ -90,3 +92,3 @@ | ||
diff_props.map( function( prop ) { | ||
time_props.map( function( prop ) { | ||
if ( !( prop in this ) ) | ||
@@ -106,6 +108,6 @@ this[prop] = !incl_remaining; | ||
case '>' : | ||
diff_calc.map( diff_excl_iter, { excl : excl, prop : prop, val : true } ); | ||
time_map.map( diff_excl_iter, { excl : excl, prop : prop, val : true } ); | ||
break; | ||
case '<' : | ||
diff_calc.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } ); | ||
time_map.slice().reverse().map( diff_excl_iter, { excl : excl, prop : prop, val : false } ); | ||
break; | ||
@@ -124,1 +126,15 @@ default : excl[val] = false; | ||
} | ||
// this ensures a diff's keys are always in descending order of | ||
// number of milliseconds per unit of time, i.e. year, ..., millisecond | ||
function diff_keys( diff ) { | ||
diff = util.copy( diff ); util.remove( diff, 'tense', 'value' ); | ||
// while this may seem like overkill, only having to run `indexOf` once for each sort item means that | ||
// the overall performance is dramatically improved | ||
return Object.keys( diff ).map( function( k ) { | ||
return [time_props.indexOf( k ), k]; | ||
} ).sort( function( a, b ) { | ||
a = a[0]; b = b[0]; | ||
return a > b ? 1 : -1; // skipping `===` check as we know all indexes are unique | ||
} ).pluck( 1 ); | ||
} |
// instance methods | ||
util.defs( Type.prototype, { | ||
adjust : adjust, between : between, clearTime : clearTime, | ||
clone : clone, diff : diff, format : format, | ||
getDayOfYear : getDayOfYear, getFirstOfTheMonth : getFirstOfTheMonth, getGMTOffset : getGMTOffset, | ||
getISODay : getISODay, getISODaysInYear : getISODaysInYear, getISOFirstMondayOfYear : getISOFirstMondayOfYear, | ||
getISOWeek : getISOWeek, getISOWeeksInYear : getISOWeeksInYear, getLastOfTheMonth : getLastOfTheMonth, | ||
getWeek : getWeek, isDST : isDST, isLeapYear : isLeapYear, | ||
setWeek : setWeek, timezone : timezone | ||
adjust : adjust, between : between, clearTime : clearTime, | ||
clone : clone, diff : diff, format : format, | ||
getDayOfYear : getDayOfYear, getFirstOfTheMonth : getFirstOfTheMonth, getGMTOffset : getGMTOffset, | ||
getISODay : getISODay, getISODaysInYear : getISODaysInYear, getISOFirstMondayOfYear : getISOFirstMondayOfYear, | ||
getISOWeek : getISOWeek, getISOWeeksInYear : getISOWeeksInYear, getLastOfTheMonth : getLastOfTheMonth, | ||
getWeek : getWeek, isDST : isDST, isLeapYear : isLeapYear, | ||
lexicalize : lexicalize, setWeek : setWeek, timezone : timezone, | ||
valid : function() { return Type.valid( this ); } | ||
}, 'r' ); | ||
@@ -15,9 +16,13 @@ | ||
// 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 }, | ||
lexicon : { value : lexicon }, time_map : { value : time_map }, time_props : { value : time_props }, | ||
// static methods | ||
getOrdinal : LOCALE.getOrdinal, isLeapYear : LOCALE.isLeapYear, setLeapYear : LOCALE.setLeapYear, toDate : toDate | ||
coerce : coerce, diffKeys : diff_keys, localize : localize, | ||
toDate : coerce, valid : valid | ||
}, 'r' ); |
// private methods | ||
function _24hrTime( o, res ) { return ( o = Number( o ) ) < 12 && _lc( res.ampm ) == _lc( LOCALE.PM ) ? o += 12 : o; } | ||
function _24hrTime( o, res ) { return ( o = Number( o ) ) < 12 && _lc( res.ampm ) == _lc( Type.locale.PM ) ? o += 12 : o; } | ||
function _adjust( d, v, k ) { return d.adjust( k, v ); } | ||
@@ -29,3 +29,4 @@ function _adjust_toobj( a ) { | ||
switch ( util.ntype( o ) ) { | ||
case 'number' : o = _adjust_toobj( arguments ); // allow fall-through | ||
case 'number' : o = arguments; // allow fall-through | ||
case 'array' : o = _adjust_toobj( o ); // allow fall-through | ||
case 'object' : Object.reduce( o, _adjust, date ); break; | ||
@@ -35,3 +36,3 @@ case 'string' : | ||
if ( fn && v !== 0 ) { | ||
LOCALE.setLeapYear( date ); | ||
Type.locale.setLeapYear( date ); | ||
@@ -64,4 +65,5 @@ if ( fn == adjust_by.month ) { | ||
function getDayOfYear() { | ||
LOCALE.setLeapYear( this ); | ||
return LOCALE.day_count.slice( 0, this.getMonth() ).reduce( sum, 0 ) + this.getDate() - 1; | ||
var L = Type.locale; | ||
L.setLeapYear( this ); | ||
return L.day_count.slice( 0, this.getMonth() ).reduce( sum, 0 ) + this.getDate() - 1; | ||
} | ||
@@ -91,4 +93,4 @@ | ||
function getLastOfTheMonth() { | ||
var m = this.getMonth(); LOCALE.setLeapYear( this ); | ||
return new Type( this.getFullYear(), m, LOCALE.day_count[m] ); | ||
var L = Type.locale, m = this.getMonth(); L.setLeapYear( this ); | ||
return new Type( this.getFullYear(), m, L.day_count[m] ); | ||
} | ||
@@ -100,3 +102,3 @@ | ||
function isLeapYear() { return LOCALE.isLeapYear( this.getFullYear() ); } | ||
function isLeapYear() { return Type.locale.isLeapYear( this.getFullYear() ); } | ||
@@ -109,1 +111,3 @@ function setWeek( v ) { this.setMonth( 0 ); this.setDate( 1 ); return ( this.adjust( Type.DAY, v * 7 ) ).getTime(); } | ||
} | ||
function valid( date ) { return util.ntype( date ) == 'date' && !isNaN( +date ); } |
function buildTemplate( date_format ) { | ||
if ( cache_format[date_format] ) return cache_format[date_format]; | ||
var LID = Type.locale.id, fn, i, l, part, parts, re_invalid; | ||
var fn = ['var out=[];'], i = -1, part, | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ), | ||
re_invalid = /^[^A-Za-z]*$/g, l = parts.length; | ||
if ( cache_format[LID][date_format] ) return cache_format[LID][date_format]; | ||
fn = ['\tvar out=[];']; | ||
parts = date_format.replace( re_add_nr, NOREPLACE_RB ).replace( re_add_enr, NOREPLACE_RE ).split( re_split ), | ||
re_invalid = /^[^A-Za-z]*$/g; | ||
i = -1; l = parts.length; | ||
while( ++i < l ) { | ||
@@ -16,5 +19,5 @@ part = parts[i]; | ||
fn.push( 'return out.join( "" );\n//@ sourceURL=d8/format/' + date_format ); | ||
fn.push( 'return out.join( "" );\n\t//@ sourceURL=d8/format/' + LID + '/' + date_format ); | ||
return cache_format[date_format] = new Function( 'filter', 'date', fn.join( '\n' ) ); | ||
return cache_format[LID][date_format] = new Function( 'filter', 'date', fn.join( '\n\n\t' ) ); | ||
} | ||
@@ -21,0 +24,0 @@ |
// utility methods | ||
function _indexOf( o, k ) { var i = o.indexOf( k ); return i == -1 ? null : i; } | ||
function _lc( o ) { return o.toLowerCase(); } | ||
function _substr( s ) { return s.substring( 0, 3 ); } | ||
function _uc( o ) { return o.toUpperCase(); } | ||
function _lc( o ) { return o.toLocaleLowerCase(); } | ||
function _uc( o ) { return o.toLocaleUpperCase(); } | ||
function associate( o, k ) { return o.reduce( function( res, v, i ) { res[k[i]] = v; return res; }, {} ); } | ||
@@ -14,3 +13,2 @@ function between_equalto( v, l, h ) { return l <= v && v <= h; } | ||
} | ||
function pluck( a, k ) { return a.reduce( function( v, o ) { !( k in o ) || v.push( o[k] ); return v; }, [] ); } | ||
function sum( v, i ) { return v + i; } |
136
src/vars.js
@@ -1,16 +0,21 @@ | ||
var LOCALE = Type.locale, U, | ||
var U, | ||
// DAY_OFFSETS is the amount of days from the current day to the Monday of the week it belongs to | ||
DAY_OFFSETS = [9, 1, 0, -1, -2, 4, 3], MS_DAY = 864e5, MS_HOUR = 3600000, MS_MINUTE = 60000, | ||
MS_MONTH = 2592e6, MS_SECOND = 1000, MS_WEEK = 6048e5, MS_YEAR = 31536e6, | ||
SHORT_DAYS = LOCALE.days.map( _substr ), SHORT_MONTHS = LOCALE.months.map( _substr ), | ||
DAY_OFFSETS = [9, 1, 0, -1, -2, 4, 3], MS_DAY = 864e5, MS_HOUR = 3600000, MS_MINUTE = 60000, | ||
MS_MONTH = 2592e6, MS_SECOND = 1000, MS_WEEK = 6048e5, MS_YEAR = 31536e6, | ||
// parser keys | ||
AMPM = 'ampm', DAY = 'day', DAYWEEK = 'dayweek', DAYYEAR = 'dayyear', HOUR = 'hour', MILLISECOND = 'ms', MINUTE ='minute', | ||
MONTH = 'month', SECOND = 'second', TIMEZONE = 'timezone', UNIX = 'unix', WEEK = 'week', YEAR = 'year', | ||
AMPM = 'ampm', DAY = 'day', DAYWEEK = 'dayweek', DAYYEAR = 'dayyear', HOUR = 'hour', | ||
MILLISECOND = 'ms', MINUTE = 'minute', MONTH = 'month', SECOND = 'second', TIMEZONE = 'timezone', | ||
UNIX = 'unix', WEEK = 'week', YEAR = 'year', | ||
// used by Date.prototype.format && Date.toDate to replace escaped chars | ||
NOREPLACE = 'NOREPLACE', NOREPLACE_RB = '<' + NOREPLACE + '<', NOREPLACE_RE = '>END' + NOREPLACE + '>', | ||
NOREPLACE = 'NOREPLACE', NOREPLACE_RB = '<' + NOREPLACE + '<', NOREPLACE_RE = '>END' + NOREPLACE + '>', | ||
adjust_by = { day : ['getDate', 'setDate'], hr : ['getHours', 'setHours'], min : ['getMinutes', 'setMinutes'], month : ['getMonth', 'setMonth'], ms : ['getMilliseconds', 'setMilliseconds'], sec : ['getSeconds', 'setSeconds'], week : ['getWeek', 'setWeek'], year : ['getFullYear', 'setFullYear'] }, | ||
adjust_order = [YEAR, MONTH, WEEK, DAY, 'hr', MINUTE.substring( 0, 3 ), SECOND.substring( 0, 3 ), MILLISECOND], | ||
// cache objects | ||
cache_format = {}, cache_parse = {}, date_members = [DAY, DAYWEEK, DAYYEAR, MONTH, WEEK, YEAR], | ||
diff_calc = [ // the order of this Array is important as it is the remainder of the larger | ||
cache_format = util.obj(), cache_parse = util.obj(), date_members = [DAY, DAYWEEK, DAYYEAR, MONTH, WEEK, YEAR], | ||
filter, filter_chars, formats, lexicon = util.obj(), locales = util.obj(), m, parser, | ||
re_ampm = '(am|pm)', re_add_enr = />/g, re_add_nr = /</g, re_compile, | ||
re_d1_2 = '([0-9]{1,2})', re_d2 = '([0-9]{2})', re_d4 = '([0-9]{4})', | ||
re_space = /\s{2,}/g, re_split = /[<>]/, re_tz = /[^\(]*\(([^\)]+)\)/g, | ||
re_tz_abbr = /[^A-Z]+/g, re_tz_off = /[\+-]?([0-9]{2}):?([0-9]{2})/, | ||
time_map = [ // the order of this Array is important as it is the remainder of the larger | ||
[YEAR + 's', MS_YEAR], // time unit that gets passed to the following time unit — as such we want | ||
@@ -25,113 +30,2 @@ [MONTH + 's', MS_MONTH], // to keep the order in case we want to exclude time units from the diff | ||
], | ||
diff_props = diff_calc.map( function( calc ) { return calc[0]; } ), | ||
filter = { | ||
// day | ||
d : function( d ) { return pad( d.getDate(), 2 ); }, // Day of the month, 2 digits with leading zeros | ||
D : function( d ) { return LOCALE.days[d.getDay()].substring( 0, 3 ); }, // A textual representation of a day, three letters | ||
j : function( d ) { return d.getDate(); }, // Day of the month without leading zeros | ||
l : function( d ) { return LOCALE.days[d.getDay()]; }, // A full textual representation of the day of the week | ||
N : function( d ) { return getISODay.call( d ); }, // ISO-8601 numeric representation of the day of the week | ||
S : function( d ) { return LOCALE.getOrdinal( d.getDate() ); }, // English ordinal suffix for the day of the month, 2 characters | ||
w : function( d ) { return d.getDay(); }, // Numeric representation of the day of the week | ||
z : function( d ) { return d.getDayOfYear(); }, // The day of the year (starting from 0) | ||
// week | ||
W : function( d ) { return getISOWeek.call( d ); }, // ISO-8601 week number of year, weeks starting on Monday | ||
// month | ||
F : function( d ) { return LOCALE.months[d.getMonth()]; }, // A full textual representation of a month | ||
m : function( d ) { return pad( ( d.getMonth() + 1 ), 2 ); }, // Numeric representation of a month, with leading zeros | ||
M : function( d ) { return LOCALE.months[d.getMonth()].substring( 0, 3 ); }, // A short textual representation of a month, three letters | ||
n : function( d ) { return d.getMonth() + 1; }, // Numeric representation of a month, without leading zeros | ||
t : function( d ) { LOCALE.setLeapYear( d ); return LOCALE.day_count[d.getMonth()]; }, // Number of days in the given month | ||
// year | ||
L : function( d ) { return d.isLeapYear() ? 1 : 0; }, // Whether it's a leap year | ||
o : function( d ) { // ISO-8601 year number. This has the same value as Y, except that if the ISO | ||
var m = d.getMonth(), w = getISOWeek.call( d ); // week number (W) belongs to the previous or next year, that year is used instead. | ||
return ( d.getFullYear() + ( w == 1 && m > 0 ? 1 : ( w >= 52 && m < 11 ? -1 : 0 ) ) ); | ||
}, | ||
Y : function( d ) { return d.getFullYear(); }, // A full numeric representation of a year, 4 digits | ||
y : function( d ) { return String( d.getFullYear() ).substring( 2, 4 ); }, // A two digit representation of a year | ||
// time | ||
a : function( d ) { return _lc( d.getHours() < 12 ? LOCALE.AM : LOCALE.PM ); }, // Lowercase Ante meridiem and Post meridiem | ||
A : function( d ) { return _uc( d.getHours() < 12 ? LOCALE.AM : LOCALE.PM ); }, // Uppercase Ante meridiem and Post meridiem | ||
g : function( d ) { return d.getHours() % 12 || 12; }, // 12-hour format of an hour without leading zeros | ||
G : function( d ) { return d.getHours(); }, // 24-hour format of an hour without leading zeros | ||
h : function( d ) { return pad( filter.g( d ), 2 ); }, // 12-hour format of an hour with leading zeros | ||
H : function( d ) { return pad( filter.G( d ), 2 ); }, // 24-hour format of an hour with leading zeros | ||
i : function( d ) { return pad( d.getMinutes(), 2 ); }, // Minutes with leading zeros | ||
s : function( d ) { return pad( d.getSeconds(), 2 ); }, // Seconds, with leading zeros | ||
u : function( d ) { return pad( d.getMilliseconds(), 3 ); }, // Milliseconds | ||
// timezone | ||
O : function( d ) { return getGMTOffset.call( d ); }, // Difference to Greenwich time (GMT) in hours | ||
P : function( d ) { return getGMTOffset.call( d, true ); }, // Difference to Greenwich time (GMT) with colon between hours and minutes | ||
T : function( d ) { return timezone.call( d ); }, // Timezone abbreviation | ||
Z : function( d ) { return d.getTimezoneOffset() * -60; }, // Timezone offset in seconds. The offset for timezones west of UTC | ||
// is always negative, and for those east of UTC is always positive. | ||
// full date/time | ||
c : function( d ) { return format.call( d, formats.ISO_8601 ); }, // ISO 8601 date | ||
r : function( d ) { return format.call( d, formats.RFC_2822 ); }, // RFC 2822 formatted date | ||
U : function( d ) { return d.getTime(); } // Seconds since the Unix Epoch January 1 1970 00:00:00 GMT | ||
}, | ||
filter_chars = Object.keys( filter ).sort().join( '' ), | ||
formats = util.copy( { | ||
ISO_8601 : 'Y-m-d<T>H:i:s.u<Z>', ISO_8601_SHORT : 'Y-m-d', | ||
RFC_850 : 'l, d-M-y H:i:s T', RFC_2822 : 'D, d M Y H:i:s O', | ||
sortable : 'Y-m-d H:i:sO' | ||
}, LOCALE.formats ), | ||
m, | ||
re_ampm = '(am|pm)', re_add_enr = />/g, re_add_nr = /</g, | ||
re_compile = new RegExp( '([^' + filter_chars + ']*)([' + filter_chars + '])([^' + filter_chars + ']*)', 'g' ), | ||
re_d1_2 = '([0-9]{1,2})', re_d2 = '([0-9]{2})', re_d4 = '([0-9]{4})', | ||
re_split = /[<>]/, re_tz = /[^\(]*\(([^\)]+)\)/g, re_tz_abbr = /[^A-Z]+/g, | ||
re_tz_off = /[\+-]?([0-9]{2}):?([0-9]{2})/, | ||
parser = { | ||
// day | ||
d : { k : DAY, fn : Number, re : re_d2 }, | ||
D : { k : DAYWEEK, fn : _indexOf.bind( null, SHORT_DAYS ), re : '(' + SHORT_DAYS.join( '|' ) + ')' }, | ||
j : { k : DAY, fn : Number, re : re_d1_2 }, | ||
l : { k : DAYWEEK, fn : _indexOf.bind( null, LOCALE.days ), re : '(' + LOCALE.days.join( '|' ) + ')' }, | ||
N : { k : DAYWEEK, fn : _zeroIndexedInt.bind( null, 7 ), re : '([1-7])' }, | ||
S : { re : '(?:' + LOCALE.ordinal.join( '|' ) + ')' }, | ||
w : { k : DAYWEEK, fn : Number, re : '([0-6])' }, | ||
z : { k : DAYYEAR, fn : Number, re : '([0-9]{1,3})' }, | ||
// week | ||
W : { k : WEEK, fn : Number, re : re_d2 }, | ||
// month | ||
F : { k : MONTH, fn : _indexOf.bind( null, LOCALE.months ), re : '(' + LOCALE.months.join( '|' ) + ')' }, | ||
m : { k : MONTH, fn : _zeroIndexedInt, re : re_d2 }, | ||
M : { k : MONTH, fn : _indexOf.bind( null, SHORT_MONTHS ), re : '(' + SHORT_MONTHS.join( '|' ) + ')' }, | ||
n : { k : MONTH, fn : _zeroIndexedInt, re : re_d1_2 }, | ||
t : { re : '[0-9]{2}' }, | ||
// year | ||
L : { re : '(?:0|1)' }, | ||
o : { k : YEAR, fn : Number, re : re_d4 }, | ||
Y : { k : YEAR, fn : Number, re : re_d4 }, | ||
y : { k : YEAR, fn : function( o ) { o = Number( o ); return o += ( o < 30 ? 2000 : 1900 ); }, re : re_d2 }, | ||
// time | ||
a : { k : AMPM, fn : util, re : re_ampm }, | ||
A : { k : AMPM, fn : _lc, re : _uc( re_ampm ) }, | ||
g : { k : HOUR, fn : _24hrTime, re : re_d1_2 }, | ||
G : { k : HOUR, fn : Number, re : re_d1_2 }, | ||
h : { k : HOUR, fn : _24hrTime, re : re_d2 }, | ||
H : { k : HOUR, fn : Number, re : re_d2 }, | ||
i : { k : MINUTE, fn : Number, re : re_d2 }, | ||
s : { k : SECOND, fn : Number, re : re_d2 }, | ||
u : { k : MILLISECOND, fn : Number, re : '([0-9]{1,})' }, | ||
// timezone | ||
O : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{4})' }, | ||
P : { k : TIMEZONE, fn : _timezoneOffset, re : '([\\+-][0-9]{2}:[0-9]{2})' }, | ||
T : { re : '[A-Z]{1,4}' }, | ||
Z : { k : TIMEZONE, fn : Number, re : '([\\+-]?[0-9]{5})' }, | ||
// full date/time | ||
U : { k : UNIX, fn : Number, re : '(-?[0-9]{1,})' } | ||
}; | ||
formats.atom = formats.ISO_8601; formats.cookie = formats.RFC_850; formats.rss = formats.RFC_2822; | ||
parser.c = { | ||
combo : [parser.Y, parser.m, parser.d, parser.H, parser.i, parser.s, parser.u, parser.P], | ||
re : [parser.Y.re, '-', parser.m.re, '-', parser.d.re, 'T', parser.H.re, ':', parser.i.re, ':', parser.s.re, '(?:\\.', parser.u.re, '){0,1}', parser.P.re, '{0,1}'].join( '' ) | ||
}; | ||
parser.r = { | ||
combo : [parser.D, parser.d, parser.M, parser.Y, parser.H, parser.i, parser.s, parser.O], | ||
re : [parser.D.re, ', ', parser.d.re, ' ', parser.M.re, ' ', parser.Y.re, ' ', parser.H.re, ':', parser.i.re, ':', parser.s.re, ' ', parser.O.re].join( '' ) | ||
}; | ||
time_props = time_map.pluck( 0 ); |
@@ -0,1 +1,2 @@ | ||
// 481 | ||
typeof m8 !== 'undefined' || ( m8 = require( 'm8' ) ); | ||
@@ -7,8 +8,13 @@ typeof chai !== 'undefined' || ( chai = require( 'chai' ) ); | ||
if ( m8.ENV == 'commonjs' ) { | ||
delete Date.locale; | ||
require( '../d8' ); | ||
require( '../locale/en-GB' ); | ||
require( '../d8' ); | ||
require( '../locale/en-US' ); | ||
require( '../locale/GR' ); | ||
require( './locale/en-US.test.js' ); | ||
require( './locale/GR.test.js' ); | ||
} | ||
suite( 'd8', function() { | ||
suite( 'd8 (en-GB/default)', function() { | ||
function MockDate( o ) { for ( var k in o ) !Object.prototype.hasOwnProperty.call( o, k ) || ( this[k] = o[k] ); } | ||
@@ -30,3 +36,8 @@ MockDate.prototype = { | ||
test( '<static> Date.getOrdinal returns the ordinal of a number', function( done ) { | ||
setup( function( done ) { | ||
Date.localize( 'en-GB' ); | ||
done(); | ||
} ); | ||
test( '<static> Date.locale.getOrdinal returns the ordinal of a number', function( done ) { | ||
expect( Date.getOrdinal( 1 ) ).to.eql( 'st' ); | ||
@@ -80,41 +91,69 @@ expect( Date.getOrdinal( 2 ) ).to.eql( 'nd' ); | ||
test( '<static> Date.toDate turns a Date String into a Date instance based on the passed format', function( done ) { | ||
expect( Date.toDate( 'Fri, 01 Jan 2010 00:00:00', 'D, d M Y H:i:s' ) ).to.eql( new Date( 2010, 0, 1 ) ); | ||
expect( Date.toDate( 'Fri, 01 Jan 2010 00:00:00 GMT+0400', 'D, d M Y H:i:s <GMT>O' ) ).to.eql( new Date( 2009, 11, 31, 20 ) ); | ||
expect( Date.toDate( 'Fri, 01 Jan 2010 00:00:00 GMT-08:00', 'D, d M Y H:i:s <GMT>P' ) ).to.eql( new Date( 2010, 0, 1, 8 ) ); | ||
test( '<static> Date.coerce turns a Date String into a Date instance based on the passed format', function( done ) { | ||
expect( Date.coerce( 'Fri, 01 Jan 2010 00:00:00', 'D, d M Y H:i:s' ) ).to.eql( new Date( 2010, 0, 1 ) ); | ||
expect( Date.coerce( 'Fri, 01 Jan 2010 00:00:00 GMT+0400', 'D, d M Y H:i:s <GMT>O' ) ).to.eql( new Date( 2009, 11, 31, 20 ) ); | ||
expect( Date.coerce( 'Fri, 01 Jan 2010 00:00:00 GMT-08:00', 'D, d M Y H:i:s <GMT>P' ) ).to.eql( new Date( 2010, 0, 1, 8 ) ); | ||
expect( Date.toDate( '1262304000000', 'U' ) ).to.eql( new Date( 2010, 0, 1 ) ); | ||
expect( Date.coerce( '1262304000000', 'U' ) ).to.eql( new Date( 2010, 0, 1 ) ); | ||
expect( Date.toDate( '2010-31', 'Y-z' ) ).to.eql( new Date( 2010, 0, 31 ) ); | ||
expect( Date.toDate( '2010-166', 'Y-z' ) ).to.eql( new Date( 2010, 5, 15 ) ); | ||
expect( Date.toDate( '2010-365', 'Y-z' ) ).to.eql( new Date( 2010, 11, 31 ) ); | ||
expect( Date.toDate( '2010-24', 'Y-W' ) ).to.eql( new Date( 2010, 5, 13 ) ); | ||
expect( Date.coerce( '2010-31', 'Y-z' ) ).to.eql( new Date( 2010, 0, 31 ) ); | ||
expect( Date.coerce( '2010-166', 'Y-z' ) ).to.eql( new Date( 2010, 5, 15 ) ); | ||
expect( Date.coerce( '2010-365', 'Y-z' ) ).to.eql( new Date( 2010, 11, 31 ) ); | ||
expect( Date.coerce( '2010-24', 'Y-W' ) ).to.eql( new Date( 2010, 5, 13 ) ); | ||
expect( Date.toDate( '2010-24-1', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 14 ) ); | ||
expect( Date.toDate( '2010-24-2', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 15 ) ); | ||
expect( Date.toDate( '2010-24-3', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 16 ) ); | ||
expect( Date.toDate( '2010-24-4', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 17 ) ); | ||
expect( Date.toDate( '2010-24-5', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 18 ) ); | ||
expect( Date.toDate( '2010-24-6', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 19 ) ); | ||
expect( Date.toDate( '2010-24-7', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 20 ) ); | ||
expect( Date.coerce( '2010-24-1', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 14 ) ); | ||
expect( Date.coerce( '2010-24-2', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 15 ) ); | ||
expect( Date.coerce( '2010-24-3', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 16 ) ); | ||
expect( Date.coerce( '2010-24-4', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 17 ) ); | ||
expect( Date.coerce( '2010-24-5', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 18 ) ); | ||
expect( Date.coerce( '2010-24-6', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 19 ) ); | ||
expect( Date.coerce( '2010-24-7', 'Y-W-N' ) ).to.eql( new Date( 2010, 5, 20 ) ); | ||
expect( Date.toDate( '2010-01-01T10:10:10+00:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 10, 10, 10 ) ); | ||
expect( Date.toDate( '2010-01-01T10:10:10+04:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 6, 10, 10 ) ); | ||
expect( Date.toDate( '2010-01-01T10:10:10-08:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 18, 10, 10 ) ); | ||
expect( Date.coerce( '2010-01-01T10:10:10+00:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 10, 10, 10 ) ); | ||
expect( Date.coerce( '2010-01-01T10:10:10+04:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 6, 10, 10 ) ); | ||
expect( Date.coerce( '2010-01-01T10:10:10-08:00', 'c' ) ).to.eql( new Date( 2010, 0, 1, 18, 10, 10 ) ); | ||
var date = Date.toDate( '2010-08-30T10:10:10+00:00', 'Y-m-d<T>H:i:sP' ), | ||
var date = Date.coerce( '2010-08-30T10:10:10+00:00', 'Y-m-d<T>H:i:sP' ), | ||
offset = date.isDST() ? 1 : 0; | ||
expect( Date.toDate( '2010-08-30T10:10:10+00:00', 'Y-m-d<T>H:i:sP' ) ).to.eql( new Date( 2010, 7, 30, ( 10 + offset ), 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10+00:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 10 + offset, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10+04:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 6 + offset, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10-08:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 18 + offset, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10+00:00', 'Y-m-d<T>H:i:sP' ) ).to.eql( new Date( 2010, 7, 30, ( 10 + offset ), 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10+00:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 10 + offset, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10+04:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 6 + offset, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10-08:00', 'c' ) ).to.eql( new Date( 2010, 7, 30, 18 + offset, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10.010+00:00Z', 'Y-m-d<T>H:i:s.uP<Z>' ) ).to.eql( new Date( 2010, 7, 30, ( 10 + offset ), 10, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10.010+00:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 10 + offset, 10, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10.010+04:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 6 + offset, 10, 10, 10 ) ); | ||
expect( Date.toDate( '2010-08-30T10:10:10.010-08:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 18 + offset, 10, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10.010+00:00Z', 'Y-m-d<T>H:i:s.uP<Z>' ) ).to.eql( new Date( 2010, 7, 30, ( 10 + offset ), 10, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10.010+00:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 10 + offset, 10, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10.010+04:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 6 + offset, 10, 10, 10 ) ); | ||
expect( Date.coerce( '2010-08-30T10:10:10.010-08:00Z', 'c' ) ).to.eql( new Date( 2010, 7, 30, 18 + offset, 10, 10, 10 ) ); | ||
done(); | ||
} ); | ||
test( '<static> Date.valid returns `true` if the passed Date is valid', function( done ) { | ||
expect( Date.valid( new Date ) ).to.be.true; | ||
expect( Date.valid( new Date( null ) ) ).to.be.true; | ||
expect( Date.valid( new Date( false ) ) ).to.be.true; // equates to new Date( 0 ) | ||
expect( Date.valid( new Date( true ) ) ).to.be.true; // equates to new Date( 1 ) | ||
expect( Date.valid( new Date( -1 ) ) ).to.be.true; | ||
expect( Date.valid( new Date( 2012 ) ) ).to.be.true; | ||
expect( Date.valid( new Date( 2012, 0 ) ) ).to.be.true; | ||
expect( Date.valid( new Date( 2012, 0 ) ) ).to.be.true; | ||
expect( Date.valid( new Date( Number.MIN_VALUE ) ) ).to.be.true; | ||
expect( Date.valid( new Date( new Date( new Date ) ) ) ).to.be.true; | ||
done(); | ||
} ); | ||
test( '<static> Date.valid returns `false` if the passed Date is valid', function( done ) { | ||
expect( Date.valid( new Date( undefined ) ) ).to.be.false; | ||
expect( Date.valid( new Date( NaN ) ) ).to.be.false; | ||
expect( Date.valid( new Date( Infinity ) ) ).to.be.false; | ||
expect( Date.valid( new Date( Number.MAX_VALUE ) ) ).to.be.false; | ||
expect( Date.valid( new Date( 'valid' ) ) ).to.be.false; | ||
expect( Date.valid( new Date( '' ) ) ).to.be.false; | ||
expect( Date.valid( new Date( [] ) ) ).to.be.false; | ||
expect( Date.valid( new Date( { year : 2012, month : 0, day : 1 } ) ) ).to.be.false; | ||
done(); | ||
} ); | ||
test( 'Date.prototype.adjust: can adjust a Date instance by any unit of time', function( done ) { | ||
@@ -181,3 +220,3 @@ var r = new Date( 2010, 0, 1 ); | ||
expect( new Date( now ).diff( new Date( now ) ) ).to.eql( { tense : 0 } ); | ||
expect( new Date( now ).diff( new Date( now ) ) ).to.eql( { tense : 0, value : 0 } ); | ||
@@ -297,2 +336,144 @@ expect( new Date( now ).diff( new Date( now ).adjust( Date.YEAR, 1 ) ) ).to.eql( { tense : -1, value : Date.MS_YEAR, years : 1 } ); | ||
test( 'Date.prototype.lexicalize `exact`', function( done ) { | ||
expect( ( new Date ).lexicalize( 'exact' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2011, 0, 1 ) ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 year ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).lexicalize( new Date( 2011, 0, 1 ), 'exact' ) ).to.equal( '1 year from now' ); | ||
expect( ( new Date( 2001, 0, 1 ) ).lexicalize( new Date( 2011, 11, 30 ), 'exact' ) ).to.equal( '11 years ago' ); | ||
expect( ( new Date( 2011, 11, 30 ) ).lexicalize( new Date( 2001, 0, 1 ), 'exact' ) ).to.equal( '11 years from now' ); | ||
expect( ( new Date( 2012, 0, 31 ) ).lexicalize( new Date( 2012, 2, 1 ), 'exact' ) ).to.equal( '1 month ago' ); | ||
expect( ( new Date( 2012, 2, 1 ) ).lexicalize( new Date( 2012, 0, 31 ), 'exact' ) ).to.equal( '1 month from now' ); | ||
// expect( ( new Date( 2012, 0, 1 ) ).lexicalize( ( new Date( 2012, 2, 31, 1, 0, 1 ) ), 'exact' ) ).to.equal( '3 months ago' ); | ||
// expect( ( new Date( 2012, 2, 31, 1, 0, 1 ) ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '3 months from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -3 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '3 weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 3 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '3 weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -5 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 month and 5 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 5 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 month and 5 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -7 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 month, 2 weeks and 5 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 7 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 month, 2 weeks and 5 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 day ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 day from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -2 ).adjust( Date.HOUR, -6 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '2 days and 6 hours ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 2 ).adjust( Date.HOUR, 6 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '2 days and 6 hours from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -3 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '3 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 3 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '3 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -4 ).adjust( Date.HOUR, -12 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '4 days and 12 hours ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 4 ).adjust( Date.HOUR, 12 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '4 days and 12 hours from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -5 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '5 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 5 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '5 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -6 ).adjust( Date.HOUR, -18 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '6 days and 18 hours ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 6 ).adjust( Date.HOUR, 18 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '6 days and 18 hours from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -7 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 7 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -8 ).adjust( Date.HOUR, -22 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week, 1 day, 21 hours, 59 minutes and 59 seconds ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 8 ).adjust( Date.HOUR, 22 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week, 1 day, 21 hours, 59 minutes and 59 seconds from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -9 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week and 2 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 9 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week and 2 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -10 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week and 3 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 10 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 week and 3 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 hour ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 hour from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -12 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '12 hours ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 12 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '12 hours from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -24 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 day ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 24 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 day from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 minute ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 minute from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -30 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '30 minutes ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 30 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '30 minutes from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -60 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 hour ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 60 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 hour from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 second ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 second from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -30 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '30 seconds ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 30 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '30 seconds from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -60 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 minute ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 60 ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 minute from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( { year : -1, month : -1, week : -1, day : -1, hr : -1, min : -1, sec : -1 } ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 year, 1 month, 1 week, 4 days, 1 hour and 1 minute ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( { year : 1, month : 1, week : 1, day : 1, hr : 1, min : 1, sec : 1 } ).lexicalize( new Date( 2012, 0, 1 ), 'exact' ) ).to.equal( '1 year, 1 month, 1 week, 5 days, 1 hour, 1 minute and 1 second from now' ); | ||
done(); | ||
} ); | ||
test( 'Date.prototype.lexicalize `approx`', function( done ) { | ||
expect( ( new Date ).lexicalize( 'approx' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2011, 0, 1 ) ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'last year' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).lexicalize( new Date( 2011, 0, 1 ), 'approx' ) ).to.equal( 'next year' ); | ||
expect( ( new Date( 2001, 0, 1 ) ).lexicalize( new Date( 2011, 11, 30 ), 'approx' ) ).to.equal( '11 years ago' ); | ||
expect( ( new Date( 2011, 11, 30 ) ).lexicalize( new Date( 2001, 0, 1 ), 'approx' ) ).to.equal( '11 years from now' ); | ||
expect( ( new Date( 2012, 0, 31 ) ).lexicalize( new Date( 2012, 2, 1 ), 'approx' ) ).to.equal( 'last month' ); | ||
expect( ( new Date( 2012, 2, 1 ) ).lexicalize( new Date( 2012, 0, 31 ), 'approx' ) ).to.equal( 'next month' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).lexicalize( new Date( 2012, 3, 1 ), 'approx' ) ).to.equal( 'about 3 months ago' ); | ||
expect( ( new Date( 2012, 3, 1 ) ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 3 months from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'last week' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'next week' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -2 ).adjust( Date.DAY, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 2 weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 2 ).adjust( Date.DAY, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 2 weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -3 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '3 weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 3 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '3 weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -5 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 1 month ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 5 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 1 month from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, -7 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 1 and a half months ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.WEEK, 7 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 1 and a half months from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'yesterday' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'tomorrow' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -2 ).adjust( Date.HOUR, -6 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 2 and a half days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 2 ).adjust( Date.HOUR, 6 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 2 and a half days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -3 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '3 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 3 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '3 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -4 ).adjust( Date.HOUR, -12 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 4 and a half days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 4 ).adjust( Date.HOUR, 12 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 4 and a half days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -5 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '5 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 5 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '5 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -6 ).adjust( Date.HOUR, -18 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 7 days ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 6 ).adjust( Date.HOUR, 18 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 7 days from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -7 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'last week' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 7 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'next week' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -8 ).adjust( Date.HOUR, -22 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 1 and a half weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 8 ).adjust( Date.HOUR, 22 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 1 and a half weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -9 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 1 and a half weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 9 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'almost 1 and a half weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, -10 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 1 and a half weeks ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.DAY, 10 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'about 1 and a half weeks from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 hour ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 hour from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -12 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '12 hours ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 12 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '12 hours from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, -24 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'yesterday' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.HOUR, 24 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'tomorrow' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 minute ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 minute from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -30 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '30 minutes ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 30 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '30 minutes from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, -60 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 hour ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.MINUTE, 60 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 hour from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 1 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -30 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 30 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, -60 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 minute ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( Date.SECOND, 60 ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( '1 minute from now' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( { year : -1, month : -1, week : -1, day : -1, hr : -1, min : -1, sec : -1 } ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 1 year ago' ); | ||
expect( ( new Date( 2012, 0, 1 ) ).adjust( { year : 1, month : 1, week : 1, day : 1, hr : 1, min : 1, sec : 1 } ).lexicalize( new Date( 2012, 0, 1 ), 'approx' ) ).to.equal( 'just over 1 year from now' ); | ||
done(); | ||
} ); | ||
test( 'Date.prototype.format: takes a format String and returns a Date String representation of the Date instance', function( done ) { | ||
@@ -310,3 +491,3 @@ function format( s ) { return '{ ' + s.split( ' ' ).map( map ).join( ', ' ) + ' }'; } | ||
expect( JSON.parse( r1.format( format( 'd D j l N S w z W F m M n t L o Y y a A g G h H i s u U' ) ) ) ).to.eql( { | ||
d : '01', D : 'Fri', j : '1', l : 'Friday', N : '5', S : 'st', w : '5', z : '0', // day | ||
d : '01', D : 'Fri', j : '1', l : 'Friday', N : '5', S : 'st', w : '5', z : '0', // day | ||
W : '53', // week | ||
@@ -322,3 +503,6 @@ F : 'January', m : '01', M : 'Jan', n : '1', t : '31', // month | ||
} ); | ||
expect( r1.format( 'e' ) ).to.equal( r1.lexicalize( 'exact' ) ); | ||
expect( r1.format( 'x' ) ).to.equal( r1.lexicalize( 'approx' ) ); | ||
done(); | ||
@@ -340,3 +524,2 @@ } ); | ||
expect( call( fn, new MockDate( { tzo : 600 } ), true ) ).to.eql( '-10:00' ); | ||
@@ -457,2 +640,31 @@ done(); | ||
} ); | ||
test( 'Date.prototype.valid returns `true` if the Date instance is valid', function( done ) { | ||
expect( ( new Date ).valid() ).to.be.true; | ||
expect( ( new Date( null ) ).valid() ).to.be.true; | ||
expect( ( new Date( false ) ).valid() ).to.be.true; // equates to new Date( 0 ) | ||
expect( ( new Date( true ) ).valid() ).to.be.true; // equates to new Date( 1 ) | ||
expect( ( new Date( -1 ) ).valid() ).to.be.true; | ||
expect( ( new Date( 2012 ) ).valid() ).to.be.true; | ||
expect( ( new Date( 2012, 0 ) ).valid() ).to.be.true; | ||
expect( ( new Date( 2012, 0 ) ).valid() ).to.be.true; | ||
expect( ( new Date( Number.MIN_VALUE ) ).valid() ).to.be.true; | ||
expect( ( new Date( new Date( new Date ) ) ).valid() ).to.be.true; | ||
done(); | ||
} ); | ||
test( 'Date.prototype.valid returns `false` if the Date instance is valid', function( done ) { | ||
expect( ( new Date( undefined ) ).valid() ).to.be.false; | ||
expect( ( new Date( NaN ) ).valid() ).to.be.false; | ||
expect( ( new Date( Infinity ) ).valid() ).to.be.false; | ||
expect( ( new Date( Number.MAX_VALUE ) ).valid() ).to.be.false; | ||
expect( ( new Date( 'valid' ) ).valid() ).to.be.false; | ||
expect( ( new Date( '' ) ).valid() ).to.be.false; | ||
expect( ( new Date( [] ) ).valid() ).to.be.false; | ||
expect( ( new Date( { year : 2012, month : 0, day : 1 } ) ).valid() ).to.be.false; | ||
done(); | ||
} ); | ||
} ); | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
239543
29
3048
431
3
Updatedm8@>= 0.3.4