A simple wrapping library that treats common if, try-catch, etc. statements as expressions.
It is preferable to be able to handle the result of a process immutably. With the recent increase in computing resources, many variables can be kept in memory, allowing programming with an emphasis on immutability.
In general, allowing mutable variables makes control of the process complicated and makes it extremely difficult to understand the state of the variables.
Kotlin, Scala, and other programming languages that allow functions to be treated as first-class objects allow ts to handle expressions provided in built-in or standard class libraries, making immutability easy to handle.
For reference, Kotlin can handle try-catch and if as expressions as follows.
val a: Int? = try { input.toInt() } catch (e: NumberFormatException) { null }Reference: https://kotlinlang.org/docs/exceptions.html#try-is-an-expression
At this time, this package provide only 2 functions: try-catch and if-else.
const r: string = attempt(() => (new ErrorService().execute()),
recover(Error, (e: Error) => ('DEFAULT')))If executeUri() throws URIError, evaluate and return 'URI Error'.
const s: string = attempt(() => (new ErrorService().executeUri()),
recover(URIError, (e: URIError) => ('URI Error')),
recover(Error, (e: Error) => ('DEFAULT')));
console.log(s); // => 'URI Error'lastly processes a function that do some recovery "fianally".
const s: string = attempt(() => (new ErrorService().execute()),
recover(Error, (e: Error) => {
return 'ERROR';
}),
lastly(() => {
console.log('Attempted');
}));
console.log(s); // => 'ERROR'if-else expression have to be finalized with else function. This triggers an evaluation of input conditions.
const r = If(base.length > 30, () => 'over 30')
.else(() => 'under 30')if-else can insert elseIf function intermediately.
const result = new StatusFindService().execute();
const res = If(result === '0', () => ({message: 'success'}))
.elseIf(result === '5', () => ({message: 'not found'}))
.else(() => ({message: 'failure'}));
console.log(res); // => ({message: 'not found'});