Optional
decodeOptional override for decoding filter arguments.
By default, all HTML-encoded symbols inside filter arguments are automatically decoded before they are passed into transform. And adding this method overrides that.
Some of the reasons for overriding this method:
To decode arguments individually, as needed, instead of all of them.
To skip decoding for filters that do not need it and thus improving the filter performance. For example, if your filter accepts only numbers as arguments, the method should just return the original list of arguments to avoid any decoding.
Forcing removal of accents (diacritical marks) during decoding, which function decodeFilterArg supports as an option.
To throw an error if the filter receives more arguments than it supports, or fewer than required.
Raw text arguments that may contain HTML-encoded symbols, using notations:
{
- decimal symbol code (1-6 digits);ƣ
- hexadecimal symbol code (1-5 hex digits, case-insensitive);List of arguments to be passed into transform.
// The example below replicates the default argument decoding, i.e.,
// implementing decodeArguments like this is the same as not having it at all.
import {IFilter} from 'custom-string-formatter';
class MyFilter implements IFilter {
decodeArguments(args: string[]): string[] {
return args.map(a => decodeFilterArg(a)); // decoding all arguments
}
}
// This filter decodes all arguments and removes accents (diacritical marks) from letters.
import {IFilter} from 'custom-string-formatter';
class MyFilter implements IFilter {
decodeArguments(args: string[]): string[] {
// decoding all arguments, plus removing accents:
return args.map(a => decodeFilterArg(a, true));
}
}
Transforms a value, to be piped into the next filter.
Value for transformation.
Arguments passed into the filter as : val1 : val2
.
By default, each argument is HTML-decoded, unless override decodeArguments is implemented.
Result of the value transformation.
import {IFilter} from 'custom-string-formatter';
class JsonFilter implements IFilter {
transform(value: any, args: string[]): any {
return JSON.stringify(value); // transform into a JSON string
}
}
// Since property `IFormatter.filters` uses interfaces,
// creating IFilter-based classes is not necessary:
// simple object with an arrow function:
const json = {transform: (value: any) => JSON.stringify(value)};
// or, as an object with a normal function:
const json = {
transform(value: any) => {
return JSON.stringify(value);
}
};
// Both approaches will work the same as the example above.
Value-Transformation Filter / Pipe
It takes the current value and then transforms / pipes it into the next filter in the chain (or the formatter, if last in the chain).