compose-ts
v2.0.1
Published
Type safe version of `compose()` util
Readme
compose-ts
compose-ts is a TypeScript library that simplifies function composition, transforming nested calls into a manageable sequence.
Installation
Install via npm or yarn:
npm install compose-ts
# or
yarn add compose-tsUsage
Import compose from compose-ts and use it to chain functions together.
Basic Example
import { compose } from 'compose-ts';
const add = (n: number, num = 1) => n + num;
const multiply = (n: number, num = 2) => n * num;
const toString = (n: number) => n.toString();
const num = compose(42)(add)(multiply)(add, 5)(toString)();
// Equivalent: const num = toString(add(multiply(add(42)), 5));
console.log(num); // "91"Advanced Example with Function Enhancers
import { compose } from 'compose-ts';
type Func<TParameters extends any[], TReturnType> =
(...args: TParameters) => TReturnType;
const addH =
<TParameters extends any[]>(func: Func<TParameters, number>, num = 1) =>
(...args: TParameters) => func(...args) + num;
const multiplyH =
<TParameters extends any[]>(func: Func<TParameters, number>, num = 2) =>
(...args: TParameters) => func(...args) * num;
const parseIntH =
(func: Func<[number], number>) =>
(arg: string) => func(parseInt(arg));
const I = (result: number) => result;
const enhanced = compose(I)(addH)(multiplyH)(addH, 5)(parseIntH)();
// Equivalent: const enhanced = parseIntH(addH(multiplyH(addH(I)), 5));
console.log(enhanced("42")); // 91Features
- Transforms complex nested function calls into linear sequences.
- Enhances readability and maintainability of TypeScript code.
- Supports function enhancers for advanced composition.
Versioning
Follows Semantic Versioning 2.0.0.
Contributing
Contributions are welcome. Feel free to submit pull requests or open issues.
License
Licensed under the MIT License.
