@@ -31,13 +31,6 @@ const Utils = {
3131
3232 isBrowser : ( new Function ( 'try {return this===window;}catch(e){ return false;}' ) ) ( ) ,
3333
34- emptyFn : function ( ) {
35- } ,
36-
37- getNow ( ) {
38- return new Date ( ) . getTime ( )
39- } ,
40-
4134 castArray ( value ) {
4235 if ( Utils . isArray ( value ) ) {
4336 return value
@@ -46,18 +39,6 @@ const Utils = {
4639 return [ value ]
4740 } ,
4841
49- addEvent ( evnt , elem , func ) {
50- if ( elem . addEventListener ) {
51- elem . addEventListener ( evnt , func , false )
52- }
53- else if ( elem . attachEvent ) {
54- elem . attachEvent ( 'on' + evnt , func )
55- }
56- else {
57- elem [ evnt ] = func
58- }
59- } ,
60-
6142 isEmpty ( obj ) {
6243 if ( obj === null || obj === undefined ) {
6344 return true
@@ -76,16 +57,6 @@ const Utils = {
7657 return true
7758 } ,
7859
79- removeEvent ( event , elem ) {
80- if ( elem . removeEventListener ) {
81- elem . removeEventListener ( event , null , false )
82- } else if ( elem . detachEvent ) {
83- elem . detachEvent ( 'on' + event , null )
84- }
85-
86- elem [ event ] = null
87- } ,
88-
8960 stringToBiteArray ( str ) {
9061 const data = new ArrayBuffer ( str . length )
9162 const ui8a = new Uint8Array ( data , 0 )
@@ -97,20 +68,6 @@ const Utils = {
9768 return ui8a
9869 } ,
9970
100- onHttpRequestErrorHandler ( xhr , errorHandler , responseParser ) {
101- return function ( ) {
102- let errorMessage = 'Unable to connect to the network'
103-
104- //this part is needed for those implementations of XMLHttpRequest
105- //which call the onerror handler on an any bad request
106- if ( xhr . status >= 400 ) {
107- errorMessage = responseParser ( xhr )
108- }
109-
110- errorHandler ( errorMessage )
111- }
112- } ,
113-
11471 toQueryParams ( params ) {
11572 params = params || { }
11673 const result = [ ]
@@ -125,6 +82,7 @@ const Utils = {
12582 } ,
12683
12784 toUri ( ) {
85+ //TODO: refactor it
12886 let uri = ''
12987 let arg
13088
@@ -173,49 +131,12 @@ const Utils = {
173131 return arr . map ( item => encodeURIComponent ( item ) ) . join ( ',' )
174132 } ,
175133
176- classWrapper ( obj ) {
177- const wrapper = obj => {
178- let wrapperName = null
179- let Wrapper = null
180-
181- for ( const property in obj ) {
182- if ( obj . hasOwnProperty ( property ) ) {
183- if ( property === '___class' ) {
184- wrapperName = obj [ property ]
185- break
186- }
187- }
188- }
189-
190- if ( wrapperName ) {
191- try {
192- Wrapper = eval ( wrapperName )
193- obj = Utils . deepExtend ( new Wrapper ( ) , obj )
194- } catch ( e ) {
195- }
196- }
197-
198- return obj
199- }
200-
201- if ( Utils . isObject ( obj ) && obj != null ) {
202- if ( Utils . isArray ( obj ) ) {
203- for ( let i = obj . length ; i -- ; ) {
204- obj [ i ] = wrapper ( obj [ i ] )
205- }
206- } else {
207- obj = wrapper ( obj )
208- }
209- }
210-
211- return obj
212- } ,
213-
214134 deepExtend ( destination , source ) {
135+ //TODO: refactor it
215136 for ( const property in source ) {
216137 if ( source [ property ] !== undefined && source . hasOwnProperty ( property ) ) {
217138 destination [ property ] = destination [ property ] || { }
218- destination [ property ] = Utils . classWrapper ( source [ property ] )
139+ destination [ property ] = classWrapper ( source [ property ] )
219140
220141 if (
221142 destination [ property ]
@@ -224,30 +145,28 @@ const Utils = {
224145 && destination [ property ] [ property ] . hasOwnProperty ( '__originSubID' )
225146 ) {
226147
227- destination [ property ] [ property ] = Utils . classWrapper ( destination [ property ] )
148+ destination [ property ] [ property ] = classWrapper ( destination [ property ] )
228149 }
229150 }
230151 }
231152
232153 return destination
233154 } ,
234155
235- cloneObject ( obj ) {
236- return Utils . isArray ( obj ) ? obj . slice ( ) : Utils . deepExtend ( { } , obj )
237- } ,
238-
239156 extractResponder ( args ) {
240- let i , len
241- for ( i = 0 , len = args . length ; i < len ; ++ i ) {
157+ for ( let i = 0 ; i < args . length ; i ++ ) {
242158 if ( args [ i ] instanceof Async ) {
243159 return args [ i ]
244160 }
245161 }
246-
247- return null
248162 } ,
249163
250164 wrapAsync ( asyncHandler , parser , context ) {
165+ //TODO: should we remove it?
166+ if ( asyncHandler instanceof Async && ! parser ) {
167+ return asyncHandler
168+ }
169+
251170 const success = data => {
252171 if ( parser ) {
253172 data = parser . call ( context , data )
@@ -285,20 +204,46 @@ const Utils = {
285204
286205 return fn . apply ( context , arguments )
287206 }
288- } ,
207+ }
208+ }
289209
290- mirrorKeys ( obj ) {
291- const mirroredObject = { }
210+ function classWrapper ( obj ) {
211+ //TODO: refactor it
212+ const wrapper = obj => {
213+ let wrapperName = null
214+ let Wrapper = null
215+
216+ for ( const property in obj ) {
217+ if ( obj . hasOwnProperty ( property ) ) {
218+ if ( property === '___class' ) {
219+ wrapperName = obj [ property ]
220+ break
221+ }
222+ }
223+ }
292224
293- for ( const key in obj ) {
294- if ( obj . hasOwnProperty ( key ) ) {
295- mirroredObject [ key ] = key
225+ if ( wrapperName ) {
226+ try {
227+ Wrapper = eval ( wrapperName )
228+ obj = Utils . deepExtend ( new Wrapper ( ) , obj )
229+ } catch ( e ) {
296230 }
297231 }
298232
299- return mirroredObject
233+ return obj
234+ }
235+
236+ if ( Utils . isObject ( obj ) && obj != null ) {
237+ if ( Utils . isArray ( obj ) ) {
238+ for ( let i = obj . length ; i -- ; ) {
239+ obj [ i ] = wrapper ( obj [ i ] )
240+ }
241+ } else {
242+ obj = wrapper ( obj )
243+ }
300244 }
301245
246+ return obj
302247}
303248
304249export default Utils
0 commit comments