function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
 * 转化 rem 单位
 * @param {string} content 输入内容
 * @param {number} factor 转化比例，默认 100
 * @param {string} unit 转化单位，默认 rpx
 * @returns {string} 转化后的结果
 *
 * @example
 * ```ts
 * transFormRem('1.22rem')
 * // 122rpx
 *
 * transFormRem('1.22rem', 50, 'px')
 * // 61px
 *
 * transFormRem('.21rem', 50, 'px')
 * // 10.50px
 * ```
 */
function transFormRem(content) {
  var factor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
  var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'rpx';
  if (content == null) {
    return content;
  }
  var pattern = new RegExp('([0-9.]*[0-9]+)([\\s]*)(rem)', 'g');
  var match = undefined;
  var records = [];
  // eslint-disable-next-line no-cond-assign
  while (match = pattern.exec(content)) {
    var keyword = match[0];
    var number = parseFloat(match[1]);
    records.push({
      index: match.index,
      length: keyword.length,
      keyword: keyword,
      number: number
    });
  }
  if (records.length > 0) {
    var buffer = "".concat(content);
    var _iterator = _createForOfIteratorHelper(records),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var record = _step.value;
        var _number = (record.number * factor).toFixed(2);
        if (+_number == parseInt(_number, 10)) {
          // 去掉无效的小数点，比如：28.00
          _number = "".concat(parseInt(_number, 10));
        }
        buffer = buffer.replace(record.keyword, "".concat(_number).concat(unit));
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    return buffer;
  }
  return content;
}

export { transFormRem };
