UNPKG

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