micromustache
micromustache copied to clipboard
Hooks
Current behaviour
The renderFn() and renderFnAsync() seem to be mainly underused if I am to trust Github search.
This feature is one of the killer features of micromustache, so let's rethink how to make it more intuitive.
Expected behavior
Add the possibility to process paths before and after they are resolved using get(). This can allow HTML escaping for example. Also see #50
The code (and its usage) will be simpler if we have some steps for resolving and allow hooks to be implemented to modify the default behaviour:
- before the path is parsed
- after the path has turned into a ref but before that ref is resolved from the scope
- after the value is resolved from the scope and before it is being interpolated
At each step, a hook can modify its input, throw, or cancel the rest of the chain
Also it should be possible to disable a step (say get() for example)
That way we can have only two methods: render() and renderAsync() and the hooks can be passed as options.
To keep the lib small and focused, it's better to offload this to the user to do whatever they want.
The users can easily achieve this using the tokenize() and get() (or parsePath() and getRef()).
This can be documented as "tips and tricks".
This will simplify the library code while giving a huge flexibility to the user to implement their resolving pipeline with whatever parsing or sanetization they may choose to have.
A side effect of this refactoring is that finally we can have progressive tokenization, parsing and stringification that are nice to the JavaScript event loop and don't block it.