Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/devextreme/js/__internal/core/format_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isPlainObject,
isString,
} from '@js/core/utils/type';
import { injector as dependencyInjector } from '@ts/core/utils/m_dependency_injector';
import { injector as dependencyInjector } from '@ts/core/utils/dependency_injector';

import { getGlobalFormatByDataType } from './global_format_config';

Expand Down
47 changes: 47 additions & 0 deletions packages/devextreme/js/__internal/core/guid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export class Guid {
private readonly _value: string;

constructor(value?: string) {
const initialValue = value ? String(value) : '';

this._value = this._normalize(initialValue || this._generate());
}

public _normalize(value: string): string {
let normalizedValue = value.replace(/[^a-f0-9]/ig, '').toLowerCase();

while (normalizedValue.length < 32) {
normalizedValue += '0';
}

return [
normalizedValue.substr(0, 8),
normalizedValue.substr(8, 4),
normalizedValue.substr(12, 4),
normalizedValue.substr(16, 4),
normalizedValue.substr(20, 12),
].join('-');
}

private _generate(): string {
let value = '';

for (let i = 0; i < 32; i += 1) {
value += Math.round(Math.random() * 15).toString(16);
}

return value;
}

public toString(): string {
return this._value;
}

public valueOf(): string {
return this._value;
}

public toJSON(): string {
return this._value;
}
}
25 changes: 17 additions & 8 deletions packages/devextreme/js/__internal/core/localization/core.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import parentLocales from '@ts/core/localization/cldr-data/parent_locales';
import getParentLocale from '@ts/core/localization/parentLocale';
import { injector as dependencyInjector } from '@ts/core/utils/m_dependency_injector';
import { injector as dependencyInjector } from '@ts/core/utils/dependency_injector';

const DEFAULT_LOCALE = 'en';

interface LocaleAccessor {
(): string;
(locale: string): void;
}

