Optional
filtersOptional set of filters.
When the parser cannot find a filter by name in this map, it will use the getDefaultFilter method when such is provided.
You can add or delete filters in it at any point, thus allowing for lazy-loading filters or any other dynamic scenario.
Optional
getOptional override for when a filter cannot be found, to provide
an alternative filter. When no alternative can be provided, the
function should return undefined
/ null
, for the parser to
default to throwing an error.
Usage Scenarios:
Filter Name.
Raw filter arguments (not decoded).
An alternative filter, or nothing (if no alternative filter can be provided).
// Example of aliasing a filter name to an existing filter;
import {IFormatter} from 'custom-string-formatter';
class BaseFormatter implements IFormatter {
getDefaultFilter(filter: string, args: string[]): IFilter | undefined {
if (filter === 'object' || filter === 'any') {
return this.filters.json; // alias to another filter
}
// else nothing, to throw default error
}
filters = {
json: new JsonFilter()
}
}
Optional
getOptional override to produce a default value whenever the target property does not exist. This will prevent throwing an error, which is not the safest approach.
Name of the property that failed to resolve the value (it does not exist).
Parameter object that the property was being resolved against.
Default value to be used whenever a property fails to be resolved.
Base formatting interface.