1 |
|
2 | var common;
|
3 |
|
4 | module.exports = common = {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 | isBareObject: function(o) {
|
11 | if ((o != null) && o.constructor === Object) {
|
12 | return true;
|
13 | }
|
14 | return false;
|
15 | },
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | typeOf: function(item) {
|
23 | var _ref;
|
24 | if (item === null) {
|
25 | return 'null';
|
26 | }
|
27 | if (typeof item !== 'object') {
|
28 | return typeof item;
|
29 | }
|
30 | if (Array.isArray(item)) {
|
31 | return 'array';
|
32 | }
|
33 | if (item.nodeName) {
|
34 | if (item.nodeType === 1) {
|
35 | return 'element';
|
36 | }
|
37 | if (item.nodeType === 3) {
|
38 | return (_ref = /\S/.test(item.nodeValue)) != null ? _ref : {
|
39 | 'textnode': 'whitespace'
|
40 | };
|
41 | }
|
42 | } else if (typeof item.length === 'number') {
|
43 | if (item.callee) {
|
44 | return 'arguments';
|
45 | }
|
46 | }
|
47 | return typeof item;
|
48 | },
|
49 | clone: function(item, includePrototype) {
|
50 | if (includePrototype == null) {
|
51 | includePrototype = false;
|
52 | }
|
53 | switch (common.typeOf(item)) {
|
54 | case 'array':
|
55 | return common._cloneArray(item, includePrototype);
|
56 | case 'object':
|
57 | return common._cloneObject(item, includePrototype);
|
58 | default:
|
59 | return item;
|
60 | }
|
61 | },
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | _cloneObject: function(o, includePrototype) {
|
68 | var clone, key;
|
69 | if (includePrototype == null) {
|
70 | includePrototype = false;
|
71 | }
|
72 | if (common.isBareObject(o)) {
|
73 | clone = {};
|
74 | for (key in o) {
|
75 | clone[key] = common.clone(o[key], includePrototype);
|
76 | }
|
77 | return clone;
|
78 | } else {
|
79 | if (!includePrototype) {
|
80 | return o;
|
81 | }
|
82 | if (o instanceof Function) {
|
83 | return o;
|
84 | }
|
85 | clone = Object.create(o.constructor.prototype);
|
86 | for (key in o) {
|
87 | if (o.hasOwnProperty(key)) {
|
88 | clone[key] = common.clone(o[key], includePrototype);
|
89 | }
|
90 | }
|
91 | return clone;
|
92 | }
|
93 | },
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | _cloneArray: function(a, includePrototype) {
|
100 | var clone, i;
|
101 | if (includePrototype == null) {
|
102 | includePrototype = false;
|
103 | }
|
104 | i = a.length;
|
105 | clone = new Array(i);
|
106 | while (i--) {
|
107 | clone[i] = common.clone(a[i], includePrototype);
|
108 | }
|
109 | return clone;
|
110 | }
|
111 | };
|