export default dependencyInjector({
locale: (() => {
locale: ((): LocaleAccessor => {
let currentLocale = DEFAULT_LOCALE;

// eslint-disable-next-line @typescript-eslint/no-invalid-void-type,consistent-return
return (locale?: string): string | void => {
function localeAccessor(): string;
function localeAccessor(locale: string): void;
// eslint-disable-next-line consistent-return
function localeAccessor(locale?: string): string | void {
if (!locale) {
return currentLocale;
}

currentLocale = locale;
};
}

return localeAccessor;
})(),

getValueByClosestLocale(
getter: (locale: string) => string | number | undefined,
): string | number | undefined {
getValueByClosestLocale<TValue>(
getter: (locale: string) => TValue | undefined,
): TValue | undefined {
let locale: string = this.locale();
let value = getter(locale);
let isRootLocale = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export default {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.callBase.apply(this, [value, format, formatConfig]);
},
getCurrencySymbol(): { symbol: string } {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getCurrencySymbol(currency?: string): { symbol: string } {
return { symbol: '$' };
},
getOpenXmlCurrencyFormat(): string {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getOpenXmlCurrencyFormat(currency?: string): string | undefined {
return '$#,##0{0}_);\\($#,##0{0}\\)';
},
};
17 changes: 9 additions & 8 deletions packages/devextreme/js/__internal/core/localization/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getFormatter as getLDMLDateFormatter } from '@ts/core/localization/ldml
import { getParser as getLDMLDateParser } from '@ts/core/localization/ldml/date.parser';
import numberLocalization from '@ts/core/localization/number';
import errors from '@ts/core/m_errors';
import { injector as dependencyInjector } from '@ts/core/utils/m_dependency_injector';
import { injector as dependencyInjector } from '@ts/core/utils/dependency_injector';
import { each } from '@ts/core/utils/m_iterator';
import { isString } from '@ts/core/utils/m_type';

Expand Down Expand Up @@ -85,7 +85,6 @@ const dateLocalization = dependencyInjector({
(presetOverride as string).toLowerCase()
] || presetOverride as string;

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return numberLocalization.convertDigits(
getLDMLDateFormatter(pattern, this)(date),
);
Expand Down Expand Up @@ -114,16 +113,20 @@ const dateLocalization = dependencyInjector({

return result;
},
getMonthNames(format: BaseFormat): string[] {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getMonthNames(format: Format, type?: string): string[] {
return defaultDateNames.getMonthNames(format);
},
getDayNames(format: BaseFormat): string[] {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getDayNames(format: Format, type?: string): string[] {
return defaultDateNames.getDayNames(format);
},
getQuarterNames(format: BaseFormat): string[] {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getQuarterNames(format: Format, type?: string): string[] {
return defaultDateNames.getQuarterNames(format);
},
getPeriodNames(format: BaseFormat): string[] {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPeriodNames(format?: Format, type?: string): string[] {
return defaultDateNames.getPeriodNames(format);
},
getTimeSeparator(): string {
Expand Down Expand Up @@ -175,7 +178,6 @@ const dateLocalization = dependencyInjector({
// eslint-disable-next-line no-param-reassign
format = (FORMATS_TO_PATTERN_MAP[(format as string).toLowerCase()] || format) as string;

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return numberLocalization.convertDigits(getLDMLDateFormatter(format, this)(date));
}
}
Expand Down Expand Up @@ -216,7 +218,6 @@ const dateLocalization = dependencyInjector({
// eslint-disable-next-line @typescript-eslint/no-shadow
const text: string = that.format(value, format);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return numberLocalization.convertDigits(text, true);
};
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
return QUARTERS;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPeriodNames(_format: Format): string[] {
getPeriodNames(_format?: Format): string[] {
return PERIODS;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@ts/core/localization/currency';
import 'globalize/currency';

import config from '@js/core/config';
import type { FormatConfig, NormalizedConfig } from '@ts/core/localization/number';
import type { FormatConfig, LocalizationFormat, NormalizedConfig } from '@ts/core/localization/number';
import numberLocalization from '@ts/core/localization/number';
import openXmlCurrencyFormat from '@ts/core/localization/open_xml_currency_format';
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -79,30 +79,29 @@ if (Globalize?.formatCurrency) {
},
format(
value: string | number,
format: number | string | FormatConfig | Function | undefined,
format?: LocalizationFormat,
): string | number {
if (typeof value !== 'number') {
return value;
}

// eslint-disable-next-line no-param-reassign
format = this._normalizeFormat(format) as FormatConfig;
const normalizedFormat = this._normalizeFormat(format) as FormatConfig;

if (format) {
if (format.currency === 'default') {
format.currency = config().defaultCurrency;
if (normalizedFormat) {
if (normalizedFormat.currency === 'default') {
normalizedFormat.currency = config().defaultCurrency;
}

if (format.type === 'currency') {
if (normalizedFormat.type === 'currency') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._formatNumber(value, this._parseNumberFormatString('currency'), format);
} if (!format.type && format.currency) {
return getFormatter(format.currency, format)(value);
return this._formatNumber(value, this._parseNumberFormatString('currency'), normalizedFormat);
} if (!normalizedFormat.type && normalizedFormat.currency) {
return getFormatter(normalizedFormat.currency, normalizedFormat)(value);
}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.callBase.apply(this, [value, format]);
return this.callBase.apply(this, [value, normalizedFormat]);
},
getCurrencySymbol(currency?: string): { symbol: string } {
if (!currency) {
Expand All @@ -113,7 +112,7 @@ if (Globalize?.formatCurrency) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Globalize.cldr.main(`numbers/currencies/${currency}`);
},
getOpenXmlCurrencyFormat(currency: string): string | undefined {
getOpenXmlCurrencyFormat(currency?: string): string | undefined {
const currencySymbol = this.getCurrencySymbol(currency).symbol;
const accountingFormat = Globalize.cldr.main('numbers/currencyFormats-numberSystem-latn').accounting;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ if (Globalize?.formatDate) {
return Globalize.locale().main(`dates/calendars/gregorian/${path}`);
},

getPeriodNames(format: Format, type: string): string[] {
getPeriodNames(format?: Format, type?: string): string[] {
// eslint-disable-next-line no-param-reassign
format = format || 'wide';
// eslint-disable-next-line no-param-reassign
Expand All @@ -137,7 +137,7 @@ if (Globalize?.formatDate) {
return [json.am, json.pm];
},

getMonthNames(format: Format, type: string): string[] {
getMonthNames(format: Format, type?: string): string[] {
const months: Record<string, string> = Globalize.locale().main(`dates/calendars/gregorian/months/${type === 'format' ? type : 'stand-alone'}/${format || 'wide'}`);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand Down Expand Up @@ -228,7 +228,7 @@ if (Globalize?.formatDate) {
return this.removeRtlMarks(formatter(date));
},

parse(text: string, format: FormatObject | DateParser | string): Date | null | undefined {
parse(text: string, format?: FormatObject | DateParser | string): Date | null | undefined {
if (!text) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if (Globalize?.formatNumber) {

format(
value: string | number,
format: LocalizationFormat,
format?: LocalizationFormat,
): string {
if (typeof value !== 'number') {
return value;
Expand All @@ -120,7 +120,7 @@ if (Globalize?.formatNumber) {

parse(
text: string,
format: FormatConfig | string,
format?: FormatConfig | string,
): number | null | undefined {
if (!text) {
return undefined;
Expand Down
10 changes: 4 additions & 6 deletions packages/devextreme/js/__internal/core/localization/intl/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default {
engine(): string {
return 'intl';
},
getMonthNames(format: Format, type: string): string[] {
getMonthNames(format: Format, type?: string): string[] {
// eslint-disable-next-line @typescript-eslint/no-shadow
const intlFormats: Record<Exclude<Format, 'short'>, Intl.DateTimeFormatOptions['month']> = {
wide: 'long',
Expand All @@ -178,14 +178,12 @@ export default {
};

const monthFormat = intlFormats[format || 'wide'];

// eslint-disable-next-line no-param-reassign
type = type === 'format' ? type : 'standalone';
const nameType = type === 'format' ? type : 'standalone';

return Array.from(
{ length: 12 },
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
(_, monthIndex): string => monthNameStrategies[type](monthIndex, monthFormat),
(_, monthIndex): string => monthNameStrategies[nameType](monthIndex, monthFormat),
);
},

Expand Down Expand Up @@ -267,7 +265,7 @@ export default {
return getIntlFormatter(format)(date);
},

parse(dateString: string, format: FormatObject | string): Date | null | undefined {
parse(dateString: string, format?: FormatObject | string): Date | null | undefined {
// eslint-disable-next-line @typescript-eslint/init-declarations
let formatter: DateFormatter | undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default {
},
format(
value: string | number,
format: LocalizationFormat,
format?: LocalizationFormat,
): string {
if (typeof value !== 'number') {
return value;
Expand Down Expand Up @@ -149,10 +149,9 @@ export default {
};
},

getCurrencySymbol(currency: string): { symbol: string } {
getCurrencySymbol(currency?: string): { symbol: string } {
if (!currency) {
// @ts-expect-error
// eslint-disable-next-line
// eslint-disable-next-line no-param-reassign
currency = dxConfig().defaultCurrency;
}

Expand All @@ -161,7 +160,7 @@ export default {
symbol: symbolInfo.symbol,
};
},
getOpenXmlCurrencyFormat(currency: string): string | undefined {
getOpenXmlCurrencyFormat(currency?: string): string | undefined {
const targetCurrency = currency || dxConfig().defaultCurrency;
const currencySymbol: string = this._getCurrencySymbolInfo(targetCurrency).symbol;
const closestAccountingFormat: string | undefined = localizationCoreUtils
Expand Down
Loading
Loading