@madkarma/result 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 madkarmaa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # @madkarma/result
2
+
3
+ Rust's Result type, for TypeScript.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @madkarma/result
9
+ # or
10
+ bun add @madkarma/result
11
+ # or
12
+ pnpm add @madkarma/result
13
+ # or
14
+ yarn add @madkarma/result
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```typescript
20
+ import { Ok, Err } from '@madkarma/result';
21
+
22
+ const getUser = (id: number) => {
23
+ if (typeof id !== 'number')
24
+ return Err({ code: 'NOT_A_NUMBER', message: 'ID must be a number' });
25
+
26
+ if (id < 0)
27
+ return Err({ code: 'INVALID_ID', message: 'ID must be positive' });
28
+
29
+ if (id === 0) return Err({ code: 'NOT_FOUND', message: 'User not found' });
30
+
31
+ return Ok({ id, name: 'Alice' });
32
+ };
33
+
34
+ const [user, error] = getUser(10);
35
+
36
+ if (error)
37
+ switch (error.code) {
38
+ case 'NOT_A_NUMBER':
39
+ console.error(error.message);
40
+ break;
41
+ case 'INVALID_ID':
42
+ console.error(error.message);
43
+ break;
44
+ case 'NOT_FOUND':
45
+ console.error(error.message);
46
+ break;
47
+ }
48
+ else console.log(`Hello, ${user.name}!`);
49
+ ```
50
+
51
+ ### How this differs from Rust
52
+
53
+ Instead of Rust's `match` expressions, check `[value, error]` directly and use a JavaScript `switch` on `error.code` to emulate pattern matching.
54
+
55
+ This library is essentially a type-safe helper for TypeScript developers. It does **not** include any of the chainable methods found in the actual Rust `Result` type. Its sole purpose is to enforce explicit error handling through control flow.
56
+
57
+ ## Contributing
58
+
59
+ Contributions are welcome! Feel free to [open an issue](https://github.com/madkarmaa/result-ts/issues/new) or submit a pull request if you'd like to improve the library or fix any bugs.
60
+
61
+ ## License
62
+
63
+ This project is licensed under the [MIT License](./LICENSE).
@@ -0,0 +1,58 @@
1
+ /**
2
+ * `Result<T, E>` is the type used for returning and propagating errors.
3
+ *
4
+ * It is a type with the parameters, `Ok(T)`, representing success and containing a value,
5
+ * and `Err(E)`, representing error and containing an error value.
6
+ *
7
+ * Functions return `Result` whenever errors are expected and recoverable.
8
+ *
9
+ * The error type `E` must be an object that contains a `code` property of type `string`.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const divide = (a: number, b: number) => {
14
+ * if (b === 0) return Err({ code: 'DIVIDE_BY_ZERO' });
15
+ * return Ok(a / b);
16
+ * }
17
+ *
18
+ * const [value, error] = divide(10, 2);
19
+ * if (error) console.error(error.code);
20
+ * else console.log(value);
21
+ * ```
22
+ *
23
+ * @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
24
+ * @template T - Contains the success value.
25
+ * @template E - Contains the error value. Must have a `code` property of type `string`.
26
+ */
27
+ export type Result<T, E extends {
28
+ code: string;
29
+ }> = [T, null] | [null, E];
30
+ /**
31
+ * Contains the success value.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const result = Ok(42);
36
+ * ```
37
+ *
38
+ * @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
39
+ * @param value - The value to wrap in a successful result.
40
+ * @returns A `Result` representing a successful outcome.
41
+ */
42
+ export declare const Ok: <T>(value: T) => Result<T, never>;
43
+ /**
44
+ * Contains the error value.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const result = Err({ code: 'NOT_FOUND' });
49
+ * ```
50
+ *
51
+ * @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
52
+ * @param error - The error to wrap in a failed result. Must have a `code` property of type `string`.
53
+ * @returns A `Result` representing a failed outcome.
54
+ */
55
+ export declare const Err: <const C extends string, E extends {
56
+ code: C;
57
+ }>(error: E) => Result<never, E>;
58
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;AAE1E;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,EAAE,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAkB,CAAC;AAEnE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,GAAG,GAAI,KAAK,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,EAC7D,OAAO,CAAC,KACT,MAAM,CAAC,KAAK,EAAE,CAAC,CAOjB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Contains the success value.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * const result = Ok(42);
7
+ * ```
8
+ *
9
+ * @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
10
+ * @param value - The value to wrap in a successful result.
11
+ * @returns A `Result` representing a successful outcome.
12
+ */
13
+ export const Ok = (value) => [value, null];
14
+ /**
15
+ * Contains the error value.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const result = Err({ code: 'NOT_FOUND' });
20
+ * ```
21
+ *
22
+ * @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
23
+ * @param error - The error to wrap in a failed result. Must have a `code` property of type `string`.
24
+ * @returns A `Result` representing a failed outcome.
25
+ */
26
+ export const Err = (error) => {
27
+ if (typeof error.code !== 'string')
28
+ throw new TypeError('Error object must have a code property of type string');
29
+ return [null, error];
30
+ };
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4BA;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,CAAI,KAAQ,EAAoB,EAAE,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAEnE;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CACf,KAAQ,EACQ,EAAE;IAClB,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAC9B,MAAM,IAAI,SAAS,CACf,uDAAuD,CAC1D,CAAC;IAEN,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACzB,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@madkarma/result",
3
+ "version": "0.0.1",
4
+ "description": "Rust's Result type, for TypeScript",
5
+ "devDependencies": {
6
+ "prettier": "3.8.3"
7
+ },
8
+ "peerDependencies": {
9
+ "typescript": "^6.0.3"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "default": "./index.js",
15
+ "types": "./index.d.ts"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "dev": "tsc --watch",
25
+ "format": "prettier --write --ignore-unknown .",
26
+ "tags": "git push origin --tags && git push",
27
+ "patch": "bun pm version patch && bun run tags",
28
+ "minor": "bun pm version minor && bun run tags",
29
+ "major": "bun pm version major && bun run tags",
30
+ "postinstall": "git config core.hooksPath .githooks"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/madkarmaa/result-ts.git"
35
+ },
36
+ "license": "MIT"
37
+ }