@mkvlrn/result 4.0.19 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,17 +11,17 @@ pnpm add @mkvlrn/result
11
11
  ## Usage
12
12
 
13
13
  ```typescript
14
- import { Result, AsyncResult, R } from "@mkvlrn/result";
14
+ import { Result, AsyncResult, ok, err } from "@mkvlrn/result";
15
15
 
16
16
  // Success
17
- const success = R.ok(42);
17
+ const success = ok(42);
18
18
 
19
19
  // Error
20
- const failure = R.error(new Error("Something went wrong"));
20
+ const failure = err(new Error("Something went wrong"));
21
21
 
22
22
  // Check result
23
- const result = R.ok(42);
24
- if (result.error) {
23
+ const result = ok(42);
24
+ if (result.isError) {
25
25
  console.log("Error:", result.error.message);
26
26
  } else {
27
27
  console.log("Value:", result.value);
@@ -35,13 +35,13 @@ if (result.error) {
35
35
  ```typescript
36
36
  function divide(a: number, b: number): Result<number, Error> {
37
37
  if (b === 0) {
38
- return R.error(new Error("Division by zero"));
38
+ return err(new Error("Division by zero"));
39
39
  }
40
- return R.ok(a / b);
40
+ return ok(a / b);
41
41
  }
42
42
 
43
43
  const result = divide(10, 2);
44
- if (!result.error) {
44
+ if (!result.isError) {
45
45
  console.log(result.value); // 5
46
46
  }
47
47
  ```
@@ -53,12 +53,12 @@ async function fetchUser(id: number): AsyncResult<User, Error> {
53
53
  try {
54
54
  const response = await fetch(`/api/users/${id}`);
55
55
  if (!response.ok) {
56
- return R.error(new Error(`HTTP ${response.status}`));
56
+ return err(new Error(`HTTP ${response.status}`));
57
57
  }
58
58
  const user = await response.json();
59
- return R.ok(user);
59
+ return ok(user);
60
60
  } catch (error) {
61
- return R.error(error instanceof Error ? error : new Error("Unknown error"));
61
+ return err(error instanceof Error ? error : new Error("Unknown error"));
62
62
  }
63
63
  }
64
64
  ```
@@ -78,13 +78,13 @@ class ValidationError extends Error {
78
78
 
79
79
  function validateEmail(email: string): Result<string, ValidationError> {
80
80
  if (!email.includes("@")) {
81
- return Result.error(new ValidationError(400, "custom"));
81
+ return err(new ValidationError(400, "custom"));
82
82
  }
83
- return Result.ok(email);
83
+ return ok(email);
84
84
  }
85
85
 
86
86
  const result = validateEmail("invalid-email");
87
- if (result.error) {
87
+ if (result.isError) {
88
88
  console.log(`${result.error.customField}: ${result.error.message}`);
89
89
  }
90
90
  ```
package/build/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ //#region src/index.d.ts
1
2
  /**
2
3
  * Result type to represent the outcome of an operation.
3
4
  * It can either be a success with a value or an error.
@@ -6,33 +7,30 @@
6
7
  * It is also an alias object containing the ok and error functions to
7
8
  * make it easier to create Result objects.
8
9
  */
9
- export type Result<T, E extends Error> = {
10
- readonly isError: false;
11
- readonly isOk: true;
12
- readonly value: T;
10
+ type Result<T, E extends Error> = {
11
+ readonly isError: false;
12
+ readonly isOk: true;
13
+ readonly value: T;
13
14
  } | {
14
- readonly isError: true;
15
- readonly isOk: false;
16
- readonly error: E;
15
+ readonly isError: true;
16
+ readonly isOk: false;
17
+ readonly error: E;
17
18
  };
18
19
  /**
19
20
  * Async version of Result type that wraps a Result in a Promise.
20
21
  */
21
- export type AsyncResult<T, E extends Error> = Promise<Result<T, E>>;
22
+ type AsyncResult<T, E extends Error> = Promise<Result<T, E>>;
22
23
  /**
23
- * Result utility functions for creating Result objects.
24
+ * Creates a successful Result with the given value.
25
+ * @param value The success value
26
+ * @returns A Result object representing success
24
27
  */
25
- export declare const R: {
26
- /**
27
- * Creates a successful Result with the given value.
28
- * @param value The success value
29
- * @returns A Result object representing success
30
- */
31
- ok<T>(value: T): Result<T, never>;
32
- /**
33
- * Creates an error Result with the given error.
34
- * @param error The error value
35
- * @returns A Result object representing error
36
- */
37
- error<E extends Error>(error: E): Result<never, E>;
38
- };
28
+ declare function ok<T>(value: T): Result<T, never>;
29
+ /**
30
+ * Creates an error Result with the given error.
31
+ * @param error The error value
32
+ * @returns A Result object representing error
33
+ */
34
+ declare function err<E extends Error>(error: E): Result<never, E>;
35
+ //#endregion
36
+ export { AsyncResult, Result, err, ok };
package/build/index.js CHANGED
@@ -1,7 +1 @@
1
- /**
2
- * Result utility functions for creating Result objects.
3
- */
4
- export const R = {
5
- ok: (value) => ({ isError: false, isOk: true, value }),
6
- error: (error) => ({ isError: true, isOk: false, error }),
7
- };
1
+ function e(e){return{isError:!1,isOk:!0,value:e}}function t(e){return{isError:!0,isOk:!1,error:e}}export{t as err,e as ok};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mkvlrn/result",
3
3
  "description": "Simple Result type/pattern for TypeScript",
4
- "version": "4.0.19",
4
+ "version": "5.0.1",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "author": "Mike Valeriano <mkvlrn@proton.me>",
@@ -15,7 +15,7 @@
15
15
  "result"
16
16
  ],
17
17
  "engines": {
18
- "node": "22.x"
18
+ "node": "24"
19
19
  },
20
20
  "files": [
21
21
  "build"
@@ -25,18 +25,20 @@
25
25
  "default": "./build/index.js"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^24.7.2",
29
- "lint-staged": "^16.2.4",
30
- "rimraf": "^6.0.1",
28
+ "@types/node": "^24.10.0",
29
+ "lint-staged": "^16.2.6",
30
+ "rimraf": "^6.1.0",
31
31
  "tsc-files": "^1.1.4",
32
+ "tsdown": "^0.16.1",
32
33
  "tsx": "^4.20.6",
33
- "typescript": "^5.9.3"
34
+ "typescript": "^5.9.3",
35
+ "vite-tsconfig-paths": "^5.1.4"
34
36
  },
35
37
  "dependencies": {
36
- "vitest": "^3.2.4"
38
+ "vitest": "^4.0.8"
37
39
  },
38
40
  "scripts": {
39
- "build": "rimraf build && tsc -p tsconfig.build.json",
41
+ "build": "tsdown",
40
42
  "typecheck": "tsc"
41
43
  }
42
44
  }