diff --git a/.gitignore b/.gitignore index 564dac4..55e76e1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules build/ coverage +.vscode diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..766bc40 --- /dev/null +++ b/biome.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.4.1/schema.json", + "organizeImports": { + "enabled": true + }, + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + }, + "formatter": { + "indentStyle": "space" + } +} diff --git a/package.json b/package.json index 3447459..1e2f180 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,8 @@ ], "scripts": { "build": "tsc", - "format": "rome format src", - "lint": "rome check src", - "test": "rome ci src && vitest --run --coverage", + "lint": "biome check .", + "test": "biome ci . && vitest --run --coverage", "prepublish": "npm run build" }, "keywords": [ @@ -27,10 +26,10 @@ "author": "Erik Mùˆller", "license": "MIT", "devDependencies": { + "@biomejs/biome": "1.4.1", "@vitest/coverage-c8": "^0.25.3", "typescript": "^4.9.3", - "vitest": "^0.25.3", - "rome": "^10.0.1" + "vitest": "^0.25.3" }, - "type": "module" + "type": "module" } diff --git a/rome.json b/rome.json deleted file mode 100644 index 88bdb1f..0000000 --- a/rome.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "linter": { - "enabled": true, - "rules": { - "recommended": true - } - } -} diff --git a/src/index.spec.ts b/src/index.spec.ts index 40b6ac0..8b7d8f7 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -2,39 +2,39 @@ import { expect, it } from "vitest"; import cond from "./index.js"; it("returns the first value for which the predicate evaluates to true", () => { - const value = cond([ - [false, "false"], - [true, "true"], - [true, "true but too late"], - ]); + const value = cond([ + [false, "false"], + [true, "true"], + [true, "true but too late"], + ]); - expect(value).toBe("true"); + expect(value).toBe("true"); }); it("returns undefined if no predicate evaluates to true", () => { - const value = cond([[false, "false"]]); + const value = cond([[false, "false"]]); - expect(value).toBeUndefined(); + expect(value).toBeUndefined(); }); it("returns provided fallback if no predicate evaluates to true", () => { - const value = cond([[false, "false"]], { - default: () => "fallback", - }); + const value = cond([[false, "false"]], { + default: () => "fallback", + }); - expect(value).toBe("fallback"); + expect(value).toBe("fallback"); }); it("Accepts functions as value for lazy evaluation", () => { - const value = cond([ - [ - false, - () => { - throw new Error("duh!"); - }, - ], - [true, () => "true"], - ]); - - expect(value).toBe("true"); + const value = cond([ + [ + false, + () => { + throw new Error("duh!"); + }, + ], + [true, () => "true"], + ]); + + expect(value).toBe("true"); }); diff --git a/src/index.ts b/src/index.ts index 95aa2c0..9bcf964 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,38 +3,38 @@ type Value = T | (() => T); type Pairs = [boolean, Value][]; type Options = { - /** - * If no match is found, the default is returned instead. - * @default undefined - */ - default?: Value; + /** + * If no match is found, the default is returned instead. + * @default undefined + */ + default?: Value; }; // Ignoring rule to allow for usage of interface // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface Cond { - (pairs: Pairs, options?: F): T | undefined; - >( - pairs: Pairs, - options?: { default: F }, - ): F extends Value ? T : T | undefined; - (pairs: Pairs, options?: Options): T | undefined; + (pairs: Pairs, options?: F): T | undefined; + >( + pairs: Pairs, + options?: { default: F1 }, + ): F1 extends Value ? T1 : T1 | undefined; + (pairs: Pairs, options?: Options): T2 | undefined; } // Manual type guard is necessary, see https://stackoverflow.com/questions/62500700/error-2349-when-dealing-with-union-type-of-generic-t-and-function function isFunction(value: T | (() => T)): value is () => T { - return typeof value === "function"; + return typeof value === "function"; } function processMatch(match: Value): T { - return isFunction(match) ? match() : match; + return isFunction(match) ? match() : match; } const cond: Cond = (pairs: Pairs, options?: Options) => { - const found = pairs.find(([predicate]) => predicate); - const match = found === undefined ? options?.default : found[1]; + const found = pairs.find(([predicate]) => predicate); + const match = found === undefined ? options?.default : found[1]; - return processMatch(match); + return processMatch(match); }; export default cond; diff --git a/vitest.config.ts b/vitest.config.ts index afeb0ce..b86069e 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,10 +1,10 @@ import { configDefaults, defineConfig } from "vitest/config"; export default defineConfig({ - test: { - exclude: [...configDefaults.exclude, "build"], - coverage: { - reporter: ["text", "html", "cobertura"], - }, - }, + test: { + exclude: [...configDefaults.exclude, "build"], + coverage: { + reporter: ["text", "html", "cobertura"], + }, + }, }); diff --git a/yarn.lock b/yarn.lock index 563c767..711167a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7,6 +7,48 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== +"@biomejs/biome@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/biome/-/biome-1.4.1.tgz#b698c67ea8cd8141c8e27f857c8e6e794320a251" + integrity sha512-JccVAwPbhi37pdxbAGmaOBjUTKEwEjWAhl7rKkVVuXHo4MLASXJ5HR8BTgrImi4/7rTBsGz1tgVD1Kwv1CHGRg== + optionalDependencies: + "@biomejs/cli-darwin-arm64" "1.4.1" + "@biomejs/cli-darwin-x64" "1.4.1" + "@biomejs/cli-linux-arm64" "1.4.1" + "@biomejs/cli-linux-x64" "1.4.1" + "@biomejs/cli-win32-arm64" "1.4.1" + "@biomejs/cli-win32-x64" "1.4.1" + +"@biomejs/cli-darwin-arm64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-1.4.1.tgz#75f9c3c9b1abed8836c8f7bc8cd23ba153fb93d1" + integrity sha512-PZWy2Idndqux38p6AXSDQM2ldRAWi32bvb7bMbTN0ALzpWYMYnxd71ornatumSSJYoNhKmxzDLq+jct7nZJ79w== + +"@biomejs/cli-darwin-x64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-darwin-x64/-/cli-darwin-x64-1.4.1.tgz#672fcce2d339de3bb7a7bd2997e94f03121a28a3" + integrity sha512-soj3BWhnsM1M2JlzR09cibUzG1owJqetwj/Oo7yg0foijo9lNH9XWXZfJBYDKgW/6Fomn+CC2EcUS+hisQzt9g== + +"@biomejs/cli-linux-arm64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-arm64/-/cli-linux-arm64-1.4.1.tgz#c816206089ad29ce866c58a6e00e9d3d64a3529d" + integrity sha512-YIZqfJUg4F+fPsBTXxgD7EU2E5OAYbmYSl/snf4PevwfQCWE/omOFZv+NnIQmjYj9I7ParDgcJvanoA3/kO0JQ== + +"@biomejs/cli-linux-x64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-x64/-/cli-linux-x64-1.4.1.tgz#2639daeab1be205cfe444a8d5a3f76aa3a59b956" + integrity sha512-9YOZw3qBd/KUj63A6Hn2zZgzGb2nbESM0qNmeMXgmqinVKM//uc4OgY5TuKITuGjMSvcVxxd4dX1IzYjV9qvNQ== + +"@biomejs/cli-win32-arm64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-win32-arm64/-/cli-win32-arm64-1.4.1.tgz#ed5e749b2e0987cf16b545beaa01be6980ae8ce1" + integrity sha512-nWQbvkNKxYn/kCQ0yVF8kCaS3VzaGvtFSmItXiMknU4521LDjJ7tNWH12Gol+pIslrCbd4E1LhJa0a3ThRsBVg== + +"@biomejs/cli-win32-x64@1.4.1": + version "1.4.1" + resolved "https://registry.yarnpkg.com/@biomejs/cli-win32-x64/-/cli-win32-x64-1.4.1.tgz#dd8ee6e14a5d74cbeb2eb9824a43c61bb5c460e4" + integrity sha512-88fR2CQxQ4YLs2BUDuywWYQpUKgU3A3sTezANFc/4LGKQFFLV2yX+F7QAdZVkMHfA+RD9Xg178HomM/6mnTNPA== + "@esbuild/android-arm@0.15.16": version "0.15.16" resolved "https://registry.yarnpkg.com/@esbuild/android-arm/-/android-arm-0.15.16.tgz#0642926178b15e3d1545efae6eee05c4f3451d15" @@ -40,36 +82,6 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@rometools/cli-darwin-arm64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-darwin-arm64/-/cli-darwin-arm64-10.0.1.tgz#de60ad5879d9b02ca24b707c17f2f8c372624b3c" - integrity sha512-MwQjk3uhZrCu6LgIwJHREAsVt/mUQTGv7p8iosfaF8lCIxMVjyS+akbF/QcBufyW5sFtHYNWUEe/uKPHK4E//A== - -"@rometools/cli-darwin-x64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-darwin-x64/-/cli-darwin-x64-10.0.1.tgz#f4f39e02af164841700cb452b52a111361cef345" - integrity sha512-n010Wc/z9L8wRkRnR5boMhdWgDVGrTG/i7zL8u/3+F5aSUgLCywf9F/b3ex74tCJJfcwBLlhaAqAVQX6U1bIkA== - -"@rometools/cli-linux-arm64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-linux-arm64/-/cli-linux-arm64-10.0.1.tgz#3b3977429873b17459b02db9ee95681bb016a3d6" - integrity sha512-JljZsnud1KCfe36VNsVh/LrYdAzgbKbcsCTzeCjW9ROkMyNj8pmQ/gIUFxZ+PyhMFgowHIDGihoNf4m+pgpxkA== - -"@rometools/cli-linux-x64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-linux-x64/-/cli-linux-x64-10.0.1.tgz#2f01698504c1d634623a7bde6faad09ad2deb1b0" - integrity sha512-jXIqd9iDyZUexk63CRfAXDA4zNDUHpErUmCejjGab3dhDt1KA40fDqKb+kxZpAhY3tQoWNSNQyo750zX5NawLw== - -"@rometools/cli-win32-arm64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-win32-arm64/-/cli-win32-arm64-10.0.1.tgz#2b573f34a33ce477bc4f03bf93358954ea531f05" - integrity sha512-G/toRrKPhhi7SMYMyROq/E2c8/4xRX/67vFhVihuMvDDzhanIb99hEt5MMbM4HbYK1nnZBPyLN6LxVsxm9M9hA== - -"@rometools/cli-win32-x64@10.0.1": - version "10.0.1" - resolved "https://registry.yarnpkg.com/@rometools/cli-win32-x64/-/cli-win32-x64-10.0.1.tgz#f643480fd72e8dfdbc5a3e3ccb27646cd2fb2dc0" - integrity sha512-y299+VGoBufZntZj0Xz7w9DODU+6E5giXStfBDoa0fspXGNkYyYfD+HC6j9gUv4zpMZJ607XVvVHjpfwM/3ftA== - "@types/chai-subset@^1.3.3": version "1.3.3" resolved "https://registry.yarnpkg.com/@types/chai-subset/-/chai-subset-1.3.3.tgz#97893814e92abd2c534de422cb377e0e0bdaac94" @@ -639,18 +651,6 @@ rollup@^2.79.1: optionalDependencies: fsevents "~2.3.2" -rome@^10.0.1: - version "10.0.1" - resolved "https://registry.yarnpkg.com/rome/-/rome-10.0.1.tgz#3974603123b75ecbf98b306bcda8b7fa007c80b0" - integrity sha512-RfaDa+cSustBsjX6bj3fWqEhoNxXrK1uNgKHpkCHAqp20QMJXnCRtbokhirNMe0utyGI9GTO/sDoK7hJP7O8Bw== - optionalDependencies: - "@rometools/cli-darwin-arm64" "10.0.1" - "@rometools/cli-darwin-x64" "10.0.1" - "@rometools/cli-linux-arm64" "10.0.1" - "@rometools/cli-linux-x64" "10.0.1" - "@rometools/cli-win32-arm64" "10.0.1" - "@rometools/cli-win32-x64" "10.0.1" - semver@^6.0.0: version "6.3.1" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"