refineable
v1.0.0
Published
A tiny TypeScript utility for chaining transformations over a value in a readable, functional style.
Readme
refineable
A tiny TypeScript utility for chaining transformations over a value in a readable, functional style.
Install
npm install refineableUsage
Wrap any value with r(), then chain .refine() calls to transform it step by step. Unwrap the final result with .value.
import { r } from 'refineable';
const result = r(42)
.refine(n => n * 2) // 84
.refine(n => `${n}!`) // "84!"
.refine(s => s.length) // 3
.value;
console.log(result); // 3TypeScript infers the type at every step — no annotations needed.
Async
Use .refineAsync() when your transformation returns a Promise. It resolves the value and keeps the chain going.
const result = await r("hello")
.refineAsync(async s => s.toUpperCase()) // "HELLO"
.then(v => v.refine(s => s + "!")) // "HELLO!"
console.log(result.value); // "HELLO!"Why?
It's just a cleaner alternative to deeply nested function calls or intermediate variables:
// instead of this:
const doubled = n * 2;
const labeled = `${doubled}!`;
const len = labeled.length;
// or this:
const len = getLength(label(double(n)));
// do this:
const len = r(n)
.refine(n => n * 2)
.refine(n => `${n}!`)
.refine(s => s.length)
.value;