add Ox.isValidDate and Ox.isInvalidDate; fix Ox.makeDate

This commit is contained in:
rolux 2015-04-15 20:01:46 +01:00
parent d42b9c25fb
commit fbc02301b5
2 changed files with 23 additions and 1 deletions

View file

@ -257,7 +257,7 @@ Ox.makeDate <f> Takes a date, number or string, returns a date
@*/ @*/
Ox.makeDate = function(date) { Ox.makeDate = function(date) {
// Safari 4/5 (<= 534.59.10) doesn't parse YYYY, YYYY-MM or YYYY-MM-DD // Safari 4/5 (<= 534.59.10) doesn't parse YYYY, YYYY-MM or YYYY-MM-DD
if (Ox.isString(date)) { if (Ox.isString(date) && Ox.isInvalidDate(new Date(date))) {
if (/^\d{4}$/.test(date)) { if (/^\d{4}$/.test(date)) {
date += '-01-01'; date += '-01-01';
} else if (/^\d{4}-\d{2}$/.test(date)) { } else if (/^\d{4}-\d{2}$/.test(date)) {

View file

@ -241,6 +241,17 @@ Ox.isInt = function(value) {
return isFinite(value) && value === Math.floor(value); return isFinite(value) && value === Math.floor(value);
}; };
/*
Ox.isInvalidDate <f> Tests if a value is an invalid date
(value) -> <b> True if the value is an invalid date
value <*> Any value
> Ox.isInvalidDate(new Date('foo'))
true
*/
Ox.isValidDate = function(value) {
return Ox.isDate(value) && Ox.isNaN(value.getTime());
};
/*@ /*@
Ox.isNaN <f> Tests if a value is `NaN` Ox.isNaN <f> Tests if a value is `NaN`
(value) -> <b> True if the value is `NaN` (value) -> <b> True if the value is `NaN`
@ -371,6 +382,17 @@ Ox.isUndefined = function(value) {
return Ox.typeOf(value) == 'undefined'; return Ox.typeOf(value) == 'undefined';
}; };
/*
Ox.isValidDate <f> Tests if a value is a valid date
(value) -> <b> True if the value is a valid date
value <*> Any value
> Ox.isValidDate(new Date())
true
*/
Ox.isValidDate = function(value) {
return Ox.isDate(value) && !Ox.isNaN(value.getTime());
};
/*@ /*@
Ox.typeOf <f> Returns the type of a value Ox.typeOf <f> Returns the type of a value
(value) -> <s> Type (value) -> <s> Type