composable-functions
Advanced tools
Comparing version 1.0.0-beta-20240506-1 to 1.0.0-beta-20240510-1
@@ -107,3 +107,3 @@ import { composable, failure, fromSuccess, success } from './constructors.js'; | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<string> | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
*/ | ||
@@ -114,2 +114,20 @@ function map(fn, mapper) { | ||
/** | ||
* 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. | ||
* @returns a new Composable that will run the given Composable with the mapped parameters. | ||
* @example | ||
* import { composable, mapParameters } from 'composable-functions' | ||
* | ||
* const incrementId = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = mapParameters(incrementId, (id: number) => [{ id }]) | ||
* // ^? Composable<(id: number) => number> | ||
*/ | ||
function mapParameters(fn, mapper) { | ||
return async (...args) => { | ||
const output = await composable(mapper)(...args); | ||
if (!output.success) | ||
return failure(output.errors); | ||
return fn(...output.data); | ||
}; | ||
} | ||
/** | ||
* **NOTE :** Try to use [collect](collect) instead wherever possible since it is much safer. `merge` can create composables that will always fail in run-time or even overwrite data from successful constituent functions application. The `collect` function does not have these issues and serves a similar purpose. | ||
@@ -227,2 +245,2 @@ * @example | ||
} | ||
export { all, branch, catchError, collect, first, map, mapError, merge, mergeObjects, pipe, sequence, trace, }; | ||
export { all, branch, catchError, collect, first, map, mapError, mapParameters, merge, mergeObjects, pipe, sequence, trace, }; |
export { applySchema, composable, failure, fromSuccess, success, withSchema, } from './constructors.js'; | ||
export { all, branch, catchError, collect, first, map, mapError, merge, mergeObjects, pipe, sequence, trace, } from './combinators.js'; | ||
export { all, branch, catchError, collect, first, map, mapError, mapParameters, merge, mergeObjects, pipe, sequence, trace, } from './combinators.js'; | ||
export { inputFromForm, inputFromFormData, inputFromSearch, inputFromUrl, } from './input-resolvers.js'; | ||
@@ -4,0 +4,0 @@ export { serialize, toErrorPayload } from './serializer.js'; |
{ | ||
"name": "composable-functions", | ||
"version": "1.0.0-beta-20240506-1", | ||
"version": "1.0.0-beta-20240510-1", | ||
"description": "Decouple your business logic from your controllers. With first-class type inference from end to end.", | ||
@@ -5,0 +5,0 @@ "author": "Seasoned", |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.merge = exports.mapError = exports.map = exports.first = exports.collect = exports.catchError = exports.branch = exports.all = void 0; | ||
exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.merge = exports.mapParameters = exports.mapError = exports.map = exports.first = exports.collect = exports.catchError = exports.branch = exports.all = void 0; | ||
const constructors_js_1 = require("./constructors.js"); | ||
@@ -115,3 +115,3 @@ const errors_js_1 = require("./errors.js"); | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<string> | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
*/ | ||
@@ -123,2 +123,21 @@ function map(fn, mapper) { | ||
/** | ||
* 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. | ||
* @returns a new Composable that will run the given Composable with the mapped parameters. | ||
* @example | ||
* import { composable, mapParameters } from 'composable-functions' | ||
* | ||
* const incrementId = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = mapParameters(incrementId, (id: number) => [{ id }]) | ||
* // ^? Composable<(id: number) => number> | ||
*/ | ||
function mapParameters(fn, mapper) { | ||
return async (...args) => { | ||
const output = await (0, constructors_js_1.composable)(mapper)(...args); | ||
if (!output.success) | ||
return (0, constructors_js_1.failure)(output.errors); | ||
return fn(...output.data); | ||
}; | ||
} | ||
exports.mapParameters = mapParameters; | ||
/** | ||
* **NOTE :** Try to use [collect](collect) instead wherever possible since it is much safer. `merge` can create composables that will always fail in run-time or even overwrite data from successful constituent functions application. The `collect` function does not have these issues and serves a similar purpose. | ||
@@ -125,0 +144,0 @@ * @example |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compat = exports.environment = exports.InputError = exports.ErrorList = exports.EnvironmentError = exports.toErrorPayload = exports.serialize = exports.inputFromUrl = exports.inputFromSearch = exports.inputFromFormData = exports.inputFromForm = exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.merge = exports.mapError = exports.map = exports.first = exports.collect = exports.catchError = exports.branch = exports.all = exports.withSchema = exports.success = exports.fromSuccess = exports.failure = exports.composable = exports.applySchema = void 0; | ||
exports.compat = exports.environment = exports.InputError = exports.ErrorList = exports.EnvironmentError = exports.toErrorPayload = exports.serialize = exports.inputFromUrl = exports.inputFromSearch = exports.inputFromFormData = exports.inputFromForm = exports.trace = exports.sequence = exports.pipe = exports.mergeObjects = exports.merge = exports.mapParameters = exports.mapError = exports.map = exports.first = exports.collect = exports.catchError = exports.branch = exports.all = exports.withSchema = exports.success = exports.fromSuccess = exports.failure = exports.composable = exports.applySchema = void 0; | ||
var constructors_js_1 = require("./constructors.js"); | ||
@@ -43,2 +43,3 @@ Object.defineProperty(exports, "applySchema", { enumerable: true, get: function () { return constructors_js_1.applySchema; } }); | ||
Object.defineProperty(exports, "mapError", { enumerable: true, get: function () { return combinators_js_1.mapError; } }); | ||
Object.defineProperty(exports, "mapParameters", { enumerable: true, get: function () { return combinators_js_1.mapParameters; } }); | ||
Object.defineProperty(exports, "merge", { enumerable: true, get: function () { return combinators_js_1.merge; } }); | ||
@@ -45,0 +46,0 @@ Object.defineProperty(exports, "mergeObjects", { enumerable: true, get: function () { return combinators_js_1.mergeObjects; } }); |
@@ -71,6 +71,17 @@ import type { BranchReturn, CanComposeInParallel, CanComposeInSequence, Composable, MergeObjs, PipeReturn, RecordToTuple, Result, SequenceReturn, UnpackData } from './types.js'; | ||
* const incrementToString = map(increment, String) | ||
* // ^? Composable<string> | ||
* // ^? Composable<({ id }: { id: number }) => string> | ||
*/ | ||
declare function map<Fn extends Composable, O>(fn: Fn, mapper: (res: UnpackData<Fn>) => O): Composable<(...args: Parameters<Fn>) => O>; | ||
/** | ||
* 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. | ||
* @returns a new Composable that will run the given Composable with the mapped parameters. | ||
* @example | ||
* import { composable, mapParameters } from 'composable-functions' | ||
* | ||
* const incrementId = composable(({ id }: { id: number }) => id + 1) | ||
* const increment = mapParameters(incrementId, (id: number) => [{ id }]) | ||
* // ^? Composable<(id: number) => number> | ||
*/ | ||
declare function mapParameters<Fn extends Composable, NewParameters extends unknown[], const O extends Parameters<Fn>>(fn: Fn, mapper: (...args: NewParameters) => Promise<O> | O): Composable<(...args: NewParameters) => UnpackData<Fn>>; | ||
/** | ||
* **NOTE :** Try to use [collect](collect) instead wherever possible since it is much safer. `merge` can create composables that will always fail in run-time or even overwrite data from successful constituent functions application. The `collect` function does not have these issues and serves a similar purpose. | ||
@@ -137,2 +148,2 @@ * @example | ||
declare function branch<SourceComposable extends Composable, Resolver extends (...args: any[]) => Composable | null | Promise<Composable | null>>(cf: SourceComposable, resolver: Resolver): BranchReturn<SourceComposable, Resolver>; | ||
export { all, branch, catchError, collect, first, map, mapError, merge, mergeObjects, pipe, sequence, trace, }; | ||
export { all, branch, catchError, collect, first, map, mapError, mapParameters, merge, mergeObjects, pipe, sequence, trace, }; |
export { applySchema, composable, failure, fromSuccess, success, withSchema, } from './constructors.js'; | ||
export { all, branch, catchError, collect, first, map, mapError, merge, mergeObjects, pipe, sequence, trace, } from './combinators.js'; | ||
export { all, branch, catchError, collect, first, map, mapError, mapParameters, merge, mergeObjects, pipe, sequence, trace, } from './combinators.js'; | ||
export { inputFromForm, inputFromFormData, inputFromSearch, inputFromUrl, } from './input-resolvers.js'; | ||
@@ -4,0 +4,0 @@ export { serialize, toErrorPayload } from './serializer.js'; |
101051
2007