createStrategies()
createStrategies<
K,In,Out>(strategies):object
Creates a strategy resolver from a record of named strategies.
Replaces the GoF Context class - instead of setStrategy() + execute(),
you simply pick a function by key and call it.
Type Parameters
K: K extends string
Union of strategy keys
In: In
The input type
Out: Out
The output type
Parameters
strategies: Record<K, Strategy<In, Out>>
Record mapping keys to strategy functions
Returns
Resolver with use (get fn) and execute (run by key)
use(): (key) => Strategy<In, Out>
Get a strategy by key. Typed keys only.
key:
K
Returns:Strategy<In, Out>
execute(): (key, input) => Out
Execute a strategy by key with the given input.
key:
K
input:In
Returns:Out
get(): (key) => Option<Strategy<In, Out>>
Safe lookup for dynamic/runtime keys. Returns Option<Strategy>.
Since
2.4.0
Example
const pricing = createStrategies({
regular: (price: number) => price,
vip: (price: number) => price * 0.8,
premium: (price: number) => price * 0.7,
});
const applyVip = pricing.use("vip");
applyVip(100); // 80
pricing.execute("premium", 100); // 70
// Safe lookup for dynamic keys (e.g. from config)
const key: string = getFromConfig();
pricing.get(key); // `Option<Strategy<number, number>>`
Strategy()<In, Out>
Strategy<
In,Out> = (input) =>Out
A Strategy is a function that transforms an input into an output. This replaces the GoF Strategy interface + concrete classes.
Type Parameters
In: In
The input type
Out: Out
The output type
Parameters
input: In
Returns: Out
Since
2.4.0