fix(localize): folder restructure and Chrome corrections

This commit is contained in:
Thijs Louisse 2021-01-06 00:25:23 +01:00 committed by Joren Broekema
parent fe708509e9
commit a9c55bc17b
33 changed files with 86 additions and 74 deletions

View file

@ -1,6 +1,6 @@
import { localize } from '../localize.js'; import { localize } from '../localize.js';
import { getLocale } from './getLocale.js'; import { getLocale } from '../utils/getLocale.js';
import { normalizeIntlDate } from './normalizeIntlDate.js'; import { normalizeIntlDate } from './utils/normalizeIntlDate.js';
/** @typedef {import('../../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */ /** @typedef {import('../../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */

View file

@ -1,5 +1,5 @@
import { sanitizedDateTimeFormat } from './sanitizedDateTimeFormat.js'; import { sanitizedDateTimeFormat } from './utils/sanitizedDateTimeFormat.js';
import { splitDate } from './splitDate.js'; import { splitDate } from './utils/splitDate.js';
/** /**
* To compute the localized date format * To compute the localized date format

View file

@ -1,17 +0,0 @@
import { localize } from '../localize.js';
/**
* Gets the locale to use
*
* @param {string|undefined} locale Locale to override browser locale
* @returns {string}
*/
export function getLocale(locale) {
if (locale) {
return locale;
}
if (localize && localize.locale) {
return localize.locale;
}
return 'en-GB';
}

View file

@ -1,4 +1,5 @@
import { normalizeIntlDate } from './normalizeIntlDate.js'; import { normalizeIntlDate } from './utils/normalizeIntlDate.js';
import { forceShortMonthNamesForEnGb } from './utils/forceShortMonthNamesForEnGb.js';
/** @type {Object.<string, Object.<string,string[]>>} */ /** @type {Object.<string, Object.<string,string[]>>} */
const monthsLocaleCache = {}; const monthsLocaleCache = {};
@ -26,7 +27,9 @@ export function getMonthNames({ locale, style = 'long' } = {}) {
const normalizedDate = normalizeIntlDate(formattedDate); const normalizedDate = normalizeIntlDate(formattedDate);
months.push(normalizedDate); months.push(normalizedDate);
} }
if (locale === 'en-GB' && style === 'short') {
months = forceShortMonthNamesForEnGb(months);
}
monthsLocaleCache[locale] = monthsLocaleCache[locale] || {}; monthsLocaleCache[locale] = monthsLocaleCache[locale] || {};
monthsLocaleCache[locale][style] = months; monthsLocaleCache[locale][style] = months;

View file

@ -1,4 +1,4 @@
import { normalizeIntlDate } from './normalizeIntlDate.js'; import { normalizeIntlDate } from './utils/normalizeIntlDate.js';
/** @type {Object.<string, Object.<string,string[]>>} */ /** @type {Object.<string, Object.<string,string[]>>} */
const weekdayNamesCache = {}; const weekdayNamesCache = {};

View file

@ -1,6 +1,6 @@
import { localize } from '../localize.js'; import { localize } from '../localize.js';
import { getDateFormatBasedOnLocale } from './getDateFormatBasedOnLocale.js'; import { getDateFormatBasedOnLocale } from './getDateFormatBasedOnLocale.js';
import { addLeadingZero } from './addLeadingZero.js'; import { addLeadingZero } from './utils/addLeadingZero.js';
/** /**
* @param {function} fn * @param {function} fn

View file

@ -0,0 +1,10 @@
/**
* @param {string[]} months
*/
export function forceShortMonthNamesForEnGb(months) {
if (months[8] === 'Sept') {
// eslint-disable-next-line no-param-reassign
months[8] = 'Sep';
}
return months;
}

View file

