@koerismo/result 0.9.1 → 0.9.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 +35 -35
- package/package.json +31 -26
- package/src/global.ts +9 -9
- package/src/index.ts +55 -55
package/README.md
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
# Result
|
|
2
|
-
A minimal Result interface for TypeScript in a 300-byte package.
|
|
3
|
-
|
|
4
|
-
## Example
|
|
5
|
-
|
|
6
|
-
```ts
|
|
7
|
-
import { Ok, Err } from '@koerismo/result';
|
|
8
|
-
|
|
9
|
-
// Or, use the globals:
|
|
10
|
-
// import '@koerismo/result/global';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// Define a sketchy function...
|
|
14
|
-
function errorProneFunc(i: number) {
|
|
15
|
-
if (i <= 0.1) return Err(-1);
|
|
16
|
-
else return Ok(Math.round(i * 100));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Throw a random number at it...
|
|
20
|
-
const result = errorProneFunc(Math.random());
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// The result can be checked by accessing the "ok" property:
|
|
24
|
-
if (result.ok) console.log('Result is okay!');
|
|
25
|
-
else console.log('Result is error!');
|
|
26
|
-
|
|
27
|
-
// But maybe the result is usable even with an error:
|
|
28
|
-
console.log('Either Value:', result.value);
|
|
29
|
-
|
|
30
|
-
// The value can be unwrapped with a fallback via .unwrapOr(...):
|
|
31
|
-
console.log('Value or Fallback:', result.unwrapOr('john fallback'));
|
|
32
|
-
|
|
33
|
-
// .unwrap() Throws (-1) if the function returned Err(-1).
|
|
34
|
-
console.log('Correct Value:', result.unwrap());
|
|
35
|
-
```
|
|
1
|
+
# Result
|
|
2
|
+
A minimal Result interface for TypeScript in a 300-byte package.
|
|
3
|
+
|
|
4
|
+
## Example
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { Ok, Err } from '@koerismo/result';
|
|
8
|
+
|
|
9
|
+
// Or, use the globals:
|
|
10
|
+
// import '@koerismo/result/global';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// Define a sketchy function...
|
|
14
|
+
function errorProneFunc(i: number) {
|
|
15
|
+
if (i <= 0.1) return Err(-1);
|
|
16
|
+
else return Ok(Math.round(i * 100));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Throw a random number at it...
|
|
20
|
+
const result = errorProneFunc(Math.random());
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
// The result can be checked by accessing the "ok" property:
|
|
24
|
+
if (result.ok) console.log('Result is okay!');
|
|
25
|
+
else console.log('Result is error!');
|
|
26
|
+
|
|
27
|
+
// But maybe the result is usable even with an error:
|
|
28
|
+
console.log('Either Value:', result.value);
|
|
29
|
+
|
|
30
|
+
// The value can be unwrapped with a fallback via .unwrapOr(...):
|
|
31
|
+
console.log('Value or Fallback:', result.unwrapOr('john fallback'));
|
|
32
|
+
|
|
33
|
+
// .unwrap() Throws (-1) if the function returned Err(-1).
|
|
34
|
+
console.log('Correct Value:', result.unwrap());
|
|
35
|
+
```
|
package/package.json
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
"name": "@koerismo/result",
|
|
3
|
+
"description": "A miniscule Result interface for TypeScript.",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/koerismo/result-js.git"
|
|
7
|
+
},
|
|
8
|
+
"version": "0.9.2",
|
|
9
|
+
"author": "Koerismo",
|
|
10
|
+
"license": "MPL-2.0",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.js",
|
|
15
|
+
"./global": "./dist/global.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "pnpm run build-code && pnpm run build-types",
|
|
19
|
+
"build-npm": "npm run build-code && npm run build-types",
|
|
20
|
+
"build-code": "esbuild ./src/* --minify --platform=neutral --sourcemap --sources-content=false --outdir=./dist/",
|
|
21
|
+
"build-types": "tsc"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"result",
|
|
25
|
+
"rust",
|
|
26
|
+
"maybe"
|
|
27
|
+
],
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"esbuild": "^0.27.2",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/global.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Ok as _Ok, Err as _Err } from './index.js';
|
|
2
|
-
|
|
3
|
-
declare global {
|
|
4
|
-
var Ok: typeof _Ok;
|
|
5
|
-
var Err: typeof _Err;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
globalThis.Ok = _Ok;
|
|
9
|
-
globalThis.Err = _Err;
|
|
1
|
+
import { Ok as _Ok, Err as _Err } from './index.js';
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
var Ok: typeof _Ok;
|
|
5
|
+
var Err: typeof _Err;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
globalThis.Ok = _Ok;
|
|
9
|
+
globalThis.Err = _Err;
|
package/src/index.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
/** Shorthand: Represents an ok {@link Result} result. */
|
|
2
|
-
export type OkResult<T> = Result<T, any, true>;
|
|
3
|
-
|
|
4
|
-
/** Shorthand: Represents an error {@link Result} result. */
|
|
5
|
-
export type ErrResult<T> = Result<any, T, false>;
|
|
6
|
-
|
|
7
|
-
/** Used to declare separate ok/error {@link Result} types. */
|
|
8
|
-
export type ResultType<V, E = V> = Result<V, E, true> | Result<V, E, false>;
|
|
9
|
-
|
|
10
|
-
/** Creates a new ok {@link Result} with the provided value. */
|
|
11
|
-
export const Ok = <V>(value: V): OkResult<V> => {
|
|
12
|
-
return new Result(true, value);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Creates a new error {@link Result} with the provided value. */
|
|
16
|
-
export const Err = <E>(error: E): ErrResult<E> => {
|
|
17
|
-
return new Result(false, error);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Represents a value and an associated `ok` value.
|
|
22
|
-
*
|
|
23
|
-
* Use {@link ResultType} instead of this class when writing type annotations!
|
|
24
|
-
*/
|
|
25
|
-
export class Result<V, E = V, O extends boolean = boolean> {
|
|
26
|
-
constructor(
|
|
27
|
-
public readonly ok: O,
|
|
28
|
-
public readonly value: O extends true ? V : E
|
|
29
|
-
) { }
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Returns this Result's value if `ok`. If not, the value of this Result is thrown.
|
|
33
|
-
*/
|
|
34
|
-
unwrap(): V | never {
|
|
35
|
-
if (this.ok) return this.value as V;
|
|
36
|
-
throw this.value;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Returns this Result's value if `ok`. If not, `value` is returned.
|
|
41
|
-
*/
|
|
42
|
-
unwrapOr(): V | undefined;
|
|
43
|
-
unwrapOr<T>(value: T): V | T;
|
|
44
|
-
unwrapOr<T>(value?: T): V | T {
|
|
45
|
-
return this.ok ? this.value as V : value!;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Returns this Result's value if `ok`. If not, `onError` is evaluated
|
|
50
|
-
* with this Result's value as a parameter, and the result is returned.
|
|
51
|
-
*/
|
|
52
|
-
unwrapOrElse<T>(onError: (err: E) => T): V | T {
|
|
53
|
-
return this.ok ? this.value as V : onError(this.value as E);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
1
|
+
/** Shorthand: Represents an ok {@link Result} result. */
|
|
2
|
+
export type OkResult<T> = Result<T, any, true>;
|
|
3
|
+
|
|
4
|
+
/** Shorthand: Represents an error {@link Result} result. */
|
|
5
|
+
export type ErrResult<T> = Result<any, T, false>;
|
|
6
|
+
|
|
7
|
+
/** Used to declare separate ok/error {@link Result} types. */
|
|
8
|
+
export type ResultType<V, E = V> = Result<V, E, true> | Result<V, E, false>;
|
|
9
|
+
|
|
10
|
+
/** Creates a new ok {@link Result} with the provided value. */
|
|
11
|
+
export const Ok = <V>(value: V): OkResult<V> => {
|
|
12
|
+
return new Result(true, value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Creates a new error {@link Result} with the provided value. */
|
|
16
|
+
export const Err = <E>(error: E): ErrResult<E> => {
|
|
17
|
+
return new Result(false, error);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Represents a value and an associated `ok` value.
|
|
22
|
+
*
|
|
23
|
+
* Use {@link ResultType} instead of this class when writing type annotations!
|
|
24
|
+
*/
|
|
25
|
+
export class Result<V, E = V, O extends boolean = boolean> {
|
|
26
|
+
constructor(
|
|
27
|
+
public readonly ok: O,
|
|
28
|
+
public readonly value: O extends true ? V : E
|
|
29
|
+
) { }
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns this Result's value if `ok`. If not, the value of this Result is thrown.
|
|
33
|
+
*/
|
|
34
|
+
unwrap(): V | never {
|
|
35
|
+
if (this.ok) return this.value as V;
|
|
36
|
+
throw this.value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns this Result's value if `ok`. If not, `value` is returned.
|
|
41
|
+
*/
|
|
42
|
+
unwrapOr(): V | undefined;
|
|
43
|
+
unwrapOr<T>(value: T): V | T;
|
|
44
|
+
unwrapOr<T>(value?: T): V | T {
|
|
45
|
+
return this.ok ? this.value as V : value!;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns this Result's value if `ok`. If not, `onError` is evaluated
|
|
50
|
+
* with this Result's value as a parameter, and the result is returned.
|
|
51
|
+
*/
|
|
52
|
+
unwrapOrElse<T>(onError: (err: E) => T): V | T {
|
|
53
|
+
return this.ok ? this.value as V : onError(this.value as E);
|
|
54
|
+
}
|
|
55
|
+
}
|