util.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.isEmpty = exports.isEqual = exports.arrayEquals = exports.looseEqual = exports.capitalize = exports.kebabCase = exports.autoprefixer = exports.isFirefox = exports.isEdge = exports.isIE = exports.coerceTruthyValueToArray = exports.arrayFind = exports.arrayFindIndex = exports.escapeRegexpString = exports.valueEquals = exports.generateId = exports.getValueByPath = undefined;
  4. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  5. exports.noop = noop;
  6. exports.hasOwn = hasOwn;
  7. exports.toObject = toObject;
  8. exports.getPropByPath = getPropByPath;
  9. exports.rafThrottle = rafThrottle;
  10. exports.objToArray = objToArray;
  11. var _vue = require('vue');
  12. var _vue2 = _interopRequireDefault(_vue);
  13. var _types = require('element-ui/lib/utils/types');
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. var hasOwnProperty = Object.prototype.hasOwnProperty;
  16. function noop() {};
  17. function hasOwn(obj, key) {
  18. return hasOwnProperty.call(obj, key);
  19. };
  20. function extend(to, _from) {
  21. for (var key in _from) {
  22. to[key] = _from[key];
  23. }
  24. return to;
  25. };
  26. function toObject(arr) {
  27. var res = {};
  28. for (var i = 0; i < arr.length; i++) {
  29. if (arr[i]) {
  30. extend(res, arr[i]);
  31. }
  32. }
  33. return res;
  34. };
  35. var getValueByPath = exports.getValueByPath = function getValueByPath(object, prop) {
  36. prop = prop || '';
  37. var paths = prop.split('.');
  38. var current = object;
  39. var result = null;
  40. for (var i = 0, j = paths.length; i < j; i++) {
  41. var path = paths[i];
  42. if (!current) break;
  43. if (i === j - 1) {
  44. result = current[path];
  45. break;
  46. }
  47. current = current[path];
  48. }
  49. return result;
  50. };
  51. function getPropByPath(obj, path, strict) {
  52. var tempObj = obj;
  53. path = path.replace(/\[(\w+)\]/g, '.$1');
  54. path = path.replace(/^\./, '');
  55. var keyArr = path.split('.');
  56. var i = 0;
  57. for (var len = keyArr.length; i < len - 1; ++i) {
  58. if (!tempObj && !strict) break;
  59. var key = keyArr[i];
  60. if (key in tempObj) {
  61. tempObj = tempObj[key];
  62. } else {
  63. if (strict) {
  64. throw new Error('please transfer a valid prop path to form item!');
  65. }
  66. break;
  67. }
  68. }
  69. return {
  70. o: tempObj,
  71. k: keyArr[i],
  72. v: tempObj ? tempObj[keyArr[i]] : null
  73. };
  74. };
  75. var generateId = exports.generateId = function generateId() {
  76. return Math.floor(Math.random() * 10000);
  77. };
  78. var valueEquals = exports.valueEquals = function valueEquals(a, b) {
  79. // see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
  80. if (a === b) return true;
  81. if (!(a instanceof Array)) return false;
  82. if (!(b instanceof Array)) return false;
  83. if (a.length !== b.length) return false;
  84. for (var i = 0; i !== a.length; ++i) {
  85. if (a[i] !== b[i]) return false;
  86. }
  87. return true;
  88. };
  89. var escapeRegexpString = exports.escapeRegexpString = function escapeRegexpString() {
  90. var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  91. return String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
  92. };
  93. // TODO: use native Array.find, Array.findIndex when IE support is dropped
  94. var arrayFindIndex = exports.arrayFindIndex = function arrayFindIndex(arr, pred) {
  95. for (var i = 0; i !== arr.length; ++i) {
  96. if (pred(arr[i])) {
  97. return i;
  98. }
  99. }
  100. return -1;
  101. };
  102. var arrayFind = exports.arrayFind = function arrayFind(arr, pred) {
  103. var idx = arrayFindIndex(arr, pred);
  104. return idx !== -1 ? arr[idx] : undefined;
  105. };
  106. // coerce truthy value to array
  107. var coerceTruthyValueToArray = exports.coerceTruthyValueToArray = function coerceTruthyValueToArray(val) {
  108. if (Array.isArray(val)) {
  109. return val;
  110. } else if (val) {
  111. return [val];
  112. } else {
  113. return [];
  114. }
  115. };
  116. var isIE = exports.isIE = function isIE() {
  117. return !_vue2.default.prototype.$isServer && !isNaN(Number(document.documentMode));
  118. };
  119. var isEdge = exports.isEdge = function isEdge() {
  120. return !_vue2.default.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1;
  121. };
  122. var isFirefox = exports.isFirefox = function isFirefox() {
  123. return !_vue2.default.prototype.$isServer && !!window.navigator.userAgent.match(/firefox/i);
  124. };
  125. var autoprefixer = exports.autoprefixer = function autoprefixer(style) {
  126. if ((typeof style === 'undefined' ? 'undefined' : _typeof(style)) !== 'object') return style;
  127. var rules = ['transform', 'transition', 'animation'];
  128. var prefixes = ['ms-', 'webkit-'];
  129. rules.forEach(function (rule) {
  130. var value = style[rule];
  131. if (rule && value) {
  132. prefixes.forEach(function (prefix) {
  133. style[prefix + rule] = value;
  134. });
  135. }
  136. });
  137. return style;
  138. };
  139. var kebabCase = exports.kebabCase = function kebabCase(str) {
  140. var hyphenateRE = /([^-])([A-Z])/g;
  141. return str.replace(hyphenateRE, '$1-$2').replace(hyphenateRE, '$1-$2').toLowerCase();
  142. };
  143. var capitalize = exports.capitalize = function capitalize(str) {
  144. if (!(0, _types.isString)(str)) return str;
  145. return str.charAt(0).toUpperCase() + str.slice(1);
  146. };
  147. var looseEqual = exports.looseEqual = function looseEqual(a, b) {
  148. var isObjectA = (0, _types.isObject)(a);
  149. var isObjectB = (0, _types.isObject)(b);
  150. if (isObjectA && isObjectB) {
  151. return JSON.stringify(a) === JSON.stringify(b);
  152. } else if (!isObjectA && !isObjectB) {
  153. return String(a) === String(b);
  154. } else {
  155. return false;
  156. }
  157. };
  158. var arrayEquals = exports.arrayEquals = function arrayEquals(arrayA, arrayB) {
  159. arrayA = arrayA || [];
  160. arrayB = arrayB || [];
  161. if (arrayA.length !== arrayB.length) {
  162. return false;
  163. }
  164. for (var i = 0; i < arrayA.length; i++) {
  165. if (!looseEqual(arrayA[i], arrayB[i])) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. };
  171. var isEqual = exports.isEqual = function isEqual(value1, value2) {
  172. if (Array.isArray(value1) && Array.isArray(value2)) {
  173. return arrayEquals(value1, value2);
  174. }
  175. return looseEqual(value1, value2);
  176. };
  177. var isEmpty = exports.isEmpty = function isEmpty(val) {
  178. // null or undefined
  179. if (val == null) return true;
  180. if (typeof val === 'boolean') return false;
  181. if (typeof val === 'number') return !val;
  182. if (val instanceof Error) return val.message === '';
  183. switch (Object.prototype.toString.call(val)) {
  184. // String or Array
  185. case '[object String]':
  186. case '[object Array]':
  187. return !val.length;
  188. // Map or Set or File
  189. case '[object File]':
  190. case '[object Map]':
  191. case '[object Set]':
  192. {
  193. return !val.size;
  194. }
  195. // Plain Object
  196. case '[object Object]':
  197. {
  198. return !Object.keys(val).length;
  199. }
  200. }
  201. return false;
  202. };
  203. function rafThrottle(fn) {
  204. var locked = false;
  205. return function () {
  206. var _this = this;
  207. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  208. args[_key] = arguments[_key];
  209. }
  210. if (locked) return;
  211. locked = true;
  212. window.requestAnimationFrame(function (_) {
  213. fn.apply(_this, args);
  214. locked = false;
  215. });
  216. };
  217. }
  218. function objToArray(obj) {
  219. if (Array.isArray(obj)) {
  220. return obj;
  221. }
  222. return isEmpty(obj) ? [] : [obj];
  223. }