UNPKG

@fle-ui/plus-goods-detail-drawer

Version:

@fle-ui/plus-goods-detail-drawer

140 lines (139 loc) 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.throttle = exports.safeGet = exports.isValidNumber = exports.generateId = exports.formatPrice = exports.formatFileSize = exports.formatDate = exports.deepClone = exports.debounce = void 0; var _dayjs = _interopRequireDefault(require("dayjs")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /** * 格式化价格显示 * @param price 价格(分) * @param unit 单位 * @returns 格式化后的价格字符串 */ var formatPrice = exports.formatPrice = function formatPrice(price) { var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '元'; if (typeof price !== 'number' || isNaN(price)) { return "0.00".concat(unit); } return "".concat((price / 100).toFixed(2)).concat(unit); }; /** * 格式化日期显示 * @param date 日期字符串或Date对象 * @param format 格式化模板 * @returns 格式化后的日期字符串 */ var formatDate = exports.formatDate = function formatDate(date) { var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'YYYY-MM-DD'; if (!date) return '-'; return (0, _dayjs.default)(date).format(format); }; /** * 验证是否为有效数字 * @param value 待验证的值 * @returns 是否为有效数字 */ var isValidNumber = exports.isValidNumber = function isValidNumber(value) { return typeof value === 'number' && !isNaN(value) && isFinite(value); }; /** * 安全获取对象属性 * @param obj 对象 * @param path 属性路径 * @param defaultValue 默认值 * @returns 属性值或默认值 */ var safeGet = exports.safeGet = function safeGet(obj, path) { var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; try { var _path$split$reduce; return (_path$split$reduce = path.split('.').reduce(function (current, key) { return current === null || current === void 0 ? void 0 : current[key]; }, obj)) !== null && _path$split$reduce !== void 0 ? _path$split$reduce : defaultValue; } catch (_unused) { return defaultValue; } }; /** * 防抖函数 * @param func 要防抖的函数 * @param wait 等待时间 * @returns 防抖后的函数 */ var debounce = exports.debounce = function debounce(func, wait) { var timeout; return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } clearTimeout(timeout); timeout = setTimeout(function () { return func.apply(void 0, args); }, wait); }; }; /** * 节流函数 * @param func 要节流的函数 * @param wait 等待时间 * @returns 节流后的函数 */ var throttle = exports.throttle = function throttle(func, wait) { var lastCall = 0; return function () { var now = Date.now(); if (now - lastCall >= wait) { lastCall = now; func.apply(void 0, arguments); } }; }; /** * 格式化文件大小 * @param bytes 字节数 * @returns 格式化后的文件大小 */ var formatFileSize = exports.formatFileSize = function formatFileSize(bytes) { if (bytes === 0) return '0 B'; var k = 1024; var sizes = ['B', 'KB', 'MB', 'GB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; /** * 生成唯一ID * @returns 唯一ID字符串 */ var generateId = exports.generateId = function generateId() { return Math.random().toString(36).substr(2, 9); }; /** * 深度克隆对象 * @param obj 要克隆的对象 * @returns 克隆后的对象 */ var deepClone = exports.deepClone = function deepClone(obj) { if (obj === null || _typeof(obj) !== 'object') { return obj; } if (obj instanceof Date) { return new Date(obj.getTime()); } if (obj instanceof Array) { return obj.map(function (item) { return deepClone(item); }); } if (_typeof(obj) === 'object') { var clonedObj = {}; for (var key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = deepClone(obj[key]); } } return clonedObj; } return obj; };