dbl-utils
Version:
Utilities for dbl, adba and others projects
384 lines (383 loc) • 11.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatCurrency = exports.formatNumberCompact = exports.formatNumber = exports.formatDateTime = exports.formatTime = exports.formatDate = exports.getLang = exports.setLang = exports.removeTask = exports.addTasks = exports.addFormatCurrency = exports.addFormatNumberCompact = exports.addFormatNumber = exports.addFormatDateTime = exports.addFormatTime = exports.addFormatDate = exports.addDictionary = void 0;
exports.trackingTexts = trackingTexts;
exports.getTexts = getTexts;
const moment_1 = __importDefault(require("moment"));
const object_mutation_1 = require("./object-mutation");
const config = {
lang: '_default',
dictionary: { default: {} },
formatDate: { default: 'MM/DD/YYYY' },
formatTime: { default: 'HH:mm:ss' },
formatDateTime: { default: 'MM/DD/YYYY HH:mm:ss' },
formatNumber: { default: {} },
formatNumberCompact: { default: '0.00a' },
formatCurrency: { default: { currency: 'USD', style: 'currency' } },
tasks: {}
};
let isTrackingText = false;
let trackingTextSet = new Set();
/**
* Enable or disable text tracking.
* When enabled every translation key requested will be stored.
*
* @param setTracking - Whether tracking should be enabled
*
* @example
* ```ts
* trackingTexts(true);
* t('hello');
* trackingTexts(false);
* getTexts(); // ['hello']
* ```
*/
function trackingTexts(setTracking = true) {
isTrackingText = setTracking;
}
/**
* Retrieve the list of tracked texts.
*
* @returns Array of unique translation keys requested
*
* @example
* ```ts
* getTexts();
* ```
*/
function getTexts() {
return Array.from(trackingTextSet);
}
/**
* Adds a dictionary to the configuration object.
* @param {Object} dictionary - The dictionary to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addDictionary({ es: { hello: 'Hola' } });
* ```
*/
const addDictionary = (dictionary) => {
if (typeof dictionary !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.dictionary, dictionary);
return true;
};
exports.addDictionary = addDictionary;
/**
* Adds date formats to the configuration object.
* @param {Object} formats - The date formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatDate({ es: 'DD/MM/YYYY' });
* ```
*/
const addFormatDate = (formats) => {
if (typeof formats !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.formatDate, formats);
return true;
};
exports.addFormatDate = addFormatDate;
/**
* Adds time formats to the configuration object.
* @param {Object} formats - The time formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatTime({ es: 'HH:mm' });
* ```
*/
const addFormatTime = (formats) => {
if (typeof formats !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.formatTime, formats);
return true;
};
exports.addFormatTime = addFormatTime;
/**
* Adds date-time formats to the configuration object.
* @param {Object} formats - The date-time formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatDateTime({ es: 'DD/MM HH:mm' });
* ```
*/
const addFormatDateTime = (formats) => {
if (typeof formats !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.formatDateTime, formats);
return true;
};
exports.addFormatDateTime = addFormatDateTime;
/**
* Adds number formats to the configuration object.
* @param {Object} formats - The number formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatNumber({ es: { short: { maximumFractionDigits: 1 } } });
* ```
*/
const addFormatNumber = (formats) => {
if (typeof formats !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.formatNumber, formats);
return true;
};
exports.addFormatNumber = addFormatNumber;
/**
* Adds compact number formats to the configuration object.
* @param {Object} formats - The compact number formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatNumberCompact({ es: '0a' });
* ```
*/
const addFormatNumberCompact = (formats) => {
if (typeof formats !== 'object')
return false;
(0, object_mutation_1.deepMerge)(config.formatNumberCompact, formats);
return true;
};
exports.addFormatNumberCompact = addFormatNumberCompact;
/**
* Adds currency formats to the configuration object.
* @param {Object} formats - The currency formats to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addFormatCurrency({ es: { short: { currency: 'EUR' } } });
* ```
*/
const addFormatCurrency = (formats) => {
if (typeof formats !== 'object')
return false;
Object.values(formats).forEach(format => (format.style = 'currency'));
(0, object_mutation_1.deepMerge)(config.formatCurrency, formats);
return true;
};
exports.addFormatCurrency = addFormatCurrency;
/**
* Adds tasks to be executed on language change.
* @param {Object} tasks - The tasks to add.
* @returns {boolean} True if added correctly, false otherwise.
*
* @example
* ```ts
* addTasks({ reload: lang => console.log(lang) });
* ```
*/
const addTasks = (tasks) => {
Object.entries(tasks).forEach(([key, task]) => {
if (typeof task === 'function')
config.tasks[key] = task;
});
return true;
};
exports.addTasks = addTasks;
/**
* Removes a specific task.
* @param {string} key - The key of the task to remove.
* @returns {boolean} True if removed, false otherwise.
*
* @example
* ```ts
* removeTask('reload');
* ```
*/
const removeTask = (key) => {
if (!config.tasks[key])
return false;
delete config.tasks[key];
return true;
};
exports.removeTask = removeTask;
/**
* Sets the current language.
* @param {string} newLang - The new language to set.
* @returns {boolean} True if set correctly, false otherwise.
*
* @example
* ```ts
* setLang('es');
* ```
*/
const setLang = (newLang) => {
if (!newLang || config.lang === newLang)
return false;
moment_1.default.locale(newLang);
config.lang = newLang;
Object.values(config.tasks).forEach(task => task(newLang));
return true;
};
exports.setLang = setLang;
/**
* Gets the current language.
* @returns {string} The current language.
*
* @example
* ```ts
* const lang = getLang();
* ```
*/
const getLang = () => {
return config.lang;
};
exports.getLang = getLang;
/**
* Selects a text from the default dictionary.
* @param {string} text - The text to select.
* @param {string} [context] - The context of the text.
* @returns {string} The selected text from the default dictionary.
*/
const selectFromDefault = (text, context) => {
const dictDefault = config.dictionary.default;
if (context && typeof dictDefault[context] === 'object' && dictDefault[context][text])
return dictDefault[context][text];
return dictDefault[text] || text;
};
/**
* Function to translate texts.
* @param {string} text - The text to translate.
* @param {string} [context] - The context of the text.
* @returns {string} The translated text.
*
* @example
* ```ts
* addDictionary({ es: { hello: 'Hola' } });
* setLang('es');
* t('hello'); // 'Hola'
* ```
*/
const t = (text, context) => {
if (isTrackingText) {
trackingTextSet.add(text);
}
if (config.lang === '_default')
return selectFromDefault(text, context);
const dict1 = config.dictionary[config.lang];
if (typeof dict1 !== 'object')
return selectFromDefault(text, context);
const objContext = context && dict1[context];
if (typeof objContext === 'object' && objContext[text])
return objContext[text];
return dict1[text] || selectFromDefault(text);
};
/**
* Formats a value according to the current language and context.
* @param {Record<string, any>} formatObject - The configuration object to use.
* @param {string} [context] - The context of the value.
* @returns {string} The formatted value.
*/
function formatGeneric(formatObject, context) {
if (context && formatObject[context]) {
return formatObject[context];
}
else if (config.lang !== '_default' && formatObject[config.lang]) {
return context && formatObject[config.lang][context]
? formatObject[config.lang][context]
: formatObject[config.lang].default || formatObject[config.lang];
}
return context && formatObject.default[context]
? formatObject.default[context]
: formatObject.default;
}
/**
* Formats a date according to the current language and context.
* @param {string} [context] - The context of the date.
* @returns {string} The formatted date.
*
* @example
* ```ts
* formatDate();
* ```
*/
const formatDate = (context) => {
return formatGeneric(config.formatDate, context);
};
exports.formatDate = formatDate;
/**
* Formats a time according to the current language and context.
* @param {string} [context] - The context of the time.
* @returns {string} The formatted time.
*
* @example
* ```ts
* formatTime();
* ```
*/
const formatTime = (context) => {
return formatGeneric(config.formatTime, context);
};
exports.formatTime = formatTime;
/**
* Formats a date-time according to the current language and context.
* @param {string} [context] - The context of the date-time.
* @returns {string} The formatted date-time.
*
* @example
* ```ts
* formatDateTime();
* ```
*/
const formatDateTime = (context) => {
return formatGeneric(config.formatDateTime, context);
};
exports.formatDateTime = formatDateTime;
/**
* Formats a number according to the current language and context.
* @param {string} [context] - The context of the number.
* @returns {string} The formatted number.
*
* @example
* ```ts
* formatNumber();
* ```
*/
const formatNumber = (context) => {
return formatGeneric(config.formatNumber, context);
};
exports.formatNumber = formatNumber;
/**
* Formats a compact number according to the current language and context.
* @param {string} [context] - The context of the compact number.
* @returns {string} The formatted compact number.
*
* @example
* ```ts
* formatNumberCompact();
* ```
*/
const formatNumberCompact = (context) => {
return formatGeneric(config.formatNumberCompact, context);
};
exports.formatNumberCompact = formatNumberCompact;
/**
* Formats a currency according to the current language and context.
* @param {string} [context] - The context of the currency.
* @returns {string} The formatted currency.
*
* @example
* ```ts
* formatCurrency();
* ```
*/
const formatCurrency = (context) => {
return formatGeneric(config.formatCurrency, context);
};
exports.formatCurrency = formatCurrency;
exports.default = t;