{"version":3,"file":"util.mjs","sources":["../../../../packages/components/common/util.js"],"sourcesContent":["/**\n * 数值单位格式化\n * @param {string/number} val\n * @param {string} [unit = rpx | px]\n */\nexport function addUnit(val, unit = 'rpx') {\n  if (!Number(val)) return val;\n  return (unit === 'rpx' ? (val * 2) : val) + unit;\n}\n\n/**\n * 获取元素节点信息\n * @param {string} 实例 this\n * @param {string} 选择器\n */\nexport function getRect(context, selector) {\n  return new Promise(\n    (resolve) => {\n      wx.createSelectorQuery()\n        .in(context)\n        .select(selector)\n        .boundingClientRect()\n        .exec((rect = []) => resolve(rect[0]));\n    }\n  );\n}\n\n/**\n * 缓动动画\n */\nexport function requestAnimationFrame() {\n  return new Promise((resolve) => {\n    const systemInfo = uni.getSystemInfoSync();\n    if (systemInfo.platform === 'devtools') {\n      return setTimeout(function () {\n        resolve();\n      }, 1000 / 30);\n    }\n    return wx\n      .createSelectorQuery()\n      .selectdivport()\n      .boundingClientRect()\n      .exec(function () {\n        resolve();\n      });\n  });\n}\n\n/**\n * 日期格式化\n * @param {string | number} value 时间 / 时间戳\n * @param {string} [formatter = 'YYYY-MM-DD HH:mm:ss'] 时间格式\n */\nexport const formatDate = (value, formatter = 'YYYY-MM-DD HH:mm:ss') => {\n  const dateReg = /^(?<year>\\d{4})?([/-](?<month>\\d{1,2}))?([/-](?<day>\\d{1,2}))?\\s?(?<hour>\\d{1,2})?(:(?<minute>\\d{1,2}))?(:(?<second>\\d{1,2}))?$/;\n  let date = new Date();\n  let groups = {};\n  if (!value) {\n    date = new Date();\n  } else if (typeof value === 'number') {\n    date = new Date(value);\n  } else if (typeof value === 'string' && dateReg.test(value)) {\n    const matched = value.match(dateReg);\n    groups = matched.groups;\n  } else if (Object.prototype.toString.call(value) === '[object Date]') {\n    date = value;\n  } else {\n    console.warn('[info] Invalid Date', value);\n    return value;\n  }\n\n  const Y = groups.year || date.getFullYear() + '';\n  const M = groups.month || date.getMonth() + 1;\n  const D = groups.day || date.getDate();\n  const H = groups.hour || date.getHours();\n  const m = groups.minute || date.getMinutes();\n  const s = groups.second || date.getSeconds();\n\n  return formatter.replace(/YYYY|yyyy/g, Y)\n    .replace(/YY|yy/g, Y.substring(2))\n    .replace(/MM/g, String(M).padStart(2, '0'))\n    .replace(/DD/g, String(D).padStart(2, '0'))\n    .replace(/HH|hh/g, String(H).padStart(2, '0'))\n    .replace(/mm/g, String(m).padStart(2, '0'))\n    .replace(/ss/g, String(s).padStart(2, '0'));\n};\n\n/**\n * merge same style value by order\n */\nexport function mergeStyle(source, target) {\n  if (!source || !target) return source ?? target ?? '';\n  const styleReg = /([\\w-]+):\\s*([^;]+)/ig;\n  const sourceStyleObj = {};\n  const targetStyleObj = {};\n  source.replace(styleReg, function (_, key, val) {\n    sourceStyleObj[key] = val;\n  });\n  target.replace(styleReg, function (_, key, val) {\n    targetStyleObj[key] = val;\n  });\n  const styleObj = Object.assign(sourceStyleObj, targetStyleObj);\n  let style = '';\n  for (const [key, val] of Object.entries(styleObj)) {\n    style += `${style ? ' ' : ''}${key}: ${val.trim()};`;\n  }\n  return style;\n}\n\n/**\n * 版本号比较\n * @param {string} v1\n * @param {string} v2\n */\nexport function compareVersion(v1, v2) {\n  v1 = v1.split('.');\n  v2 = v2.split('.');\n  const len = Math.max(v1.length, v2.length);\n\n  while (v1.length < len) {\n    v1.push('0');\n  }\n  while (v2.length < len) {\n    v2.push('0');\n  }\n\n  for (let i = 0; i < len; i++) {\n    const num1 = parseInt(v1[i]);\n    const num2 = parseInt(v2[i]);\n\n    if (num1 > num2) {\n      return 1;\n    } else if (num1 < num2) {\n      return -1;\n    }\n  }\n\n  return 0;\n}\n"],"names":["addUnit","val","unit"],"mappings":"AAKO,SAASA,EAAQC,GAAKC,IAAO,OAAO;AACzC,SAAK,OAAOD,CAAG,KACPC,MAAS,QAASD,IAAM,IAAKA,KAAOC,IADnBD;AAE3B;"}