Core Concepts
Modular expressions
Split one large expression into reusable sub-expressions, then compose them into a final result that stays reactive.
See how modular expressions are implemented in the advanced modular expressions page.
Practical value
Key points
- Sub-expressions stay observable, so changes propagate through the graph automatically.
- Shared logic is evaluated once and reused by dependents.
- Works with synchronous and asynchronous values (observables/promises).
- You can move from long formulas to maintainable expression modules without changing reactive behavior.
From monolithic formula to reusable modules
A large formula often duplicates parts of the same calculation, which hurts readability and performance. In the credit-risk example, the risk score logic appears multiple times in one expression string.
With modular expressions, that same logic is split into smaller units. Each unit has one responsibility, and a final expression combines them. This removes duplication and makes each piece independently understandable.
Reactive behavior across expression boundaries
When a sub-expression changes, dependent expressions update automatically. You do not need custom plumbing code to keep derived values in sync.
Because each expression is observable, the dependency graph remains explicit and predictable, even when modules are nested.
Performance and maintainability benefits
Expression parser services cache parse/evaluation structures, and modular design avoids repeated heavy computation.
As rules grow, modular expressions let you evolve one part at a time while keeping the full formula behavior stable.
Example
import { InjectionContainer } from '@rs-x/core';
import { rsx, RsXExpressionParserModule } from '@rs-x/expression-parser';
await InjectionContainer.load(RsXExpressionParserModule);
const model = {
customer: {
age: 31,
income: 52000,
employmentYears: 4,
},
credit: {
score: 640,
outstandingDebt: 12000,
},
riskParameters: {
market: { baseInterestRate: 0.04 },
risk: { volatilityIndex: 0.22, recessionProbability: 0.18 },
},
};
// Sub-expressions