Skip to content

๐Ÿ”— | A lightweight utility, inspired by Elixirโ€™s pipe operator |>, for composing sync and async functions in a clean, readable pipeline.

License

Notifications You must be signed in to change notification settings

HenriqueArtur/cano-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Cano Ts

A lightweight utility, inspired by Elixirโ€™s pipe operator |>, for composing sync and async functions in a clean, readable pipeline.

npm version MIT license Gitmoji

tests tests tests

๐Ÿ“š 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-ts

You 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() for async, pipeSync() for sync
  • โœ… Function History Tracking โ€“ Debug easily with .log()
  • โœ… Array Utilities โ€“ Built-in E module 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"

Made with ๐Ÿ’œ by Henrique Artur

About

๐Ÿ”— | A lightweight utility, inspired by Elixirโ€™s pipe operator |>, for composing sync and async functions in a clean, readable pipeline.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •