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