1 | 'use strict';
|
2 |
|
3 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
4 |
|
5 | var isBigNumber = require('./bignumber/isBigNumber');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | exports.clone = function clone(x) {
|
20 | var type = _typeof(x);
|
21 |
|
22 |
|
23 | if (type === 'number' || type === 'string' || type === 'boolean' || x === null || x === undefined) {
|
24 | return x;
|
25 | }
|
26 |
|
27 |
|
28 | if (typeof x.clone === 'function') {
|
29 | return x.clone();
|
30 | }
|
31 |
|
32 |
|
33 | if (Array.isArray(x)) {
|
34 | return x.map(function (value) {
|
35 | return clone(value);
|
36 | });
|
37 | }
|
38 |
|
39 | if (x instanceof Date) return new Date(x.valueOf());
|
40 | if (isBigNumber(x)) return x;
|
41 |
|
42 | if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x);
|
43 |
|
44 |
|
45 | return exports.map(x, clone);
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | exports.map = function (object, callback) {
|
56 | var clone = {};
|
57 |
|
58 | for (var key in object) {
|
59 | if (exports.hasOwnProperty(object, key)) {
|
60 | clone[key] = callback(object[key]);
|
61 | }
|
62 | }
|
63 |
|
64 | return clone;
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | exports.extend = function (a, b) {
|
75 | for (var prop in b) {
|
76 | if (exports.hasOwnProperty(b, prop)) {
|
77 | a[prop] = b[prop];
|
78 | }
|
79 | }
|
80 |
|
81 | return a;
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | exports.deepExtend = function deepExtend(a, b) {
|
92 |
|
93 | if (Array.isArray(b)) {
|
94 | throw new TypeError('Arrays are not supported by deepExtend');
|
95 | }
|
96 |
|
97 | for (var prop in b) {
|
98 | if (exports.hasOwnProperty(b, prop)) {
|
99 | if (b[prop] && b[prop].constructor === Object) {
|
100 | if (a[prop] === undefined) {
|
101 | a[prop] = {};
|
102 | }
|
103 |
|
104 | if (a[prop].constructor === Object) {
|
105 | deepExtend(a[prop], b[prop]);
|
106 | } else {
|
107 | a[prop] = b[prop];
|
108 | }
|
109 | } else if (Array.isArray(b[prop])) {
|
110 | throw new TypeError('Arrays are not supported by deepExtend');
|
111 | } else {
|
112 | a[prop] = b[prop];
|
113 | }
|
114 | }
|
115 | }
|
116 |
|
117 | return a;
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | exports.deepEqual = function deepEqual(a, b) {
|
128 | var prop, i, len;
|
129 |
|
130 | if (Array.isArray(a)) {
|
131 | if (!Array.isArray(b)) {
|
132 | return false;
|
133 | }
|
134 |
|
135 | if (a.length !== b.length) {
|
136 | return false;
|
137 | }
|
138 |
|
139 | for (i = 0, len = a.length; i < len; i++) {
|
140 | if (!exports.deepEqual(a[i], b[i])) {
|
141 | return false;
|
142 | }
|
143 | }
|
144 |
|
145 | return true;
|
146 | } else if (a instanceof Object) {
|
147 | if (Array.isArray(b) || !(b instanceof Object)) {
|
148 | return false;
|
149 | }
|
150 |
|
151 | for (prop in a) {
|
152 |
|
153 | if (!exports.deepEqual(a[prop], b[prop])) {
|
154 | return false;
|
155 | }
|
156 | }
|
157 |
|
158 | for (prop in b) {
|
159 |
|
160 | if (!exports.deepEqual(a[prop], b[prop])) {
|
161 | return false;
|
162 | }
|
163 | }
|
164 |
|
165 | return true;
|
166 | } else {
|
167 | return a === b;
|
168 | }
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | exports.canDefineProperty = function () {
|
177 |
|
178 | try {
|
179 | if (Object.defineProperty) {
|
180 | Object.defineProperty({}, 'x', {
|
181 | get: function get() {}
|
182 | });
|
183 | return true;
|
184 | }
|
185 | } catch (e) {}
|
186 |
|
187 | return false;
|
188 | };
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | exports.lazy = function (object, prop, fn) {
|
202 | if (exports.canDefineProperty()) {
|
203 | var _uninitialized = true;
|
204 |
|
205 | var _value;
|
206 |
|
207 | Object.defineProperty(object, prop, {
|
208 | get: function get() {
|
209 | if (_uninitialized) {
|
210 | _value = fn();
|
211 | _uninitialized = false;
|
212 | }
|
213 |
|
214 | return _value;
|
215 | },
|
216 | set: function set(value) {
|
217 | _value = value;
|
218 | _uninitialized = false;
|
219 | },
|
220 | configurable: true,
|
221 | enumerable: true
|
222 | });
|
223 | } else {
|
224 |
|
225 | object[prop] = fn();
|
226 | }
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | exports.traverse = function (object, path) {
|
238 | var obj = object;
|
239 |
|
240 | if (path) {
|
241 | var names = path.split('.');
|
242 |
|
243 | for (var i = 0; i < names.length; i++) {
|
244 | var name = names[i];
|
245 |
|
246 | if (!(name in obj)) {
|
247 | obj[name] = {};
|
248 | }
|
249 |
|
250 | obj = obj[name];
|
251 | }
|
252 | }
|
253 |
|
254 | return obj;
|
255 | };
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | exports.hasOwnProperty = function (object, property) {
|
264 | return object && Object.hasOwnProperty.call(object, property);
|
265 | };
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | exports.isFactory = function (object) {
|
281 | return object && typeof object.factory === 'function';
|
282 | }; |
\ | No newline at end of file |