Files
jasmine/src/core/util.js
Steve Gravrock b0034797fe Merge branch 'fix-save-args-by-value' of https://github.com/coyoteecd/jasmine
* Fixes cloning of Date objects in saveArgumentsByValue()
* Merges #1955 from @coyoteecd
* Fixes #1885
2022-02-02 21:18:38 -08:00

139 lines
3.5 KiB
JavaScript

getJasmineRequireObj().util = function(j$) {
var util = {};
util.inherit = function(childClass, parentClass) {
var Subclass = function() {};
Subclass.prototype = parentClass.prototype;
childClass.prototype = new Subclass();
};
util.argsToArray = function(args) {
var arrayOfArgs = [];
for (var i = 0; i < args.length; i++) {
arrayOfArgs.push(args[i]);
}
return arrayOfArgs;
};
util.isUndefined = function(obj) {
return obj === void 0;
};
util.arrayContains = function(array, search) {
var i = array.length;
while (i--) {
if (array[i] === search) {
return true;
}
}
return false;
};
util.clone = function(obj) {
if (Object.prototype.toString.apply(obj) === '[object Array]') {
return obj.slice();
}
var cloned = {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
cloned[prop] = obj[prop];
}
}
return cloned;
};
util.cloneArgs = function(args) {
var clonedArgs = [];
var argsAsArray = j$.util.argsToArray(args);
for (var i = 0; i < argsAsArray.length; i++) {
var str = Object.prototype.toString.apply(argsAsArray[i]),
primitives = /^\[object (Boolean|String|RegExp|Number)/;
// All falsey values are either primitives, `null`, or `undefined.
if (!argsAsArray[i] || str.match(primitives)) {
clonedArgs.push(argsAsArray[i]);
} else if (str === '[object Date]') {
clonedArgs.push(new Date(argsAsArray[i].valueOf()));
} else {
clonedArgs.push(j$.util.clone(argsAsArray[i]));
}
}
return clonedArgs;
};
util.getPropertyDescriptor = function(obj, methodName) {
var descriptor,
proto = obj;
do {
descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
proto = Object.getPrototypeOf(proto);
} while (!descriptor && proto);
return descriptor;
};
util.objectDifference = function(obj, toRemove) {
var diff = {};
for (var key in obj) {
if (util.has(obj, key) && !util.has(toRemove, key)) {
diff[key] = obj[key];
}
}
return diff;
};
util.has = function(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};
util.errorWithStack = function errorWithStack() {
// Don't throw and catch. That makes it harder for users to debug their
// code with exception breakpoints, and it's unnecessary since all
// supported environments populate new Error().stack
return new Error();
};
function callerFile() {
var trace = new j$.StackTrace(util.errorWithStack());
return trace.frames[2].file;
}
util.jasmineFile = (function() {
var result;
return function() {
if (!result) {
result = callerFile();
}
return result;
};
})();
function StopIteration() {}
StopIteration.prototype = Object.create(Error.prototype);
StopIteration.prototype.constructor = StopIteration;
util.validateTimeout = function(timeout, msgPrefix) {
// Timeouts are implemented with setTimeout, which only supports a limited
// range of values. The limit is unspecified, as is the behavior when it's
// exceeded. But on all currently supported JS runtimes, setTimeout calls
// the callback immediately when the timeout is greater than 2147483647
// (the maximum value of a signed 32 bit integer).
var max = 2147483647;
if (timeout > max) {
throw new Error(
(msgPrefix || 'Timeout value') + ' cannot be greater than ' + max
);
}
};
return util;
};