Options
All
  • Public
  • Public/Protected
  • All
Menu

Contains formatting methods to convert values to strings.

Hierarchy

  • Formats

Index

Methods

  • toCurrency(value: string | number): string
  • Converts the given value to a currency string.

    example
    const format = formatters.create();
    format.toCurrency(-123456.7890); // '-$123,456.79'

    Parameters

    • value: string | number

      The value to convert to currency.

    Returns string

    The value in currency format.

  • toDate(value: number | Date): string
  • Converts the given value to a Date string with proper formatting.

    example
    const format = formatters.create();
    format.toDate(new Date(2000, 0, 1)); // '1/1/2000'

    Parameters

    • value: number | Date

      The value to format as a Date.

    Returns string

    The value in Date format.

  • toDateParts(value: number | Date): DateTimeFormatPart[]
  • Converts the given value to an array of date parts you can use to construct your own date representations.

    example
    const format = formatters.create();
    format.toDateParts(Date.now())
    .map(entry => entry.value)
    .join('');

    Parameters

    • value: number | Date

      The value to parse into date parts.

    Returns DateTimeFormatPart[]

    The date parts array.

  • toDigits(value: string | number): string
  • Strips any non-digit characters from the input, leaving only numbers in the result string.

    example
    const format = formatters.create();
    format.toDigits('a1b2c3'); // '123'

    Parameters

    • value: string | number

      The value to convert to all-digits.

    Returns string

    The value stripped of any non-digits.

  • toList(items?: any[], conjunction?: boolean): string
  • Formats a list of items for display to a user.

    example
    const format = formatters.create();
    const items = ['dog', 'cat', 'mouse'];
    format.toList(items); // 'dog, cat, and mouse'
    format.toList(items, false); // 'dog, cat, or mouse'

    Parameters

    • Optional items: any[]

      The items to list. Will be converted to Strings before formatting.

    • Optional conjunction: boolean

      Whether to consider the items combined (e.g. "and") or separate (e.g. "or").

    Returns string

    The formatted list.

  • toNumber(value: number): string
  • Converts the given value to a number with proper formatting.

    example
    const format = formatters.create();
    format.toNumber(-12345.67890); // '-12,345.679'

    Parameters

    • value: number

      The value to format as a number.

    Returns string

    The value in number format.

  • toNumberParts(value: number | bigint): NumberFormatPart[]
  • Converts the given value to an array of number parts you can use to construct your own numeric representations.

    example
    const format = formatters.create();
    format.toNumberParts(123_456_789)
    .map(entry => entry.value)
    .join('');

    Parameters

    • value: number | bigint

      The value to parse into number parts.

    Returns NumberFormatPart[]

    The number parts array.

  • Represents the difference between 2 dates in human-readable, friendly format.

    example
    const ONE_DAY = 1000 * 60 * 60 * 24;
    const format = formatters.create();
    const today = Date.now();
    const tomorrow = today + ONE_DAY;
    const dayAfterTomorrow = tomorrow + ONE_DAY;
    format.toRelativeDateTime(today, tomorrow); // 'tomorrow'
    format.toRelativeDateTime(today, dayAfterTomorrow); // 'in 2 days'

    Parameters

    Returns string

    A human-readably friendly representation of the difference between the 2 dates.

  • Converts the given value to an array of parts you can use to construct your own friendly representation of the difference between 2 dates.

    example
    const ONE_DAY = 1000 * 60 * 60 * 24;
    const format = formatters.create();
    const today = Date.now();
    const tomorrow = today + ONE_DAY;
    format.toRelativeDateTimeParts(today, tomorrow)
    .map(entry => entry.value)
    .join('');

    Parameters

    Returns RelativeTimeFormatPart[]

    Time relative time format array.

  • toTime(value: number | Date): string
  • Converts the given value to a time with proper formatting.

    example
    const format = formatters.create();
    format.toTime(new Date('1/2/2000 23:45'))); // '11:45 PM'

    Parameters

    • value: number | Date

      The value to format as a time.

    Returns string

    The value in time format.

  • toTimeParts(value: number | Date): DateTimeFormatPart[]
  • Converts the given value to an array of date parts you can use to construct your own time representation.

    example
    const format = formatters.create();
    format.toTimeParts(Date.now())
    .map(entry => entry.value)
    .join('');

    Parameters

    • value: number | Date

      The value to parse into time parts.

    Returns DateTimeFormatPart[]

    The time parts array.