@mkvlrn/result 5.0.0 → 5.0.2

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.
Files changed (2) hide show
  1. package/README.md +25 -14
  2. package/package.json +8 -8
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Type-safe Result pattern for TypeScript representing success or error. Anything to avoid try/catch hell.
4
4
 
5
+ [![npm](https://img.shields.io/npm/v/@mkvlrn/result)](https://www.npmjs.com/package/@mkvlrn/result)
6
+
5
7
  ## Installation
6
8
 
7
9
  ```bash
@@ -11,23 +13,32 @@ pnpm add @mkvlrn/result
11
13
  ## Usage
12
14
 
13
15
  ```typescript
14
- import { Result, AsyncResult, R } from "@mkvlrn/result";
16
+ import { Result, AsyncResult, ok, err } from "@mkvlrn/result";
15
17
 
16
18
  // Success
17
- const success = R.ok(42);
19
+ const success = ok(42);
18
20
 
19
21
  // Error
20
- const failure = R.error(new Error("Something went wrong"));
22
+ const failure = err(new Error("Something went wrong"));
21
23
 
22
24
  // Check result
23
- const result = R.ok(42);
24
- if (result.error) {
25
+ const result = ok(42);
26
+ if (result.isError) {
25
27
  console.log("Error:", result.error.message);
26
28
  } else {
27
29
  console.log("Value:", result.value);
28
30
  }
29
31
  ```
30
32
 
33
+ ## API
34
+
35
+ | Export | Description |
36
+ | ------------------- | -------------------------------------------- |
37
+ | `Result<T, E>` | Union type representing success or failure |
38
+ | `AsyncResult<T, E>` | `Promise<Result<T, E>>` for async operations |
39
+ | `ok(value)` | Creates a success result |
40
+ | `err(error)` | Creates an error result |
41
+
31
42
  ## Examples
32
43
 
33
44
  ### Basic Function
@@ -35,13 +46,13 @@ if (result.error) {
35
46
  ```typescript
36
47
  function divide(a: number, b: number): Result<number, Error> {
37
48
  if (b === 0) {
38
- return R.error(new Error("Division by zero"));
49
+ return err(new Error("Division by zero"));
39
50
  }
40
- return R.ok(a / b);
51
+ return ok(a / b);
41
52
  }
42
53
 
43
54
  const result = divide(10, 2);
44
- if (!result.error) {
55
+ if (result.isOk) {
45
56
  console.log(result.value); // 5
46
57
  }
47
58
  ```
@@ -53,12 +64,12 @@ async function fetchUser(id: number): AsyncResult<User, Error> {
53
64
  try {
54
65
  const response = await fetch(`/api/users/${id}`);
55
66
  if (!response.ok) {
56
- return R.error(new Error(`HTTP ${response.status}`));
67
+ return err(new Error(`HTTP ${response.status}`));
57
68
  }
58
69
  const user = await response.json();
59
- return R.ok(user);
70
+ return ok(user);
60
71
  } catch (error) {
61
- return R.error(error instanceof Error ? error : new Error("Unknown error"));
72
+ return err(error instanceof Error ? error : new Error("Unknown error"));
62
73
  }
63
74
  }
64
75
  ```
@@ -78,13 +89,13 @@ class ValidationError extends Error {
78
89
 
79
90
  function validateEmail(email: string): Result<string, ValidationError> {
80
91
  if (!email.includes("@")) {
81
- return Result.error(new ValidationError(400, "custom"));
92
+ return err(new ValidationError(400, "custom"));
82
93
  }
83
- return Result.ok(email);
94
+ return ok(email);
84
95
  }
85
96
 
86
97
  const result = validateEmail("invalid-email");
87
- if (result.error) {
98
+ if (result.isError) {
88
99
  console.log(`${result.error.customField}: ${result.error.message}`);
89
100
  }
90
101
  ```
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": "5.0.0",
4
+ "version": "5.0.2",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "author": "Mike Valeriano <mkvlrn@proton.me>",
@@ -25,17 +25,17 @@
25
25
  "default": "./build/index.js"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^24.10.0",
29
- "lint-staged": "^16.2.6",
30
- "rimraf": "^6.1.0",
28
+ "@types/node": "^25.0.3",
29
+ "lint-staged": "^16.2.7",
30
+ "rimraf": "^6.1.2",
31
31
  "tsc-files": "^1.1.4",
32
- "tsdown": "^0.16.1",
33
- "tsx": "^4.20.6",
32
+ "tsdown": "^0.18.2",
33
+ "tsx": "^4.21.0",
34
34
  "typescript": "^5.9.3",
35
- "vite-tsconfig-paths": "^5.1.4"
35
+ "vite-tsconfig-paths": "^6.0.3"
36
36
  },
37
37
  "dependencies": {
38
- "vitest": "^4.0.8"
38
+ "vitest": "^4.0.16"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsdown",