@mkvlrn/result 1.1.0

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) 2025 Mike Valeriano <mkvlrn@proton.me>
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,43 @@
1
+ # Result
2
+
3
+ A lightweight, zero-dependency TypeScript utility for type-safe error handling using the Result pattern.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Using yarn
9
+ yarn add @mkvlrn/result
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ts
15
+ import { Result } from "@mkvlrn/result";
16
+
17
+ // Success case
18
+ const success = Result.success(42);
19
+ if (success.ok) {
20
+ console.log(success.value); // 42
21
+ }
22
+
23
+ // Error case
24
+ const error = Result.error(new Error("Something went wrong"));
25
+ if (!error.ok) {
26
+ console.log(error.error.message); // "Something went wrong"
27
+ }
28
+
29
+ // Custom error type
30
+ type ValidationError = { field: string; message: string };
31
+ const validationError = Result.error<ValidationError>({
32
+ field: "email",
33
+ message: "Invalid email format",
34
+ });
35
+ ```
36
+
37
+ ## Features
38
+
39
+ - Type-safe error handling
40
+ - Zero dependencies
41
+ - Simple API
42
+ - Full TypeScript support
43
+ - Tiny bundle size
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This module exports a Result type and helper functions that represent the outcome of an operation.
3
+ * @module
4
+ */
5
+ /**
6
+ * Result type to represent the outcome of an operation.
7
+ * It can either be a success with a value or an error with an error message.
8
+ * This is a generic type that can be used with any type of value and error.
9
+ */
10
+ export type Result<T, E = Error> = {
11
+ readonly ok: true;
12
+ readonly value: T;
13
+ } | {
14
+ readonly ok: false;
15
+ readonly error: E;
16
+ };
17
+ /**
18
+ * Helper functions to create Result objects.
19
+ */
20
+ export declare const Result: {
21
+ success<T>(value: T): Result<T, never>;
22
+ error<E = Error>(error: E): Result<never, E>;
23
+ };
package/build/index.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * This module exports a Result type and helper functions that represent the outcome of an operation.
3
+ * @module
4
+ */
5
+ /**
6
+ * Creates a successful Result with the given value.
7
+ * @param value The success value
8
+ * @returns A Result object representing success
9
+ */
10
+ function success(value) {
11
+ return { ok: true, value: value };
12
+ }
13
+ /**
14
+ * Creates a successful Result with the given value.
15
+ * @param value The success value
16
+ * @returns A Result object representing success
17
+ */
18
+ function error(error) {
19
+ return { ok: false, error: error };
20
+ }
21
+ /**
22
+ * Helper functions to create Result objects.
23
+ */
24
+ export const Result = {
25
+ success: success,
26
+ error: error,
27
+ };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@mkvlrn/result",
3
+ "version": "1.1.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public",
8
+ "registry": "https://registry.npmjs.org"
9
+ },
10
+ "exports": {
11
+ "types": "./build/index.d.ts",
12
+ "default": "./build/index.js"
13
+ },
14
+ "files": [
15
+ "build"
16
+ ],
17
+ "scripts": {
18
+ "build": "rm -rf build && tsc"
19
+ },
20
+ "devDependencies": {
21
+ "@biomejs/biome": "^2.0.0-beta.1",
22
+ "typescript": "^5.8.2"
23
+ }
24
+ }