@fizzwiz/fluent
Mirror the natural flow of thought in your code.
@fizzwiz/fluent
is a lightweight and expressive JavaScript library that simplifies complex logic
into elegant, intuitive, and fluent syntax. It encourages code that reads like natural language,
while remaining concise and powerful.
β¨ Features
- π§ Thoughtful API β Code reads like how you think.
- π§© Modular Structure β Clean separation by concept and responsibility.
- π Dual Environment Support β Works in both Node.js and browsers.
- β‘ Async Support β Seamlessly handle promises and async iterables with
AsyncEach
. - π Zero Learning Curve β Familiar, fluent, and instantly productive.
π§ Guides & Concepts
Learn how to apply fluent thinking in real-world code: π fluent.blog.fizzwiz.cloud
β° Each
Class β Abstract Iteration
The Each
class represents a composable, lazy iterable. It defines an [Symbol.iterator]()
method and enables expressive operations for building and manipulating iterations without eagerly executing them.
Key Highlights
- Fluent operations: filter, map, reduce, combine, and traverse sequences.
- Supports infinite and recursive sequences with lazy evaluation.
- Declarative composition: slice, zip, concatenate, and compute Cartesian products.
π AsyncEach
Class β Asynchronous Iteration
The AsyncEach
class complements Each
by providing a fluent interface for async iterables and promises. It allows the same composable operations as Each
, but asynchronously.
Key Highlights
- Async resolution:
.when()
without arguments converts iterables of promises into anAsyncEach
. - Fluent async transformations:
sthen()
,if()
,else()
,match()
, andeach()
work with async iterables. - Seamless integration: Combine synchronous
Each
and asynchronousAsyncEach
pipelines. - Lazy async evaluation: No promises are awaited until iteration occurs.
Example
const results = Each.of(
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
)
.sthen(async n => n * 2);
for await (const value of results.when()) {
console.log(value); // 2, 4, 6
}
β What
Class β Functional Abstraction
The What
class provides a functional abstraction for declarative, functional logic. Every What
returned by its methods is callable as a function and, at the same time, provides a fluent semantic to restrict, compose, and transform functions.
Key Highlights
- Works seamlessly with
Each
andAsyncEach
. - Supports the same 9 core semantic methods shared across all three classes.
Example
const double = What.as(x => x * 2);
const result = double.what(5);
console.log(result); // 10
console.log(double.what(5) === double(5)); // true
const conditional = double.if(x => x >= 3).else(x => x);
console.log(conditional.what(2)); // 2
console.log(conditional.what(4)); // 8
βοΈ Shared Semantic Across Classes
Each
, AsyncEach
, and What
share a common fluent API composed of 9 methods:
Method | Purpose |
---|---|
if() |
Conditional filtering |
sthen() |
A safe then, distinguishable from thenable then |
else() |
Fallback for undefined or missing values |
which() |
Filter sequences by predicate |
when() |
Slice sequences or resolve async iterables |
match() |
Zip or match multiple sequences |
each() |
Cartesian product or nested expansion |
self() |
Infinite repetition or functional path expansion |
what() |
Reduce or retrieve single value |
This unified interface allows seamless interchange between synchronous iteration (Each
), asynchronous iteration (AsyncEach
), and functional abstraction (What
) without breaking the natural flow of your code.
π§ Philosophy
The design of @fizzwiz/fluent
encourages:
- Lazy evaluation β computations occur only when needed.
- Fluent composition β operations chain naturally.
- Clear distinction from native promises β
sthen()
avoids ambiguity with thenable Promises in built-in constructs (then
/await
). - Declarative problem solving β express complex sequences and logic as readable statements which can even handle infinite iterations.