all files / addon/utils/ util.js

26.44% Statements 23/87
25.93% Branches 14/54
7.69% Functions 1/13
28.21% Lines 22/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                                                                                                                                                                                                                                           
/* eslint-disable  */
const hasOwnProperty = Object.prototype.hasOwnProperty;
 
export function noop() {}
 
export function hasOwn(obj, key) {
  return hasOwnProperty.call(obj, key);
}
 
 
function extend(to, _from) {
  for (let key in _from) {
    to[key] = _from[key];
  }
  return to;
}
 
export function toObject(arr) {
  var res = {};
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) {
      extend(res, arr[i]);
    }
  }
  return res;
}
 
export const getValueByPath = function(object, prop) {
  prop = prop || '';
  const paths = prop.split('.');
  let current = object;
  let result = null;
  for (let i = 0, j = paths.length; i < j; i++) {
    const path = paths[i];
    if (!current) break;
 
    if (i === j - 1) {
      result = current[path];
      break;
    }
    current = current[path];
  }
  return result;
};
 
export function getPropByPath(obj, path, strict) {
  let tempObj = obj;
  path = path.replace(/\[(\w+)\]/g, '.$1');
  path = path.replace(/^\./, '');
 
  let keyArr = path.split('.');
  let i = 0;
  for (let len = keyArr.length; i < len - 1; ++i) {
    if (!tempObj && !strict) break;
    let key = keyArr[i];
    if (key in tempObj) {
      tempObj = tempObj[key];
    } else {
      if (strict) {
        throw new Error('please transfer a valid prop path to form item!');
      }
      break;
    }
  }
  return {
    o: tempObj,
    k: keyArr[i],
    v: tempObj ? tempObj[keyArr[i]] : null
  };
}
 
export const generateId = function() {
  return Math.floor(Math.random() * 10000);
};
 
export const valueEquals = (a, b) => {
  // see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
  if (a === b) return true;
  if (!(a instanceof Array)) return false;
  if (!(b instanceof Array)) return false;
  if (a.length !== b.length) return false;
  for (let i = 0; i !== a.length; ++i) {
    if (a[i] !== b[i]) return false;
  }
  return true;
};
 
export const escapeRegexpString = (value = '') => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
 
// TODO: use native Array.find, Array.findIndex when IE support is dropped
export const arrayFindIndex = function(arr, pred) {
  for (let i = 0; i !== arr.length; ++i) {
    if (pred(arr[i])) {
      return i;
    }
  }
  return -1;
};
 
export const arrayFind = function(arr, pred) {
  const idx = arrayFindIndex(arr, pred);
  return idx !== -1 ? arr[idx] : undefined;
};
 
// coerce truthy value to array
export const coerceTruthyValueToArray = function(val) {
  if (Array.isArray(val)) {
    return val;
  } else if (val) {
    return [val];
  } else {
    return [];
  }
};
 
 
var isBrowserOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
 
// Firefox 1.0+
var isBrowserFirefox = typeof InstallTrigger !== 'undefined';
 
// Safari 3.0+ "[object HTMLElementConstructor]"
var isBrowserSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
 
// Internet Explorer 6-11
var isBrowserIE = /*@cc_on!@*/false || !!document.documentMode;
 
// Edge 20+
var isBrowserEdge = !isBrowserIE && !!window.StyleMedia;
 
// Chrome 1+
var isBrowserChrome = !!window.chrome && !!window.chrome.webstore;
 
// Blink engine detection
var isBrowserBlink = (isBrowserChrome || isBrowserOpera) && !!window.CSS;
 
 
export const isIE = function() {
  return isBrowserIE;
  // return !Vue.prototype.$isServer && !isNaN(Number(document.documentMode));
};
 
export const isEdge = function() {
  return isBrowserEdge;
  // return !Vue.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1;
};