handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
64 lines (60 loc) • 2.32 kB
JavaScript
;
exports.__esModule = true;
exports.intlFormatter = intlFormatter;
exports.isNumbroScheme = isNumbroScheme;
exports.numbroFormatter = numbroFormatter;
var _numbro = _interopRequireDefault(require("numbro"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const DEFAULT_INTL_FORMAT = {
useGrouping: false,
maximumFractionDigits: 20
};
/**
* Formats the value using the numbro format.
*
* @param {*} value Value to be formatted.
* @param {CellMeta} cellProperties Cell meta object.
* @returns {*} Returns the formatted value.
*/
function numbroFormatter(value, cellProperties) {
const {
numericFormat
} = cellProperties;
const cellCulture = numericFormat && numericFormat.culture || '-';
const cellFormatPattern = numericFormat && numericFormat.pattern;
if (typeof cellCulture !== 'undefined' && !_numbro.default.languages()[cellCulture]) {
const shortTag = cellCulture.replace('-', '');
const langData = _numbro.default.allLanguages ? _numbro.default.allLanguages[cellCulture] : _numbro.default[shortTag];
if (langData) {
_numbro.default.registerLanguage(langData);
}
}
_numbro.default.setLanguage(cellCulture);
return (0, _numbro.default)(value).format(cellFormatPattern || '0');
}
/**
* Formats the value using the intl format.
*
* @param {*} value Value to be formatted.
* @param {CellMeta} cellProperties Cell meta object.
* @returns {*} Returns the formatted value.
*/
function intlFormatter(value, cellProperties) {
const {
numericFormat,
locale
} = cellProperties;
return new Intl.NumberFormat(locale, numericFormat !== null && numericFormat !== void 0 ? numericFormat : DEFAULT_INTL_FORMAT).format(value);
}
/**
* Checks if the numericFormat object contains any numbro-specific format keys.
*
* @param {CellMeta} cellProperties Cell meta object.
* @returns {boolean} Returns true if the numericFormat object contains any numbro-specific format keys, false otherwise.
*/
function isNumbroScheme(cellProperties) {
const {
numericFormat
} = cellProperties;
return (numericFormat === null || numericFormat === void 0 ? void 0 : numericFormat.pattern) !== undefined || (numericFormat === null || numericFormat === void 0 ? void 0 : numericFormat.culture) !== undefined;
}