fix(localize): align on common Intl type and reduce duplicates

This commit is contained in:
Joren Broekema 2020-09-14 18:30:56 +02:00 committed by Thomas Allmer
parent 32202a88ec
commit b01cacbded
14 changed files with 43 additions and 72 deletions

View file

@ -5,31 +5,17 @@ import { normalizeIntlDate } from './normalizeIntlDate.js';
* Formats date based on locale and options
*
* @param {Date} date
* @param {Object} [options] Intl options are available
* @param {string} [options.locale]
* @param {string} [options.localeMatcher]
* @param {string} [options.formatMatcher]
* @param {boolean}[options.hour12]
* @param {string} [options.numberingSystem]
* @param {string} [options.calendar]
* @param {string} [options.timeZone]
* @param {string} [options.timeZoneName]
* @param {string} [options.weekday]
* @param {string} [options.era]
* @param {string} [options.year]
* @param {string} [options.month]
* @param {string} [options.day]
* @param {string} [options.hour]
* @param {string} [options.minute]
* @param {string} [options.second]
* @param {import('@lion/localize/types/LocalizeMixinTypes').FormatDateOptions} [options] Intl options are available
* @returns {string}
*/
export function formatDate(date, options) {
if (!(date instanceof Date)) {
return '';
}
/** @type {options} */
const formatOptions = options || {};
const formatOptions =
options ||
/** @type {import('@lion/localize/types/LocalizeMixinTypes').FormatDateOptions} */ ({});
/**
* Set smart defaults if:
* 1) no options object is passed

View file

@ -3,7 +3,7 @@ import { localize } from '../localize.js';
/**
* Gets the locale to use
*
* @param {string} locale Locale to override browser locale
* @param {string|undefined} locale Locale to override browser locale
* @returns {string}
*/
export function getLocale(locale) {

View file

@ -3,7 +3,7 @@ import { localize } from '../localize.js';
/**
* When number is NaN we should return an empty string or returnIfNaN param
*
* @param {string} returnIfNaN
* @param {string|undefined} returnIfNaN
* @returns {string}
*/
export function emptyStringWhenNumberNan(returnIfNaN) {

View file

@ -10,14 +10,12 @@ const CURRENCY_CODE_SYMBOL_MAP = {
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {Object} [options]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]}
*/
export function forceENAUSymbols(formattedParts, { currency, currencyDisplay } = {}) {
const result = formattedParts;
if (formattedParts.length > 1 && currencyDisplay === 'symbol') {
if (formattedParts.length > 1 && currencyDisplay === 'symbol' && currency) {
if (Object.keys(CURRENCY_CODE_SYMBOL_MAP).includes(currency)) {
result[0].value = CURRENCY_CODE_SYMBOL_MAP[currency];
}

View file

@ -3,9 +3,7 @@
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {Object} [options]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]}
*/
export function forceSpaceBetweenCurrencyCodeAndNumber(

View file

@ -1,9 +1,7 @@
/**
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {Object} [options]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]}
*/
export function forceTryCurrencyCode(formattedParts, { currency, currencyDisplay } = {}) {

View file

@ -1,9 +1,7 @@
/**
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {Object} [options]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]}
*/
export function forceYenSymbol(formattedParts, { currency, currencyDisplay } = {}) {

View file

@ -5,25 +5,12 @@ import { formatNumberToParts } from './formatNumberToParts.js';
* Formats a number based on locale and options. It uses Intl for the formatting.
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @typedef {import('@lion/localize/types/LocalizeMixinTypes').FormatNumberOptions} FormatOptions
* @param {number} number Number to be formatted
* @param {Object} [options] Intl options are available extended by roundMode and returnIfNaN
* @param {string} [options.roundMode]
* @param {string} [options.returnIfNaN]
* @param {string} [options.locale]
* @param {string} [options.localeMatcher]
* @param {string} [options.numberingSystem]
* @param {string} [options.style]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {boolean}[options.useGrouping]
* @param {number} [options.minimumIntegerDigits]
* @param {number} [options.minimumFractionDigits]
* @param {number} [options.maximumFractionDigits]
* @param {number} [options.minimumSignificantDigits]
* @param {number} [options.maximumSignificantDigits]
* @param {FormatOptions} [options] Intl options are available extended by roundMode and returnIfNaN
* @returns {string}
*/
export function formatNumber(number, options = {}) {
export function formatNumber(number, options = /** @type {FormatOptions} */ ({})) {
if (number === undefined || number === null) return '';
const formattedToParts = formatNumberToParts(number, options);
// If number is not a number

View file

@ -11,21 +11,7 @@ import { roundNumber } from './roundNumber.js';
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {number} number Number to split up
* @param {Object} [options] Intl options are available extended by roundMode,returnIfNaN
* @param {string} [options.roundMode]
* @param {string} [options.returnIfNaN]
* @param {string} [options.locale]
* @param {string} [options.localeMatcher]
* @param {string} [options.numberingSystem]
* @param {string} [options.style]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {boolean}[options.useGrouping]
* @param {number} [options.minimumIntegerDigits]
* @param {number} [options.minimumFractionDigits]
* @param {number} [options.maximumFractionDigits]
* @param {number} [options.minimumSignificantDigits]
* @param {number} [options.maximumSignificantDigits]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] Intl options are available extended by roundMode,returnIfNaN
* @returns {string | FormatNumberPart[]} Array with parts or (an empty string or returnIfNaN if not a number)
*/
export function formatNumberToParts(number, options = {}) {

View file

@ -5,7 +5,7 @@ import { formatNumberToParts } from './formatNumberToParts.js';
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {string} currencyIso iso code like USD
* @param {Object} options Intl options are available extended by roundMode
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] Intl options are available extended by roundMode
* @returns {string} currency name like 'US dollar'
*/
export function getCurrencyName(currencyIso, options) {

View file

@ -3,7 +3,7 @@ import { getLocale } from './getLocale.js';
/**
* To get the decimal separator
*
* @param {string} locale To override the browser locale
* @param {string|null} locale To override the browser locale
* @returns {string} The separator
*/
export function getDecimalSeparator(locale) {

View file

@ -3,7 +3,7 @@ import { localize } from '../localize.js';
/**
* Gets the locale to use
*
* @param {string} [locale] Locale to override browser locale
* @param {string|null} [locale] Locale to override browser locale
* @returns {string}
*/
export function getLocale(locale) {

View file

@ -13,10 +13,7 @@ import { forceENAUSymbols } from './forceENAUSymbols.js';
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {Object} options
* @param {string} [options.style]
* @param {string} [options.currency]
* @param {string} [options.currencyDisplay]
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} options
* @param {string} _locale
* @returns {FormatNumberPart[]}
*/

View file

@ -5,6 +5,29 @@ export interface FormatNumberPart {
value: string;
}
// Take the DateTimeFormat and add the missing resolved options as well as optionals
export declare interface FormatDateOptions extends Intl.DateTimeFormatOptions {
locale?: string;
calendar?: string;
numberingSystem?: string;
timeZone?: string;
roundMode?: string;
returnIfNaN?: string;
decimalSeparator?: string;
mode?: 'pasted' | 'auto';
}
// Take the DateTimeFormat and add the missing resolved options as well as optionals, and our own
export declare interface FormatNumberOptions extends Intl.NumberFormatOptions {
locale?: string;
numberingSystem?: string;
roundMode?: string;
returnIfNaN?: string;
decimalSeparator?: string;
mode?: 'pasted' | 'auto';
}
interface StringToFunctionMap {
[key: string]: Function;
}