How It Works
Understand the core principles of Regist, including lazy evaluation, chaining, reusability, and error handling.
Regist is designed to be intuitive, but understanding its core principles will help you unlock its full potential. This guide covers the key concepts of lazy evaluation, chaining, reusability, and error handling.
Lazy Evaluation
At the heart of Regist is the principle of lazy evaluation. This means that no operations are actually performed when you chain methods together. A Regist chain is simply a blueprint of the steps to be taken.
The chain is only executed when you call the .try() method. This design has several advantages:
- Performance: No unnecessary work is done until you explicitly ask for the result.
- Predictability: The order of operations is clear and the execution is deterministic.
- Reusability: Since chains are just blueprints, they can be stored, passed around, and reused multiple times.
Chaining and Interoperability
Regist features two distinct but interoperable APIs: assertThat for validation and stringTransform for transformation. You can seamlessly switch between them in a single, fluent chain.
- Use
.assertThat()to switch from a transformation chain to a validation chain. - Use
.stringTransform()to switch from a validation chain to a transformation chain.
This allows you to create powerful and expressive logic that combines validation and transformation in a natural way.
Reusability
Because of lazy evaluation, Regist chains are highly reusable. You can create a base chain of operations and then branch off from it to create more specialized chains without affecting the original.
This is incredibly useful for creating a library of common validation or transformation patterns that can be used throughout your application.
Error Handling
Regist provides a simple yet powerful way to handle errors. The .try() method can optionally accept a handler function that will be called if any step in the chain fails.
This handler receives three arguments:
error: AnErrorobject with a descriptive message.stepName: The name of the method that failed (e.g.,"extract","isEmail").valueAtFailure: The value of the string just before the failing step was executed.
If you don't provide a handler, Regist will throw an error, which you can catch with a standard try...catch block. If no error occurs, .try() returns the result of the chain (true/false for assertions, or the transformed string).