@ -1,4 +1,4 @@
import { formatDate } from './formatDate.js'; import { formatDate } from '../formatDate.js';
import { clean } from './clean.js'; import { clean } from './clean.js';
/** /**

View file

@ -1,5 +1,5 @@
import { localize } from '../localize.js'; import { localize } from '../localize.js';
import { getLocale } from './getLocale.js'; import { getLocale } from '../utils/getLocale.js';
import { formatNumberToParts } from './formatNumberToParts.js'; import { formatNumberToParts } from './formatNumberToParts.js';
/** @typedef {import('../../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */ /** @typedef {import('../../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */

View file

@ -1,10 +1,30 @@
import { emptyStringWhenNumberNan } from './emptyStringWhenNumberNan.js'; import { emptyStringWhenNumberNan } from './utils/emptyStringWhenNumberNan.js';
import { getDecimalSeparator } from './getDecimalSeparator.js'; import { getDecimalSeparator } from './getDecimalSeparator.js';
import { getGroupSeparator } from './getGroupSeparator.js'; import { getGroupSeparator } from './getGroupSeparator.js';
import { getLocale } from './getLocale.js'; import { getLocale } from '../utils/getLocale.js';
import { normalizeIntl } from './normalizeIntl.js'; import { normalizeIntl } from './utils/normalize-format-number-to-parts/normalizeIntl.js';
import { normalSpaces } from './normalSpaces.js'; import { normalSpaces } from './utils/normalSpaces.js';
import { roundNumber } from './roundNumber.js';
/**
* Round the number based on the options
*
* @param {number} number
* @param {string} roundMode
* @throws {Error} roundMode can only be round|floor|ceiling
* @returns {number}
*/
export function roundNumber(number, roundMode) {
switch (roundMode) {
case 'floor':
return Math.floor(number);
case 'ceiling':
return Math.ceil(number);
case 'round':
return Math.round(number);
default:
throw new Error('roundMode can only be round|floor|ceiling');
}
}
/** /**
* Splits a number up in parts for integer, fraction, group, literal, decimal and currency. * Splits a number up in parts for integer, fraction, group, literal, decimal and currency.

View file

@ -1,4 +1,6 @@
import { formatNumberToParts } from './formatNumberToParts.js'; import { formatNumberToParts } from './formatNumberToParts.js';
import { localize } from '../localize.js';
import { forceCurrencyNameForPHPEnGB } from './utils/normalize-get-currency-name/forceCurrencyNameForPHPEnGB.js';
/** /**
* Based on number, returns currency name like 'US dollar' * Based on number, returns currency name like 'US dollar'
@ -15,9 +17,13 @@ export function getCurrencyName(currencyIso, options) {
currency: currencyIso, currency: currencyIso,
currencyDisplay: 'name', currencyDisplay: 'name',
})); }));
const currencyName = parts let currencyName = parts
.filter(p => p.type === 'currency') .filter(p => p.type === 'currency')
.map(o => o.value) .map(o => o.value)
.join(' '); .join(' ');
const locale = options?.locale || localize.locale;
if (currencyIso === 'PHP' && locale === 'en-GB') {
currencyName = forceCurrencyNameForPHPEnGB(currencyName);
}
return currencyName; return currencyName;
} }

View file

@ -1,4 +1,4 @@
import { getLocale } from './getLocale.js'; import { getLocale } from '../utils/getLocale.js';
/** /**
* To get the decimal separator * To get the decimal separator

View file

@ -1,5 +1,5 @@
import { getLocale } from './getLocale.js'; import { getLocale } from '../utils/getLocale.js';
import { normalSpaces } from './normalSpaces.js'; import { normalSpaces } from './utils/normalSpaces.js';
/** /**
* Gets the group separator * Gets the group separator

View file

@ -1,20 +0,0 @@
/**
* Round the number based on the options
*
* @param {number} number
* @param {string} roundMode
* @throws {Error} roundMode can only be round|floor|ceiling
* @returns {number}
*/
export function roundNumber(number, roundMode) {
switch (roundMode) {
case 'floor':
return Math.floor(number);
case 'ceiling':
return Math.ceil(number);
case 'round':
return Math.round(number);
default:
throw new Error('roundMode can only be round|floor|ceiling');
}
}

