timekeeper
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -9,6 +9,9 @@ /** | ||
/** | ||
* Dependencies. | ||
* Native Date constructor reference. | ||
* | ||
* @type {Object} | ||
*/ | ||
var util = require('util'); | ||
var NativeDate = Date; | ||
/** | ||
@@ -43,17 +46,2 @@ * `TimeKeeper`. | ||
/** | ||
* Helper function that creates a new instance with | ||
* supplied arguments. | ||
* | ||
* @param {Function} Constructor. | ||
* @param {Object} Arguments. | ||
* @returns {Object} | ||
*/ | ||
function create(ctor, args) { | ||
var a = []; | ||
args = Array.prototype.slice.call(args); | ||
for (var i = 0; i < args.length; i++) a[i] = 'args[' + i + ']'; | ||
return eval('new ctor(' + a.join() + ')'); | ||
}; | ||
/** | ||
* Return the elapsed time. | ||
@@ -63,5 +51,5 @@ * | ||
*/ | ||
function ms() { | ||
return travel + (FakeDate.super_.now() - started); | ||
}; | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
} | ||
@@ -71,21 +59,46 @@ /** | ||
*/ | ||
function FakeDate() { | ||
var args = arguments.length === 0; | ||
if (args && freeze) return freeze; | ||
if (args && travel) return new FakeDate.super_(ms()); | ||
return new create(FakeDate.super_, arguments); | ||
}; | ||
function FakeDate(Y, M, D, h, m, s, ms) { | ||
var length = arguments.length; | ||
/** | ||
* `FakeDate` inheris from `Date`. | ||
*/ | ||
util.inherits(FakeDate, Date); | ||
if (this instanceof NativeDate) { | ||
/** | ||
* Attach `Date` class methods to `FakeDate` except `Date#now`. | ||
*/ | ||
['parse', 'UTC'].forEach(function(method) { | ||
FakeDate[method] = Date[method]; | ||
}); | ||
if (!length && freeze) return freeze; | ||
if (!length && travel) return new NativeDate(time()); | ||
var date = length == 1 && String(Y) === Y ? // isString(Y) | ||
// We explicitly pass it through parse: | ||
new NativeDate(NativeDate.parse(Y)) : | ||
// We have to manually make calls depending on argument | ||
// length here | ||
length >= 7 ? new NativeDate(Y, M, D, h, m, s, ms) : | ||
length >= 6 ? new NativeDate(Y, M, D, h, m, s) : | ||
length >= 5 ? new NativeDate(Y, M, D, h, m) : | ||
length >= 4 ? new NativeDate(Y, M, D, h) : | ||
length >= 3 ? new NativeDate(Y, M, D) : | ||
length >= 2 ? new NativeDate(Y, M) : | ||
length >= 1 ? new NativeDate(Y) : | ||
new NativeDate(); | ||
// Prevent mixups with unfixed Date object | ||
date.constructor = NativeDate; | ||
return date; | ||
} | ||
return NativeDate.apply(this, arguments); | ||
} | ||
// Copy any custom methods a 3rd party library may have added | ||
(function() { | ||
for (var key in NativeDate) { | ||
FakeDate[key] = NativeDate[key]; | ||
} | ||
}()); | ||
// Copy "native" methods explicitly; they may be non-enumerable | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
// Setup inheritance | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.prototype.constructor = NativeDate; | ||
/** | ||
@@ -104,4 +117,4 @@ * Replace the original now method. | ||
if (freeze) return freeze.getTime(); | ||
if (travel) return ms(); | ||
return FakeDate.super_.now(); | ||
if (travel) return time(); | ||
return NativeDate.now(); | ||
}; | ||
@@ -112,6 +125,10 @@ | ||
* | ||
* @param {Object} Date. | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.freeze = function(date) { | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
freeze = date; | ||
@@ -123,8 +140,12 @@ }; | ||
* | ||
* @param {Object} Date. | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.travel = function(date) { | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
travel = date.getTime(); | ||
started = FakeDate.super_.now(); | ||
started = NativeDate.now(); | ||
}; | ||
@@ -131,0 +152,0 @@ |
{ | ||
"name": "timekeeper" | ||
, "description": "Easy testing of time-dependent code." | ||
, "version": "0.0.1" | ||
, "keywords": ["fake date", "date test"] | ||
, "version": "0.0.2" | ||
, "keywords": ["fake date", "date test", "mocking date"] | ||
, "author": "Veselin Todorov <hi@vesln.com>" | ||
, "contributors": [ | ||
{"name": "Oleg Slobodskoi", "email": "oleg008@gmail.com"} | ||
] | ||
, "devDependencies": { | ||
@@ -22,4 +25,4 @@ "mocha": "0.x.x" | ||
, "engines": { | ||
"node": ">= 0.6.0" | ||
"node": "x.x.x" | ||
} | ||
} |
@@ -10,2 +10,8 @@ [![Build Status](https://secure.travis-ci.org/vesln/timekeeper.png)](http://travis-ci.org/vesln/timekeeper) | ||
## Features/problems | ||
- Please note, that if you are using time freezing, the `setTimeout` and | ||
`setInteval` won't work as exepcted, since they are using the `Date` | ||
class but the time will not change until you call `timekeeper#reset`. | ||
## Synopsis | ||
@@ -21,12 +27,9 @@ | ||
setTimeout(function() { | ||
// The time hasn't changed at all. | ||
// The time hasn't changed at all. | ||
var date = new Date; | ||
var ms = Date.now(); | ||
var date = new Date; | ||
var ms = Date.now(); | ||
tk.reset(); // Reset. | ||
tk.reset(); // Reset. | ||
}, 500); | ||
``` | ||
@@ -33,0 +36,0 @@ |
@@ -86,2 +86,8 @@ /** | ||
}); | ||
describe('inheritance', function() { | ||
describe('should create an instance of Date', function() { | ||
(new Date instanceof Date).should.be.eql(true); | ||
}); | ||
}); | ||
}); |
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
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
9089
8
223
101
0