timekeeper
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -0,2 +1,7 @@ | ||
0.0.5 / 2014-04-24 | ||
================== | ||
* Add support for browser | ||
* Add bower.json definition | ||
0.0.4 / 2013-11-22 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -8,182 +8,195 @@ /** | ||
/** | ||
* Native Date constructor reference. | ||
* | ||
* @type {Object} | ||
*/ | ||
var NativeDate = Date; | ||
!(function(name, root, definition) { | ||
definition = definition(); | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = definition; | ||
} | ||
exports[name] = definition; | ||
} else { | ||
root[name] = definition; | ||
} | ||
})('timekeeper', this, function() { | ||
/** | ||
* `TimeKeeper`. | ||
* | ||
* @type {Object} | ||
*/ | ||
var timekeeper = {}; | ||
/** | ||
* Native Date constructor reference. | ||
* | ||
* @type {Object} | ||
*/ | ||
var NativeDate = Date; | ||
/** | ||
* Frozen date time container. | ||
* | ||
* @type {Object} | ||
*/ | ||
var freeze = null; | ||
/** | ||
* `TimeKeeper`. | ||
* | ||
* @type {Object} | ||
*/ | ||
var timekeeper = {}; | ||
/** | ||
* Fake date time container. | ||
* | ||
* @type {Number} | ||
*/ | ||
var travel = null; | ||
/** | ||
* Frozen date time container. | ||
* | ||
* @type {Object} | ||
*/ | ||
var freeze = null; | ||
/** | ||
* Travel start container. | ||
* | ||
* @type {Number} | ||
*/ | ||
var started = null; | ||
/** | ||
* Fake date time container. | ||
* | ||
* @type {Number} | ||
*/ | ||
var travel = null; | ||
/** | ||
* Return the elapsed time. | ||
* | ||
* @returns {Number} | ||
*/ | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
} | ||
/** | ||
* Travel start container. | ||
* | ||
* @type {Number} | ||
*/ | ||
var started = null; | ||
/** | ||
* `FakeDate` constructor. | ||
*/ | ||
function FakeDate(Y, M, D, h, m, s, ms) { | ||
var length = arguments.length; | ||
/** | ||
* Return the elapsed time. | ||
* | ||
* @returns {Number} | ||
*/ | ||
function time() { | ||
return travel + (NativeDate.now() - started); | ||
} | ||
if (this instanceof NativeDate) { | ||
/** | ||
* `FakeDate` constructor. | ||
*/ | ||
function FakeDate(Y, M, D, h, m, s, ms) { | ||
var length = arguments.length; | ||
if (!length && freeze) return freeze; | ||
if (!length && travel) return new NativeDate(time()); | ||
if (this instanceof NativeDate) { | ||
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); | ||
} | ||
if (!length && freeze) return freeze; | ||
if (!length && travel) return new NativeDate(time()); | ||
// Copy any custom methods a 3rd party library may have added | ||
(function() { | ||
for (var key in NativeDate) { | ||
FakeDate[key] = NativeDate[key]; | ||
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 "native" methods explicitly; they may be non-enumerable | ||
FakeDate.UTC = NativeDate.UTC; | ||
FakeDate.parse = NativeDate.parse; | ||
// Copy any custom methods a 3rd party library may have added | ||
(function() { | ||
for (var key in NativeDate) { | ||
FakeDate[key] = NativeDate[key]; | ||
} | ||
}()); | ||
// Setup inheritance | ||
FakeDate.prototype = NativeDate.prototype; | ||
FakeDate.prototype.constructor = NativeDate; | ||
// 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; | ||
/** | ||
* Replace the original now method. | ||
* | ||
* Check if the time is | ||
* | ||
* - frozen and if so return the frozen time | ||
* - faked and if so return the elapsed time | ||
* | ||
* @returns {Number} | ||
* @api public | ||
*/ | ||
FakeDate.now = function() { | ||
if (freeze) return freeze.getTime(); | ||
if (travel) return time(); | ||
return NativeDate.now(); | ||
}; | ||
/** | ||
* Set current Date Tieme and freeze it. | ||
* | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.freeze = function(date) { | ||
useFakeDate(); | ||
/** | ||
* Replace the original now method. | ||
* | ||
* Check if the time is | ||
* | ||
* - frozen and if so return the frozen time | ||
* - faked and if so return the elapsed time | ||
* | ||
* @returns {Number} | ||
* @api public | ||
*/ | ||
FakeDate.now = function() { | ||
if (freeze) return freeze.getTime(); | ||
if (travel) return time(); | ||
return NativeDate.now(); | ||
}; | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
/** | ||
* Set current Date Tieme and freeze it. | ||
* | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.freeze = function(date) { | ||
useFakeDate(); | ||
freeze = date; | ||
}; | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
/** | ||
* Set current DateTime. | ||
* | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.travel = function(date) { | ||
useFakeDate(); | ||
freeze = date; | ||
}; | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
/** | ||
* Set current DateTime. | ||
* | ||
* @param {Object|String|Number} Date. | ||
* @api public | ||
*/ | ||
timekeeper.travel = function(date) { | ||
useFakeDate(); | ||
travel = date.getTime(); | ||
started = NativeDate.now(); | ||
}; | ||
if (typeof date !== 'object') { | ||
date = new NativeDate(date); | ||
} | ||
/** | ||
* Reset the `timekeeper` behavior. | ||
* | ||
* @api public | ||
*/ | ||
timekeeper.reset = function() { | ||
useNativeDate(); | ||
freeze = null; | ||
started = null; | ||
travel = null; | ||
}; | ||
travel = date.getTime(); | ||
started = NativeDate.now(); | ||
}; | ||
/** | ||
* Reflection: Are we currently modifying the native Date object? | ||
* | ||
* @api public | ||
*/ | ||
timekeeper.isKeepingTime = function() { | ||
return Date === FakeDate; | ||
}; | ||
/** | ||
* Reset the `timekeeper` behavior. | ||
* | ||
* @api public | ||
*/ | ||
timekeeper.reset = function() { | ||
useNativeDate(); | ||
freeze = null; | ||
started = null; | ||
travel = null; | ||
}; | ||
/** | ||
* Replace the `Date` with `FakeDate`. | ||
*/ | ||
function useFakeDate() { | ||
Date = FakeDate | ||
} | ||
/** | ||
* Reflection: Are we currently modifying the native Date object? | ||
* | ||
* @api public | ||
*/ | ||
timekeeper.isKeepingTime = function() { | ||
return Date === FakeDate; | ||
}; | ||
/** | ||
* Restore the `Date` to `NativeDate`. | ||
*/ | ||
function useNativeDate() { | ||
Date = NativeDate | ||
} | ||
/** | ||
* Replace the `Date` with `FakeDate`. | ||
*/ | ||
function useFakeDate() { | ||
Date = FakeDate | ||
} | ||
/** | ||
* Restore the `Date` to `NativeDate`. | ||
*/ | ||
function useNativeDate() { | ||
Date = NativeDate | ||
} | ||
/** | ||
* Expose `timekeeper` | ||
*/ | ||
module.exports = timekeeper; | ||
/** | ||
* Expose `timekeeper` | ||
*/ | ||
return timekeeper; | ||
}); |
{ | ||
"name": "timekeeper" | ||
, "description": "Easy testing of time-dependent code." | ||
, "version": "0.0.4" | ||
, "keywords": ["fake date", "date test", "mocking date"] | ||
, "author": "Veselin Todorov <hi@vesln.com>" | ||
, "contributors": [ | ||
{"name": "Oleg Slobodskoi", "email": "oleg008@gmail.com"} | ||
] | ||
, "devDependencies": { | ||
"mocha": "0.x.x" | ||
, "chai": "*" | ||
, "jack": "*" | ||
} | ||
, "repository" : { | ||
"type" : "git" | ||
, "url" : "http://github.com/vesln/timekeeper.git" | ||
} | ||
, "homepage": "http://github.com/vesln/timekeeper" | ||
, "scripts": { | ||
"test": "make test" | ||
} | ||
, "main": "./lib/timekeeper.js" | ||
"name": "timekeeper", | ||
"description": "Easy testing of time-dependent code.", | ||
"version": "0.0.5", | ||
"keywords": [ | ||
"fake date", | ||
"date test", | ||
"mocking date" | ||
], | ||
"author": "Veselin Todorov <hi@vesln.com>", | ||
"contributors": [ | ||
{ | ||
"name": "Oleg Slobodskoi", | ||
"email": "oleg008@gmail.com" | ||
}, | ||
{ | ||
"name": "Guilherme Tramontina", | ||
"email": "guilherme.tramontina@gmail.com" | ||
} | ||
], | ||
"devDependencies": { | ||
"chai": "^1.9.2", | ||
"jack": "0.0.2", | ||
"mocha": "^2.0.1", | ||
"mocha-phantomjs": "^3.5.1", | ||
"phantomjs": "^1.9.12" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/vesln/timekeeper.git" | ||
}, | ||
"homepage": "http://github.com/vesln/timekeeper", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"main": "./lib/timekeeper.js" | ||
} |
@@ -16,2 +16,7 @@ [![Build Status](https://secure.travis-ci.org/vesln/timekeeper.png)](http://travis-ci.org/vesln/timekeeper) | ||
## Installation | ||
- NPM: `npm install timekeeper --save` | ||
- Bower: `bower install timekeeper` | ||
## Synopsis | ||
@@ -18,0 +23,0 @@ |
@@ -9,7 +9,2 @@ /** | ||
/** | ||
* Support | ||
*/ | ||
var should = require('chai').should(); | ||
/** | ||
* Sleep implementation. | ||
@@ -29,7 +24,2 @@ * | ||
/** | ||
* Context. | ||
*/ | ||
var tk = require('../lib/timekeeper'); | ||
describe('TimeKeeper', function() { | ||
@@ -36,0 +26,0 @@ describe('freeze', function() { |
Sorry, the diff of this file is not supported yet
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
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
12593
11
282
118
5
1