@koerismo/result 0.9.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/README.md +35 -0
- package/dist/global.d.ts +5 -0
- package/dist/global.js +2 -0
- package/dist/global.js.map +6 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +6 -0
- package/package.json +27 -0
- package/src/global.ts +9 -0
- package/src/index.ts +55 -0
package/README.md
ADDED
|
@@ -0,0 +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
|
+
```
|
package/dist/global.d.ts
ADDED
package/dist/global.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Shorthand: Represents an ok {@link Result} result. */
|
|
2
|
+
export type OkResult<T> = Result<T, any, true>;
|
|
3
|
+
/** Shorthand: Represents an error {@link Result} result. */
|
|
4
|
+
export type ErrResult<T> = Result<any, T, false>;
|
|
5
|
+
/** Used to declare separate ok/error {@link Result} types. */
|
|
6
|
+
export type ResultType<V, E = V> = Result<V, E, true> | Result<V, E, false>;
|
|
7
|
+
/** Creates a new ok {@link Result} with the provided value. */
|
|
8
|
+
export declare const Ok: <V>(value: V) => OkResult<V>;
|
|
9
|
+
/** Creates a new error {@link Result} with the provided value. */
|
|
10
|
+
export declare const Err: <E>(error: E) => ErrResult<E>;
|
|
11
|
+
/**
|
|
12
|
+
* Represents a value and an associated `ok` value.
|
|
13
|
+
*
|
|
14
|
+
* Use {@link ResultType} instead of this class when writing type annotations!
|
|
15
|
+
*/
|
|
16
|
+
export declare class Result<V, E = V, O extends boolean = boolean> {
|
|
17
|
+
readonly ok: O;
|
|
18
|
+
readonly value: O extends true ? V : E;
|
|
19
|
+
constructor(ok: O, value: O extends true ? V : E);
|
|
20
|
+
/**
|
|
21
|
+
* Returns this Result's value if `ok`. If not, the value of this Result is thrown.
|
|
22
|
+
*/
|
|
23
|
+
unwrap(): V | never;
|
|
24
|
+
/**
|
|
25
|
+
* Returns this Result's value if `ok`. If not, `value` is returned.
|
|
26
|
+
*/
|
|
27
|
+
unwrapOr(): V | undefined;
|
|
28
|
+
unwrapOr<T>(value: T): V | T;
|
|
29
|
+
/**
|
|
30
|
+
* Returns this Result's value if `ok`. If not, `onError` is evaluated
|
|
31
|
+
* with this Result's value as a parameter, and the result is returned.
|
|
32
|
+
*/
|
|
33
|
+
unwrapOrElse<T>(onError: (err: E) => T): V | T;
|
|
34
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export const Ok=e=>new Result(!0,e),Err=e=>new Result(!1,e);export class Result{constructor(r,t){this.ok=r;this.value=t}unwrap(){if(this.ok)return this.value;throw this.value}unwrapOr(r){return this.ok?this.value:r}unwrapOrElse(r){return this.ok?this.value:r(this.value)}}
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"mappings": "AAUO,aAAM,GAASA,GACd,IAAI,OAAO,GAAMA,CAAK,EAIjB,IAAUC,GACf,IAAI,OAAO,GAAOA,CAAK,EAQxB,aAAM,MAA8C,CAC1D,YACiBC,EACAF,EACd,CAFc,QAAAE,EACA,WAAAF,CACZ,CAKL,QAAoB,CACnB,GAAI,KAAK,GAAI,OAAO,KAAK,MACzB,MAAM,KAAK,KACZ,CAOA,SAAYA,EAAkB,CAC7B,OAAO,KAAK,GAAK,KAAK,MAAaA,CACpC,CAMA,aAAgBG,EAA+B,CAC9C,OAAO,KAAK,GAAK,KAAK,MAAaA,EAAQ,KAAK,KAAU,CAC3D,CACD",
|
|
5
|
+
"names": ["value", "error", "ok", "onError"]
|
|
6
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@koerismo/result",
|
|
3
|
+
"description": "A miniscule Result interface for TypeScript.",
|
|
4
|
+
"version": "0.9.1",
|
|
5
|
+
"author": "Koerismo",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/index.js",
|
|
11
|
+
"./global": "./dist/global.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"result",
|
|
15
|
+
"rust",
|
|
16
|
+
"maybe"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"esbuild": "^0.27.2",
|
|
20
|
+
"typescript": "^5.9.3"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "pnpm run build-code && pnpm run build-types",
|
|
24
|
+
"build-code": "esbuild ./src/* --minify --platform=neutral --sourcemap --sources-content=false --outdir=./dist/",
|
|
25
|
+
"build-types": "tsc"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/global.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +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
|
+
}
|