{"name":"timer-node","version":"5.0.8","keywords":["timer","timer.js","node timer","js timer"],"author":{"name":"Eyas Ranjous","email":"https://github.com/eyas-ranjous"},"license":"MIT","_id":"timer-node@5.0.8","homepage":"https://github.com/eyas-ranjous/timer-node#readme","bugs":{"url":"https://github.com/eyas-ranjous/timer-node/issues"},"dist":{"shasum":"1528b4696300477286ab1cc49f62e9bace616c17","tarball":"http://registry-npm.ywwl.com/timer-node/download/timer-node-5.0.8.tgz","fileCount":8,"integrity":"sha512-nXFHbqm5W9PussuOsWL87OLg71rSr9UBGHJcE43Uif1ADHrFhjKYKb027uuahZBKnT7XaTpjOmnpFWt8fzrtng==","signatures":[{"sig":"MEQCIEWPB0+ljJB7GvRYNaBCt3g4de5gCapfV1F2pJ6rGpHmAiBTxhxSX5SmmfQkQFbCLJ5sEUwIkR9jdQcTl7BuFpzhfw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":15898,"size":5297,"noattachment":false,"key":"/timer-node/-/timer-node-5.0.8.tgz"},"main":"index.js","types":"./index.d.ts","gitHead":"fea96a359d957ecd79a5012d7b999a08f6b2373c","scripts":{"test":"grunt test"},"_npmUser":{"name":"eyas-ranjous","email":"eyas.ranjous@gmail.com"},"repository":{"url":"git+https://github.com/eyas-ranjous/timer-node.git","type":"git"},"_npmVersion":"10.8.2","description":"A timestamp-based timer that enables recording elapsed time and formatting the result.","directories":{},"_nodeVersion":"20.18.0","_hasShrinkwrap":false,"devDependencies":{"chai":"^4.2.0","grunt":"^1.0.4","mocha":"^6.2.2","sinon":"^9.0.2","eslint":"^6.7.2","istanbul":"^0.4.5","grunt-eslint":"^22.0.0","grunt-mocha-test":"^0.13.3","eslint-plugin-import":"^2.19.1","grunt-mocha-istanbul":"^5.0.2","eslint-config-airbnb-base":"^14.0.0"},"_npmOperationalInternal":{"tmp":"tmp/timer-node_5.0.8_1737847232490_0.35597280276700305","host":"s3://npm-registry-packages-npm-production"},"readme":"# timer-node\n\n[![npm](https://img.shields.io/npm/v/timer-node.svg)](https://www.npmjs.com/package/timer-node)\n[![npm](https://img.shields.io/npm/dm/timer-node.svg)](https://www.npmjs.com/package/timer-node)\n\nA **lightweight** JavaScript library for measuring elapsed time in Node.js or the browser without using `setInterval` or `setTimeout`. It also has type definitions for TypeScript.\n\n## Installation\n\n```bash\nnpm install timer-node\n```\n\n## API\n### Import\n\n```javascript\n// JS\nconst { Timer } = require('timer-node');\n\n// TS\nimport { Timer, TimerOptions, Time } from 'timer-node';\n```\n\n### Timer Class\n\nA timestamp-based timer that can be started, paused, resumed, and stopped. It calculates durations based on system time.\n\n#### Constructor\n\n```ts\nnew Timer(options?: TimerOptions)\n```\n\n- `options`: An optional configuration object with:\n  - `label?: string` – A label for this timer.\n  - `startTimestamp?: number` – Start time (if you want to initialize from the past).\n  - `endTimestamp?: number` – End time (if already stopped).\n  - `currentStartTimestamp?: number` – Most recent resume time.\n  - `pauseCount?: number` – Number of times paused.\n  - `accumulatedMs?: number` – Accumulated milliseconds from previous runs.\n\n#### Common Methods\n\n- **start()**: Starts (or restarts) the timer.\n- **pause()**: Pauses the timer (if running).\n- **resume()**: Resumes the timer (if paused).\n- **stop()**: Stops the timer.\n- **clear()**: Clears all timer data, resetting it to an unstarted state.\n- **isStarted() / isPaused() / isRunning() / isStopped()**: Check the timer state.\n- **ms()**: Returns the elapsed time in milliseconds.\n- **time()**: Returns an object of time fractions (`d`, `h`, `m`, `s`, `ms`).\n- **pauseMs() / pauseTime()**: Similar to `ms()` / `time()`, but for paused duration.\n- **pauseCount()**: Returns how many times the timer has been paused.\n- **format(template?: string)**: Formats the elapsed time using placeholders (`%label`, `%d`, `%h`, `%m`, `%s`, `%ms`).\n- **serialize()**: Returns a JSON string representing the timer state.\n\n#### Static Methods\n\n- **Timer.deserialize(serializedTimer: string): Timer**\\\n  Re-creates a timer from a serialized string generated by `.serialize()`.\n\n- **Timer.benchmark(fn: () => any): Timer**\\\n  Measures the synchronous execution time of `fn`. Returns a stopped `Timer`.\n\n---\n\n### Time Object\n\nAny method returning an object breakdown of time uses the following shape:\n\n```ts\ninterface Time {\n  d: number;  // days\n  h: number;  // hours\n  m: number;  // minutes\n  s: number;  // seconds\n  ms: number; // milliseconds\n}\n```\n\n## Example\n\n```js\nconst { Timer } = require('timer-node');\n\nconst timer = new Timer({ label: 'demo' });\ntimer.start();\n\n// ... some operations ...\n\ntimer.pause();\nconsole.log('Paused at', timer.ms(), 'ms');\ntimer.resume();\n\n// ... more operations ...\n\ntimer.stop();\nconsole.log('Total elapsed:', timer.time());\n// e.g. { d: 0, h: 0, m: 1, s: 12, ms: 345 }\n```\n\n## License\n\nThis library is licensed under the MIT License. See [LICENSE](https://github.com/eyas-ranjous/timer-node/blob/master/LICENSE) for details.\n\n","publish_time":1737847232684,"maintainers":[{"name":"eyas-ranjous","email":"eyas.ranjous@gmail.com"}],"_cnpm_publish_time":1737847232684,"dist-tags":{"latest":"5.0.9"}}