View file

@ -1,4 +1,4 @@
import { localize } from '../localize.js'; import { localize } from '../../localize.js';
/** /**
* When number is NaN we should return an empty string or returnIfNaN param * When number is NaN we should return an empty string or returnIfNaN param

View file

@ -1,7 +1,7 @@
/** /**
* Add separators when they are not present * Add separators when they are not present
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @param {string} groupSeparator * @param {string} groupSeparator
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}

View file

@ -1,7 +1,7 @@
/** /**
* For Dutch and Belgian amounts the currency should be at the end of the string * For Dutch and Belgian amounts the currency should be at the end of the string
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */

View file

@ -8,9 +8,9 @@ const CURRENCY_CODE_SYMBOL_MAP = {
/** /**
* Change the symbols for locale 'en-AU', due to bug in Chrome * Change the symbols for locale 'en-AU', due to bug in Chrome
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] * @param {import('../../../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */
export function forceENAUSymbols(formattedParts, { currency, currencyDisplay } = {}) { export function forceENAUSymbols(formattedParts, { currency, currencyDisplay } = {}) {

View file

@ -1,9 +1,9 @@
import { normalSpaces } from './normalSpaces.js'; import { normalSpaces } from '../normalSpaces.js';
/** /**
* Parts with forced "normal" spaces * Parts with forced "normal" spaces
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */

View file

@ -1,9 +1,9 @@
/** /**
* When in some locales there is no space between currency and amount it is added * When in some locales there is no space between currency and amount it is added
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] * @param {import('../../../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */
export function forceSpaceBetweenCurrencyCodeAndNumber( export function forceSpaceBetweenCurrencyCodeAndNumber(

View file

@ -2,7 +2,7 @@
* @desc Intl uses 0 as group separator for bg-BG locale. * @desc Intl uses 0 as group separator for bg-BG locale.
* This should be a ' ' * This should be a ' '
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @returns {FormatNumberPart[]} corrected formatted parts * @returns {FormatNumberPart[]} corrected formatted parts
*/ */

View file

@ -1,7 +1,7 @@
/** /**
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] * @param {import('../../../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */

View file

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

View file

@ -1,4 +1,4 @@
import { getGroupSeparator } from './getGroupSeparator.js'; import { getGroupSeparator } from '../../getGroupSeparator.js';
import { forceAddGroupSeparators } from './forceAddGroupSeparators.js'; import { forceAddGroupSeparators } from './forceAddGroupSeparators.js';
import { forceCurrencyToEnd } from './forceCurrencyToEnd.js'; import { forceCurrencyToEnd } from './forceCurrencyToEnd.js';
import { forceNormalSpaces } from './forceNormalSpaces.js'; import { forceNormalSpaces } from './forceNormalSpaces.js';
@ -9,11 +9,11 @@ import { forceTryCurrencyCode } from './forceTryCurrencyCode.js';
import { forceENAUSymbols } from './forceENAUSymbols.js'; import { forceENAUSymbols } from './forceENAUSymbols.js';
/** /**
* Function with all fixes on localize * Normalizes function "formatNumberToParts"
* *
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart * @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts * @param {FormatNumberPart[]} formattedParts
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} options * @param {import('../../../../types/LocalizeMixinTypes').FormatNumberOptions} options
* @param {string} _locale * @param {string} _locale
* @returns {FormatNumberPart[]} * @returns {FormatNumberPart[]}
*/ */

View file

@ -0,0 +1,10 @@
/**
* @param {string} currencyName
*/
export function forceCurrencyNameForPHPEnGB(currencyName) {
if (currencyName === 'Philippine pesos') {
// eslint-disable-next-line no-param-reassign
currencyName = 'Philippine pisos';
}
return currencyName;
}