try-typescript
Advanced tools
Comparing version
@@ -0,39 +1,36 @@ | ||
import { Result } from "./Result"; | ||
export type Step = ((() => Promise<Result>) | ((prev: Result) => Promise<Result>)); | ||
export declare class Try<T> { | ||
private value; | ||
private executionStack; | ||
private internalError?; | ||
_finalResult?: Result; | ||
readonly steps: Step[]; | ||
private constructor(); | ||
static of<T>(func: () => T | Promise<T>): Try<T>; | ||
static combine<T extends any[], R>(...args: [...{ | ||
[K in keyof T]: Try<T[K]>; | ||
}, (...values: T) => R]): Try<R>; | ||
static success<T>(value: T): Try<T>; | ||
static failure<T>(error: Error): Try<T>; | ||
static of<T>(fn: () => T | Promise<T>): Try<Awaited<T>>; | ||
private setValue; | ||
private setError; | ||
private executeElement; | ||
private runExecutionStack; | ||
}, (...values: T) => R | Promise<R>]): Try<R>; | ||
static success<U>(value: U): Try<U>; | ||
static failure<T>(err: Error): Try<T>; | ||
map<U>(func: (value: T) => U | Promise<U>): Try<U>; | ||
mapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, func: (value: T) => U | Promise<U>): Try<U>; | ||
mapFailureWith<E extends Error, U extends Error>(errorType: new (...args: any[]) => E, func: (ex: E) => U | Promise<U>): Try<T>; | ||
flatMap<U>(func: (value: T) => Try<U> | Promise<Try<U>>): Try<U>; | ||
flatMapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, func: (value: T) => Try<U> | Promise<Try<U>>): Try<U>; | ||
getOrElse<U>(fallbackValue: U): Promise<U | T>; | ||
getOrElseGet<U>(func: (ex: Error) => U | Promise<U>): Promise<T | U>; | ||
getOrElseThrow(func: (error: Error) => Promise<Error> | Error): Promise<T>; | ||
peek(func: (value: T) => Promise<void> | void): Try<T>; | ||
andThen(func: (value: T) => Promise<void> | void): Try<T>; | ||
filter(predicateFunc: (value: T) => boolean | Promise<boolean>, errorProvider?: (value: T) => Error): Try<T>; | ||
filterNot(predicateFunc: (value: T) => boolean | Promise<boolean>, errorProvider?: (value: T) => Error): Try<T>; | ||
recover<U>(func: (error: Error) => U | Promise<U>): Try<T | U>; | ||
recoverWith<U>(func: (error: Error) => Try<U> | Promise<Try<U>>): Try<U | T>; | ||
get(): Promise<T>; | ||
run(): Promise<Try<T>>; | ||
get(): Promise<T>; | ||
getOrElse<U>(defaultValue: U): Promise<U | T>; | ||
getOrElseGet<U>(fn: (ex: Error) => U): Promise<T | U>; | ||
getOrElseThrow<U>(fn: (error: Error) => U): Promise<T | U>; | ||
map<U>(fn: (value: T) => U): Try<Awaited<U>>; | ||
mapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, fn: (value: T) => U): Try<Awaited<U>>; | ||
flatMap<U>(fn: (value: T) => Try<U> | Promise<Try<U>>): Try<Awaited<U>>; | ||
flatMapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, fn: (value: T) => Try<U> | Promise<Try<U>>): Try<Awaited<U>>; | ||
mapFailure<E extends Error, U extends Error>(func: (ex: E) => U | Promise<U>): Try<T>; | ||
mapFailureWith<E extends Error, U extends Error>(errorType: new (...args: any[]) => E, func: (ex: E) => U | Promise<U>): Try<T>; | ||
filter(predicateFunc: (value: T) => boolean | Promise<boolean>, throwbackFunction?: (value: T) => Error): Try<T>; | ||
filterNot(predicateFunc: (value: T) => boolean | Promise<boolean>, throwbackFunction?: (value: T) => Error): Try<T>; | ||
peek(fn: (value: T) => void): Try<T>; | ||
andThen(fn: (value: T) => any): Try<T>; | ||
andFinally(fn: () => any): Try<T>; | ||
recover<U>(fn: (error: Error) => U): Try<T | U>; | ||
recoverWith<U>(fn: (error: Error) => Try<U>): Try<U | T>; | ||
isSuccess(): boolean; | ||
isFailure(): boolean; | ||
onSuccess(fn: (value: T) => void): Try<T>; | ||
onFailure(fn: (ex: Error) => void): Try<T>; | ||
andFinally(func: () => Promise<void> | void): Try<T>; | ||
mapFailure(func: (ex: Error) => Error | Promise<Error>): Try<T>; | ||
getCause(): Error | undefined; | ||
onSuccess(func: (value: T) => Promise<void> | void): Try<T>; | ||
onFailure(func: (value: Error) => Promise<void> | void): Try<T>; | ||
} |
@@ -13,340 +13,104 @@ "use strict"; | ||
exports.Try = void 0; | ||
const NoSuchElementException_1 = require("../exceptions/NoSuchElementException"); | ||
const TryFunctions = { | ||
OF: "OF", | ||
COMBINE: 'COMBINE', | ||
MAP: 'MAP', | ||
MAPIF: 'MAPIF', | ||
ANDTHEN: 'ANDTHEN', | ||
ANDFINALLY: 'ANDFINALLY', | ||
FLATMAP: 'FLATMAP', | ||
FLATMAPIF: 'FLATMAPIF', | ||
FILTER: 'FILTER', | ||
FILTERNOT: 'FILTERNOT', | ||
PEEK: 'PEEK', | ||
RECOVER: 'RECOVER', | ||
RECOVERWITH: 'RECOVERWITH', | ||
ONSUCCESS: 'ONSUCCESS', | ||
ONFAILURE: 'ONFAILURE', | ||
MAPFAILURE: 'MAPFAILURE', | ||
MAPFAILUREWITH: 'MAPFAILUREWITH', | ||
}; | ||
const functions_1 = require("./functions"); | ||
class Try { | ||
constructor(initExecution) { | ||
this.executionStack = []; | ||
if (initExecution) | ||
this.executionStack.push(initExecution); | ||
constructor(steps = []) { | ||
this._finalResult = undefined; | ||
this.steps = []; | ||
this.steps = steps; | ||
} | ||
static of(func) { | ||
return new Try([() => (0, functions_1.of)(func)]); | ||
} | ||
static combine(...args) { | ||
const trys = args.slice(0, -1); | ||
const func = args[args.length - 1]; | ||
return new Try({ | ||
name: TryFunctions.COMBINE, | ||
functionData: { func: () => func(...trys.map(tryObj => tryObj.value)), trys }, | ||
returning: true | ||
}); | ||
// @ts-ignore | ||
return new Try([() => (0, functions_1.combine)(...args)]); | ||
} | ||
//Static methods to create a Try object | ||
static success(value) { | ||
return new Try().setValue(value); | ||
return new Try([() => (0, functions_1.success)(value)]); | ||
} | ||
static failure(error) { | ||
return new Try().setError(error); | ||
static failure(err) { | ||
return new Try([() => (0, functions_1.failure)(err)]); | ||
} | ||
static of(fn) { | ||
return new Try({ | ||
name: TryFunctions.OF, | ||
functionData: { func: fn }, | ||
returning: true | ||
}); | ||
map(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.map)(prev, func)]); | ||
} | ||
//Private methods to modify the internal state | ||
setValue(value) { | ||
this.value = value; | ||
return this; | ||
mapIf(predicateFunc, func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.mapIf)(prev, predicateFunc, func)]); | ||
} | ||
setError(err) { | ||
this.internalError = err; | ||
return this; | ||
mapFailureWith(errorType, func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.mapFailureWith)(prev, errorType, func)]); | ||
} | ||
executeElement(executionElement) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (executionElement.returning) { | ||
return this.value = executionElement.name === TryFunctions.OF | ||
? yield executionElement.functionData.func() | ||
: yield executionElement.functionData.func(this.value); | ||
} | ||
yield executionElement.functionData.func(this.value); | ||
}); | ||
flatMap(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.flatMap)(prev, func)]); | ||
} | ||
runExecutionStack() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (const executionElement of this.executionStack) { | ||
try { | ||
if (executionElement.name === TryFunctions.MAP) { | ||
if (this.isSuccess()) | ||
yield this.executeElement(executionElement); | ||
} | ||
else if (executionElement.name === TryFunctions.MAPIF) { | ||
if (this.isSuccess()) { | ||
if (yield executionElement.functionData.fallbackFunction(this.value)) { | ||
yield this.executeElement(executionElement); | ||
} | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.FLATMAP) { | ||
if (this.isSuccess()) { | ||
const tryObject = yield executionElement.functionData.func(this.value); | ||
this.value = yield tryObject.get(); | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.FLATMAPIF) { | ||
if (this.isSuccess()) { | ||
if (yield executionElement.functionData.fallbackFunction(this.value)) { | ||
const tryObject = yield executionElement.functionData.func(this.value); | ||
this.value = yield tryObject.get(); | ||
} | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.ANDTHEN) { | ||
if (this.isSuccess()) | ||
yield this.executeElement(executionElement); | ||
} | ||
else if (executionElement.name === TryFunctions.ANDFINALLY) { | ||
yield this.executeElement(executionElement); | ||
} | ||
else if (executionElement.name === TryFunctions.FILTER) { | ||
if (this.isSuccess()) { | ||
if (yield executionElement.functionData.func(this.value)) { | ||
throw yield executionElement.functionData.fallbackFunction(this.value); | ||
} | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.FILTERNOT) { | ||
if (this.isSuccess()) { | ||
if (!(yield executionElement.functionData.func(this.value))) { | ||
throw yield executionElement.functionData.fallbackFunction(this.value); | ||
} | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.PEEK) { | ||
if (this.isSuccess()) | ||
yield executionElement.functionData.func(this.value); | ||
} | ||
else if (executionElement.name === TryFunctions.RECOVER) { | ||
if (this.isFailure()) { | ||
this.value = yield executionElement.functionData.func(this.internalError); | ||
this.internalError = undefined; | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.RECOVERWITH) { | ||
if (this.isFailure()) { | ||
const tryObject = yield executionElement.functionData.func(this.internalError); | ||
this.internalError = undefined; | ||
this.value = yield tryObject.get(); | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.ONSUCCESS) { | ||
if (this.isSuccess()) | ||
yield executionElement.functionData.func(this.value); | ||
} | ||
else if (executionElement.name === TryFunctions.ONFAILURE) { | ||
if (this.isFailure()) | ||
yield executionElement.functionData.func(this.internalError); | ||
} | ||
else if (executionElement.name === TryFunctions.COMBINE) { | ||
const values = yield Promise.all(executionElement.functionData.trys.map((tryObject) => __awaiter(this, void 0, void 0, function* () { return yield tryObject.run(); }))); | ||
for (const v of values) { | ||
if (v.isFailure()) { | ||
this.internalError = v.internalError; | ||
break; | ||
} | ||
} | ||
this.value = yield executionElement.functionData.func(...values); | ||
} | ||
else if (executionElement.name === TryFunctions.MAPFAILURE) { | ||
if (this.isFailure()) { | ||
this.internalError = yield executionElement.functionData.func(this.internalError); | ||
} | ||
} | ||
else if (executionElement.name === TryFunctions.MAPFAILUREWITH) { | ||
if (this.isFailure() && this.internalError instanceof executionElement.functionData.errorType) { | ||
this.internalError = yield executionElement.functionData.func(this.internalError); | ||
} | ||
} | ||
else { | ||
yield this.executeElement(executionElement); | ||
} | ||
} | ||
catch (ex) { | ||
this.internalError = ex; | ||
} | ||
} | ||
}); | ||
flatMapIf(predicateFunc, func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.flatMapIf)(prev, predicateFunc, func)]); | ||
} | ||
//----- Public interface ----- | ||
run() { | ||
getOrElse(fallbackValue) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runExecutionStack(); | ||
return this; | ||
return (0, functions_1.getOrElse)(this, fallbackValue); | ||
}); | ||
} | ||
get() { | ||
getOrElseGet(func) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runExecutionStack(); | ||
if (this.isFailure()) | ||
throw this.internalError; | ||
return this.value; | ||
return (0, functions_1.getOrElseGet)(this, func); | ||
}); | ||
} | ||
getOrElse(defaultValue) { | ||
getOrElseThrow(func) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runExecutionStack(); | ||
return this.isFailure() ? defaultValue : this.value; | ||
return (0, functions_1.getOrElseThrow)(this, func); | ||
}); | ||
} | ||
getOrElseGet(fn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runExecutionStack(); | ||
return this.isFailure() ? yield fn(this.internalError) : this.value; | ||
}); | ||
peek(func) { | ||
//According to the docs, peek is the same as `andThen` | ||
return new Try([...this.steps, (prev) => (0, functions_1.andThen)(prev, func)]); | ||
} | ||
getOrElseThrow(fn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.runExecutionStack(); | ||
if (this.isFailure()) | ||
throw fn(this.internalError); | ||
return this.value; | ||
}); | ||
andThen(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.andThen)(prev, func)]); | ||
} | ||
map(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.MAP, | ||
functionData: { func: fn }, | ||
returning: true | ||
}); | ||
return this; | ||
filter(predicateFunc, errorProvider) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.filter)(prev, predicateFunc, errorProvider)]); | ||
} | ||
mapIf(predicateFunc, fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.MAPIF, | ||
functionData: { func: fn, fallbackFunction: predicateFunc }, | ||
returning: true | ||
}); | ||
return this; | ||
filterNot(predicateFunc, errorProvider) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.filterNot)(prev, predicateFunc, errorProvider)]); | ||
} | ||
flatMap(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.FLATMAP, | ||
functionData: { func: fn }, | ||
returning: true | ||
}); | ||
return this; | ||
recover(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.recover)(prev, func)]); | ||
} | ||
flatMapIf(predicateFunc, fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.FLATMAPIF, | ||
functionData: { func: fn, fallbackFunction: predicateFunc }, | ||
returning: true | ||
}); | ||
return this; | ||
recoverWith(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.recoverWith)(prev, func)]); | ||
} | ||
mapFailure(func) { | ||
this.executionStack.push({ | ||
name: TryFunctions.MAPFAILURE, | ||
functionData: { func: func }, | ||
returning: false | ||
get() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (0, functions_1.get)(this); | ||
}); | ||
return this; | ||
} | ||
mapFailureWith(errorType, func) { | ||
this.executionStack.push({ | ||
name: TryFunctions.MAPFAILUREWITH, | ||
functionData: { func: func, errorType: errorType }, | ||
returning: false | ||
run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield (0, functions_1.run)(this); | ||
return this; | ||
}); | ||
return this; | ||
} | ||
filter(predicateFunc, throwbackFunction) { | ||
this.executionStack.push({ | ||
name: TryFunctions.FILTER, | ||
functionData: { func: predicateFunc, fallbackFunction: throwbackFunction !== null && throwbackFunction !== void 0 ? throwbackFunction : ((value) => { throw new NoSuchElementException_1.NoSuchElementException(`Predicate does not hold for ${value}`); }) }, | ||
returning: false | ||
}); | ||
return this; | ||
} | ||
filterNot(predicateFunc, throwbackFunction) { | ||
this.executionStack.push({ | ||
name: TryFunctions.FILTERNOT, | ||
functionData: { func: predicateFunc, fallbackFunction: throwbackFunction !== null && throwbackFunction !== void 0 ? throwbackFunction : ((value) => { throw new NoSuchElementException_1.NoSuchElementException(`Predicate does not hold for ${value}`); }) }, | ||
returning: false | ||
}); | ||
return this; | ||
} | ||
peek(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.PEEK, | ||
functionData: { func: fn }, | ||
returning: false | ||
}); | ||
return this; | ||
} | ||
andThen(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.ANDTHEN, | ||
functionData: { func: fn }, | ||
returning: false | ||
}); | ||
return this; | ||
} | ||
andFinally(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.ANDFINALLY, | ||
functionData: { func: fn }, | ||
returning: false | ||
}); | ||
return this; | ||
} | ||
recover(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.RECOVER, | ||
functionData: { func: fn }, | ||
returning: true | ||
}); | ||
return this; | ||
} | ||
recoverWith(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.RECOVERWITH, | ||
functionData: { func: fn }, | ||
returning: true | ||
}); | ||
return this; | ||
} | ||
isSuccess() { | ||
return this.internalError === undefined; | ||
return !this._finalResult.isError(); | ||
} | ||
isFailure() { | ||
return this.internalError !== undefined; | ||
return this._finalResult.isError(); | ||
} | ||
onSuccess(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.ONSUCCESS, | ||
functionData: { func: fn }, | ||
returning: false | ||
}); | ||
return this; | ||
andFinally(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.andFinally)(prev, func)]); | ||
} | ||
onFailure(fn) { | ||
this.executionStack.push({ | ||
name: TryFunctions.ONFAILURE, | ||
functionData: { func: fn }, | ||
returning: false | ||
}); | ||
return this; | ||
mapFailure(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.mapFailure)(prev, func)]); | ||
} | ||
getCause() { | ||
return this.internalError; | ||
return this._finalResult.getError(); | ||
} | ||
onSuccess(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.onSuccess)(prev, func)]); | ||
} | ||
onFailure(func) { | ||
return new Try([...this.steps, (prev) => (0, functions_1.onFailure)(prev, func)]); | ||
} | ||
} | ||
exports.Try = Try; |
@@ -154,2 +154,10 @@ "use strict"; | ||
})); | ||
test("Try.mapFailureWith should not map an instance of CustomException to MappedCustomException", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.failure(new CustomException("This is a test!")) | ||
.mapFailureWith(Error, (err) => __awaiter(void 0, void 0, void 0, function* () { | ||
return new MappedCustomException("Mapped Custom Exception", err.message); | ||
})); | ||
yield expect(result.get()).rejects.toThrow(CustomException); | ||
expect(result.isSuccess()).toBe(false); | ||
})); | ||
}); | ||
@@ -221,5 +229,5 @@ describe("Try.map", () => { | ||
}); | ||
describe("Try.filterNot", () => { | ||
describe("Try.filter", () => { | ||
test("filter should return Failure if predicate does not hold", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filterNot(v => v > 2); | ||
const result = __1.Try.success(2).filter(v => v <= 2); | ||
yield expect(result.get()).rejects.toThrow("Predicate does not hold for 2"); | ||
@@ -229,3 +237,3 @@ expect(result.isFailure()).toBe(true); | ||
test("filter should throw custom exception if predicate does not hold", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filterNot(v => v > 2, v => Error("Custom Predicate does not hold for " + v)); | ||
const result = __1.Try.success(2).filter(v => v <= 2, v => Error("Custom Predicate does not hold for " + v)); | ||
yield expect(result.get()).rejects.toThrow("Custom Predicate does not hold for 2"); | ||
@@ -235,3 +243,3 @@ expect(result.isFailure()).toBe(true); | ||
test("filter should return Success if predicate holds", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filterNot(v => v <= 2); | ||
const result = __1.Try.success(2).filter(v => v > 2); | ||
yield expect(result.get()).resolves.toBe(2); | ||
@@ -241,5 +249,5 @@ expect(result.isSuccess()).toBe(true); | ||
}); | ||
describe("Try.filter", () => { | ||
describe("Try.filterNot", () => { | ||
test("filterNot should return Failure if predicate does not hold", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filter(v => v <= 2); | ||
const result = __1.Try.success(2).filterNot(v => v > 2); | ||
yield expect(result.get()).rejects.toThrow("Predicate does not hold for 2"); | ||
@@ -249,3 +257,3 @@ expect(result.isFailure()).toBe(true); | ||
test("filterNot should throw custom exception if predicate does not hold", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filter(v => v <= 2, v => Error("Custom Predicate does not hold for " + v)); | ||
const result = __1.Try.success(2).filterNot(v => v > 2, v => Error("Custom Predicate does not hold for " + v)); | ||
yield expect(result.get()).rejects.toThrow("Custom Predicate does not hold for 2"); | ||
@@ -255,3 +263,3 @@ expect(result.isFailure()).toBe(true); | ||
test("filterNot should return Success if predicate holds", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = __1.Try.success(2).filter(v => v > 2); | ||
const result = __1.Try.success(2).filterNot(v => v <= 2); | ||
yield expect(result.get()).resolves.toBe(2); | ||
@@ -302,3 +310,3 @@ expect(result.isSuccess()).toBe(true); | ||
let tempResult = 0; | ||
const result = __1.Try.success(2).onSuccess(v => tempResult = v); | ||
const result = __1.Try.success(2).onSuccess(v => { tempResult = v; }); | ||
yield expect(result.get()).resolves.toBe(2); | ||
@@ -312,3 +320,3 @@ expect(tempResult).toBe(2); | ||
let tempResult = ""; | ||
const result = __1.Try.failure(new Error("test error")).onFailure(e => tempResult = e.message); | ||
const result = __1.Try.failure(new NoSuchElementException_1.NoSuchElementException("test error")).onFailure(e => { tempResult = e.message; }); | ||
yield expect(result.get()).rejects.toThrow("test error"); | ||
@@ -315,0 +323,0 @@ expect(tempResult).toBe("test error"); |
{ | ||
"name": "try-typescript", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "This repository implements a Try class inspired by the Vavr library in Java. The Try class is a functional programming construct for handling computations that may succeed or fail. It encapsulates exceptions and streamlines error handling, reducing boilerplate code and enhancing code readability. ", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -22,3 +22,3 @@ # Try-Typescript | ||
### `of<T>(fn: () => T): Try<T>` | ||
### `static of<T>(func: () => T | Promise<T>): Try<T>` | ||
Creates a Try instance from a function that may throw an error. | ||
@@ -37,3 +37,3 @@ ```typescript | ||
### `combine<T extends any[], R>(...args: [...{ [K in keyof T]: Try<T[K]> }, (...values: T) => R]): Try<R>` | ||
### `combine<T extends any[], R>(...args: [...{ [K in keyof T]: Try<T[K]> }, (...values: T) => R | Promise<R>]): Try<R>` | ||
Sometimes you may want to combine multiple Try instances into one. This function allows you to do that. It takes multiple Try instances and a function that will be executed if all Try instances are successful. If one of the Try instances is a failure, the function will not be executed and the resulting Try instance will be a failure. <br> | ||
@@ -70,3 +70,3 @@ ```typescript | ||
### `success<T>(value: T): Try<T>` | ||
### `success<U>(value:U): Try<U>` | ||
Creates a Try instance with a successful value. | ||
@@ -153,3 +153,3 @@ ```typescript | ||
### `getOrElseGet<U>(fn: (ex: Error) => U): Promise<T | U>` | ||
### `getOrElseGet<U>(func: (ex: Error) => U | Promise<U>): Promise<T | U>` | ||
Returns the value of the Try instance if it is a Success, otherwise returns the value returned by the function. | ||
@@ -166,3 +166,3 @@ ```typescript | ||
### `getOrElseThrow<U>(fn: (error: Error) => U): Promise<T | U>` | ||
### `getOrElseThrow(func: (error: Error) => Promise<Error> | Error): Promise<T>` | ||
Returns the value of the Try instance if it is a Success, otherwise throws the error returned by the function. | ||
@@ -205,3 +205,3 @@ ```typescript | ||
### `map<U>(fn: (value: T) => U): Try<U>` | ||
### `map<U>(func: (value: T) => U | Promise<U>): Try<U>` | ||
Maps the value of the Try instance if it is a Success, otherwise returns the Failure instance. | ||
@@ -221,3 +221,3 @@ ```typescript | ||
### `mapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, fn: (value: T) => U): Try<U>` | ||
### `mapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, func: (value: T) => U | Promise<U>): Try<U>` | ||
Maps the value of the Try instance if it is a Success and the predicate function evaluates to true. If not, the original state will be returned. | ||
@@ -243,3 +243,3 @@ ```typescript | ||
### `flatMap<U>(fn: (value: T) => Try<U>) : Try<U>` | ||
### `flatMap<U>(func: (value: T) => Try<U> | Promise<Try<U>>): Try<U>` | ||
Maps the value of the Try instance if it is a Success, otherwise returns the Failure instance. | ||
@@ -259,3 +259,3 @@ ```typescript | ||
### `flatMapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, fn: (value: T) => Try<U> | Promise<Try<U>>): Try<U>` | ||
### `flatMapIf<U>(predicateFunc: (value: T) => boolean | Promise<boolean>, func: (value: T) => Try<U> | Promise<Try<U>>): Try<U>` | ||
Maps the value of the Try instance if it is a Success and the predicate is true. If not, the original state will be returned. | ||
@@ -280,3 +280,3 @@ ```typescript | ||
### `mapFailure<E extends Error, U extends Error>(func: (ex: E) => U | Promise<U>): Try<T>` | ||
### `mapFailure(func: (ex: Error) => Error | Promise<Error>): Try<T>` | ||
Maps a failure of the Try instance if it is a Failure, otherwise returns the Success instance. | ||
@@ -300,7 +300,5 @@ ```typescript | ||
test("Try.mapFailure should map an instance of CustomException to MappedCustomException", async () => { | ||
const result = Try.failure(new CustomException("This is a test!")) | ||
.mapFailure(async (_)=> new MappedCustomException("Mapped Custom Exception", "Custom Exception")); | ||
await expect(result.get()).rejects.toThrow(MappedCustomException); | ||
expect(result.isSuccess()).toBe(false); | ||
.mapFailure(async (_)=> new MappedCustomException("Mapped Custom Exception", "Custom Exception")) | ||
}); | ||
@@ -342,3 +340,3 @@ ``` | ||
### `recover<U>(fn: (error: Error) => U): Try<T | U>` | ||
### `recover<U>(func: (error: Error) => U | Promise<U>): Try<T | U>` | ||
Recovers the value of the Try instance if it is a Failure, otherwise returns the Success instance. | ||
@@ -360,3 +358,3 @@ ```typescript | ||
### `recoverWith<U>(fn: (error: Error) => Try<U>): Try<U | T>` | ||
### `recoverWith<U>(func: (error: Error) => Try<U> | Promise<Try<U>>): Try<U | T>` | ||
Recovers the value of the Try instance if it is a Failure, otherwise returns the Success instance. | ||
@@ -378,3 +376,3 @@ ```typescript | ||
### `andThen(fn: (value: T) => any): Try<T>` | ||
### `andThen(func: (value: T) => Promise<void> | void): Try<T>` | ||
Runs the function if the Try instance is a Success. | ||
@@ -394,3 +392,3 @@ ```typescript | ||
### `andFinally(fn: () => any): Try<T>` | ||
### `andFinally(func: () => Promise<void> | void): Try<T>` | ||
Runs the function no matter the internal state (success or failure). | ||
@@ -411,3 +409,3 @@ ```typescript | ||
### `filter(predicateFunc: (value: T) => boolean, throwbackFunction?: (value: T) => Error): Try<T>` | ||
### `filter(predicateFunc: (value: T) => boolean | Promise<boolean>, errorProvider?: (value: T) => Error): Try<T>` | ||
Will throw default or custom Error if predicate is true. | ||
@@ -434,3 +432,3 @@ ```typescript | ||
### `filterNot(predicateFunc: (value: T) => boolean, throwbackFunction?: (value: T) => Error): Try<T>` | ||
### `filterNot(predicateFunc: (value: T) => boolean | Promise<boolean>, errorProvider?: (value: T) => Error): Try<T>` | ||
Will throw default or custom Error if predicate is false. | ||
@@ -456,3 +454,3 @@ ```typescript | ||
### `onFailure(fn: (ex: Error) => void): Try<T>` | ||
### `onFailure(func: (value: Error) => Promise<void> | void): Try<T>` | ||
Runs the function if the Try instance is a Failure. | ||
@@ -474,3 +472,3 @@ ```typescript | ||
### `onSuccess(fn: (value: T) => void): Try<T>` | ||
### `onSuccess(func: (value: T) => Promise<void> | void): Try<T>` | ||
Runs the function if the Try instance is a Success. | ||
@@ -505,3 +503,3 @@ ```typescript | ||
### `peek(fn: (value: T) => void): Try<T>` | ||
### `peek(func: (value: T) => Promise<void> | void): Try<T>` | ||
Peeks the value of the Try instance if it is a Success, otherwise returns the Failure instance. | ||
@@ -508,0 +506,0 @@ ```typescript |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
107138
95.25%91
600%1823
141.14%497
-0.4%1
Infinity%