A lightweight utility, inspired by Elixirโs pipe operator |>, for composing sync and async functions in a clean, readable pipeline.
๐ Full Documentation: Cano Ts Docs
import { pipeSync } from "cano-ts";
const add = (x: number, y: number) => x + y;
const multiply = (x: number, factor: number) => x * factor;
const format = (x: number, prefix: string) => `${prefix} ${x}`;
const result = pipeSync(5)
.next(add, 3) // 5 + 3 = 8
.next(multiply, 2) // 8 * 2 = 16
.next(format, "Result:")
.result();
console.log(result); // "Result: 16"npm
npm install cano-tsYou can also use other package manager:
pnpm add cano-ts
# OR
yarn add cano-tsWhen working with transformations in JavaScript and TypeScript, we often end up with deeply nested function calls or complex .then() chains for asynchronous operations. Cano Ts solves this problem by introducing a fluent, pipeline-based API for function chaining.
/* BEFORE: Traditional Promise Chaining */
async function fetchUser(id: number, db: DbInstance): Promise<User> {
return db.getUserById(id);
}
function updateRoleTo(user: User, newRole: string): User {
return { ...user, role: newRole };
}
async function saveToDB(user: User, db: DbInstance): Promise<User> {
await db.updateUser(user);
return user;
}
// โ Callbacks required for passing extra arguments
fetchUser(1, DB)
.then((user) => updateRoleTo(user, "admin"))
.then((updatedUser) => saveToDB(updatedUser, DB))
.then(console.log)
.catch(console.error);
/* โ
AFTER: Using cano-ts for a Clean Pipeline */
const result = await pipe(1)
.next(fetchUser, DB)
.next(updateRoleTo, "admin")
.next(saveToDB, DB)
.result();
console.log(result);- โ
Fluent API โ Chain functions using
.next() - โ
Supports async & sync pipelines โ
pipe()forasync,pipeSync()for sync - โ
Function History Tracking โ Debug easily with
.log() - โ
Array Utilities โ Built-in
Emodule with functional array operations - โ Fully Type-Safe โ Leverages TypeScript generics for strong typings
Cano TS includes a powerful E module for functional array operations that work seamlessly with pipes:
import { pipeSync, E } from "cano-ts";
const users = [
{ id: 1, name: "Alice", age: 25, active: true },
{ id: 2, name: "Bob", age: 30, active: false },
{ id: 3, name: "Charlie", age: 35, active: true },
{ id: 4, name: "Diana", age: 28, active: true },
];
const result = pipeSync(users)
.next(E.filter, (user) => user.active) // Filter active users
.next(E.sort, (a, b) => b.age - a.age) // Sort by age descending
.next(E.map, (user) => user.name) // Extract names
.next(E.slice, 0, 2) // Take first 2
.next(E.join, " & ") // Join with separator
.result();
console.log(result); // "Charlie & Diana"Made with ๐ by Henrique Artur