Comparing version 5.0.0 to 5.0.1
5.0.1 / 2019-10-10 | ||
================== | ||
* Upgrade eslint, add prettier | ||
* Use `--no-detect-globals` to bundle and test lolex (#270) | ||
5.0.0 / 2019-10-07 | ||
@@ -3,0 +9,0 @@ ================== |
353
lolex.js
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.lolex = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
(function (global){ | ||
"use strict"; | ||
// eslint-disable-next-line complexity | ||
function withGlobal(_global) { | ||
@@ -9,30 +9,40 @@ var userAgent = _global.navigator && _global.navigator.userAgent; | ||
var maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint | ||
var NOOP = function () { return undefined; }; | ||
var NOOP_ARRAY = function () { return []; }; | ||
var NOOP = function() { | ||
return undefined; | ||
}; | ||
var NOOP_ARRAY = function() { | ||
return []; | ||
}; | ||
var timeoutResult = _global.setTimeout(NOOP, 0); | ||
var addTimerReturnsObject = typeof timeoutResult === "object"; | ||
var hrtimePresent = (_global.process && typeof _global.process.hrtime === "function"); | ||
var hrtimeBigintPresent = (hrtimePresent && typeof _global.process.hrtime.bigint === "function"); | ||
var nextTickPresent = (_global.process && typeof _global.process.nextTick === "function"); | ||
var performancePresent = (_global.performance && typeof _global.performance.now === "function"); | ||
var hasPerformancePrototype = (_global.Performance && (typeof _global.Performance).match(/^(function|object)$/)); | ||
var queueMicrotaskPresent = (_global.hasOwnProperty("queueMicrotask")); | ||
var requestAnimationFramePresent = ( | ||
_global.requestAnimationFrame && typeof _global.requestAnimationFrame === "function" | ||
); | ||
var cancelAnimationFramePresent = ( | ||
_global.cancelAnimationFrame && typeof _global.cancelAnimationFrame === "function" | ||
); | ||
var requestIdleCallbackPresent = ( | ||
_global.requestIdleCallback && typeof _global.requestIdleCallback === "function" | ||
); | ||
var cancelIdleCallbackPresent = ( | ||
_global.cancelIdleCallback && typeof _global.cancelIdleCallback === "function" | ||
); | ||
var setImmediatePresent = ( | ||
_global.setImmediate && typeof _global.setImmediate === "function" | ||
); | ||
var hrtimePresent = | ||
_global.process && typeof _global.process.hrtime === "function"; | ||
var hrtimeBigintPresent = | ||
hrtimePresent && typeof _global.process.hrtime.bigint === "function"; | ||
var nextTickPresent = | ||
_global.process && typeof _global.process.nextTick === "function"; | ||
var performancePresent = | ||
_global.performance && typeof _global.performance.now === "function"; | ||
var hasPerformancePrototype = | ||
_global.Performance && | ||
(typeof _global.Performance).match(/^(function|object)$/); | ||
var queueMicrotaskPresent = _global.hasOwnProperty("queueMicrotask"); | ||
var requestAnimationFramePresent = | ||
_global.requestAnimationFrame && | ||
typeof _global.requestAnimationFrame === "function"; | ||
var cancelAnimationFramePresent = | ||
_global.cancelAnimationFrame && | ||
typeof _global.cancelAnimationFrame === "function"; | ||
var requestIdleCallbackPresent = | ||
_global.requestIdleCallback && | ||
typeof _global.requestIdleCallback === "function"; | ||
var cancelIdleCallbackPresent = | ||
_global.cancelIdleCallback && | ||
typeof _global.cancelIdleCallback === "function"; | ||
var setImmediatePresent = | ||
_global.setImmediate && typeof _global.setImmediate === "function"; | ||
// Make properties writable in IE, as per | ||
// http://www.adequatelygood.com/Replacing-setTimeout-Globally.html | ||
/* eslint-disable no-self-assign */ | ||
if (isRunningInIE) { | ||
@@ -52,2 +62,3 @@ _global.setTimeout = _global.setTimeout; | ||
} | ||
/* eslint-enable no-self-assign */ | ||
@@ -88,3 +99,5 @@ _global.clearTimeout(timeoutResult); | ||
if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) { | ||
throw new Error("tick only understands numbers, 'm:s' and 'h:m:s'. Each part must be two digits"); | ||
throw new Error( | ||
"tick only understands numbers, 'm:s' and 'h:m:s'. Each part must be two digits" | ||
); | ||
} | ||
@@ -99,3 +112,3 @@ | ||
ms += parsed * Math.pow(60, (l - i - 1)); | ||
ms += parsed * Math.pow(60, l - i - 1); | ||
} | ||
@@ -127,5 +140,11 @@ | ||
function getEpoch(epoch) { | ||
if (!epoch) { return 0; } | ||
if (typeof epoch.getTime === "function") { return epoch.getTime(); } | ||
if (typeof epoch === "number") { return epoch; } | ||
if (!epoch) { | ||
return 0; | ||
} | ||
if (typeof epoch.getTime === "function") { | ||
return epoch.getTime(); | ||
} | ||
if (typeof epoch === "number") { | ||
return epoch; | ||
} | ||
throw new TypeError("now should be milliseconds since UNIX epoch"); | ||
@@ -202,5 +221,20 @@ } | ||
case 6: | ||
return new NativeDate(year, month, date, hour, minute, second); | ||
return new NativeDate( | ||
year, | ||
month, | ||
date, | ||
hour, | ||
minute, | ||
second | ||
); | ||
default: | ||
return new NativeDate(year, month, date, hour, minute, second, ms); | ||
return new NativeDate( | ||
year, | ||
month, | ||
date, | ||
hour, | ||
minute, | ||
second, | ||
ms | ||
); | ||
} | ||
@@ -229,3 +263,7 @@ } | ||
if (clock.loopLimit && i > clock.loopLimit) { | ||
throw new Error("Aborting after running " + clock.loopLimit + " timers, assuming an infinite loop!"); | ||
throw new Error( | ||
"Aborting after running " + | ||
clock.loopLimit + | ||
" timers, assuming an infinite loop!" | ||
); | ||
} | ||
@@ -267,3 +305,4 @@ } | ||
timer.createdAt = clock.now; | ||
timer.callAt = clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0)); | ||
timer.callAt = | ||
clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0)); | ||
@@ -275,5 +314,11 @@ clock.timers[timer.id] = timer; | ||
id: timer.id, | ||
ref: function () { return res; }, | ||
unref: function () { return res; }, | ||
refresh: function () { return res; } | ||
ref: function() { | ||
return res; | ||
}, | ||
unref: function() { | ||
return res; | ||
}, | ||
refresh: function() { | ||
return res; | ||
} | ||
}; | ||
@@ -332,3 +377,6 @@ return res; | ||
if (isInRange && (!timer || compareTimers(timer, timers[id]) === 1)) { | ||
if ( | ||
isInRange && | ||
(!timer || compareTimers(timer, timers[id]) === 1) | ||
) { | ||
timer = timers[id]; | ||
@@ -402,16 +450,25 @@ } | ||
// its .id field is the actual timer id. | ||
if (typeof timerId === "object") { | ||
timerId = timerId.id; | ||
} | ||
var id = typeof timerId === "object" ? timerId.id : timerId; | ||
if (clock.timers.hasOwnProperty(timerId)) { | ||
if (clock.timers.hasOwnProperty(id)) { | ||
// check that the ID matches a timer of the correct type | ||
var timer = clock.timers[timerId]; | ||
var timer = clock.timers[id]; | ||
if (timer.type === ttype) { | ||
delete clock.timers[timerId]; | ||
delete clock.timers[id]; | ||
} else { | ||
var clear = ttype === "AnimationFrame" ? "cancelAnimationFrame" : "clear" + ttype; | ||
var schedule = timer.type === "AnimationFrame" ? "requestAnimationFrame" : "set" + timer.type; | ||
throw new Error("Cannot clear timer: timer created with " + schedule | ||
+ "() but cleared with " + clear + "()"); | ||
var clear = | ||
ttype === "AnimationFrame" | ||
? "cancelAnimationFrame" | ||
: "clear" + ttype; | ||
var schedule = | ||
timer.type === "AnimationFrame" | ||
? "requestAnimationFrame" | ||
: "set" + timer.type; | ||
throw new Error( | ||
"Cannot clear timer: timer created with " + | ||
schedule + | ||
"() but cleared with " + | ||
clear + | ||
"()" | ||
); | ||
} | ||
@@ -422,5 +479,3 @@ } | ||
function uninstall(clock, target, config) { | ||
var method, | ||
i, | ||
l; | ||
var method, i, l; | ||
var installedHrTime = "_hrtime"; | ||
@@ -436,5 +491,16 @@ var installedNextTick = "_nextTick"; | ||
} else if (method === "performance") { | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor(clock, "_" + method); | ||
if (originalPerfDescriptor && originalPerfDescriptor.get && !originalPerfDescriptor.set) { | ||
Object.defineProperty(target, method, originalPerfDescriptor); | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor( | ||
clock, | ||
"_" + method | ||
); | ||
if ( | ||
originalPerfDescriptor && | ||
originalPerfDescriptor.get && | ||
!originalPerfDescriptor.set | ||
) { | ||
Object.defineProperty( | ||
target, | ||
method, | ||
originalPerfDescriptor | ||
); | ||
} else if (originalPerfDescriptor.configurable) { | ||
@@ -446,3 +512,6 @@ target[method] = clock["_" + method]; | ||
target[method] = clock["_" + method]; | ||
if (method === "clearInterval" && config.shouldAdvanceTime === true) { | ||
if ( | ||
method === "clearInterval" && | ||
config.shouldAdvanceTime === true | ||
) { | ||
target[method](clock.attachedInterval); | ||
@@ -453,3 +522,5 @@ } | ||
delete target[method]; | ||
} catch (ignore) { /* eslint no-empty: "off" */ } | ||
} catch (ignore) { | ||
/* eslint no-empty: "off" */ | ||
} | ||
} | ||
@@ -473,3 +544,6 @@ } | ||
var prop; | ||
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(target, method); | ||
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call( | ||
target, | ||
method | ||
); | ||
clock["_" + method] = target[method]; | ||
@@ -481,8 +555,22 @@ | ||
} else if (method === "performance") { | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor(target, method); | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor( | ||
target, | ||
method | ||
); | ||
// JSDOM has a read only performance field so we have to save/copy it differently | ||
if (originalPerfDescriptor && originalPerfDescriptor.get && !originalPerfDescriptor.set) { | ||
Object.defineProperty(clock, "_" + method, originalPerfDescriptor); | ||
if ( | ||
originalPerfDescriptor && | ||
originalPerfDescriptor.get && | ||
!originalPerfDescriptor.set | ||
) { | ||
Object.defineProperty( | ||
clock, | ||
"_" + method, | ||
originalPerfDescriptor | ||
); | ||
var perfDescriptor = Object.getOwnPropertyDescriptor(clock, method); | ||
var perfDescriptor = Object.getOwnPropertyDescriptor( | ||
clock, | ||
method | ||
); | ||
Object.defineProperty(target, method, perfDescriptor); | ||
@@ -493,3 +581,3 @@ } else { | ||
} else { | ||
target[method] = function () { | ||
target[method] = function() { | ||
return clock[method].apply(clock, arguments); | ||
@@ -557,14 +645,16 @@ }; | ||
var keys = Object.keys || function (obj) { | ||
var ks = []; | ||
var key; | ||
var keys = | ||
Object.keys || | ||
function(obj) { | ||
var ks = []; | ||
var key; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
ks.push(key); | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
ks.push(key); | ||
} | ||
} | ||
} | ||
return ks; | ||
}; | ||
return ks; | ||
}; | ||
@@ -576,3 +666,5 @@ /** | ||
function createClock(start, loopLimit) { | ||
// eslint-disable-next-line no-param-reassign | ||
start = Math.floor(getEpoch(start)); | ||
// eslint-disable-next-line no-param-reassign | ||
loopLimit = loopLimit || 1000; | ||
@@ -583,4 +675,6 @@ var nanos = 0; | ||
if (NativeDate === undefined) { | ||
throw new Error("The global scope doesn't have a `Date` object" | ||
+ " (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)"); | ||
throw new Error( | ||
"The global scope doesn't have a `Date` object" + | ||
" (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)" | ||
); | ||
} | ||
@@ -603,8 +697,13 @@ | ||
var millisSinceStart = clock.now - adjustedSystemTime[0] - start; | ||
var secsSinceStart = Math.floor( millisSinceStart / 1000); | ||
var remainderInNanos = (millisSinceStart - secsSinceStart * 1e3 ) * 1e6 + nanos - adjustedSystemTime[1]; | ||
var secsSinceStart = Math.floor(millisSinceStart / 1000); | ||
var remainderInNanos = | ||
(millisSinceStart - secsSinceStart * 1e3) * 1e6 + | ||
nanos - | ||
adjustedSystemTime[1]; | ||
if (Array.isArray(prev)) { | ||
if ( prev[1] > 1e9 ) { | ||
throw new TypeError("Number of nanoseconds can't exceed a billion"); | ||
if (prev[1] > 1e9) { | ||
throw new TypeError( | ||
"Number of nanoseconds can't exceed a billion" | ||
); | ||
} | ||
@@ -627,3 +726,3 @@ | ||
if (hrtimeBigintPresent) { | ||
hrtime.bigint = function () { | ||
hrtime.bigint = function() { | ||
var parts = hrtime(); | ||
@@ -634,3 +733,6 @@ return BigInt(parts[0]) * BigInt(1e9) + BigInt(parts[1]); // eslint-disable-line | ||
clock.requestIdleCallback = function requestIdleCallback(func, timeout) { | ||
clock.requestIdleCallback = function requestIdleCallback( | ||
func, | ||
timeout | ||
) { | ||
var timeToNextIdlePeriod = 0; | ||
@@ -645,3 +747,6 @@ | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: typeof timeout === "undefined" ? timeToNextIdlePeriod : Math.min(timeout, timeToNextIdlePeriod) | ||
delay: | ||
typeof timeout === "undefined" | ||
? timeToNextIdlePeriod | ||
: Math.min(timeout, timeToNextIdlePeriod) | ||
}); | ||
@@ -680,2 +785,3 @@ | ||
clock.setInterval = function setInterval(func, timeout) { | ||
// eslint-disable-next-line no-param-reassign | ||
timeout = parseInt(timeout, 10); | ||
@@ -709,3 +815,6 @@ return addTimer(clock, { | ||
clock.countTimers = function countTimers() { | ||
return Object.keys(clock.timers || {}).length + (clock.jobs || []).length; | ||
return ( | ||
Object.keys(clock.timers || {}).length + | ||
(clock.jobs || []).length | ||
); | ||
}; | ||
@@ -734,5 +843,8 @@ | ||
* @param {tickValue} {String|Number} number of milliseconds or a human-readable value like "01:11:15" | ||
*/ | ||
*/ | ||
clock.tick = function tick(tickValue) { | ||
var msFloat = typeof tickValue === "number" ? tickValue : parseTime(tickValue); | ||
var msFloat = | ||
typeof tickValue === "number" | ||
? tickValue | ||
: parseTime(tickValue); | ||
var ms = Math.floor(msFloat); | ||
@@ -860,3 +972,7 @@ var remainder = nanoRemainder(msFloat); | ||
throw new Error("Aborting after running " + clock.loopLimit + " timers, assuming an infinite loop!"); | ||
throw new Error( | ||
"Aborting after running " + | ||
clock.loopLimit + | ||
" timers, assuming an infinite loop!" | ||
); | ||
}; | ||
@@ -913,12 +1029,10 @@ | ||
Object | ||
.getOwnPropertyNames(proto) | ||
.forEach(function (name) { | ||
if (name.indexOf("getEntries") === 0) { | ||
// match expected return type for getEntries functions | ||
clock.performance[name] = NOOP_ARRAY; | ||
} else { | ||
clock.performance[name] = NOOP; | ||
} | ||
}); | ||
Object.getOwnPropertyNames(proto).forEach(function(name) { | ||
if (name.indexOf("getEntries") === 0) { | ||
// match expected return type for getEntries functions | ||
clock.performance[name] = NOOP_ARRAY; | ||
} else { | ||
clock.performance[name] = NOOP; | ||
} | ||
}); | ||
} | ||
@@ -928,3 +1042,3 @@ | ||
var hrt = hrtime(); | ||
var millis = (hrt[0] * 1000 + hrt[1] / 1e6); | ||
var millis = hrt[0] * 1000 + hrt[1] / 1e6; | ||
return millis; | ||
@@ -934,3 +1048,2 @@ }; | ||
if (hrtimePresent) { | ||
@@ -952,7 +1065,18 @@ clock.hrtime = hrtime; | ||
*/ | ||
// eslint-disable-next-line complexity | ||
function install(config) { | ||
if ( arguments.length > 1 || config instanceof Date || Array.isArray(config) || typeof config === "number") { | ||
throw new TypeError("lolex.install called with " + String(config) + | ||
" lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex"); | ||
if ( | ||
arguments.length > 1 || | ||
config instanceof Date || | ||
Array.isArray(config) || | ||
typeof config === "number" | ||
) { | ||
throw new TypeError( | ||
"lolex.install called with " + | ||
String(config) + | ||
" lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex" | ||
); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
config = typeof config !== "undefined" ? config : {}; | ||
@@ -966,3 +1090,3 @@ config.shouldAdvanceTime = config.shouldAdvanceTime || false; | ||
clock.uninstall = function () { | ||
clock.uninstall = function() { | ||
return uninstall(clock, target, config); | ||
@@ -975,3 +1099,3 @@ }; | ||
// do not fake nextTick by default - GitHub#126 | ||
clock.methods = keys(timers).filter(function (key) { | ||
clock.methods = keys(timers).filter(function(key) { | ||
return key !== "nextTick" && key !== "queueMicrotask"; | ||
@@ -983,15 +1107,29 @@ }); | ||
if (clock.methods[i] === "hrtime") { | ||
if (target.process && typeof target.process.hrtime === "function") { | ||
if ( | ||
target.process && | ||
typeof target.process.hrtime === "function" | ||
) { | ||
hijackMethod(target.process, clock.methods[i], clock); | ||
} | ||
} else if (clock.methods[i] === "nextTick") { | ||
if (target.process && typeof target.process.nextTick === "function") { | ||
if ( | ||
target.process && | ||
typeof target.process.nextTick === "function" | ||
) { | ||
hijackMethod(target.process, clock.methods[i], clock); | ||
} | ||
} else { | ||
if (clock.methods[i] === "setInterval" && config.shouldAdvanceTime === true) { | ||
var intervalTick = doIntervalTick.bind(null, clock, config.advanceTimeDelta); | ||
if ( | ||
clock.methods[i] === "setInterval" && | ||
config.shouldAdvanceTime === true | ||
) { | ||
var intervalTick = doIntervalTick.bind( | ||
null, | ||
clock, | ||
config.advanceTimeDelta | ||
); | ||
var intervalId = target[clock.methods[i]]( | ||
intervalTick, | ||
config.advanceTimeDelta); | ||
config.advanceTimeDelta | ||
); | ||
clock.attachedInterval = intervalId; | ||
@@ -1014,3 +1152,5 @@ } | ||
var defaultImplementation = withGlobal(global || window); | ||
var defaultImplementation = withGlobal( | ||
typeof global !== "undefined" ? global : window | ||
); | ||
@@ -1022,4 +1162,3 @@ exports.timers = defaultImplementation.timers; | ||
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
},{}]},{},[1])(1) | ||
}); |
{ | ||
"name": "lolex", | ||
"description": "Fake JavaScript timers", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"homepage": "http://github.com/sinonjs/lolex", | ||
@@ -19,6 +19,6 @@ "author": "Christian Johansen", | ||
"test-node": "mocha test/ integration-test/ -R dot --check-leaks", | ||
"test-headless": "mochify", | ||
"test-cloud": "mochify --wd", | ||
"test-headless": "mochify --no-detect-globals", | ||
"test-cloud": "mochify --wd --no-detect-globals", | ||
"test": "npm run lint && npm run test-node && npm run test-headless", | ||
"bundle": "browserify -s lolex -o lolex.js src/lolex-src.js", | ||
"bundle": "browserify --no-detect-globals -s lolex -o lolex.js src/lolex-src.js", | ||
"prepublishOnly": "npm run bundle", | ||
@@ -40,5 +40,7 @@ "preversion": "./scripts/preversion.sh", | ||
"eslint": "6.5.1", | ||
"eslint-config-sinon": "1.0.3", | ||
"eslint-config-prettier": "6.4.0", | ||
"eslint-config-sinon": "3.0.1", | ||
"eslint-plugin-ie11": "1.0.0", | ||
"eslint-plugin-mocha": "6.1.1", | ||
"eslint-plugin-prettier": "3.1.1", | ||
"husky": "3.0.8", | ||
@@ -48,4 +50,5 @@ "jsdom": "15.1.1", | ||
"mocha": "6.2.1", | ||
"mochify": "6.5.0", | ||
"npm-run-all": "4.1.5" | ||
"mochify": "6.6.0", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "1.18.2" | ||
}, | ||
@@ -52,0 +55,0 @@ "eslintConfig": { |
"use strict"; | ||
// eslint-disable-next-line complexity | ||
function withGlobal(_global) { | ||
@@ -7,30 +8,40 @@ var userAgent = _global.navigator && _global.navigator.userAgent; | ||
var maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint | ||
var NOOP = function () { return undefined; }; | ||
var NOOP_ARRAY = function () { return []; }; | ||
var NOOP = function() { | ||
return undefined; | ||
}; | ||
var NOOP_ARRAY = function() { | ||
return []; | ||
}; | ||
var timeoutResult = _global.setTimeout(NOOP, 0); | ||
var addTimerReturnsObject = typeof timeoutResult === "object"; | ||
var hrtimePresent = (_global.process && typeof _global.process.hrtime === "function"); | ||
var hrtimeBigintPresent = (hrtimePresent && typeof _global.process.hrtime.bigint === "function"); | ||
var nextTickPresent = (_global.process && typeof _global.process.nextTick === "function"); | ||
var performancePresent = (_global.performance && typeof _global.performance.now === "function"); | ||
var hasPerformancePrototype = (_global.Performance && (typeof _global.Performance).match(/^(function|object)$/)); | ||
var queueMicrotaskPresent = (_global.hasOwnProperty("queueMicrotask")); | ||
var requestAnimationFramePresent = ( | ||
_global.requestAnimationFrame && typeof _global.requestAnimationFrame === "function" | ||
); | ||
var cancelAnimationFramePresent = ( | ||
_global.cancelAnimationFrame && typeof _global.cancelAnimationFrame === "function" | ||
); | ||
var requestIdleCallbackPresent = ( | ||
_global.requestIdleCallback && typeof _global.requestIdleCallback === "function" | ||
); | ||
var cancelIdleCallbackPresent = ( | ||
_global.cancelIdleCallback && typeof _global.cancelIdleCallback === "function" | ||
); | ||
var setImmediatePresent = ( | ||
_global.setImmediate && typeof _global.setImmediate === "function" | ||
); | ||
var hrtimePresent = | ||
_global.process && typeof _global.process.hrtime === "function"; | ||
var hrtimeBigintPresent = | ||
hrtimePresent && typeof _global.process.hrtime.bigint === "function"; | ||
var nextTickPresent = | ||
_global.process && typeof _global.process.nextTick === "function"; | ||
var performancePresent = | ||
_global.performance && typeof _global.performance.now === "function"; | ||
var hasPerformancePrototype = | ||
_global.Performance && | ||
(typeof _global.Performance).match(/^(function|object)$/); | ||
var queueMicrotaskPresent = _global.hasOwnProperty("queueMicrotask"); | ||
var requestAnimationFramePresent = | ||
_global.requestAnimationFrame && | ||
typeof _global.requestAnimationFrame === "function"; | ||
var cancelAnimationFramePresent = | ||
_global.cancelAnimationFrame && | ||
typeof _global.cancelAnimationFrame === "function"; | ||
var requestIdleCallbackPresent = | ||
_global.requestIdleCallback && | ||
typeof _global.requestIdleCallback === "function"; | ||
var cancelIdleCallbackPresent = | ||
_global.cancelIdleCallback && | ||
typeof _global.cancelIdleCallback === "function"; | ||
var setImmediatePresent = | ||
_global.setImmediate && typeof _global.setImmediate === "function"; | ||
// Make properties writable in IE, as per | ||
// http://www.adequatelygood.com/Replacing-setTimeout-Globally.html | ||
/* eslint-disable no-self-assign */ | ||
if (isRunningInIE) { | ||
@@ -50,2 +61,3 @@ _global.setTimeout = _global.setTimeout; | ||
} | ||
/* eslint-enable no-self-assign */ | ||
@@ -86,3 +98,5 @@ _global.clearTimeout(timeoutResult); | ||
if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) { | ||
throw new Error("tick only understands numbers, 'm:s' and 'h:m:s'. Each part must be two digits"); | ||
throw new Error( | ||
"tick only understands numbers, 'm:s' and 'h:m:s'. Each part must be two digits" | ||
); | ||
} | ||
@@ -97,3 +111,3 @@ | ||
ms += parsed * Math.pow(60, (l - i - 1)); | ||
ms += parsed * Math.pow(60, l - i - 1); | ||
} | ||
@@ -125,5 +139,11 @@ | ||
function getEpoch(epoch) { | ||
if (!epoch) { return 0; } | ||
if (typeof epoch.getTime === "function") { return epoch.getTime(); } | ||
if (typeof epoch === "number") { return epoch; } | ||
if (!epoch) { | ||
return 0; | ||
} | ||
if (typeof epoch.getTime === "function") { | ||
return epoch.getTime(); | ||
} | ||
if (typeof epoch === "number") { | ||
return epoch; | ||
} | ||
throw new TypeError("now should be milliseconds since UNIX epoch"); | ||
@@ -200,5 +220,20 @@ } | ||
case 6: | ||
return new NativeDate(year, month, date, hour, minute, second); | ||
return new NativeDate( | ||
year, | ||
month, | ||
date, | ||
hour, | ||
minute, | ||
second | ||
); | ||
default: | ||
return new NativeDate(year, month, date, hour, minute, second, ms); | ||
return new NativeDate( | ||
year, | ||
month, | ||
date, | ||
hour, | ||
minute, | ||
second, | ||
ms | ||
); | ||
} | ||
@@ -227,3 +262,7 @@ } | ||
if (clock.loopLimit && i > clock.loopLimit) { | ||
throw new Error("Aborting after running " + clock.loopLimit + " timers, assuming an infinite loop!"); | ||
throw new Error( | ||
"Aborting after running " + | ||
clock.loopLimit + | ||
" timers, assuming an infinite loop!" | ||
); | ||
} | ||
@@ -265,3 +304,4 @@ } | ||
timer.createdAt = clock.now; | ||
timer.callAt = clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0)); | ||
timer.callAt = | ||
clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0)); | ||
@@ -273,5 +313,11 @@ clock.timers[timer.id] = timer; | ||
id: timer.id, | ||
ref: function () { return res; }, | ||
unref: function () { return res; }, | ||
refresh: function () { return res; } | ||
ref: function() { | ||
return res; | ||
}, | ||
unref: function() { | ||
return res; | ||
}, | ||
refresh: function() { | ||
return res; | ||
} | ||
}; | ||
@@ -330,3 +376,6 @@ return res; | ||
if (isInRange && (!timer || compareTimers(timer, timers[id]) === 1)) { | ||
if ( | ||
isInRange && | ||
(!timer || compareTimers(timer, timers[id]) === 1) | ||
) { | ||
timer = timers[id]; | ||
@@ -400,16 +449,25 @@ } | ||
// its .id field is the actual timer id. | ||
if (typeof timerId === "object") { | ||
timerId = timerId.id; | ||
} | ||
var id = typeof timerId === "object" ? timerId.id : timerId; | ||
if (clock.timers.hasOwnProperty(timerId)) { | ||
if (clock.timers.hasOwnProperty(id)) { | ||
// check that the ID matches a timer of the correct type | ||
var timer = clock.timers[timerId]; | ||
var timer = clock.timers[id]; | ||
if (timer.type === ttype) { | ||
delete clock.timers[timerId]; | ||
delete clock.timers[id]; | ||
} else { | ||
var clear = ttype === "AnimationFrame" ? "cancelAnimationFrame" : "clear" + ttype; | ||
var schedule = timer.type === "AnimationFrame" ? "requestAnimationFrame" : "set" + timer.type; | ||
throw new Error("Cannot clear timer: timer created with " + schedule | ||
+ "() but cleared with " + clear + "()"); | ||
var clear = | ||
ttype === "AnimationFrame" | ||
? "cancelAnimationFrame" | ||
: "clear" + ttype; | ||
var schedule = | ||
timer.type === "AnimationFrame" | ||
? "requestAnimationFrame" | ||
: "set" + timer.type; | ||
throw new Error( | ||
"Cannot clear timer: timer created with " + | ||
schedule + | ||
"() but cleared with " + | ||
clear + | ||
"()" | ||
); | ||
} | ||
@@ -420,5 +478,3 @@ } | ||
function uninstall(clock, target, config) { | ||
var method, | ||
i, | ||
l; | ||
var method, i, l; | ||
var installedHrTime = "_hrtime"; | ||
@@ -434,5 +490,16 @@ var installedNextTick = "_nextTick"; | ||
} else if (method === "performance") { | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor(clock, "_" + method); | ||
if (originalPerfDescriptor && originalPerfDescriptor.get && !originalPerfDescriptor.set) { | ||
Object.defineProperty(target, method, originalPerfDescriptor); | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor( | ||
clock, | ||
"_" + method | ||
); | ||
if ( | ||
originalPerfDescriptor && | ||
originalPerfDescriptor.get && | ||
!originalPerfDescriptor.set | ||
) { | ||
Object.defineProperty( | ||
target, | ||
method, | ||
originalPerfDescriptor | ||
); | ||
} else if (originalPerfDescriptor.configurable) { | ||
@@ -444,3 +511,6 @@ target[method] = clock["_" + method]; | ||
target[method] = clock["_" + method]; | ||
if (method === "clearInterval" && config.shouldAdvanceTime === true) { | ||
if ( | ||
method === "clearInterval" && | ||
config.shouldAdvanceTime === true | ||
) { | ||
target[method](clock.attachedInterval); | ||
@@ -451,3 +521,5 @@ } | ||
delete target[method]; | ||
} catch (ignore) { /* eslint no-empty: "off" */ } | ||
} catch (ignore) { | ||
/* eslint no-empty: "off" */ | ||
} | ||
} | ||
@@ -471,3 +543,6 @@ } | ||
var prop; | ||
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(target, method); | ||
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call( | ||
target, | ||
method | ||
); | ||
clock["_" + method] = target[method]; | ||
@@ -479,8 +554,22 @@ | ||
} else if (method === "performance") { | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor(target, method); | ||
var originalPerfDescriptor = Object.getOwnPropertyDescriptor( | ||
target, | ||
method | ||
); | ||
// JSDOM has a read only performance field so we have to save/copy it differently | ||
if (originalPerfDescriptor && originalPerfDescriptor.get && !originalPerfDescriptor.set) { | ||
Object.defineProperty(clock, "_" + method, originalPerfDescriptor); | ||
if ( | ||
originalPerfDescriptor && | ||
originalPerfDescriptor.get && | ||
!originalPerfDescriptor.set | ||
) { | ||
Object.defineProperty( | ||
clock, | ||
"_" + method, | ||
originalPerfDescriptor | ||
); | ||
var perfDescriptor = Object.getOwnPropertyDescriptor(clock, method); | ||
var perfDescriptor = Object.getOwnPropertyDescriptor( | ||
clock, | ||
method | ||
); | ||
Object.defineProperty(target, method, perfDescriptor); | ||
@@ -491,3 +580,3 @@ } else { | ||
} else { | ||
target[method] = function () { | ||
target[method] = function() { | ||
return clock[method].apply(clock, arguments); | ||
@@ -555,14 +644,16 @@ }; | ||
var keys = Object.keys || function (obj) { | ||
var ks = []; | ||
var key; | ||
var keys = | ||
Object.keys || | ||
function(obj) { | ||
var ks = []; | ||
var key; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
ks.push(key); | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
ks.push(key); | ||
} | ||
} | ||
} | ||
return ks; | ||
}; | ||
return ks; | ||
}; | ||
@@ -574,3 +665,5 @@ /** | ||
function createClock(start, loopLimit) { | ||
// eslint-disable-next-line no-param-reassign | ||
start = Math.floor(getEpoch(start)); | ||
// eslint-disable-next-line no-param-reassign | ||
loopLimit = loopLimit || 1000; | ||
@@ -581,4 +674,6 @@ var nanos = 0; | ||
if (NativeDate === undefined) { | ||
throw new Error("The global scope doesn't have a `Date` object" | ||
+ " (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)"); | ||
throw new Error( | ||
"The global scope doesn't have a `Date` object" + | ||
" (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)" | ||
); | ||
} | ||
@@ -601,8 +696,13 @@ | ||
var millisSinceStart = clock.now - adjustedSystemTime[0] - start; | ||
var secsSinceStart = Math.floor( millisSinceStart / 1000); | ||
var remainderInNanos = (millisSinceStart - secsSinceStart * 1e3 ) * 1e6 + nanos - adjustedSystemTime[1]; | ||
var secsSinceStart = Math.floor(millisSinceStart / 1000); | ||
var remainderInNanos = | ||
(millisSinceStart - secsSinceStart * 1e3) * 1e6 + | ||
nanos - | ||
adjustedSystemTime[1]; | ||
if (Array.isArray(prev)) { | ||
if ( prev[1] > 1e9 ) { | ||
throw new TypeError("Number of nanoseconds can't exceed a billion"); | ||
if (prev[1] > 1e9) { | ||
throw new TypeError( | ||
"Number of nanoseconds can't exceed a billion" | ||
); | ||
} | ||
@@ -625,3 +725,3 @@ | ||
if (hrtimeBigintPresent) { | ||
hrtime.bigint = function () { | ||
hrtime.bigint = function() { | ||
var parts = hrtime(); | ||
@@ -632,3 +732,6 @@ return BigInt(parts[0]) * BigInt(1e9) + BigInt(parts[1]); // eslint-disable-line | ||
clock.requestIdleCallback = function requestIdleCallback(func, timeout) { | ||
clock.requestIdleCallback = function requestIdleCallback( | ||
func, | ||
timeout | ||
) { | ||
var timeToNextIdlePeriod = 0; | ||
@@ -643,3 +746,6 @@ | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: typeof timeout === "undefined" ? timeToNextIdlePeriod : Math.min(timeout, timeToNextIdlePeriod) | ||
delay: | ||
typeof timeout === "undefined" | ||
? timeToNextIdlePeriod | ||
: Math.min(timeout, timeToNextIdlePeriod) | ||
}); | ||
@@ -678,2 +784,3 @@ | ||
clock.setInterval = function setInterval(func, timeout) { | ||
// eslint-disable-next-line no-param-reassign | ||
timeout = parseInt(timeout, 10); | ||
@@ -707,3 +814,6 @@ return addTimer(clock, { | ||
clock.countTimers = function countTimers() { | ||
return Object.keys(clock.timers || {}).length + (clock.jobs || []).length; | ||
return ( | ||
Object.keys(clock.timers || {}).length + | ||
(clock.jobs || []).length | ||
); | ||
}; | ||
@@ -732,5 +842,8 @@ | ||
* @param {tickValue} {String|Number} number of milliseconds or a human-readable value like "01:11:15" | ||
*/ | ||
*/ | ||
clock.tick = function tick(tickValue) { | ||
var msFloat = typeof tickValue === "number" ? tickValue : parseTime(tickValue); | ||
var msFloat = | ||
typeof tickValue === "number" | ||
? tickValue | ||
: parseTime(tickValue); | ||
var ms = Math.floor(msFloat); | ||
@@ -858,3 +971,7 @@ var remainder = nanoRemainder(msFloat); | ||
throw new Error("Aborting after running " + clock.loopLimit + " timers, assuming an infinite loop!"); | ||
throw new Error( | ||
"Aborting after running " + | ||
clock.loopLimit + | ||
" timers, assuming an infinite loop!" | ||
); | ||
}; | ||
@@ -911,12 +1028,10 @@ | ||
Object | ||
.getOwnPropertyNames(proto) | ||
.forEach(function (name) { | ||
if (name.indexOf("getEntries") === 0) { | ||
// match expected return type for getEntries functions | ||
clock.performance[name] = NOOP_ARRAY; | ||
} else { | ||
clock.performance[name] = NOOP; | ||
} | ||
}); | ||
Object.getOwnPropertyNames(proto).forEach(function(name) { | ||
if (name.indexOf("getEntries") === 0) { | ||
// match expected return type for getEntries functions | ||
clock.performance[name] = NOOP_ARRAY; | ||
} else { | ||
clock.performance[name] = NOOP; | ||
} | ||
}); | ||
} | ||
@@ -926,3 +1041,3 @@ | ||
var hrt = hrtime(); | ||
var millis = (hrt[0] * 1000 + hrt[1] / 1e6); | ||
var millis = hrt[0] * 1000 + hrt[1] / 1e6; | ||
return millis; | ||
@@ -932,3 +1047,2 @@ }; | ||
if (hrtimePresent) { | ||
@@ -950,7 +1064,18 @@ clock.hrtime = hrtime; | ||
*/ | ||
// eslint-disable-next-line complexity | ||
function install(config) { | ||
if ( arguments.length > 1 || config instanceof Date || Array.isArray(config) || typeof config === "number") { | ||
throw new TypeError("lolex.install called with " + String(config) + | ||
" lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex"); | ||
if ( | ||
arguments.length > 1 || | ||
config instanceof Date || | ||
Array.isArray(config) || | ||
typeof config === "number" | ||
) { | ||
throw new TypeError( | ||
"lolex.install called with " + | ||
String(config) + | ||
" lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex" | ||
); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
config = typeof config !== "undefined" ? config : {}; | ||
@@ -964,3 +1089,3 @@ config.shouldAdvanceTime = config.shouldAdvanceTime || false; | ||
clock.uninstall = function () { | ||
clock.uninstall = function() { | ||
return uninstall(clock, target, config); | ||
@@ -973,3 +1098,3 @@ }; | ||
// do not fake nextTick by default - GitHub#126 | ||
clock.methods = keys(timers).filter(function (key) { | ||
clock.methods = keys(timers).filter(function(key) { | ||
return key !== "nextTick" && key !== "queueMicrotask"; | ||
@@ -981,15 +1106,29 @@ }); | ||
if (clock.methods[i] === "hrtime") { | ||
if (target.process && typeof target.process.hrtime === "function") { | ||
if ( | ||
target.process && | ||
typeof target.process.hrtime === "function" | ||
) { | ||
hijackMethod(target.process, clock.methods[i], clock); | ||
} | ||
} else if (clock.methods[i] === "nextTick") { | ||
if (target.process && typeof target.process.nextTick === "function") { | ||
if ( | ||
target.process && | ||
typeof target.process.nextTick === "function" | ||
) { | ||
hijackMethod(target.process, clock.methods[i], clock); | ||
} | ||
} else { | ||
if (clock.methods[i] === "setInterval" && config.shouldAdvanceTime === true) { | ||
var intervalTick = doIntervalTick.bind(null, clock, config.advanceTimeDelta); | ||
if ( | ||
clock.methods[i] === "setInterval" && | ||
config.shouldAdvanceTime === true | ||
) { | ||
var intervalTick = doIntervalTick.bind( | ||
null, | ||
clock, | ||
config.advanceTimeDelta | ||
); | ||
var intervalId = target[clock.methods[i]]( | ||
intervalTick, | ||
config.advanceTimeDelta); | ||
config.advanceTimeDelta | ||
); | ||
clock.attachedInterval = intervalId; | ||
@@ -1012,3 +1151,5 @@ } | ||
var defaultImplementation = withGlobal(global || window); | ||
var defaultImplementation = withGlobal( | ||
typeof global !== "undefined" ? global : window | ||
); | ||
@@ -1015,0 +1156,0 @@ exports.timers = defaultImplementation.timers; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98424
1940
15
2