Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Sep 23, 2024
0 parents commit 7a7264d
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yml
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
20 changes: 20 additions & 0 deletions LICENSE.md
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.
44 changes: 44 additions & 0 deletions README.md
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.
10 changes: 10 additions & 0 deletions deno.json
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"
}
}
42 changes: 42 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createStopwatch, type Stopwatch } from "@/src/createStopwatch.ts";
50 changes: 50 additions & 0 deletions src/createStopwatch.ts
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;
},
};
};
20 changes: 20 additions & 0 deletions src/createStopwatch_test.ts
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);
});

0 comments on commit 7a7264d

Please sign in to comment.