chronokinesis
Advanced tools
Comparing version 1.3.2 to 2.0.0
@@ -1,97 +0,125 @@ | ||
var chronokinesis = (function () { | ||
'use strict'; | ||
var chronokinesis = (function (exports) { | ||
'use strict'; | ||
/** | ||
* Borrowed from Time keeper - EEasy testing of time-dependent code. | ||
* | ||
* Veselin Todorov <hi@vesln.com> | ||
* MIT License. | ||
*/ | ||
/** | ||
* Inspired by Time keeper - EEasy testing of time-dependent code. | ||
* | ||
* Veselin Todorov <hi@vesln.com> | ||
* MIT License. | ||
*/ | ||
var NativeDate = Date; | ||
var instance = {}; | ||
var NativeDate = Date; | ||
var freeze = null; | ||
var travel = null; | ||
var started = null; | ||
var freezedAt = null; | ||
var traveledTo = null; | ||
var started = null; | ||
instance.freeze = function () { | ||
useFakeDate(); | ||
freeze = instantiate(Date, arguments); | ||
return freeze; | ||
}; | ||
function FakeDate() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
instance.defrost = function () { | ||
freeze = null; | ||
}; | ||
var length = args.length; | ||
instance.travel = function () { | ||
useFakeDate(); | ||
if (!length && freezedAt) return new NativeDate(freezedAt); | ||
if (!length && traveledTo) return new NativeDate(time()); | ||
var travelToDate = instantiate(Date, arguments); | ||
return instantiate(NativeDate, args); | ||
} | ||
travel = travelToDate.getTime(); | ||
started = NativeDate.now(); | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
if (freeze) { | ||
freeze = travelToDate; | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.now = function () { | ||
if (freezedAt) return freezedAt.getTime(); | ||
return time(); | ||
}; | ||
var chronokinesis = { | ||
freeze: freeze, | ||
defrost: defrost, | ||
travel: travel, | ||
reset: reset, | ||
isKeepingTime: isKeepingTime | ||
}; | ||
function freeze() { | ||
useFakeDate(); | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
freezedAt = instantiate(Date, args); | ||
return freezedAt; | ||
} | ||
return travelToDate; | ||
}; | ||
function defrost() { | ||
freezedAt = null; | ||
} | ||
instance.reset = function (callback) { | ||
useNativeDate(); | ||
freeze = null; | ||
started = null; | ||
travel = null; | ||
return callback && callback(); | ||
}; | ||
function travel() { | ||
useFakeDate(); | ||
instance.isKeepingTime = function () { | ||
return Date === FakeDate; | ||
}; | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
function useFakeDate() { | ||
Date = FakeDate; // eslint-disable-line no-global-assign | ||
} | ||
var travelToDate = instantiate(Date, args); | ||
function useNativeDate() { | ||
Date = NativeDate; // eslint-disable-line no-global-assign | ||
} | ||
traveledTo = travelToDate.getTime(); | ||
started = NativeDate.now(); | ||
function FakeDate() { | ||
var length = arguments.length; | ||
if (freezedAt) { | ||
freezedAt = travelToDate; | ||
} | ||
if (!length && freeze) return new NativeDate(freeze); | ||
if (!length && travel) return new NativeDate(time()); | ||
return travelToDate; | ||
} | ||
var date = instantiate(NativeDate, arguments); | ||
function reset() { | ||
useNativeDate(); | ||
freezedAt = null; | ||
started = null; | ||
traveledTo = null; | ||
} | ||
return date; | ||
} | ||
function isKeepingTime() { | ||
return Date === FakeDate; | ||
} | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
} | ||
function useFakeDate() { | ||
Date = FakeDate; // eslint-disable-line no-global-assign | ||
} | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
function useNativeDate() { | ||
Date = NativeDate; // eslint-disable-line no-global-assign | ||
} | ||
FakeDate.prototype = NativeDate.prototype; | ||
function time() { | ||
return traveledTo + (NativeDate.now() - started); | ||
} | ||
FakeDate.now = function () { | ||
if (freeze) return freeze.getTime(); | ||
return time(); | ||
}; | ||
function instantiate(type, args) { | ||
var ctorArgs = args.slice(); | ||
ctorArgs.unshift(null); | ||
return new (Function.prototype.bind.apply(type, ctorArgs))(); | ||
} | ||
var chronokinesis_1 = chronokinesis.freeze; | ||
var chronokinesis_2 = chronokinesis.defrost; | ||
var chronokinesis_3 = chronokinesis.travel; | ||
var chronokinesis_4 = chronokinesis.reset; | ||
var chronokinesis_5 = chronokinesis.isKeepingTime; | ||
function instantiate(type, args) { | ||
var ctorArgs = Array.prototype.slice.call(args); | ||
return new (Function.prototype.bind.apply(type, [null].concat(ctorArgs)))(); | ||
} | ||
exports.default = chronokinesis; | ||
exports.freeze = chronokinesis_1; | ||
exports.defrost = chronokinesis_2; | ||
exports.travel = chronokinesis_3; | ||
exports.reset = chronokinesis_4; | ||
exports.isKeepingTime = chronokinesis_5; | ||
var index = instance; | ||
return exports; | ||
return index; | ||
}()); | ||
}({})); |
/** | ||
* Borrowed from Time keeper - EEasy testing of time-dependent code. | ||
* Inspired by Time keeper - EEasy testing of time-dependent code. | ||
* | ||
@@ -9,44 +9,82 @@ * Veselin Todorov <hi@vesln.com> | ||
var NativeDate = Date; | ||
var instance = {}; | ||
var freeze = null; | ||
var travel = null; | ||
var freezedAt = null; | ||
var traveledTo = null; | ||
var started = null; | ||
instance.freeze = function () { | ||
useFakeDate(); | ||
freeze = instantiate(Date, arguments); | ||
return freeze; | ||
function FakeDate() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var length = args.length; | ||
if (!length && freezedAt) return new NativeDate(freezedAt); | ||
if (!length && traveledTo) return new NativeDate(time()); | ||
return instantiate(NativeDate, args); | ||
} | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.now = function () { | ||
if (freezedAt) return freezedAt.getTime(); | ||
return time(); | ||
}; | ||
instance.defrost = function () { | ||
freeze = null; | ||
var chronokinesis = { | ||
freeze: freeze, | ||
defrost: defrost, | ||
travel: travel, | ||
reset: reset, | ||
isKeepingTime: isKeepingTime | ||
}; | ||
instance.travel = function () { | ||
function freeze() { | ||
useFakeDate(); | ||
var travelToDate = instantiate(Date, arguments); | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
travel = travelToDate.getTime(); | ||
freezedAt = instantiate(Date, args); | ||
return freezedAt; | ||
} | ||
function defrost() { | ||
freezedAt = null; | ||
} | ||
function travel() { | ||
useFakeDate(); | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
var travelToDate = instantiate(Date, args); | ||
traveledTo = travelToDate.getTime(); | ||
started = NativeDate.now(); | ||
if (freeze) { | ||
freeze = travelToDate; | ||
if (freezedAt) { | ||
freezedAt = travelToDate; | ||
} | ||
return travelToDate; | ||
}; | ||
} | ||
instance.reset = function (callback) { | ||
function reset() { | ||
useNativeDate(); | ||
freeze = null; | ||
freezedAt = null; | ||
started = null; | ||
travel = null; | ||
return callback && callback(); | ||
}; | ||
traveledTo = null; | ||
} | ||
instance.isKeepingTime = function () { | ||
function isKeepingTime() { | ||
return Date === FakeDate; | ||
}; | ||
} | ||
@@ -61,34 +99,18 @@ function useFakeDate() { | ||
function FakeDate() { | ||
var length = arguments.length; | ||
if (!length && freeze) return new NativeDate(freeze); | ||
if (!length && travel) return new NativeDate(time()); | ||
var date = instantiate(NativeDate, arguments); | ||
return date; | ||
} | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
return traveledTo + (NativeDate.now() - started); | ||
} | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.now = function () { | ||
if (freeze) return freeze.getTime(); | ||
return time(); | ||
}; | ||
function instantiate(type, args) { | ||
var ctorArgs = Array.prototype.slice.call(args); | ||
return new (Function.prototype.bind.apply(type, [null].concat(ctorArgs)))(); | ||
var ctorArgs = args.slice(); | ||
ctorArgs.unshift(null); | ||
return new (Function.prototype.bind.apply(type, ctorArgs))(); | ||
} | ||
var chronokinesis_1 = chronokinesis.freeze; | ||
var chronokinesis_2 = chronokinesis.defrost; | ||
var chronokinesis_3 = chronokinesis.travel; | ||
var chronokinesis_4 = chronokinesis.reset; | ||
var chronokinesis_5 = chronokinesis.isKeepingTime; | ||
var index = instance; | ||
export default index; | ||
export default chronokinesis; | ||
export { chronokinesis_1 as freeze, chronokinesis_2 as defrost, chronokinesis_3 as travel, chronokinesis_4 as reset, chronokinesis_5 as isKeepingTime }; |
102
index.js
/** | ||
* Borrowed from Time keeper - EEasy testing of time-dependent code. | ||
* Inspired by Time keeper - EEasy testing of time-dependent code. | ||
* | ||
@@ -7,48 +7,72 @@ * Veselin Todorov <hi@vesln.com> | ||
*/ | ||
'use strict'; | ||
const NativeDate = Date; | ||
const instance = {}; | ||
let freeze = null; | ||
let travel = null; | ||
let freezedAt = null; | ||
let traveledTo = null; | ||
let started = null; | ||
instance.freeze = function() { | ||
useFakeDate(); | ||
freeze = instantiate(Date, arguments); | ||
return freeze; | ||
function FakeDate(...args) { | ||
const length = args.length; | ||
if (!length && freezedAt) return new NativeDate(freezedAt); | ||
if (!length && traveledTo) return new NativeDate(time()); | ||
return instantiate(NativeDate, args); | ||
} | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.now = function() { | ||
if (freezedAt) return freezedAt.getTime(); | ||
return time(); | ||
}; | ||
instance.defrost = function() { | ||
freeze = null; | ||
module.exports = { | ||
freeze, | ||
defrost, | ||
travel, | ||
reset, | ||
isKeepingTime, | ||
}; | ||
instance.travel = function() { | ||
function freeze(...args) { | ||
useFakeDate(); | ||
freezedAt = instantiate(Date, args); | ||
return freezedAt; | ||
} | ||
const travelToDate = instantiate(Date, arguments); | ||
function defrost() { | ||
freezedAt = null; | ||
} | ||
travel = travelToDate.getTime(); | ||
function travel(...args) { | ||
useFakeDate(); | ||
const travelToDate = instantiate(Date, args); | ||
traveledTo = travelToDate.getTime(); | ||
started = NativeDate.now(); | ||
if (freeze) { | ||
freeze = travelToDate; | ||
if (freezedAt) { | ||
freezedAt = travelToDate; | ||
} | ||
return travelToDate; | ||
}; | ||
} | ||
instance.reset = function(callback) { | ||
function reset() { | ||
useNativeDate(); | ||
freeze = null; | ||
freezedAt = null; | ||
started = null; | ||
travel = null; | ||
return callback && callback(); | ||
}; | ||
traveledTo = null; | ||
} | ||
instance.isKeepingTime = function() { | ||
function isKeepingTime() { | ||
return Date === FakeDate; | ||
}; | ||
} | ||
@@ -63,32 +87,10 @@ function useFakeDate() { | ||
function FakeDate() { | ||
const length = arguments.length; | ||
if (!length && freeze) return new NativeDate(freeze); | ||
if (!length && travel) return new NativeDate(time()); | ||
const date = instantiate(NativeDate, arguments); | ||
return date; | ||
} | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
return traveledTo + (NativeDate.now() - started); | ||
} | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.now = function() { | ||
if (freeze) return freeze.getTime(); | ||
return time(); | ||
}; | ||
function instantiate(type, args) { | ||
const ctorArgs = Array.prototype.slice.call(args); | ||
return new (Function.prototype.bind.apply(type, [null].concat(ctorArgs)))(); | ||
const ctorArgs = args.slice(); | ||
ctorArgs.unshift(null); | ||
return new (Function.prototype.bind.apply(type, ctorArgs))(); | ||
} | ||
module.exports = instance; |
{ | ||
"name": "chronokinesis", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"description": "Module for testing time-dependent code", | ||
"author": "Pal Edman <pal.edman@outlook.com>", | ||
"license": "BSD-3-Clause", | ||
"author": { | ||
"name": "Pål Edman", | ||
"url": "https://github.com/paed01" | ||
}, | ||
"license": "MIT", | ||
"main": "index.js", | ||
@@ -15,3 +18,3 @@ "browser": "dist/chronokinesis.js", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=8" | ||
}, | ||
@@ -21,3 +24,3 @@ "scripts": { | ||
"test": "make test-cov-lcov", | ||
"prepublish": "npm run toc && rollup -c" | ||
"prepare": "npm run toc && rollup -c" | ||
}, | ||
@@ -28,3 +31,3 @@ "files": [ | ||
"dist/index.es.js", | ||
"LICENCE", | ||
"LICENSE", | ||
"README.md" | ||
@@ -47,14 +50,14 @@ ], | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
"code": "^4.0.0", | ||
"coveralls": "^2.11.15", | ||
"lab": "^12.1.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"code": "^5.2.0", | ||
"coveralls": "^3.0.2", | ||
"lab": "^15.5.0", | ||
"lodash.clonedeep": "^4.5.0", | ||
"markdown-toc": "^1.1.0", | ||
"moment": "^2.17.1", | ||
"rollup": "^0.41.4", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-commonjs": "^7.0.0" | ||
"markdown-toc": "^1.2.0", | ||
"moment": "^2.22.2", | ||
"rollup": "^0.64.1", | ||
"rollup-plugin-babel": "^3.0.7", | ||
"rollup-plugin-commonjs": "^9.1.5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
13248
254
1