-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7a7264d
Showing
8 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: set up Deno | ||
uses: denoland/setup-deno@v1 | ||
with: | ||
deno-version: v1.x | ||
- name: run Deno tests | ||
run: deno test | ||
- name: publish to JSR | ||
run: npx jsr publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 github.com/117 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# stopwatch | ||
|
||
![version](https://img.shields.io/jsr/v/%40117/stopwatch?style=flat-square&color=%23ff51bc&label=version) | ||
![status](https://img.shields.io/github/actions/workflow/status/117/stopwatch/deploy.yml?style=flat-square) | ||
|
||
An easy-to-use stopwatch for Deno. | ||
|
||
## Contents | ||
|
||
- [Features](#features) | ||
- [Install](#install) | ||
- [Example](#example) | ||
- [Contributing](#contributing) | ||
|
||
## Features | ||
|
||
- [x] Simple and easy to use. | ||
- [x] Measures elapsed time accurately. | ||
|
||
## Install | ||
|
||
For Deno: | ||
|
||
```sh | ||
$ deno add @117/stopwatch | ||
``` | ||
|
||
## Example | ||
|
||
```ts | ||
import { createStopwatch } from "@117/stopwatch"; | ||
|
||
const stopwatch = createStopwatch(); | ||
|
||
stopwatch.start(); | ||
await new Promise((resolve) => setTimeout(resolve, 150)); | ||
stopwatch.stop(); | ||
|
||
console.log(stopwatch.getElapsedTime()); // 150 | ||
``` | ||
|
||
## Contributing | ||
|
||
Feel free to contribute and PR to your 💖's content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@117/stopwatch", | ||
"description": "An easy-to-use stopwatch for Deno.", | ||
"version": "1.0.0", | ||
"exports": "./mod.ts", | ||
"imports": { | ||
"@/": "./", | ||
"assert": "https://deno.land/std@0.224.0/assert/mod.ts" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createStopwatch, type Stopwatch } from "@/src/createStopwatch.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Represents a stopwatch with start, stop, reset, and elapsed time functionality. | ||
* @property {function(): void} start - Starts the stopwatch. | ||
* @property {function(): void} stop - Stops the stopwatch. | ||
* @property {function(): void} reset - Resets the stopwatch. | ||
* @property {function(): number} getElapsedTime - Returns the elapsed time in milliseconds. | ||
*/ | ||
export type Stopwatch = { | ||
start: () => void; | ||
stop: () => void; | ||
reset: () => void; | ||
getElapsedTime: () => number; | ||
}; | ||
|
||
/** | ||
* Creates a new stopwatch instance. | ||
* @returns {Stopwatch} The stopwatch instance. | ||
*/ | ||
export const createStopwatch = (): Stopwatch => { | ||
let startTime: number | null = null; | ||
let elapsedTime: number = 0; | ||
let running: boolean = false; | ||
|
||
return { | ||
start() { | ||
if (!running) { | ||
startTime = Date.now() - elapsedTime; | ||
running = true; | ||
} | ||
}, | ||
stop() { | ||
if (running) { | ||
elapsedTime = Date.now() - startTime!; | ||
running = false; | ||
} | ||
}, | ||
reset() { | ||
elapsedTime = 0; | ||
if (running) { | ||
startTime = Date.now(); | ||
} | ||
}, | ||
getElapsedTime() { | ||
if (running) { | ||
return Date.now() - startTime!; | ||
} | ||
return elapsedTime; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { assert } from "assert"; | ||
|
||
import { createStopwatch } from "@/src/createStopwatch.ts"; | ||
|
||
Deno.test("can measure elapsed time", () => { | ||
const stopwatch = createStopwatch(); | ||
|
||
stopwatch.start(); | ||
|
||
const timeoutId = setTimeout(() => { | ||
stopwatch.stop(); | ||
const elapsed = stopwatch.getElapsedTime(); | ||
assert(elapsed >= 100); | ||
}, 150); | ||
|
||
stopwatch.reset(); | ||
|
||
assert(stopwatch.getElapsedTime() === 0); | ||
clearTimeout(timeoutId); | ||
}); |