Comparing version 1.0.4 to 1.0.5
import type { Matcher } from "./matcher"; | ||
import type { Result } from "./result"; | ||
import { Ok, Err } from "./result"; | ||
import tryAsync from "./tryAsync"; | ||
import try$ from "./try"; | ||
import tryFn$ from "./tryFn"; | ||
import when from "./when"; | ||
export type { Matcher, Result }; | ||
export { Ok, Err, tryAsync, when }; | ||
export { Ok, Err, try$, tryFn$, when }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.when = exports.tryAsync = exports.Err = exports.Ok = void 0; | ||
exports.when = exports.tryFn$ = exports.try$ = exports.Err = exports.Ok = void 0; | ||
console.error("experimental library, not ready for production use. everything maybe subject to change!"); | ||
@@ -12,6 +12,8 @@ const result_1 = require("./result"); | ||
Object.defineProperty(exports, "Err", { enumerable: true, get: function () { return result_1.Err; } }); | ||
const tryAsync_1 = __importDefault(require("./tryAsync")); | ||
exports.tryAsync = tryAsync_1.default; | ||
const try_1 = __importDefault(require("./try")); | ||
exports.try$ = try_1.default; | ||
const tryFn_1 = __importDefault(require("./tryFn")); | ||
exports.tryFn$ = tryFn_1.default; | ||
const when_1 = __importDefault(require("./when")); | ||
exports.when = when_1.default; | ||
//# sourceMappingURL=index.js.map |
import type { Matcher } from "./matcher"; | ||
declare const tupleConstructor: new <T, Y>(...p: [T | undefined, Y | undefined]) => [ | ||
T | undefined, | ||
Y | undefined | ||
declare const tupleConstructor: new <T, Y>(...p: [T | null, Y | null]) => [ | ||
T | null, | ||
Y | null | ||
]; | ||
declare class Result<TResult, TError> extends tupleConstructor<TResult, TError> { | ||
get res(): TResult | undefined; | ||
get err(): TError | undefined; | ||
get res(): TResult | null; | ||
get err(): TError | null; | ||
constructor({ result, error, }: { | ||
result?: TResult | undefined; | ||
error?: TError | undefined; | ||
result?: TResult | null; | ||
error?: TError | null; | ||
}); | ||
@@ -13,0 +13,0 @@ isOk: () => boolean; |
@@ -12,6 +12,3 @@ "use strict"; | ||
} | ||
constructor({ result = undefined, error = undefined, }) { | ||
if (result === undefined && error === undefined) { | ||
throw Error("No result nor error where provided, You should provide at least one in order to make a result"); | ||
} | ||
constructor({ result = null, error = null, }) { | ||
if (result && error) { | ||
@@ -21,4 +18,4 @@ throw Error("Both result and error where provided, You should only pass one to the constructor"); | ||
super(result, error); | ||
this.isOk = () => this.res !== undefined; | ||
this.isErr = () => this.err !== undefined; | ||
this.isOk = () => this.err === null; | ||
this.isErr = () => this.err !== null; | ||
this.ok = () => this.res; | ||
@@ -25,0 +22,0 @@ this.error = () => this.err; |
{ | ||
"name": "tryumph", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Bring the \"Umph\" to the javascript's async error handling", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -9,3 +9,3 @@ # tryumph | ||
```js | ||
const result = await tryAsync(itMayThrow()); | ||
const result = await try$(itMayThrow()); | ||
result.match( | ||
@@ -19,3 +19,3 @@ when(Ok, consumeResult), | ||
```js | ||
const [res, err] = await tryAsync(itMayThrow()); | ||
const [res, err] = await try$(itMayThrow()); | ||
if (!!err) { | ||
@@ -82,5 +82,5 @@ handleError(err); | ||
```js | ||
const userResult = await tryAsync(axios.get("/user/12345")); | ||
const userResult = await try$(axios.get("/user/12345")); | ||
const user = userResult.unwrapOr(defaultUser); | ||
const avatarResult = await tryAsync(axios.get(user.avatarImage)); | ||
const avatarResult = await try$(axios.get(user.avatarImage)); | ||
const avatar = avatarResult.unwrapOr(defaultAvatar); | ||
@@ -98,1 +98,33 @@ consumeResult(user, avatar); | ||
- [ ] Publish production ready release | ||
## More examples | ||
What is in Result? | ||
```js | ||
const a = await try$(itWillBeFine()); | ||
console.log(result.isOk()); // true | ||
console.log(result.isErr()); // false | ||
console.log(result.ok()); // "Result" | ||
console.log(result.error()); // undefined | ||
console.log(result.unwrap()); // "Result" | ||
console.log(result.unwrapOr("Default")); // "Result" | ||
const b = await try$(itWillThrow()); | ||
console.log(result.isOk()); // false | ||
console.log(result.isErr()); // true | ||
console.log(result.ok()); // undefined | ||
console.log(result.error()); // "Error" | ||
console.log(result.unwrap()); // CRASH!! it will throw the error! | ||
console.log(result.unwrapOr("Default")); // "Default" | ||
``` | ||
Here is another way of calling function. | ||
```js | ||
const sum = async (a, b) => a + b; | ||
const [res, err] = await tryFn$(sum, 1, 2); | ||
if (!!err) { | ||
handleError(err); | ||
return; | ||
} | ||
consumeResult(res); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21951
35
224
127