cano-ts
v1.3.0
Published
<h1 align="center">Cano Ts</h1>
Maintainers
Readme
📚 Full Documentation: Cano Ts Docs
🚀 Get Started
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"📦 Installation
npm
npm install cano-tsYou can also use other package manager:
pnpm add cano-ts
# OR
yarn add cano-ts📖 About cano-ts
When 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);✨ Features
- ✅ 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
🔧 Array Utilities with E Module
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"