rsult 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/deno.json +5 -0
- package/package.json +4 -3
- package/readme.md +2 -2
- package/src/lib.ts +2 -2
- package/src/option.ts +11 -8
- package/src/result.test.ts +1 -1
- package/src/result.ts +13 -9
- package/tsconfig.json +1 -0
package/deno.json
ADDED
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rsult",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/lib.ts",
|
|
6
|
+
"repository": "https://github.com/indicium-ag/rsult",
|
|
6
7
|
"keywords": [],
|
|
7
|
-
"author": "",
|
|
8
|
-
"license": "
|
|
8
|
+
"author": "Kenan Sulayman <kenan@sly.mn>",
|
|
9
|
+
"license": "MIT",
|
|
9
10
|
"devDependencies": {
|
|
10
11
|
"@swc/core": "^1.4.12",
|
|
11
12
|
"@swc/jest": "^0.2.36",
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="rsult.svg" width="300px"/><br/>
|
|
3
|
-
<img src="rsult-test.svg" width="450px"/>
|
|
2
|
+
<img src="https://github.com/indicium-ag/rsult/raw/master/rsult.svg" width="300px"/><br/>
|
|
3
|
+
<img src="https://github.com/indicium-ag/rsult/raw/master/rsult-test.svg" width="450px"/>
|
|
4
4
|
</p>
|
|
5
5
|
|
|
6
6
|
<hr/>
|
package/src/lib.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './result';
|
|
2
|
-
export * from './option';
|
|
1
|
+
export * from './result.ts';
|
|
2
|
+
export * from './option.ts';
|
package/src/option.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export type Option<T> =
|
|
1
|
+
export type Option<T> =
|
|
2
|
+
OptionSome<T> | OptionNone<T>;
|
|
2
3
|
|
|
3
4
|
export interface IOptionCheck<T> {
|
|
4
5
|
/**
|
|
@@ -178,7 +179,7 @@ export interface IOptionUtility<T> {
|
|
|
178
179
|
* const myOption is None<number>();
|
|
179
180
|
* const value = myOption.unwrap_or_default(); // 0
|
|
180
181
|
*/
|
|
181
|
-
unwrap_or_default(): T;
|
|
182
|
+
unwrap_or_default(): T | null;
|
|
182
183
|
}
|
|
183
184
|
|
|
184
185
|
export interface IOptionMutate<T> {
|
|
@@ -280,7 +281,8 @@ export interface IOption<T> extends
|
|
|
280
281
|
|
|
281
282
|
export class OptionSome<T> implements IOption<T> {
|
|
282
283
|
readonly _tag = 'Some' as const;
|
|
283
|
-
|
|
284
|
+
// @ts-ignore
|
|
285
|
+
readonly _T: T;
|
|
284
286
|
|
|
285
287
|
constructor(readonly value: T) { }
|
|
286
288
|
|
|
@@ -352,7 +354,7 @@ export class OptionSome<T> implements IOption<T> {
|
|
|
352
354
|
return this.value;
|
|
353
355
|
}
|
|
354
356
|
|
|
355
|
-
unwrap_or_default(): T {
|
|
357
|
+
unwrap_or_default(): T | null {
|
|
356
358
|
return this.value;
|
|
357
359
|
}
|
|
358
360
|
|
|
@@ -408,7 +410,8 @@ export class OptionSome<T> implements IOption<T> {
|
|
|
408
410
|
|
|
409
411
|
export class OptionNone<T> implements IOption<T> {
|
|
410
412
|
private readonly _tag: 'None' = 'None';
|
|
411
|
-
|
|
413
|
+
// @ts-ignore
|
|
414
|
+
private readonly _T: T;
|
|
412
415
|
|
|
413
416
|
is_some(): this is never {
|
|
414
417
|
return false;
|
|
@@ -470,7 +473,7 @@ export class OptionNone<T> implements IOption<T> {
|
|
|
470
473
|
return fn();
|
|
471
474
|
}
|
|
472
475
|
|
|
473
|
-
unwrap_or_default() {
|
|
476
|
+
unwrap_or_default(): T | null {
|
|
474
477
|
return null as any;
|
|
475
478
|
}
|
|
476
479
|
|
|
@@ -499,11 +502,11 @@ export class OptionNone<T> implements IOption<T> {
|
|
|
499
502
|
}
|
|
500
503
|
}
|
|
501
504
|
|
|
502
|
-
export const Some = <T>(val: T) => {
|
|
505
|
+
export const Some = <T>(val: T): Option<T> => {
|
|
503
506
|
return new OptionSome<T>(val);
|
|
504
507
|
};
|
|
505
508
|
|
|
506
|
-
export const None = <T>() => {
|
|
509
|
+
export const None = <T>(): Option<T> => {
|
|
507
510
|
return new OptionNone<T>();
|
|
508
511
|
};
|
|
509
512
|
|
package/src/result.test.ts
CHANGED
|
@@ -452,7 +452,7 @@ describe('Result', () => {
|
|
|
452
452
|
const promise = Promise.reject(new Error(errorMsg));
|
|
453
453
|
const result = await result_from_promise(promise);
|
|
454
454
|
expect(result.is_err()).toBe(true);
|
|
455
|
-
expect(
|
|
455
|
+
expect(result.unwrap_err().message).toBe(errorMsg);
|
|
456
456
|
});
|
|
457
457
|
});
|
|
458
458
|
});
|
package/src/result.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Option, None, Some } from './option';
|
|
1
|
+
import { Option, None, Some } from './option.ts';
|
|
2
2
|
|
|
3
|
-
export type Result<T, E> =
|
|
3
|
+
export type Result<T, E> =
|
|
4
|
+
ResultOk<T, E> | ResultErr<T, E>;
|
|
4
5
|
|
|
5
6
|
export interface IResultCore<T, E> {
|
|
6
7
|
/**
|
|
@@ -365,8 +366,10 @@ export const isResultErr = <T, E>(val: any): val is ResultErr<T, E> => {
|
|
|
365
366
|
|
|
366
367
|
export class ResultOk<T, E> implements IResult<T, E> {
|
|
367
368
|
private readonly _tag = 'Ok' as const;
|
|
368
|
-
|
|
369
|
-
private readonly
|
|
369
|
+
// @ts-ignore
|
|
370
|
+
private readonly _T: T;
|
|
371
|
+
// @ts-ignore
|
|
372
|
+
private readonly _E: E;
|
|
370
373
|
|
|
371
374
|
constructor(readonly value: T) {
|
|
372
375
|
}
|
|
@@ -494,8 +497,10 @@ export class ResultOk<T, E> implements IResult<T, E> {
|
|
|
494
497
|
|
|
495
498
|
export class ResultErr<T, E> implements IResult<T, E> {
|
|
496
499
|
private readonly _tag: 'Err' = 'Err';
|
|
497
|
-
|
|
498
|
-
private readonly
|
|
500
|
+
// @ts-ignore
|
|
501
|
+
private readonly _T: T;
|
|
502
|
+
// @ts-ignore
|
|
503
|
+
private readonly _E: E;
|
|
499
504
|
|
|
500
505
|
constructor(readonly value: E) {
|
|
501
506
|
}
|
|
@@ -613,12 +618,11 @@ export class ResultErr<T, E> implements IResult<T, E> {
|
|
|
613
618
|
return this.value;
|
|
614
619
|
}
|
|
615
620
|
}
|
|
616
|
-
|
|
617
|
-
export const Ok = <T, E>(val: T) => {
|
|
621
|
+
export const Ok = <T, E>(val: T): Result<T, never> => {
|
|
618
622
|
return new ResultOk<T, E>(val) as Result<T, never>;
|
|
619
623
|
};
|
|
620
624
|
|
|
621
|
-
export const Err = <T, E>(val: E) => {
|
|
625
|
+
export const Err = <T, E>(val: E): Result<never, E> => {
|
|
622
626
|
return new ResultErr<T, E>(val) as Result<never, E>;
|
|
623
627
|
};
|
|
624
628
|
|