{"_id":"clock","_rev":"324419","name":"clock","description":"clock interface for inversion-of-control","dist-tags":{"latest":"1.0.2"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"time":{"modified":"2021-06-04T01:59:00.000Z","created":"2011-08-08T10:57:55.136Z","1.0.2":"2017-07-13T02:25:20.404Z","1.0.1":"2017-07-13T02:19:58.670Z","1.0.0":"2017-07-13T01:20:12.451Z","0.1.6":"2014-04-28T11:28:21.230Z","0.1.5":"2012-09-27T18:46:56.048Z","0.1.4":"2012-06-13T14:29:39.350Z","0.1.3":"2012-01-22T17:01:31.967Z","0.1.2":"2012-01-22T16:59:23.818Z","0.1.1":"2011-08-08T14:58:09.555Z","0.1.0":"2011-08-08T10:57:55.136Z"},"users":{},"author":{"name":"jsdnxx"},"repository":{"type":"git","url":"git+ssh://git@github.com/js-n/clock.git"},"versions":{"1.0.2":{"name":"clock","version":"1.0.2","description":"clock interface for inversion-of-control","repository":{"type":"git","url":"git+ssh://git@github.com/js-n/clock.git"},"main":"index.js","types":"index.d.ts","scripts":{"prebuild":"npm run clean","build":"tsc","clean":"rm -f {*.d.ts,*.js.map,*.js}","test":"true","prepack":"npm run build","postpack":"npm run clean"},"files":["index.js","index.d.ts","index.js.map"],"keywords":["typescript","clock","interface","ioc","inversion-of-control","inject","testing","test","mock","stub","time","date"],"author":{"name":"jsdnxx"},"license":"ISC","devDependencies":{"typescript":"^2.4.1"},"gitHead":"6d6a3821f6d90b8c3da4b23b8aee60a4a4ac5c47","bugs":{"url":"https://github.com/js-n/clock/issues"},"homepage":"https://github.com/js-n/clock#readme","_id":"clock@1.0.2","_npmVersion":"5.0.3","_nodeVersion":"8.1.4","_npmUser":{"name":"jsdnxx","email":"jason@denizac.org"},"dist":{"shasum":"716ee41586d18f0263e1bb1a5f4c4433e094e6f9","size":3197,"noattachment":false,"key":"/clock/-/clock-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-1.0.2.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/clock-1.0.2.tgz_1499912720292_0.8086873493157327"},"directories":{},"publish_time":1499912720404,"_hasShrinkwrap":false,"_cnpm_publish_time":1499912720404},"1.0.1":{"name":"clock","version":"1.0.1","description":"clock interface for inversion-of-control","repository":{"type":"git","url":"git+ssh://git@github.com/js-n/clock.git"},"main":"index.ts","types":"index.d.ts","scripts":{"prebuild":"npm run clean","build":"tsc","clean":"rm -f {*.d.ts,*.js.map,*.js}","test":"true","prepublish":"npm run build","postpublish":"npm run clean"},"files":["index.js","index.d.ts","index.js.map"],"keywords":["typescript","clock","interface","ioc","inversion-of-control","inject","testing","test","mock","stub","time","date"],"author":{"name":"jsdnxx"},"license":"ISC","devDependencies":{"typescript":"^2.4.1"},"gitHead":"5a3135a88bac1dccefcc0ffda7ce477214b099fa","bugs":{"url":"https://github.com/js-n/clock/issues"},"homepage":"https://github.com/js-n/clock#readme","_id":"clock@1.0.1","_npmVersion":"5.0.3","_nodeVersion":"8.1.4","_npmUser":{"name":"jsdnxx","email":"jason@denizac.org"},"dist":{"shasum":"6d24f6b25e79faef2cb05f35003df82b7de70a6d","size":3406,"noattachment":false,"key":"/clock/-/clock-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-1.0.1.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/clock-1.0.1.tgz_1499912398553_0.8372391546145082"},"directories":{},"publish_time":1499912398670,"_hasShrinkwrap":false,"_cnpm_publish_time":1499912398670},"1.0.0":{"name":"clock","version":"1.0.0","description":"clock interface for inversion-of-control","repository":{"type":"git","url":"git+ssh://git@github.com/js-n/clock.git"},"main":".","types":".","scripts":{"build":"tsc","clean":"rm {*.d.ts,*.js.map,*.js}","test":"true"},"keywords":[],"author":{"name":"jsdnxx"},"license":"ISC","devDependencies":{"typescript":"^2.4.1"},"gitHead":"a8e8818856b796958ec56eb0e6a866ffde534e40","bugs":{"url":"https://github.com/js-n/clock/issues"},"homepage":"https://github.com/js-n/clock#readme","_id":"clock@1.0.0","_npmVersion":"5.0.3","_nodeVersion":"8.1.4","_npmUser":{"name":"jsdnxx","email":"jason@denizac.org"},"dist":{"shasum":"a8adc5434b899a5a9107ff6c205c84dd04f9a371","size":3102,"noattachment":false,"key":"/clock/-/clock-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-1.0.0.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/clock-1.0.0.tgz_1499908812333_0.8019808477256447"},"directories":{},"publish_time":1499908812451,"_hasShrinkwrap":false,"_cnpm_publish_time":1499908812451},"0.1.6":{"name":"clock","version":"0.1.6","description":"Indicate and co-ordinate time events","author":{"name":"Mariusz Nowak","email":"medikoo@medikoo.com","url":"http://www.medikoo.com/"},"keywords":["time","watch","event","events","clock","async","asynchronous"],"main":"lib","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"dependencies":{"es5-ext":"~0.10.2","event-emitter":"~0.3.1"},"devDependencies":{"tad":"~0.1.21"},"scripts":{"test":"node ./node_modules/tad/bin/tad lib"},"licence":"MIT","bugs":{"url":"https://github.com/medikoo/clock/issues"},"homepage":"https://github.com/medikoo/clock","_id":"clock@0.1.6","dist":{"shasum":"b9d94018b60d32d2f8ef4101b7fa1c4746ea2807","size":3073,"noattachment":false,"key":"/clock/-/clock-0.1.6.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.6.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"medikoo","email":"medikoo+npm@medikoo.com"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1398684501230,"_cnpm_publish_time":1398684501230,"_hasShrinkwrap":false},"0.1.5":{"name":"clock","version":"0.1.5","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.9.x","event-emitter":"0.2.x"},"scripts":{"test":"node ./node_modules/tad/bin/tad lib"},"devDependencies":{"tad":"0.1.x"},"optionalDependencies":{},"engines":{"node":">=0.4"},"_id":"clock@0.1.5","dist":{"shasum":"29e7c41e08e78465ba6e21be16f3685e538a7f17","size":2870,"noattachment":false,"key":"/clock/-/clock-0.1.5.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.5.tgz"},"_npmVersion":"1.1.61","_npmUser":{"name":"medikoo","email":"medikoo+npm@medikoo.com"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1348771616048,"_hasShrinkwrap":false,"_cnpm_publish_time":1348771616048},"0.1.4":{"name":"clock","version":"0.1.4","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.8.x","event-emitter":"0.1.x"},"scripts":{"test":"node ./node_modules/tad/bin/tad lib"},"devDependencies":{"tad":"0.1.x"},"optionalDependencies":{},"engines":{"node":"*"},"_npmUser":{"name":"medikoo","email":"medikoo+npm@medikoo.com"},"_id":"clock@0.1.4","_engineSupported":true,"_npmVersion":"1.1.24","_nodeVersion":"v0.6.19","_defaultsLoaded":true,"dist":{"shasum":"e0d2f31b7afcacd3c5d3d444a4746a0b3de1ab3d","size":2594,"noattachment":false,"key":"/clock/-/clock-0.1.4.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.4.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1339597779350,"_hasShrinkwrap":false,"_cnpm_publish_time":1339597779350},"0.1.3":{"name":"clock","version":"0.1.3","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.7.x","event-emitter":"0.1.x"},"scripts":{"test":"node ./node_modules/tad/bin/tad lib"},"devDependencies":{"tad":"0.1.x"},"_npmUser":{"name":"medikoo","email":"medikoo+npm@medikoo.com"},"_id":"clock@0.1.3","optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.0-2","_nodeVersion":"v0.6.8","_defaultsLoaded":true,"dist":{"shasum":"dcf52a2292512f9d0b0fce31c9065f8bb9092deb","size":2430,"noattachment":false,"key":"/clock/-/clock-0.1.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.3.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1327251691967,"_hasShrinkwrap":false,"_cnpm_publish_time":1327251691967},"0.1.2":{"name":"clock","version":"0.1.2","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.7.x","event-emitter":"0.1.x"},"scripts":{"test":"node ./node_modules/tad/bin/tad lib"},"devDependencies":{"tad":"0.1.x"},"_npmUser":{"name":"medikoo","email":"medikoo+npm@medikoo.com"},"_id":"clock@0.1.2","optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.0-2","_nodeVersion":"v0.6.8","_defaultsLoaded":true,"dist":{"shasum":"38d2f568d60cb3004de25d87aad7a93c0e7eb17c","size":2404,"noattachment":false,"key":"/clock/-/clock-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.2.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1327251563818,"_hasShrinkwrap":false,"_cnpm_publish_time":1327251563818},"0.1.1":{"name":"clock","version":"0.1.1","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.6.x","event-emitter":"0.1.x"},"scripts":{"test":"./node_modules/tad/bin/tad lib"},"devDependencies":{"tad":"0.1.x"},"_npmJsonOpts":{"file":"/Users/medikoo/.npm/clock/0.1.1/package/package.json","wscript":false,"contributors":false,"serverjs":false},"_id":"clock@0.1.1","engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.0.23","_nodeVersion":"v0.5.4-pre","_defaultsLoaded":true,"dist":{"shasum":"91c0714a66e9d8c8c3b654119ccd2790bfc6cc2e","size":1877,"noattachment":false,"key":"/clock/-/clock-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.1.tgz"},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1312815489555,"_hasShrinkwrap":false,"_cnpm_publish_time":1312815489555},"0.1.0":{"name":"clock","version":"0.1.0","description":"Indicate and co-ordinate time events","keywords":["time","watch","event","events","clock","async","asynchronous"],"author":{"name":"Mariusz Nowak","email":"medikoo+clock@medikoo.com","url":"http://www.medikoo.com/"},"main":"lib/index","repository":{"type":"git","url":"git://github.com/medikoo/clock.git"},"bugs":{"email":"medikoo+clock@medikoo.com","url":"https://github.com/medikoo/clock/issues"},"dependencies":{"es5-ext":"0.6.x","event-emitter":"0.1.x"},"_npmJsonOpts":{"file":"/Users/medikoo/.npm/clock/0.1.0/package/package.json","wscript":false,"contributors":false,"serverjs":false},"_id":"clock@0.1.0","devDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.0.23","_nodeVersion":"v0.5.4-pre","_defaultsLoaded":true,"dist":{"shasum":"8ee4ce7862c8bc43593ca0a76df356f9628cf041","size":1761,"noattachment":false,"key":"/clock/-/clock-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/clock/download/clock-0.1.0.tgz"},"scripts":{},"maintainers":[{"name":"jsdnxx","email":"jason@denizac.org"},{"name":"medikoo","email":""}],"directories":{},"publish_time":1312801075136,"_hasShrinkwrap":false,"_cnpm_publish_time":1312801075136}},"readme":"# clock\n\na JavaScript idiomatic clock interface for inversion-of-control\n\n## installation\n\n```\n> npm install clock\n```\n\n\n## example usage\n\nin JavaScript\n```js\nfunction announceTime(clock) {\n  clock = clock || Date\n  return 'It is now ' + clock.now()\n}\n\nannounceTime()\n// It is now 1499904588386\n\nannounceTime({now: function () { return 1234 }})\n// It is now 1234\n```\n\nin TypeScript\n```ts\nimport { Clock } from 'clock'\n\nfunction announceTime(clock: Clock): string {\n  clock = clock || Date\n  return 'It is now ' + clock.now()\n}\n```\n\n## `clock` is an interface package\nA `Clock` is any object with a method `now()` which returns a timestamp\nin milliseconds since the Unix epoch:\n\n```ts\ninterface Clock {\n  now (): number\n}\n```\n\nThe `Date` object satisfies the `Clock` interface.\n\nThis package will always be on the 1.x.x version range. Depend on this package\nso that other people will know that your module expects a `Clock` interface.\n\n\n## what problem this solves\nOften in JavaScript, when we have code that needs to get the current time,\nwe'll do `Date.now()` or `new Date()`. This is easy and works great!\n\n```js\nfunction announceTime() {\n  return 'It is now ' + Date.now()\n}\n```\n\nHowever, this makes your code hard to test and difficult to predict,\nbecause your return values depend on side-effects other than your arguments.\nIn functional programming terms, we no longer have [referential transparency](https://en.wikipedia.org/wiki/Referential_transparency),\nsince multiple calls to `announceTime()` will have different outputs for the\nsame arguments.\n\nWhat if we could **abstract over** the concept of time in our code?\n\nWith inversion of control, we can have our function ask someone else to\nprovide a way to get the time, and just advertise that we expect something\nthat lets us call `.now()` - either the default `Date` object, or anything\nelse with a method `now()`. And to make life easier, we can fall back to\nthe `Date` object by default.\n\n```js\nfunction announceTime(clock) {\n  clock = clock || Date\n  return 'It is now ' + clock.now()\n}\n```\n\nNow that `clock` is a parameter, when we call `announceTime`, we can decide\nwhat time it is:\n\n```js\nconst constantTime = {now: () => 0 }\n```\n\n`constantTime` is a clock that will always return 0, the Unix epoch.\n\nIf we use that, multiple calls to `announceTime` will always return the same value:\n\n```js\nannounceTime(constantTime)\n// It is now 0\nannounceTime(constantTime)\n// It is now 0\nannounceTime(constantTime)\n// It is now 0\n```\n\nThis means we can write a test:\n\n```js\nconst assert = require('assert')\n\nassert.equal('It is now 0', announceTime(constantTime))\n```\n\nAbstracting `clock`s is something common in other platforms, such as Java. See the [Java 8 Clock class](https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html)\nfor an example. Other uses for abstract clocks include skew-corrected clocks,\nsimulating slowed down or sped up passage of time, and other fun tricks.\n\nIf you have a good example, analogy, or clearer way to express any of this\ninformation, please open a GitHub issue or PR!\n\n## api\nThis package includes some helpers:\n\n### `systemClock : Clock` (default export)\nA facade for `Date.now()`\nJavaScript:\n```js\nconst systemClock = require('clock')\nsystemClock.now()\n// 1499907736846\n```\nTypeScript:\n```ts\nimport { systemClock } from 'clock'\nsystemClock.now()\n// 1499907736846\n```\n\n### `constantClock: (time: number = 0) => Clock`\nCreate a clock that always returns the same time. Useful for tests. The `time`\nargument defaults to `0`, the Unix epoch.\nJavaScript:\n```js\nconst constantClock = require('clock').constantClock\nconst epochClock = constantClock()\nepochClock.now()\n// 0\nepochClock.now()\n// 0\n```\nTypeScript:\n```ts\nimport { constantClock } from 'clock'\nconst epochClock = constantClock()\nepochClock.now()\n// 0\nepochClock.now()\n// 0\n```\n\n## contributing\nThis package is developed in TypeScript but usable in all JavaScript projects, on Node.js or in browsers.\nThe npm package includes compiled ES5 JavaScript, as well as TypeScript .d.ts type definition files.\n\nWanted: clarifications, example usage, translation of the readme into other human languages\n\n\n## special thank you\nto @medikoo for the package name. Repo for `clock<1.0.0` can be found at https://github.com/medikoo/clock. Thanks again!\n\n\n## license\nISC\n\n\ncheers!\n","_attachments":{},"homepage":"https://github.com/js-n/clock#readme","bugs":{"url":"https://github.com/js-n/clock/issues"},"license":"ISC"}