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 valueDate
objects compared correctly (using getTime()) Returns 0 if values are equal, otherwise 1 or -1
Arguments:
v1
: *v2
: *
# formatDate (dateVal: *, patternName: string, langopt: string) → 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
: stringOne of
formatByPattern.datePatterns
lang
= defaultLang: stringUB 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: *, patternName: string, langopt: string) → string static
Format number by pattern. Use parseFloat to convert non-number numVal argument into Number. Returns empty string for !numVal
and NaN
Arguments:
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 (patternName: string) → object static
Gets date pattern by name
Return:
Pattern description for Intl
Arguments:
patternName
: stringPattern name
# getNumberPattern (patternName: string) → object static
Gets number pattern by name
Return:
Pattern description for Intl
Arguments:
patternName
: stringPattern name
# registerCustomDateTimeFormatter (lang: string, patternName: string, formatFunction: function) static
Register custom (instead of Intl) Date/DateTime formatter for specified language and pattern.
# registerCustomNumberFormatter (lang: string, patternName: string, formatFunction: function) static
Register custom (instead of Intl) Number formatter for specified language and pattern.
# registerDatePattern (patternName: string, intlOptions: object) static
Registers custom date pattern (should be called once)
Arguments:
// 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 (patternName: string, intlOptions: object) static
Registers custom number pattern
# setLang2LocaleHook (newL2lHook: function) 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
: functionfunction what takes a UB language string and returns a ICU locale string
# addIrregularLangToLocales (additionLang2locales: object) inner
Add language to locales map, what overrides defaults
Arguments:
additionLang2locales
: object
# lang2locale (lang: string) → string inner
Return a ICU locale based on UB language
Arguments:
lang
: string
# setDefaultLang (lang: string) inner
Set a default language to use with strCmp
, formatNumber
and formatDate
.
For UI this is usually a logged-in user language
Arguments:
lang
: string