juxta 1.0.0
juxta: ^1.0.0 copied to clipboard
A lightweight Dart library for expression-oriented programming.
juxta #
A lightweight Dart library for expression-oriented programming. Inspired from my time with Clojure.
juxta provides small, composable utilities for writing cleaner conditional logic, reusable predicates, and fluent
value transformations. It is designed for developers who prefer concise, readable building blocks over repeated
control-flow boilerplate.
Highlights #
- Express multi-branch logic as values
- Compose predicates into reusable rules
- Transform values fluently with piping helpers
- Keep nullable flows readable and safe
- Add side effects without breaking expression chains
Features #
Conditional helpers #
Write branching logic in a compact, expression-friendly style.
condcondpwhenwhenNot
Predicate composition #
Build reusable boolean logic from small functions.
Predicate<T>andornotcomplementeveryPredicatesomePredicate
Threading and piping extensions #
Transform values inline without introducing temporary variables.
pipetapsomePipesomeTapapply
Why juxta? #
Dart already gives you solid language features. juxta aims to complement them with a focused set of helpers for code
that is:
- more declarative
- easier to compose
- simpler to reuse
- pleasant to read in pipelines
It is especially useful when you want business rules and transformations to read like expressions instead of scaffolding.
Installation #
Add the package to your pubspec.yaml:
dependencies:
juxta: ^1.0.0
Then fetch dependencies:
dart pub get
Import #
import 'package:juxta/juxta.dart';
Usage #
Conditional expressions #
import 'package:juxta/juxta.dart';
void main() {
final score = 82;
final grade = cond<String>(
[
(score >= 90, () => 'A'),
(score >= 80, () => 'B'),
(score >= 70, () => 'C'),
],
orElse: () => 'D',
);
print(grade); // B
}
Predicate composition #
import 'package:juxta/juxta.dart';
void main() {
final isPositive = (int value) => value > 0;
final isEven = (int value) => value.isEven;
final isPositiveAndEven = and<int>(isPositive, isEven);
print(isPositiveAndEven(4)); // true
print(isPositiveAndEven(3)); // false
print(isPositiveAndEven(-2)); // false
}
Fluent transformations #
import 'package:juxta/juxta.dart';
void main() {
final result = ' dart '
.pipe((value) => value.trim())
.tap((value) => print('Trimmed: $value'))
.pipe((value) => value.toUpperCase());
print(result); // DART
}
Nullable-friendly pipelines #
import 'package:juxta/juxta.dart';
void main() {
final greeting = 'dart'
.somePipe((value) => value.toUpperCase())
.somePipe((value) => 'Hello, $value');
final missing = (null as String?)
.somePipe((value) => value.toUpperCase());
print(greeting); // Hello, DART
print(missing); // null
}
API overview #
Conditionals #
Use these helpers when you want conditional logic to stay expression-based:
condreturns the first matching clause resultcondpmatches using custom logicwhenruns a branch only when a condition is truewhenNotruns a branch only when a condition is false
Predicates #
Use these helpers to combine boolean rules cleanly:
andandorcombine predicatesnotandcomplementinvert predicateseveryPredicaterequires all predicates to matchsomePredicaterequires at least one predicate to match
Extensions #
Use these helpers to keep transformations fluent:
pipetransforms a valuetapperforms a side effect and returns the original valuesomePipetransforms nullable values safelysomeTapperforms side effects on nullable values safelyapplyperforms a side effect and returns the original value
Example #
import 'package:juxta/juxta.dart';
void main() {
final isValidName = everyPredicate<String>([
(value) => value.isNotEmpty,
(value) => value.length >= 3,
(value) => value.startsWith('j'),
]);
final label = cond<String>(
[
(isValidName('juxta'), () => 'valid'),
],
orElse: () => 'invalid',
);
final output = 'juxta'
.pipe((value) => value.toUpperCase())
.apply((value) => print('Computed: $value'));
print(label); // valid
print(output); // JUXTA
}