diff --git a/packages/localize/src/date/formatDate.js b/packages/localize/src/date/formatDate.js index ed191435c..ebc922a16 100644 --- a/packages/localize/src/date/formatDate.js +++ b/packages/localize/src/date/formatDate.js @@ -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 diff --git a/packages/localize/src/date/getLocale.js b/packages/localize/src/date/getLocale.js index a8e96b988..88cba75bc 100644 --- a/packages/localize/src/date/getLocale.js +++ b/packages/localize/src/date/getLocale.js @@ -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) { diff --git a/packages/localize/src/number/emptyStringWhenNumberNan.js b/packages/localize/src/number/emptyStringWhenNumberNan.js index c077e89f4..6a4140f96 100644 --- a/packages/localize/src/number/emptyStringWhenNumberNan.js +++ b/packages/localize/src/number/emptyStringWhenNumberNan.js @@ -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) { diff --git a/packages/localize/src/number/forceENAUSymbols.js b/packages/localize/src/number/forceENAUSymbols.js index 4c9822492..e477783fc 100644 --- a/packages/localize/src/number/forceENAUSymbols.js +++ b/packages/localize/src/number/forceENAUSymbols.js @@ -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]; } diff --git a/packages/localize/src/number/forceSpaceBetweenCurrencyCodeAndNumber.js b/packages/localize/src/number/forceSpaceBetweenCurrencyCodeAndNumber.js index 0fa2a1271..f43822c2f 100644 --- a/packages/localize/src/number/forceSpaceBetweenCurrencyCodeAndNumber.js +++ b/packages/localize/src/number/forceSpaceBetweenCurrencyCodeAndNumber.js @@ -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( diff --git a/packages/localize/src/number/forceTryCurrencyCode.js b/packages/localize/src/number/forceTryCurrencyCode.js index 54a24875f..67907f1e1 100644 --- a/packages/localize/src/number/forceTryCurrencyCode.js +++ b/packages/localize/src/number/forceTryCurrencyCode.js @@ -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 } = {}) { diff --git a/packages/localize/src/number/forceYenSymbol.js b/packages/localize/src/number/forceYenSymbol.js index eee6d0eef..ccb9dfec1 100644 --- a/packages/localize/src/number/forceYenSymbol.js +++ b/packages/localize/src/number/forceYenSymbol.js @@ -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 } = {}) { diff --git a/packages/localize/src/number/formatNumber.js b/packages/localize/src/number/formatNumber.js index 1da70f2cf..f1bed42f4 100644 --- a/packages/localize/src/number/formatNumber.js +++ b/packages/localize/src/number/formatNumber.js @@ -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 diff --git a/packages/localize/src/number/formatNumberToParts.js b/packages/localize/src/number/formatNumberToParts.js index 704e110e0..1a5f4c1d3 100644 --- a/packages/localize/src/number/formatNumberToParts.js +++ b/packages/localize/src/number/formatNumberToParts.js @@ -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 = {}) { diff --git a/packages/localize/src/number/getCurrencyName.js b/packages/localize/src/number/getCurrencyName.js index 45bc00df0..14fe56e14 100644 --- a/packages/localize/src/number/getCurrencyName.js +++ b/packages/localize/src/number/getCurrencyName.js @@ -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) { diff --git a/packages/localize/src/number/getDecimalSeparator.js b/packages/localize/src/number/getDecimalSeparator.js index 8cd0a3400..aa593aa04 100644 --- a/packages/localize/src/number/getDecimalSeparator.js +++ b/packages/localize/src/number/getDecimalSeparator.js @@ -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) { diff --git a/packages/localize/src/number/getLocale.js b/packages/localize/src/number/getLocale.js index 0725d2f8d..73f0b1542 100644 --- a/packages/localize/src/number/getLocale.js +++ b/packages/localize/src/number/getLocale.js @@ -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) { diff --git a/packages/localize/src/number/normalizeIntl.js b/packages/localize/src/number/normalizeIntl.js index fb2dbd87c..b66ee582c 100644 --- a/packages/localize/src/number/normalizeIntl.js +++ b/packages/localize/src/number/normalizeIntl.js @@ -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[]} */ diff --git a/packages/localize/types/LocalizeMixinTypes.d.ts b/packages/localize/types/LocalizeMixinTypes.d.ts index a25e65d24..be0af9e3c 100644 --- a/packages/localize/types/LocalizeMixinTypes.d.ts +++ b/packages/localize/types/LocalizeMixinTypes.d.ts @@ -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; }