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