UNPKG

@procraft/widget-order

Version:

Order widget for landing

104 lines 3.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDateOptionalTime = exports.formatTimeHoursMinutes = exports.hasTime = exports.formatDate = exports.isNowYear = exports.getCurrencySuffix = exports.getCurrencyPrefix = exports.roundStd = exports.roundByStep = void 0; const moment_1 = __importDefault(require("moment")); const roundByStep = (v, s = 1.0) => { const inv = 1.0 / s; return Math.round(v * inv) / inv; }; exports.roundByStep = roundByStep; const roundStd = (num, decimalPlaces) => { const p = Math.pow(10, decimalPlaces); const e = Number.EPSILON * num * p; return Math.round(num * p + e) / p; }; exports.roundStd = roundStd; const getCurrencyPrefix = (currency) => { switch (currency) { case 'USD': return '$'; default: return ''; } }; exports.getCurrencyPrefix = getCurrencyPrefix; const getCurrencySuffix = (currency) => { switch (currency) { case 'RUB': return ' ₽'; case 'USD': return ''; case 'KZT': return ' ₸'; case 'UAH': return ' ₴'; default: return `\u202F${currency}`; } }; exports.getCurrencySuffix = getCurrencySuffix; function isNowYear(dateMoment) { if (dateMoment) { dateMoment = (0, moment_1.default)(dateMoment); return (0, moment_1.default)().year() === dateMoment.year(); } else { return false; } } exports.isNowYear = isNowYear; /** * Returns date in format like "September 11" or "11 Сентября" */ function formatDate(dateMoment) { if (dateMoment) { dateMoment = (0, moment_1.default)(dateMoment); if (!isNowYear(dateMoment)) { return dateMoment.format('D MMMM YYYY'); } else { return dateMoment.format('D MMMM'); } } else { return ''; } } exports.formatDate = formatDate; function hasTime(dateMoment, optionalTimeReplacement = '00:00') { if (optionalTimeReplacement == '23:59') { return !(dateMoment.hours() === 23 && dateMoment.minutes() === 59); } else { return dateMoment.hours() * 60 + dateMoment.minutes() > 0; } } exports.hasTime = hasTime; function formatTimeHoursMinutes(dateMoment) { if (dateMoment) { return (0, moment_1.default)(dateMoment).format('HH:mm'); } else { return ''; } } exports.formatTimeHoursMinutes = formatTimeHoursMinutes; function formatDateOptionalTime(dateMoment, separator = ' в ', optionalTimeReplacement = '00:00') { if (dateMoment) { const timestamp = (0, moment_1.default)(dateMoment); if (hasTime(timestamp, optionalTimeReplacement)) { return `${formatDate(timestamp)}${separator}${formatTimeHoursMinutes(timestamp)}`; } else { return formatDate(timestamp); } } else { return ''; } } exports.formatDateOptionalTime = formatDateOptionalTime; //# sourceMappingURL=utils.js.map