@worktif/purei
Version:
Work TIF Material UI Theme Provider and Customization Suite for React applications with dark mode support and dynamic color schemes
65 lines • 2.94 kB
JavaScript
;
/*
* Business Source License 1.1
*
* Copyright (C) 2025 Raman Marozau, raman@worktif.com
* Use of this software is governed by the Business Source License included in the LICENSE file and at www.mariadb.com/bsl11.
*
* Change Date: Never
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
* Additional Use Grant: Free for personal and non-commercial research use only.
*
*
* SPDX-License-Identifier: BUSL-1.1
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.localeDate = localeDate;
const common_1 = require("./common");
/**
* A constant that defines the default locale setting for the application.
* The locale is typically used for formatting dates, numbers, and other
* locale-specific data within the application.
*
* The value 'en-US' represents English as used in the United States.
* This locale string is based on the IETF BCP 47 language tag standard.
*/
const DEFAULT_LOCALE = 'en-US';
/**
* Formats a given date into a localized string based on specified locale and options.
*
* @param {Maybe<Date>} date - The date object to be formatted. If `undefined`, the method returns a placeholder string.
* @param {Maybe<LocaleDateOptions>} [options] - Optional formatting options. Defines whether to include weekday or day in the date string.
* @param {string} [locale=navigator.language] - The locale used for formatting. Defaults to the user's browser language or a default locale if unavailable.
* @return {string} The formatted localized date string.
*/
function localeDate(date, options, locale = typeof window !== 'undefined' ? navigator.language : DEFAULT_LOCALE) {
var _a;
if (date === void 0) {
return options ? (_a = options.presentText) !== null && _a !== void 0 ? _a : 'present' : 'present';
}
const defaultLocalDate = date.toLocaleDateString(DEFAULT_LOCALE, {
weekday: options && options.hasWeekday ? 'long' : void 0,
year: 'numeric',
month: 'long',
day: options && options.hasDay ? 'numeric' : void 0,
});
const currentLocaleDate = date.toLocaleDateString(locale, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
});
const enDateFormat = [
currentLocaleDate.split(`${common_1.COMMA}${common_1.SPACE}`)[1],
currentLocaleDate.split(`${common_1.COMMA}${common_1.SPACE})`)[2],
].join(`${common_1.COMMA}${common_1.SPACE}`);
const ruDateFormat = currentLocaleDate.split(`${common_1.COMMA}${common_1.SPACE}`)[1];
// @note: compose the full list formatter through the list of format locales
const dateFormat = {
['en-US']: enDateFormat,
['en-AU']: ruDateFormat,
['ru-RU']: ruDateFormat,
}[locale] || defaultLocalDate;
return dateFormat;
}
//# sourceMappingURL=date.js.map