Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Switch to TC39 Decorators #171

Merged
merged 13 commits into from
Jun 27, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/nine-cars-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"moderndash": minor
---

Introduce type-fest dependency to improve various function types
5 changes: 5 additions & 0 deletions .changeset/wild-fans-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"moderndash": major
---

Breaking | Bump required node version to >=18.x
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}

- name : Use Node.js 20.x
- name : Use Node.js 22.x
uses : actions/setup-node@v4
with :
node-version : 20.x
node-version : 22.x
cache: npm

- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Node.js 20.x
- name: Setup Node.js 22.x
uses: actions/setup-node@v4
with:
node-version: 20.x
node-version: 22.x
cache: npm

- name: Install Dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x, 20.x, 21.x]
node-version: [18.x, 20.x, 22.x]

steps:
- name: Checkout Repo
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"LICENSE": true,
".npmrc": true
},
"vitest.commandLine": "npx vitest --root package"
"vitest.rootConfig": "package/vitest.config.ts",
}
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
✅ Typescript Strict Mode (no any)
<br>
✅ 100% Test Coverage
✅ Zero dependencies
✅ Zero runtime dependencies
✅ Hoverable Docs
✅ TS Decorators
</div>
<p></p>

Expand Down Expand Up @@ -57,13 +56,11 @@ npm install moderndash
```
## 📋 Requirements

**NodeJS**: >=16.15 | **Typescript**: >=4.8
**NodeJS**: >=18.x | **Typescript**: >=5.0

> `NodeJS 16-18`: Enable the [experimental-global-webcrypto](https://nodejs.dev/en/api/v16/cli#--experimental-global-webcrypto) flag to use crypto functions.
*Works out of the box with NodeJS 19+*

> `TypeScript`: Enable the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to use decorator functions.

## 🚀 Performance

ModernDash aims to outperform Lodash and deliver lightning-fast utility functions. Performance is not an afterthought, but a top priority. You can expect ModernDash to exceed or at least match the performance of Lodash in most benchmarks, ensuring that your project stays speedy and efficient.
Expand Down
1 change: 0 additions & 1 deletion benchmark/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["ESNext", "DOM"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"module": "ESNext", /* Specify what module code is generated. */
Expand Down
3,563 changes: 1,853 additions & 1,710 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
"devDependencies": {
"@svitejs/changesets-changelog-github-compact": "1.1.0",
"@tsconfig/strictest": "2.0.5",
"@changesets/cli": "2.27.1",
"@changesets/cli": "2.27.6",
"eslint-config-dewald": "1.0.17",
"eslint": "8.57.0",
"typedoc": "0.25.13",
"typescript": "5.4.5",
"typedoc": "0.26.2",
"typescript": "5.5.2",
"vitest": "1.6.0",
"@vitest/coverage-v8": "1.6.0",
"@vitest/ui": "1.6.0",
"vite": "5.2.11"
"vite": "5.3.2",
"vite-tsconfig-paths": "4.3.2"
}
}
7 changes: 4 additions & 3 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"description": "A Typescript-First utility library inspired by Lodash. Optimized for modern browsers.",
"engines": {
"node": ">=16.15",
"node": ">=18",
"npm": ">=8"
},
"sideEffects": false,
Expand Down Expand Up @@ -52,11 +52,12 @@
],
"homepage": "https://moderndash.io",
"dependencies": {
"hotscript": "1.0.13"
"hotscript": "1.0.13",
"type-fest": "4.20.1"
},
"devDependencies": {
"barrelsby": "2.8.1",
"tsup": "8.0.2",
"tsup": "8.1.0",
"cross-env": "7.0.3"
}
}
2 changes: 0 additions & 2 deletions package/src/decorator/decDebounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { debounce } from "@function/debounce.js";
*
* Look at {@link debounce} for the non-decorator version.
*
* *Requires the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to be set.*
*
* @example
* ```typescript
* class TestClass {
Expand Down
2 changes: 0 additions & 2 deletions package/src/decorator/decMaxCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { maxCalls } from "@function/maxCalls.js";
*
* Look at {@link maxCalls} for the non-decorator version.
*
* *Requires the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to be set.*
*
* @example
* ```typescript
* class TestClass {
Expand Down
2 changes: 0 additions & 2 deletions package/src/decorator/decMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { memoize } from "@function/memoize.js";
*
* Look at {@link memoize} for the non-decorator version.
*
* *Requires the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to be set.*
*
* @example
* ```typescript
* class TestClass {
Expand Down
1 change: 0 additions & 1 deletion package/src/decorator/decMinCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { minCalls } from "@function/minCalls.js";
*
* Look at {@link minCalls} for the non-decorator version.
*
* *Requires the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to be set.*
* @example
* ```typescript
* class TestClass {
Expand Down
2 changes: 0 additions & 2 deletions package/src/decorator/decThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { throttle } from "@function/throttle.js";
*
* Look at {@link throttle} for the non-decorator version.
*
* *Requires the [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) flag to be set.*
*
* @example
* ```typescript
* class TestClass {
Expand Down
8 changes: 4 additions & 4 deletions package/src/decorator/toDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type Tail<T extends unknown[]> = T extends [infer _Head, ...infer Tail] ? Tail :
* @param func The function to transform.
* @returns A decorator function that can be used to decorate a method.
*/

// waiting for https://github.com/evanw/esbuild/issues/104
export function toDecorator<TFunc extends GenericFunction<TFunc>>(func: TFunc) {
return function (...args: Tail<Parameters<TFunc>>) {
return function (_target: unknown, _key: string, descriptor: PropertyDescriptor) {
const creatorArgs = [descriptor.value, ...args] as Parameters<TFunc>;
descriptor.value = func(...creatorArgs);
return function (originalMethod: unknown, _context: ClassMethodDecoratorContext) {
const funcArgs = [originalMethod, ...args] as Parameters<TFunc>;
return func(...funcArgs);
};
};
}
1 change: 0 additions & 1 deletion package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
"types": ["vitest/globals"],
"experimentalDecorators": true,
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["ESNext", "DOM"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"module": "ESNext", /* Specify what module code is generated. */
Expand Down
23 changes: 5 additions & 18 deletions package/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
/// <reference types="vitest" />
// Configure Vitest (https://vitest.dev/config/)

import { fileURLToPath, URL } from "node:url";

import tsconfigPaths from "vite-tsconfig-paths";
import { defineConfig } from "vitest/config";

const getPath = (path: string) => fileURLToPath(new URL(path, import.meta.url));

export default defineConfig({
plugins: [tsconfigPaths()],
test: {
globals: true,
coverage: {
exclude: ["**/index.ts", "src/type", "test/**", "src/helpers/ArrayTypeUtils.ts"]
}
},
resolve: {
alias: {
"@array": getPath("src/array"),
"@object": getPath("src/object"),
"@function": getPath("src/function"),
"@string": getPath("src/string"),
"@crypto": getPath("src/crypto"),
"@number": getPath("src/number"),
"@helpers": getPath("src/helpers"),
"@promise": getPath("src/promise"),
"@validate": getPath("src/validate"),
"@type": getPath("src/type"),
"@decorator": getPath("src/decorator")
}
// Workaround: Remove when vitest supports decorators
esbuild: {
target: "es2022"
}
});