{"name":"timer-node","version":"3.1.0","description":"a simple timer in nodejs","main":"index.js","scripts":{"test":"grunt test"},"repository":{"type":"git","url":"git+https://github.com/js-shelf/timer-node.git"},"keywords":["timer","timer.js","node timer","js timer"],"author":{"name":"Eyas Ranjous","email":"https://github.com/eyas-ranjous"},"license":"MIT","bugs":{"url":"https://github.com/js-shelf/timer-node/issues"},"homepage":"https://github.com/js-shelf/timer-node#readme","devDependencies":{"chai":"^4.2.0","eslint":"^6.7.2","eslint-config-airbnb-base":"^14.0.0","eslint-plugin-import":"^2.19.1","grunt":"^1.0.4","grunt-eslint":"^22.0.0","grunt-mocha-istanbul":"^5.0.2","grunt-mocha-test":"^0.13.3","istanbul":"^0.4.5","mocha":"^6.2.2","sinon":"^9.0.2"},"gitHead":"cebfefdf3d85e73bdad4dd0ff7b8ff5d2eea8f3d","_id":"timer-node@3.1.0","_nodeVersion":"14.4.0","_npmVersion":"6.14.5","dist":{"integrity":"sha512-LdY0X89Cdo3DjG26D6fM23kHc3zIeXc2KhGs3OYpumgHX0+0LYh+ATqdfdJklsloLv03jA2fyJnHzdmo1fzR1g==","shasum":"c78deeac47c8cb546c072a71507f92032a687b7a","tarball":"http://registry-npm.ywwl.com/timer-node/download/timer-node-3.1.0.tgz","fileCount":6,"unpackedSize":8591,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgHrUACRA9TVsSAnZWagAA+U4P/j3MyiFuEzbsvVM/NeB2\nRHROcfuScPxQvGN0KS1cc3IrhE1IvfWoOapEjrN6ZyRdn/tXpyljTibH6iWI\nOxbOdjkS41zouIthr90i/A03IyrUNXjUL8xRx1hTKZ/KV+A9buXbIt2alx+u\nhmdYkt1KKLcR9i2Q62BLtpjA1jn6kuiBZqMhJ6E6N90axEo7foED9MeFn28H\nS5N3WQwbHAs7T2ekmrUsAjCpT25wx/ba3DSd12Li6XGK7zevBsed4ibabdAT\nEDoEBBm8c1fUXyMIh2TcNUIsHF9dKLOnm6Ol0Axh0AJVUqvbzidi4H1vUo88\nggNv1XRZZD+2LOSwWNVOqI7rgVkvFO2SWGo1aLZmR3aL03eN+IGfKtXJyYHg\nipAqNBaWY5j/BIgihxvFyBA/Dd1dnuv/etkDZkmYlsMYn48C2jJEj2Thk5tQ\ny2eKvnNseKNsT89keNi1J5a2MuG7hP0rkFpC89EtsedMdPzWs+T9TPb6o8kD\n5HcVYVDWeKdqtTsvSKruwxVO5qEpLsHVIf9eqmtY26g3YD9WisE4JGHgi5ps\nsQe/9iXezhnrSSimZkaRa7cui0gj+U+kM9QSPjM0TrT2sTPE9dWDr/T8bsXI\nswgvh44qvtO1XnRWJat/VuGTp/17prlxvtmUpdUkN7kuox/Cd5MuQcb/lmY5\nJ6rf\r\n=A+hG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFO7y5q54NCNxCByc1C47xgVBCg0drJb0h0uEt0pFBZkAiEA+MeEI9Ocrl/QEFU9RmeLTxwWVtQUo75t9o85IEaDNik="}],"size":3252,"noattachment":false,"key":"/timer-node/-/timer-node-3.1.0.tgz"},"_npmUser":{"name":"eyas-ranjous","email":"eyas.ranjous@gmail.com"},"directories":{},"maintainers":[{"name":"eyas-ranjous","email":"eyas.ranjous@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/timer-node_3.1.0_1612625151953_0.6858969412943019"},"_hasShrinkwrap":false,"readme":"# timer-node\n\n[![npm](https://img.shields.io/npm/v/timer-node.svg)](https://www.npmjs.com/package/timer-node) [![npm](https://img.shields.io/npm/dm/timer-node.svg)](https://www.npmjs.com/package/timer-node) [![npm](https://img.shields.io/badge/node-%3E=%206.0-blue.svg)](https://www.npmjs.com/package/timer-node)\n\nA timestamp-based timer that enables recording elapsed time and formatting the result.\n\nIt does **NOT** use *setInterval*, *setTimeout* or *process*\n\n\\[**Start**\\]---ms---\\[**Pause**\\]--pause ms--\\[**Resume**\\]---ms---\\[**Pause**\\]--pause ms--\\[**Resume**\\]---ms---\\[**Stop**\\]\n\n<img src=\"https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg\">\n\n# Contents\n* [Install](#install)\n* [require](#require)\n* [import](#import)\n* [API](#api)\n  * [constructor](#constructor)\n  * [start](#start)\n  * [isStarted](#isstarted)\n  * [startedAt](#startedat)\n  * [pause](#pause)\n  * [isPaused](#ispaused)\n  * [resume](#resume)\n  * [isRunning](#isrunning)\n  * [ms](#ms)\n  * [time](#time)\n  * [format](#format)\n  * [pauseMs](#pauseMs)\n  * [pauseCount](#pauseMs)\n  * [pauseTime](#pauseTime)\n  * [stop](#stop)\n  * [isStopped](#isstopped)\n  * [stoppedAt](#stoppedat)\n  * [serialize](#serialize)\n  * [getLabel](#getlabel)\n  * [clear](#clear)\n  * [Timer.deserialize](#timerdeserialize)\n  * [Timer.benchmark](#timerbenchmarkfn)\n * [Build](#build)\n * [License](#license)\n\n## Install\n\n```\nnpm install --save timer-node\n```\n\n## require\n\n```js\nconst { Timer } = require('timer-node');\n```\n\n## import\n\n```js\nimport { Timer, Time, TimerOptions } from 'timer-node';\n```\n\n## API\n\n### constructor\n\n```js\nconst timer = new Timer({ label: 'test-timer' });\n```\n\nIt's also possible to create the timer from a past timestamp.\n\n```js\nconst timer = new Timer({\n  label: 'test-timer',\n  startTimestamp: 1563074001233 // 2019-07-14 03:13:21.233Z\n});\n\nconsole.log(timer.isStarted()); // true\nconsole.log(timer.time()); // { d: 619, h: 16, m: 26, s: 11, ms: 207 }\n```\n\n### start\nstarts the timer.\n\n```js\ntimer.start();\n```\n\n### isStarted\nreturns true if the timer is started.\n\n```js\nconsole.log(timer.isStarted()); // true\n```\n\n### startedAt\nreturns the starting timestamp.\n\n```js\nconsole.log(timer.startedAt()); // 1616535899945\n```\n\n### pause\npauses the timer and memoizes elapsed running time.\n\n```js\ntimer.pause();\n```\n\n### isPaused\nchecks if the timer is paused.\n\n```js\nconsole.log(timer.isPaused()); // true\n```\n\n### resume\nresumes the timer.\n\n```js\ntimer.resume();\n```\n\n### isRunning\nchecks if the timer is started and not paused or stopped.\n\n```js\ntimer.isRunning(); // true\n```\n\n### ms\nreturns the running duration in milliseconds. It can be measured while timer is running or when paused or stopped.\n\n```js\n// when timer is running, calling .ms() will dynamically calculate progressing milliseconds\nconsole.log(timer.ms()); // 37606\nconsole.log(timer.ms()); // 91843\nconsole.log(timer.ms()); // 135377\n\n// when timer is paused or stopped, .ms() will return the same value\nconsole.log(timer.ms()); // 270754\nconsole.log(timer.ms()); // 270754\n```\n\n### time\nreturns the running duration as an object of time fractions. It can be measured while timer is running or when stopped.\n\n* `ms`: milliseconds\n* `s`: seconds\n* `m`: minutes\n* `h`: hours\n* `d`: days\n\n```js\n// when timer is running, calling .time() will dynamically calculate progressing time\nconsole.log(timer.time()); // { d: 0, h: 0, m: 0, s: 7, ms: 921 }\nconsole.log(timer.time()); // { d: 0, h: 0, m: 4, s: 44, ms: 321 }\nconsole.log(timer.time()); // { d: 0, h: 3, m: 55, s: 12, ms: 910 }\n\n// when timer is paused or stopped, .time() will return the same value\nconsole.log(timer.time()); // { d: 0, h: 4, m: 5, s: 52, ms: 770 }\nconsole.log(timer.time()); // { d: 0, h: 4, m: 5, s: 52, ms: 770 }\n```\n\n### format\nformats the running duration using a custom or default template.\n\nThe function replaces time placeholders in a string. Placeholders are:\n\n* `%label` for timer label.\n* `%ms` for milliseconds.\n* `%s` for seconds.\n* `%m` for minutes.\n* `%h` for hours.\n* `%d` for days.\n\n```js\n// using the default template\nconsole.log(timer.format()); // test-timer: 0 d, 1 h, 44 m, 23 s, 977 ms\n\n// using a custom template\nconsole.log(timer.format('%label [%s] seconds [%ms] ms')); // test-timer [4] seconds [254] ms\n```\n\n### pauseMs\nreturns the pause duration in milliseconds. It can be measured while timer is paused or when running.\n\n```js\n// when timer is paused, calling pauseMs will dynamically calculate progressing pause milliseconds\nconsole.log(timer.pauseMs()); // 3878\nconsole.log(timer.pauseMs()); // 5990\nconsole.log(timer.pauseMs()); // 7997\n\n// when timer is resumed, pauseMs will return the same previousely accomulated pauses\ntimer.stop();\nconsole.log(timer.pauseMs()); // 97264\nconsole.log(timer.pauseMs()); // 97264\n```\n\n### pauseTime\nreturns the pause duration as an object of time fractions. It can be measured while timer is paused or when running.\n\n* `ms`: milliseconds\n* `s`: seconds\n* `m`: minutes\n* `h`: hours\n* `d`: days\n\n```js\n// when timer is paused, calling pauseMs will dynamically calculate progressing pause time\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 4, ms: 675 }\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 6, ms: 328 }\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 7, ms: 904 }\n\n// when timer is resumed, pauseMs will return the same previousely accomulated pauses\ntimer.resume();\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 12, ms: 143 }\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 12, ms: 143 }\n```\n\n### pauseCount\nreturns the number of times the timer was paused.\n\n```js\nconsole.log(timer.pauseCount()); // 2\n```\n\n### stop\nstops the timer. The timer can be started again by calling `.start()` which clears all recorded values.\n\n```js\ntimer.stop();\n\nconsole.log(timer.time()); // { d: 0, h: 0, m: 2, s: 44, ms: 453 }\nconsole.log(timer.time()); // { d: 0, h: 0, m: 2, s: 44, ms: 453 }\n```\n\n### isStopped\nchecks if the timer has been stopped.\n\n```js\nconsole.log(timer.isStopped()); // true\n```\n\n### stoppedAt\nreturns the stop timestamp.\n\n```js\nconsole.log(timer.stoppedAt()); // undefined\ntimer.stop();\nconsole.log(timer.stoppedAt()); // 1616535948456\n```\n\n### serialize\nserializes the timer in its current state.\n\n```js\nconsole.log(timer.serialize());\n// '{\"startTimestamp\":1616535216209,\"currentStartTimestamp\":1616535227790,\"endTimestamp\":1616535258945,\"accumulatedMs\":6249,\"pauseCount\":3,\"label\":\"test\"}'\n```\n\n### getLabel\nreturns the timer's label\n\n```js\nconsole.log(timer.getLabel()); // test-timer\n```\n\n### clear\nclears the timer values. can be started again by calling `.start()`.\n\n```js\ntimer.clear();\nconsole.log(timer.time()); // { d: 0, h: 0, m: 0, s: 0, ms: 0 }\nconsole.log(timer.pauseTime()); // { d: 0, h: 0, m: 0, s: 0, ms: 0 }\n```\n\n### Timer.deserialize\nre-construct a timer from its serialized form.\n\n```js\nconst timerStr = '{\"startTimestamp\":1616535216209,\"currentStartTimestamp\":1616535227790,\"endTimestamp\":1616535258945,\"accumulatedMs\":6249,\"pauseCount\":3,\"label\":\"test\"}';\n\nconst timer = Timer.deserialize(timerStr);\n\nconsole.log(timer.isStopped()); // true\nconsole.log(timer.time()); // { d: 0, h: 0, m: 0, s: 37, ms: 404 }\n```\n\n### Timer.benchmark(fn)\ncreates a benchmark timer for a function call.\n\n```js\nconst fn = (a) => {\n  let sum = 0;\n  for (let i = 0; i < 10000000; i += 1) {\n    sum += a * i;\n  }\n  return sum;\n}\n\nconst benchmark = Timer.benchmark(fn.bind(fn, 5));\nconsole.log(benchmark.time()); // { d: 0, h: 0, m: 0, s: 0, ms: 53 }\nconsole.log(benchmark.format('%label: %ms ms')); // bound fn: 53 ms\n```\n\n## Build\n```\ngrunt build\n```\n\n## License\nThe MIT License. Full License is [here](https://github.com/eyas-ranjous/timer-node/blob/master/LICENSE)\n","publish_time":1612625152075,"_cnpm_publish_time":1612625152075,"dist-tags":{"latest":"5.0.9"}}