composable-functions
Advanced tools
Comparing version 1.0.0-beta-20240521-1 to 1.0.0-beta-20240522-1
@@ -47,8 +47,4 @@ import { composable, failure, fromSuccess, success } from './constructors.js'; | ||
function pipe(...fns) { | ||
return (async (...args) => { | ||
const res = await sequence(...fns)(...args); | ||
return !res.success | ||
? failure(res.errors) | ||
: success(res.data[res.data.length - 1]); | ||
}); | ||
const last = (arr) => arr.at(-1); | ||
return map(sequence(...fns), last); | ||
} | ||
@@ -133,3 +129,4 @@ /** | ||
/** | ||
* It takes a Composable and a predicate to apply a transformation over the resulting `data`. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* It takes a Composable and a mapper to apply a transformation over the resulting output. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* The mapper also receives the original input parameters. | ||
* | ||
@@ -141,9 +138,16 @@ * @example | ||
* | ||
* const increment = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = composable((n: number) => n + 1) | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
* // ^? Composable<(n: number) => string> | ||
* const result = await map(increment, (result, n) => `${n} -> ${result}`)(1) | ||
* // result === '1 -> 2' | ||
* ``` | ||
*/ | ||
function map(fn, mapper) { | ||
return pipe(fn, composable(mapper)); | ||
return (async (...args) => { | ||
const result = await fn(...args); | ||
if (!result.success) | ||
return failure(result.errors); | ||
return composable(mapper)(result.data, ...args); | ||
}); | ||
} | ||
@@ -150,0 +154,0 @@ /** |
{ | ||
"name": "composable-functions", | ||
"version": "1.0.0-beta-20240521-1", | ||
"version": "1.0.0-beta-20240522-1", | ||
"description": "Types and functions to make composition easy and safe", | ||
@@ -5,0 +5,0 @@ "author": "Seasoned", |
@@ -51,8 +51,4 @@ "use strict"; | ||
function pipe(...fns) { | ||
return (async (...args) => { | ||
const res = await sequence(...fns)(...args); | ||
return !res.success | ||
? (0, constructors_js_1.failure)(res.errors) | ||
: (0, constructors_js_1.success)(res.data[res.data.length - 1]); | ||
}); | ||
const last = (arr) => arr.at(-1); | ||
return map(sequence(...fns), last); | ||
} | ||
@@ -141,3 +137,4 @@ exports.pipe = pipe; | ||
/** | ||
* It takes a Composable and a predicate to apply a transformation over the resulting `data`. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* It takes a Composable and a mapper to apply a transformation over the resulting output. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* The mapper also receives the original input parameters. | ||
* | ||
@@ -149,9 +146,16 @@ * @example | ||
* | ||
* const increment = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = composable((n: number) => n + 1) | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
* // ^? Composable<(n: number) => string> | ||
* const result = await map(increment, (result, n) => `${n} -> ${result}`)(1) | ||
* // result === '1 -> 2' | ||
* ``` | ||
*/ | ||
function map(fn, mapper) { | ||
return pipe(fn, (0, constructors_js_1.composable)(mapper)); | ||
return (async (...args) => { | ||
const result = await fn(...args); | ||
if (!result.success) | ||
return (0, constructors_js_1.failure)(result.errors); | ||
return (0, constructors_js_1.composable)(mapper)(result.data, ...args); | ||
}); | ||
} | ||
@@ -158,0 +162,0 @@ exports.map = map; |
@@ -98,3 +98,4 @@ import type { BranchReturn, CanComposeInParallel, CanComposeInSequence, Composable, MergeObjects, PipeReturn, RecordToTuple, Result, SequenceReturn, UnpackData } from './types.js'; | ||
/** | ||
* It takes a Composable and a predicate to apply a transformation over the resulting `data`. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* It takes a Composable and a mapper to apply a transformation over the resulting output. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes. | ||
* The mapper also receives the original input parameters. | ||
* | ||
@@ -106,8 +107,10 @@ * @example | ||
* | ||
* const increment = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = composable((n: number) => n + 1) | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
* // ^? Composable<(n: number) => string> | ||
* const result = await map(increment, (result, n) => `${n} -> ${result}`)(1) | ||
* // result === '1 -> 2' | ||
* ``` | ||
*/ | ||
declare function map<Fn extends Composable, O>(fn: Fn, mapper: (res: UnpackData<Fn>) => O): Composable<(...args: Parameters<Fn>) => O>; | ||
declare function map<Fn extends Composable, O>(fn: Fn, mapper: (res: UnpackData<Fn>, ...originalInput: Parameters<Fn>) => O): Composable<(...args: Parameters<Fn>) => O>; | ||
/** | ||
@@ -114,0 +117,0 @@ * It takes a Composable and a function that will map the input parameters to the expected input of the given Composable. Good to adequate the output of a composable into the input of the next composable in a composition. The function must return an array of parameters that will be passed to the Composable. |
@@ -7,3 +7,3 @@ export { applySchema, composable, failure, fromSuccess, success, withSchema, } from './constructors.js'; | ||
export { EnvironmentError, ErrorList, InputError, isEnvironmentError, isInputError, } from './errors.js'; | ||
export type { Composable, Failure, MergeObjects, ParserSchema, Result, SerializableError, SerializableResult, Success, UnpackAll, UnpackData, } from './types.js'; | ||
export type { BranchReturn, CanComposeInParallel, CanComposeInSequence, Composable, Failure, MergeObjects, ParserSchema, PipeReturn, Result, SequenceReturn, SerializableError, SerializableResult, Success, UnpackAll, UnpackData, } from './types.js'; | ||
export * as environment from './environment/index.js'; |
104812
2337