Skip to content

Commit

Permalink
✨ 🎉 Initial Version of Lib (#1)
Browse files Browse the repository at this point in the history
* 🚧 Current State

* 🐛 Fixed issue with wrong convertions

* ✨ Implemented Debounce Decorator

* ✨ Implemented Throttle Decorator

* 💡 Documented Debounce

* ✨ Implemented Retry Decorator

It ensures the return type stays unchanged.

* 💡 Documented Types + Helpers

* 🔧 Altered TODO Config

* 🚧 Added temporary plans

* ✅ Setup Jest Tests

* 🐛 Fixed Issues with Debounce Decorator

Both with Typings and Operation

* 🚧 Current state of other decorators

* ✅ Wrote basic set of tests for debounce decorator

* ✅ Organized Test in describe

* 🐛 Smaller fixes to type + calling

* ♻️ Corrected Types & Calling

* ✅ Created tests

* ♻️ Fixed up throttle

* 🚚 Moved Tests to folder

* 🐛 Fixed imports

* ✨ Implemented Broadcaster for Metrics

Will be used by Metric Decorators

* ✨ Implemented Counter Decorator

Currently increases metric for every call

* ✨ Implemented Time capture

* ✅ Created Tests for counter

* ♻️ Uses jest.mock for call recording

* 🐛 Should not floor

* ♻️ Minor

* ✨ Started measure decorator

* ✅ Extended Tests

* ✅ Finished Tests for Transformers

* ✨ Created new Counter Decorator and renamed old

* ✨ Created Once Decorator

* 📝 Fixed Typo

* ✅ Addes tests for new counter and renamed old

* ✅ Added test for throttle

* ✅ Added test for once

* 📝 Added Documentation

* ✨ Implemented Gauge Decorator

* ♻️ Exporting Decorators

* ✅ Added predicate tests

* 🔧 Specified Includes

* 🧑‍💻 Added GH Action Syntax Highlighter to Devcontainer

* ♻️ Renamed entry

* 👷 Basic CI Setup

* 👷 Forgot installing deps

* 🔧 Updated Script for CI

* 🔧 Updated Dependabot Config

* 📝 Initial README
  • Loading branch information
Templum committed Mar 15, 2024
1 parent b42c67e commit 1e084b4
Show file tree
Hide file tree
Showing 34 changed files with 5,669 additions and 880 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"kavod-io.vscode-jest-test-adapter",
"ms-vscode.test-adapter-converter",
"hbenl.vscode-test-explorer",
"pflannery.vscode-versionlens"
"pflannery.vscode-versionlens",
"me-dutour-mathieu.vscode-github-actions"
]
}
}
Expand Down
21 changes: 21 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@ updates:
directory: "/"
schedule:
interval: weekly
labels:
- "dependencies"
- "devcontainer"
rebase-strategy: auto
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
- "github-actions"
rebase-strategy: auto
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: weekly
labels:
- "dependencies"
- "npm"
rebase-strategy: auto
open-pull-requests-limit: 5
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI Flow
on:
push:
paths-ignore:
- ".gitignore"
- "LICENSE"
- "README.md"
branches:
- "main"
- "develop"
jobs:
flow:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['18.x', '20.x']
steps:
- uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: Check Lint
run: npm run check:lint
- name: Check Typescript
run: npm run check:tsc
- name: Run Unit Tests
run: npm run test:unit
30 changes: 30 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: PR Flow
on: [pull_request]
jobs:
flow:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['18.x', '20.x']
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: Check Lint
run: npm run check:lint
- name: Check Typescript
run: npm run check:tsc
- name: Run Unit Tests
run: npm run test:unit
- uses: ArtiomTr/jest-coverage-report-action@v2
with:
package-manager: npm
continue-on-error: true

25 changes: 25 additions & 0 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Prepare Release
on:
workflow_dispatch:
inputs:
new_version:
required: true
description: "SEMVER Version of the next release"
type: string
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: "Generate Changelog"
uses: sercanuste/gitmoji-changelog-action@v1
with:
add_author: true
- uses: rickstaa/action-create-tag@v1
id: "tag_create"
with:
tag: "${{ github.event.inputs.new_version }}"
message: ":bookmark: Releasing ${{ github.event.inputs.new_version }}"
tag_exists_error: true
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PR Flow
on:
push: # Only trigger for tags with format v****
tags:
- v[0-9]+.[0-9]+.[0-9]+
jobs:
flow:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: "20.x"
- run: npm ci
- run: npm run check:tsc
- run: npm run test:unit
- run: npx jsr publish



23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runInBand",
"false"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"HACK",
"FIXME",
"TODO",
"IDEA",
],
"todo-tree.general.statusBar": "current file",
}
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @templum/decorators

This library is exclusively published in the JavaScript Registry ([JSR](https://jsr.io/)) and offers a variety of decorators with different functionalities. They are based on TypeScript 5.0 decorators and don't require the legacy flag.

## Installation

Deno:
```sh
deno add @templum/decorators
```

NPM (use any of npx, yarn dlx, pnpm dlx, or bunx):
```sh
npx jsr add @templum/decorators
```

Afterwards can be used like any ESModule:
```TypeScript
import { Once } from "@templum/decorators";

class Test {
@Once()
public init(): void {
...
}
}
```

## Documentation

Follows...
52 changes: 52 additions & 0 deletions __tests__/execution/debounce.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Debounce } from "../../lib/execution/debounce.js";
import { UnitOfTime } from "../../lib/util/types.js";

class Test {
constructor(private spy: jest.Mock){}

@Debounce(5, UnitOfTime.Millisecond)
public increment(): void {
this.spy();
}
}

describe('Debounce Decorator', () => {
test('should call through if not in debounce window', async () => {
const callRecorder = jest.fn();
const target = new Test(callRecorder);

target.increment();
expect(callRecorder).toHaveBeenCalledTimes(1);
});

test('should block additional calls while in debounce window', async () => {
const callRecorder = jest.fn();
const target = new Test(callRecorder);

target.increment();
target.increment();
target.increment();
target.increment();
target.increment();

expect(callRecorder).toHaveBeenCalledTimes(1);
});

test('should allow a call after debounce window passed', async () => {
const callRecorder = jest.fn();
const target = new Test(callRecorder)

target.increment();
target.increment();
target.increment();

expect(callRecorder).toHaveBeenCalledTimes(1);
await new Promise((resolve) => setTimeout(resolve, 10));

target.increment();
target.increment();
target.increment();

expect(callRecorder).toHaveBeenCalledTimes(2);
});
});
38 changes: 38 additions & 0 deletions __tests__/execution/once.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Once } from "../../lib/execution/once";


class Test {
constructor(private spy: jest.Mock){}

@Once()
public init(): void {
this.spy();
}

@Once(true)
public strictInit(): void {
this.spy();
}
}


describe('Once Decorator', () => {
test('should call original method once and drop further calls if force=false', async () => {
const callRecorder = jest.fn();
const target = new Test(callRecorder);

target.init();
target.init();

expect(callRecorder).toHaveBeenCalledTimes(1);
});

test('should call original method once and raise error for further calls if force=true', async () => {
const callRecorder = jest.fn();
const target = new Test(callRecorder);

target.strictInit();
expect(() => target.strictInit()).toThrow(`strictInit is only callable once and was already called`);
expect(callRecorder).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit 1e084b4

Please sign in to comment.