Dates and Numbers formatting and string comparison using Intl On client this module exposed as UB.formatter and Vue.prototype.$UB.formatter

  • for available date format options see https://tc39.es/ecma402/#datetimeformat-objects
  • for available number format options see https://tc39.es/ecma402/#numberformat-objects

Members

# datePatterns : Array.<string> static

Available date patterns

# numberPatterns : Array.<string> static

Available Number patterns

# langToICUDefaults inner

default language to ICU locale transformation. Can be extended/overridden by formatByPattern.addIrregularLangToLocales

Methods

# collationCompare (v1*, v2*) → number static

Compare two value:

  • if one of value is string takes into account current client locale (see setDefaultLang)
  • null' and undefined' are always smaller than any other value
  • Date objects compared correctly (using getTime()) Returns 0 if values are equal, otherwise 1 or -1

Arguments:
  • v1: *
  • v2: *

# formatDate (dateVal*, patternNamestring, langoptstring) → string static

Format date by pattern

Arguments:
  • dateVal: *

    Date object or Number/String what will be converted to Date using new Date(); null, undefined and empty string will be converted to empty string

  • patternName: string

    One of formatByPattern.datePatterns

  • lang = defaultLang: string

    UB language code. If not specified value defined by setDefaultLang is used

  
      const formatByPattern = require('@unitybase/cs-shared').formatByPattern
const d = new Date(2020, 04, 23, 13, 14)
formatByPattern.formatDate(d, 'date') // on client can be called without 3rd lang parameter - will be formatted for user default lang (for uk - 23.05.2020)
formatByPattern.formatDate('2020-05-23', 'date', 'uk') // 23.05.2020
formatByPattern.formatDate(d, 'date', 'en') // 05/23/2020
formatByPattern.formatDate(d, 'dateTime', 'uk') // 23.05.2020 13:14
formatByPattern.formatDate(d, 'dateTimeFull', 'uk') // 23.05.2020 13:14:00
  

# formatNumber (numVal*, patternNamestring, langoptstring) → string static

Format number by pattern. Use parseFloat to convert non-number numVal argument into Number. Returns empty string for !numVal and NaN

Arguments:
  • numVal: *
  • patternName: string

    One of formatByPattern.datePatterns

  • lang = defaultLang: string

    UB language code. If not specified value defined by setDefaultLang is used

  
      const formatByPattern = require('@unitybase/cs-shared').formatByPattern
const n = 2305.1
formatByPattern.formatNumber(n, 'sum', 'en') // 2,305.10
formatByPattern.formatNumber('2305.1', 'sum', 'en') // 2,305.10
formatByPattern.formatNumber(n, 'sum') // on client can be called without 3rd lang parameter - will be formatted for user default lang (for uk "2 305,10")
  

# getDatePattern (patternNamestring) → object static

Gets date pattern by name

Return:

Pattern description for Intl

Arguments:
  • patternName: string

    Pattern name

# getNumberPattern (patternNamestring) → object static

Gets number pattern by name

Return:

Pattern description for Intl

Arguments:
  • patternName: string

    Pattern name

# registerCustomDateTimeFormatter (langstring, patternNamestring, formatFunctionfunction) static

Register custom (instead of Intl) Date/DateTime formatter for specified language and pattern.

Arguments:
  • lang: string
  • patternName: string
  • formatFunction: function

    function what accept Date as parameter and returns string formatted according to lang and patternName

# registerCustomNumberFormatter (langstring, patternNamestring, formatFunctionfunction) static

Register custom (instead of Intl) Number formatter for specified language and pattern.

Arguments:
  • lang: string
  • patternName: string
  • formatFunction: function

    function what accept number as parameter and returns string formatted according to lang and patternName

# registerDatePattern (patternNamestring, intlOptionsobject) static

Registers custom date pattern (should be called once)

Arguments:
  • patternName: string

    Pattern name

  • intlOptions: object

    Intl.DateFormat constructor options - see https://tc39.es/ecma402/#datetimeformat-objects

  
      // format Date for New_York time zone
$UB.formatter.registerDatePattern('dateTimeInNewYork', {
  month: '2-digit', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit',
  timeZone: 'America/New_York'
})
  

# registerNumberPattern (patternNamestring, intlOptionsobject) static

Registers custom number pattern

Arguments:
  • patternName: string

    Pattern name

  • intlOptions: object

    Intl.NumberFormat constructor options - see https://tc39.es/ecma402/#numberformat-objects

# setLang2LocaleHook (newL2lHookfunction) static

Set application-specific UB lang to ICU locale transformation hook. Default hook uses {en: 'en-US', ru: 'ru-RU', uk: 'uk-UA', az: 'az'} translation, any other language ln translated into ln-LN.

Application can redefine this rule by sets his own hook, for example to translate en -> 'en-GB' etc.

Arguments:
  • newL2lHook: function

    function what takes a UB language string and returns a ICU locale string

# addIrregularLangToLocales (additionLang2localesobject) inner

Add language to locales map, what overrides defaults

Arguments:

# lang2locale (langstring) → string inner

Return a ICU locale based on UB language

Arguments:

# setDefaultLang (langstring) inner

Set a default language to use with strCmp, formatNumber and formatDate. For UI this is usually a logged-in user language

Arguments: