Custom String Formatter API
    Preparing search index...

    Interface IFormatter

    Base formatting interface.

    interface IFormatter {
        filters?: { [name: string]: IFilter<any, any> };
        format(value: any): string;
        getDefaultFilter(
            filter: string,
            args: string[],
        ): undefined | IFilter<any, any>;
        getDefaultValue(prop: string, params: { [key: string]: any }): any;
    }
    Index

    Properties

    filters?: { [name: string]: IFilter<any, any> }

    Optional 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.

    Methods

    • Formats any value, according to its type.

      Parameters

      • value: any

      Returns string

      Formatted string.

      import {IFormatter} from 'custom-string-formatter';

      class BaseFormatter implements IFormatter {
      format(value: any): string {
      // your own value formatting here;
      return (value ?? 'null').toString();
      }
      }
    • Optional 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:

      • Support for filter aliases when multiple filter names must be resolved into one filter.
      • Support for dynamic filters. For example, to lazy-load filters and add them to the list upon first use.

      Parameters

      • filter: string

        Filter Name.

      • args: string[]

        Raw filter arguments (not decoded).

      Returns undefined | IFilter<any, any>

      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 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.

      Parameters

      • prop: string

        Name of the property that failed to resolve the value (it does not exist).

      • params: { [key: string]: any }

        Parameter object that the property was being resolved against.

      Returns any

      Default value to be used whenever a property fails to be resolved.

      import {IFormatter} from 'custom-string-formatter';

      class BaseFormatter implements IFormatter {
      getDefaultValue(prop: string, params: { [key: string]: any }) {
      // return whatever is to be the default for properties,
      // including nothing (undefined)
      }
      }