UNPKG

10.3 kBJavaScriptView Raw
1var _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; };
2
3/* -------------------
4判断是否是空对象
5------------------- */
6Object.isEmptyObject = function (obj) {
7 if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') return false;
8 for (var n in obj) {
9 if (obj.hasOwnProperty(n)) {
10 return false;
11 }
12 }
13 return true;
14};
15/* -------------------
16判断是否是纯对象
17------------------- */
18Object.isPlainObject = function (obj) {
19 var proto;
20 var Ctor;
21 if (!obj || toString.call(obj) !== '[object Object]') {
22 return false;
23 }
24
25 proto = Object.getPrototypeOf(obj);
26
27 // 没有原型的对象(例如`Object.create(null)`),则直接返回true
28 if (!proto) {
29 return true;
30 }
31
32 // 如果原型的对象是由全局Object函数构造的,则它们是纯对象
33 Ctor = {}.hasOwnProperty.call(proto, 'constructor') && proto.constructor;
34 return typeof Ctor === 'function' && {}.hasOwnProperty.toString.call(Ctor) === {}.hasOwnProperty.toString.call(Object);
35};
36
37/* -------------------
38 克隆对象字面量、Array
39 ------------------- */
40Object.clone = function (obj) {
41 var copy;
42 if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') {
43 if (obj === null) {
44 copy = null;
45 } else {
46 if (obj instanceof Array) {
47 copy = [];
48 for (var i = 0; i < obj.length; i++) {
49 copy.push(this.clone(obj[i]));
50 }
51 } else {
52 copy = {};
53 for (var j in obj) {
54 copy[j] = this.clone(obj[j]);
55 }
56 }
57 }
58 } else {
59 copy = obj;
60 }
61 return copy;
62};
63
64/*
65 * 用于get请求,将Json参数转为params字符串
66 * 参数: obj:请求参数; splitter:dot点 | bracket中括号, isNotEnCode:是否不使用encodeURIComponent编码
67 * 返回: splitter为dot时,返回obj.key=value&obj.key=value; bracket时,返回obj[key]=value&obj[key]=value
68 * */
69Object.params = function (obj, splitter, isNotEnCode) {
70 if (!Object.isPlainObject(obj)) return obj;
71 if (obj instanceof Object && obj.length > 0) return '';
72 // 把{jsonReq:[{0:'0', 1:'1'}]}转成jsonReq=[{0:'0', 1:'1'}]的方式
73 if (splitter === 'dot') {
74 var arr = [];
75 for (var n in obj) {
76 arr.push(n + '=' + JSON.stringify(obj[n]));
77 }
78 return arr.join('&');
79 }
80 // 把{jsonReq:[{0:'0', 1:'1'}]}转成jsonReq.0=0&jsonReq.1=1的方式(支持嵌套Json)
81 var result = '';
82 function buildParams(obj, prevKey) {
83 for (var key in obj) {
84 if (obj[key] instanceof Object) {
85 var prefix = prevKey ? prevKey + '.' + key : key;
86 buildParams(obj[key], prefix);
87 } else {
88 if (prevKey) {
89 // result += '&' + prevKey + '.' + key + '=' + obj[key]
90 if (splitter !== 'bracket') result += '&' + prevKey + '.' + key + '=' + (isNotEnCode ? obj[key] : encodeURIComponent(obj[key]));
91 if (splitter === 'bracket') result += '&' + prevKey + '[' + key + ']=' + (isNotEnCode ? obj[key] : encodeURIComponent(obj[key]));
92 } else {
93 // result += '&' + key + '=' + obj[key]
94 result += '&' + key + '=' + (isNotEnCode ? obj[key] : encodeURIComponent(obj[key]));
95 }
96 }
97 }
98 return result;
99 }
100 buildParams(obj);
101 // 删除result第一个字符
102 if (result) {
103 result = result.slice(1);
104 }
105 return result;
106};
107/* Object.params = function (obj, isNotEnCode) {
108 var result = ''
109 var item
110 for (item in obj) {
111 if (isNotEnCode) result += '&' + item + '=' + obj[item]
112 else result += '&' + item + '=' + encodeURIComponent(obj[item]) // 使用decodeURIComponent解码
113 }
114 if (result) {
115 result = result.slice(1)
116 }
117 return result
118} */
119
120/* -------------------
121获得类型, boolean | number | string | function | array | date | regexp | object | json
122------------------- */
123Object.type = function (obj) {
124 if (!obj) {
125 return obj + '';
126 }
127 var type = Object.prototype.toString.call(obj).replace('[', '').replace(']', '').split(' ')[1].toLowerCase();
128 if (type === 'object') {
129 var objStr = JSON.stringify(obj);
130 try {
131 JSON.parse(objStr);
132 return 'json';
133 } catch (e) {}
134 }
135 return type;
136};
137
138/* -------------------
139字符类型
140------------------- */
141Object.charType = function (char) {
142 if (char >= 48 && char <= 57) return 'number'; // 数字
143 if (char >= 65 && char <= 90) return 'capitalize'; // 大写
144 if (char >= 97 && char <= 122) return 'lowercase'; // 小写
145 else return 'other';
146};
147Object.passwordLvl = function (value) {
148 var mode = {};
149 for (var i = 0; i < value.length; i++) {
150 mode[Object.charType(value.charCodeAt(i))] = '';
151 }
152 var lvl = 0;
153 /* eslint-disable */
154 for (m in mode) {
155 lvl++;
156 }
157 /* eslint-enable */
158 if (value.length > 0 && value.length < 6) return 1;
159 return lvl;
160};
161/* -------------------
162 是否是方法
163 ------------------- */
164Object.isFunction = function (obj) {
165 return Object.type(obj) === 'function';
166};
167
168/* -------------------
169 是否是窗口
170 ------------------- */
171Object.isWindow = function (obj) {
172 return obj != null && obj === obj.window;
173};
174
175/* -------------------
176 继承合并
177 ------------------- */
178Object.extend = function () {
179 var options;
180 var name;
181 var src;
182 var copy;
183 var copyIsArray;
184 var clone;
185 var target = arguments[0] || {};
186 var i = 1;
187 var length = arguments.length;
188 var deep = false;
189
190 // Handle a deep copy situation
191 if (typeof target === 'boolean') {
192 deep = target;
193
194 // Skip the boolean and the target
195 target = arguments[i] || {};
196 i++;
197 }
198
199 // Handle case when target is a string or something (possible in deep copy)
200 if ((typeof target === 'undefined' ? 'undefined' : _typeof(target)) !== 'object' && !Object.isFunction(target)) {
201 target = {};
202 }
203
204 // Extend jQuery itself if only one argument is passed
205 if (i === length) {
206 target = this;
207 i--;
208 }
209
210 for (; i < length; i++) {
211 // 只处理非空/未定义的值
212 options = arguments[i];
213 if (options != null) {
214 // 扩展基础对象
215 for (name in options) {
216 src = target[name];
217 copy = options[name];
218
219 // 防止永无止境的循环
220 if (target === copy) {
221 continue;
222 }
223
224 // 如果我们合并了普通的对象或数组,就会重新出现
225 if (deep && copy && (Object.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
226 // eslint-disable-line
227 if (copyIsArray) {
228 copyIsArray = false;
229 clone = src && Array.isArray(src) ? src : [];
230 } else {
231 clone = src && Object.isPlainObject(src) ? src : {};
232 }
233
234 // 永远不要修改原始对象,而是克隆它们
235 target[name] = Object.extend(deep, clone, copy);
236
237 // 不要带入未定义的值
238 } else if (copy !== undefined) {
239 target[name] = copy;
240 }
241 }
242 }
243 }
244
245 // 返回修改后的对象
246 return target;
247};
248/* -------------------
249 生成唯一标识
250 ------------------- */
251Object.generateGUID = function () {
252 var d = new Date().getTime();
253 var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
254 var r = (d + Math.random() * 16) % 16 | 0;
255 d = Math.floor(d / 16);
256 return (c === 'x' ? r : r & 0x3 | 0x8).toString(16); // eslint-disable-line
257 });
258 return uuid;
259};
260/* -------------------
261 比较两个对象是否相同
262 ------------------- */
263Object.equals = function (object1, object2) {
264 // 用window.Object.prototype.equals在react或者vue中会默认为组件绑定此方法而报错,故不用
265 // 第一个循环,只检查类型
266 for (var propName in object1) {
267 // 检查继承的方法和属性 - 比如.equals本身
268 // 如果返回值不同,则返回false
269 if (object1.hasOwnProperty(propName) !== object2.hasOwnProperty(propName)) {
270 return false;
271 }
272 // 检查实例类型
273 else if (_typeof(object1[propName]) !== _typeof(object2[propName])) {
274 // 不同的类型=>不等于
275 return false;
276 }
277 }
278 // 现在更深入地检查使用其他对象的属性名称
279 for (propName in object2) {
280 // 无论如何必须检查实例,可能有一个只存在于object2中的属性
281 if (object1.hasOwnProperty(propName) !== object2.hasOwnProperty(propName)) {
282 return false;
283 } else if (_typeof(object1[propName]) !== _typeof(object2[propName])) {
284 return false;
285 }
286 // 如果该属性是继承的,则不要再检查(如果两个对象都继承它,则必须相等)
287 if (!object1.hasOwnProperty(propName)) continue;
288
289 // 现在详细检查和递归
290
291 // 这将脚本返回到数组比较
292 /** 需要Array.equals **/
293 if (object1[propName] instanceof Array && object2[propName] instanceof Array) {
294 // 递归到嵌套数组中
295 if (!Object.equals(object1[propName], object2[propName])) return false;
296 } else if (object1[propName] instanceof Object && object2[propName] instanceof Object) {
297 // 递归到另一个对象中
298 // console.log('递归比较 ', object1[propName],'和',object2[propName], ' 都命名 \''+propName+'\'')
299 if (!Object.equals(object1[propName], object2[propName])) return false;
300 }
301 // 字符串和数字的正常值比较
302 else if (object1[propName] !== object2[propName]) {
303 return false;
304 }
305 }
306 // 如果一切顺利,返回true
307 return true;
308};
309
310/* -------------------
311SeedsUI组件: 获取参数
312@params e => 事件对象
313@params parameters => 其它参数, '$event'字符串将用e代替后返回
314@return 若无parameters,将返回e; 若有parameters,将parameters中的'$event'替换成e后返回
315------------------- */
316Object.getArgs = function (e, parameters) {
317 var args = parameters ? Object.clone(parameters) : parameters;
318 if (args !== undefined) {
319 if (typeof args === 'string' && args === '$event') {
320 args = e;
321 } else if (Array.isArray(args) && args.indexOf('$event') > -1) {
322 args[args.indexOf('$event')] = e;
323 }
324 } else {
325 args = e;
326 }
327 return args;
328};
\No newline at end of file