refactor(angular): isDate / isArray test iframe independent fix

This commit is contained in:
Misko Hevery 2011-11-07 21:54:01 -08:00
parent 923da410bd
commit 78c7066422
4 changed files with 58 additions and 50 deletions

View file

@ -80,7 +80,6 @@ var _undefined = undefined,
$name = 'name',
$noop = 'noop',
$null = 'null',
$number = 'number',
$object = 'object',
$string = 'string',
$value = 'value',
@ -94,6 +93,7 @@ var _undefined = undefined,
jQuery, // delay binding
slice = [].slice,
push = [].push,
toString = Object.prototype.toString,
error = window[$console]
? bind(window[$console], window[$console]['error'] || noop)
: noop,
@ -363,7 +363,7 @@ function isString(value){return typeof value == $string;}
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is a `Number`.
*/
function isNumber(value){return typeof value == $number;}
function isNumber(value){return typeof value == 'number';}
/**
@ -377,7 +377,9 @@ function isNumber(value){return typeof value == $number;}
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is a `Date`.
*/
function isDate(value){return value instanceof Date;}
function isDate(value){
return toString.apply(value) == '[object Date]';
}
/**
@ -391,7 +393,9 @@ function isDate(value){return value instanceof Date;}
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is an `Array`.
*/
function isArray(value) {return value instanceof Array;}
function isArray(value) {
return toString.apply(value) == '[object Array]';
}
/**
@ -769,7 +773,7 @@ function equals(o1, o2) {
if (o1 === null || o2 === null) return false;
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2 && t1 == 'object') {
if (o1 instanceof Array) {
if (isArray(o1)) {
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;

View file

@ -107,7 +107,7 @@ function createInjector(modulesToLoad, moduleRegistry) {
path.shift();
}
case 'object':
if (value instanceof Array) {
if (isArray(value)) {
return invoke(null, value);
}
default:
@ -122,11 +122,11 @@ function createInjector(modulesToLoad, moduleRegistry) {
length,
key;
if (fn instanceof Function) {
if (typeof fn == 'function') {
$inject = inferInjectionArgs(fn);
length = $inject.length;
} else {
if (fn instanceof Array) {
if (isArray(fn)) {
$inject = fn;
length = $inject.length;
fn = $inject[--length];

83
src/angular-mocks.js vendored
View file

@ -476,92 +476,92 @@ angular.mock.$LogProvider = function(){
*
*/
angular.mock.TzDate = function (offset, timestamp) {
var self = new Date(0);
if (angular.isString(timestamp)) {
var tsStr = timestamp;
this.origDate = angular.fromJson(angular.toJson({date:timestamp})).date;
self.origDate = angular.fromJson(angular.toJson({date:timestamp})).date;
timestamp = this.origDate.getTime();
timestamp = self.origDate.getTime();
if (isNaN(timestamp))
throw {
name: "Illegal Argument",
message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
};
} else {
this.origDate = new Date(timestamp);
self.origDate = new Date(timestamp);
}
var localOffset = new Date(timestamp).getTimezoneOffset();
this.offsetDiff = localOffset*60*1000 - offset*1000*60*60;
this.date = new Date(timestamp + this.offsetDiff);
self.offsetDiff = localOffset*60*1000 - offset*1000*60*60;
self.date = new Date(timestamp + self.offsetDiff);
this.getTime = function() {
return this.date.getTime() - this.offsetDiff;
self.getTime = function() {
return self.date.getTime() - self.offsetDiff;
};
this.toLocaleDateString = function() {
return this.date.toLocaleDateString();
self.toLocaleDateString = function() {
return self.date.toLocaleDateString();
};
this.getFullYear = function() {
return this.date.getFullYear();
self.getFullYear = function() {
return self.date.getFullYear();
};
this.getMonth = function() {
return this.date.getMonth();
self.getMonth = function() {
return self.date.getMonth();
};
this.getDate = function() {
return this.date.getDate();
self.getDate = function() {
return self.date.getDate();
};
this.getHours = function() {
return this.date.getHours();
self.getHours = function() {
return self.date.getHours();
};
this.getMinutes = function() {
return this.date.getMinutes();
self.getMinutes = function() {
return self.date.getMinutes();
};
this.getSeconds = function() {
return this.date.getSeconds();
self.getSeconds = function() {
return self.date.getSeconds();
};
this.getTimezoneOffset = function() {
self.getTimezoneOffset = function() {
return offset * 60;
};
this.getUTCFullYear = function() {
return this.origDate.getUTCFullYear();
self.getUTCFullYear = function() {
return self.origDate.getUTCFullYear();
};
this.getUTCMonth = function() {
return this.origDate.getUTCMonth();
self.getUTCMonth = function() {
return self.origDate.getUTCMonth();
};
this.getUTCDate = function() {
return this.origDate.getUTCDate();
self.getUTCDate = function() {
return self.origDate.getUTCDate();
};
this.getUTCHours = function() {
return this.origDate.getUTCHours();
self.getUTCHours = function() {
return self.origDate.getUTCHours();
};
this.getUTCMinutes = function() {
return this.origDate.getUTCMinutes();
self.getUTCMinutes = function() {
return self.origDate.getUTCMinutes();
};
this.getUTCSeconds = function() {
return this.origDate.getUTCSeconds();
self.getUTCSeconds = function() {
return self.origDate.getUTCSeconds();
};
this.getDay = function() {
return this.origDate.getDay();
self.getDay = function() {
return self.origDate.getDay();
};
//hide all methods not implemented in this mock that the Date prototype exposes
var self = this,
unimplementedMethods = ['getMilliseconds', 'getUTCDay',
var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
'getUTCMilliseconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
@ -570,12 +570,11 @@ angular.mock.TzDate = function (offset, timestamp) {
angular.forEach(unimplementedMethods, function(methodName) {
self[methodName] = function() {
throw {
name: "MethodNotImplemented",
message: "Method '" + methodName + "' is not implemented in the TzDate mock"
};
throw Error("Method '" + methodName + "' is not implemented in the TzDate mock");
};
});
return self;
}
//make "tzDateInstance instanceof Date" return true

View file

@ -7,6 +7,11 @@ describe('mocks', function() {
return min*60*1000;
}
it('should look like a Date', function() {
var date = new angular.mock.TzDate(0,0);
expect(angular.isDate(date)).toBe(true);
});
it('should take millis as constructor argument', function() {
expect(new angular.mock.TzDate(0, 0).getTime()).toBe(0);
expect(new angular.mock.TzDate(0, 1283555108000).getTime()).toBe(1283555108000);