@serkonda7/ts-result 1.0.0 → 1.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.
- package/README.md +61 -0
- package/dist/scripts/build.d.ts +1 -0
- package/dist/src/result.d.ts +33 -0
- package/dist/src/result.test.d.ts +1 -0
- package/package.json +16 -13
package/README.md
CHANGED
|
@@ -1 +1,62 @@
|
|
|
1
1
|
# ts-result
|
|
2
|
+
[![CI][ci-badge]][ci-status]
|
|
3
|
+
[![npm version][npm-badge]][npm-link]
|
|
4
|
+
[![npm updated][npm-date-badge]][npm-link]
|
|
5
|
+
|
|
6
|
+
Minimalistic Result type for TypeScript.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
```sh
|
|
11
|
+
npm install @serkonda7/ts-result
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## 🚀 Usage
|
|
16
|
+
```ts
|
|
17
|
+
import { ok, err, unwrap, type Result } from '@serkonda7/ts-result'
|
|
18
|
+
|
|
19
|
+
// To use, wrap a functions return type into `Result<>`
|
|
20
|
+
function parsePort (input: string): Result<number> {
|
|
21
|
+
const port = Number.parseInt(input, 10)
|
|
22
|
+
|
|
23
|
+
if (!Number.isInteger(port)) {
|
|
24
|
+
// Return an error result by using the `err()` helper
|
|
25
|
+
return err('PORT must be an integer')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (port < 1 || port > 65535) {
|
|
29
|
+
// It supports strings, Error classes or any custom object
|
|
30
|
+
return err(new Error('PORT must be between 1 and 65535'))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Return an successful result using the `ok()` helper
|
|
34
|
+
return ok(port)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const portResult = parsePort(process.env.PORT)
|
|
38
|
+
|
|
39
|
+
// Check the result for an error:
|
|
40
|
+
if (portResult.error) {
|
|
41
|
+
console.error(portResult.error)
|
|
42
|
+
process.exit(1)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Use the value afterwards
|
|
46
|
+
let port = portResult.value
|
|
47
|
+
|
|
48
|
+
// Use the `unwrap()` helper to throw any error or return the value
|
|
49
|
+
const port = unwrap(portResult)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## 📜 License
|
|
54
|
+
This repo is licensed under the [MIT License](LICENSE.txt).
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
<!-- links -->
|
|
58
|
+
[ci-badge]: https://github.com/serkonda7/ts-result/actions/workflows/ci.yml/badge.svg
|
|
59
|
+
[ci-status]: https://github.com/serkonda7/ts-result/actions/workflows/ci.yml
|
|
60
|
+
[npm-badge]: https://nodei.co/npm/@serkonda7/ts-result.svg?style=shields&data=v&color=blue
|
|
61
|
+
[npm-date-badge]: https://nodei.co/npm/@serkonda7/ts-result.svg?style=shields&data=u,d&color=blue
|
|
62
|
+
[npm-link]: https://www.npmjs.com/package/@serkonda7/ts-result
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result utilities inspired by Rust's Result<T, E> type.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a way to enforce explicit error handling.
|
|
5
|
+
*/
|
|
6
|
+
/** */
|
|
7
|
+
type Ok<V> = {
|
|
8
|
+
value: V;
|
|
9
|
+
error?: never;
|
|
10
|
+
};
|
|
11
|
+
type Err<E> = {
|
|
12
|
+
error: E;
|
|
13
|
+
value?: never;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Union type representing either success or failure.
|
|
17
|
+
*/
|
|
18
|
+
export type Result<V, E = Error> = Ok<V> | Err<E>;
|
|
19
|
+
/**
|
|
20
|
+
* Helper to create a successful Result.
|
|
21
|
+
*/
|
|
22
|
+
export declare const ok: <V>(value: V) => Ok<V>;
|
|
23
|
+
/**
|
|
24
|
+
* Helper to create a failed Result.
|
|
25
|
+
*/
|
|
26
|
+
export declare const err: <E>(error: E) => Err<E>;
|
|
27
|
+
/**
|
|
28
|
+
* Returns the `value` from a Result.
|
|
29
|
+
*
|
|
30
|
+
* In case of an error, it is thrown.
|
|
31
|
+
*/
|
|
32
|
+
export declare function unwrap<V, E>(result: Result<V, E>): V;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serkonda7/ts-result",
|
|
3
3
|
"description": "Minimalistic Result type for TypeScript.",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "serkonda7",
|
|
7
7
|
"homepage": "https://github.com/serkonda7/ts-result",
|
|
@@ -12,24 +12,27 @@
|
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/serkonda7/ts-result/issues"
|
|
14
14
|
},
|
|
15
|
-
"keywords": [],
|
|
15
|
+
"keywords": ["result", "typescript", "error-handling"],
|
|
16
16
|
"type": "module",
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
],
|
|
17
|
+
"main": "dist/result.js",
|
|
18
|
+
"types": "dist/result.d.ts",
|
|
19
|
+
"files": ["dist/"],
|
|
21
20
|
"scripts": {
|
|
22
|
-
"lint
|
|
21
|
+
"lint": "biome check --write",
|
|
23
22
|
"lint:ci": "biome ci",
|
|
24
|
-
"check": "
|
|
25
|
-
"
|
|
23
|
+
"check": "tsgo --noEmit",
|
|
24
|
+
"test": "bun test",
|
|
25
|
+
"build": "tsgo && bun run scripts/build.ts",
|
|
26
26
|
"prepack": "bun run build"
|
|
27
27
|
},
|
|
28
|
-
"
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
},
|
|
31
|
+
"packageManager": "bun@1.3.11",
|
|
29
32
|
"dependencies": {},
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"@biomejs/biome": "
|
|
32
|
-
"@types/bun": "~1.3.
|
|
33
|
-
"@typescript/native-preview": "~7.0.0-dev.
|
|
34
|
+
"@biomejs/biome": "2.4.10",
|
|
35
|
+
"@types/bun": "~1.3.11",
|
|
36
|
+
"@typescript/native-preview": "~7.0.0-dev.20260325.1"
|
|
34
37
|
}
|
|
35
38
|
}
|