Custom String Formatter API
    Preparing search index...

    Function createFormatter

    • Creates a formatter function.

      Parameters

      Returns (text: string, params: { [key: string]: any }) => string

      A function that formats a string from an object-parameter, and according to the specified configurator.

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

      class BaseFormatter implements IFormatter {
      format(value: any): string {
      return (value ?? 'null').toString();
      }
      }

      const format = createFormatter(new BaseFormatter());

      format('Hello ${title} ${name}!', {title: 'Mr.', name: 'Foreman'});
      //=> Hello Mr. Foreman!
      // Function createFormatter expects only an interface,
      // so using a class is not really necessary:

      const format = createFormatter({
      format(value: any): string {
      return (value ?? 'null').toString();
      }
      });