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