Make doubley sure Date.now, clearTimeout, and setTimeout are mockable.

This commit is contained in:
John-David Dalton
2016-07-29 20:47:16 -07:00
parent 0bfa1ec2d0
commit 1468c74c36

View File

@@ -1282,7 +1282,6 @@
/** Built-in constructor references. */ /** Built-in constructor references. */
var Array = context.Array, var Array = context.Array,
Date = context.Date,
Error = context.Error, Error = context.Error,
Math = context.Math, Math = context.Math,
RegExp = context.RegExp, RegExp = context.RegExp,
@@ -1342,9 +1341,10 @@
splice = arrayProto.splice, splice = arrayProto.splice,
spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined; spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
/** Built-in method references that are mockable. */ /** Mocked built-ins. */
var clearTimeout = function(id) { return context.clearTimeout.call(root, id); }, var ctxClearTimeout = context.clearTimeout !== root.clearTimeout && context.clearTimeout,
setTimeout = function(func, wait) { return context.setTimeout.call(root, func, wait); }; ctxNow = context.Date && context.Date.now !== root.Date.now && context.Date.now,
ctxSetTimeout = context.setTimeout !== root.setTimeout && context.setTimeout;
/* Built-in method references for those with the same name as other `lodash` methods. */ /* Built-in method references for those with the same name as other `lodash` methods. */
var nativeCeil = Math.ceil, var nativeCeil = Math.ceil,
@@ -4157,6 +4157,16 @@
return (!start && end >= length) ? array : baseSlice(array, start, end); return (!start && end >= length) ? array : baseSlice(array, start, end);
} }
/**
* A simple wrapper around the global [`clearTimeout`](https://mdn.io/clearTimeout).
*
* @private
* @param {number} id The id of the timer to clear.
*/
var clearTimeout = ctxClearTimeout || function(id) {
return root.clearTimeout(id);
};
/** /**
* Creates a clone of `buffer`. * Creates a clone of `buffer`.
* *
@@ -6187,6 +6197,18 @@
}; };
}()); }());
/**
* A simple wrapper around the global [`setTimeout`](https://mdn.io/setTimeout).
*
* @private
* @param {Function} func The function to delay.
* @param {number} wait The number of milliseconds to delay invocation.
* @returns {number} Returns the timer id.
*/
var setTimeout = ctxSetTimeout || function(func, wait) {
return root.setTimeout(func, wait);
};
/** /**
* Sets the `toString` method of `wrapper` to mimic the source of `reference` * Sets the `toString` method of `wrapper` to mimic the source of `reference`
* with wrapper details in a comment at the top of the source body. * with wrapper details in a comment at the top of the source body.
@@ -9487,9 +9509,9 @@
* }, _.now()); * }, _.now());
* // => Logs the number of milliseconds it took for the deferred invocation. * // => Logs the number of milliseconds it took for the deferred invocation.
*/ */
function now() { var now = ctxNow || function() {
return Date.now(); return root.Date.now();
} };
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/