UNPKG

dbl-utils

Version:

Utilities for dbl, adba and others projects

93 lines (92 loc) 3.87 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = formatValue; const moment_1 = __importDefault(require("moment")); const numeral_1 = __importDefault(require("numeral")); const i18n_1 = __importStar(require("./i18n")); /** * Formats a value based on the provided configuration. * * @param value - The value to format * @param conf - Configuration options for formatting * @returns The formatted value or the original value if the format is not specified * * @example * ```ts * formatValue(1000, { format: 'currency', currency: 'USD' }); * // => "$1,000.00" in an English locale * ``` */ function formatValue(value, conf) { if (!(conf === null || conf === void 0 ? void 0 : conf.format)) return value; switch (conf.format) { case 'number-compact': case 'numbercompact': { numeral_1.default.locale((0, i18n_1.getLang)()); return (0, numeral_1.default)(value).format(conf.formatConf || (0, i18n_1.formatNumberCompact)(conf.context)); } case 'number': { return typeof value === 'boolean' ? Number(value) : value.toLocaleString((0, i18n_1.getLang)(), conf.formatConf || (0, i18n_1.formatNumber)(conf.context)); } case 'currency': { const globalConf = (conf.formatConf || (0, i18n_1.formatCurrency)(conf.context)); if (typeof globalConf === 'string') throw new Error("currency format must have formatConf as an Intl.NumberFormatOptions"); return value.toLocaleString((0, i18n_1.getLang)(), Object.assign(Object.assign({}, globalConf), { style: "currency", currency: conf.currency || globalConf.currency })); } case 'dictionary': { return (0, i18n_1.default)(value, conf.context); } case 'date': { return (0, moment_1.default)(value).format(conf.formatConf || (0, i18n_1.formatDate)(conf.context)); } case 'time': { return (0, moment_1.default)(value).format(conf.formatConf || (0, i18n_1.formatTime)(conf.context)); } case 'date-time': case 'datetime': { return (0, moment_1.default)(value).format(conf.formatConf || (0, i18n_1.formatDateTime)(conf.context)); } default: { return value; } } }