@@ -22,25 +22,43 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
22
22
describe ( '@object: any' , ( ) => {
23
23
24
24
it ( 'transforms keys of simple object' , ( ) => {
25
- deepMapKeys ( { one : 1 , two : 2 } , caps ) . should . deep . equal ( { ONE : 1 , TWO : 2 } ) ;
25
+ deepMapKeys ( { one : 1 , two : 2 } , caps ) . should . deep . equal ( { ONE : 1 , TWO : 2 } ) ;
26
26
} ) ;
27
27
28
28
it ( 'transforms keys of object with nested objects/arrays' , ( ) => {
29
- deepMapKeys ( { one : 1 , obj : { two : 2 , three : 3 } , arr : [ 4 , 5 ] } , caps )
30
- . should . deep . equal ( { ONE : 1 , OBJ : { TWO : 2 , THREE : 3 } , ARR : [ 4 , 5 ] } ) ;
29
+ deepMapKeys ( { one : 1 , obj : { two : 2 , three : 3 } , arr : [ 4 , 5 ] } , caps )
30
+ . should . deep . equal ( { ONE : 1 , OBJ : { TWO : 2 , THREE : 3 } , ARR : [ 4 , 5 ] } ) ;
31
31
} ) ;
32
32
33
33
it ( 'transforms keys of array with nested object/array' , ( ) => {
34
- deepMapKeys ( [ 1 , { two : 2 , three : 3 , arr : [ 4 , { five : 5 } ] } ] , caps )
35
- . should . deep . equal ( [ 1 , { TWO : 2 , THREE : 3 , ARR : [ 4 , { FIVE : 5 } ] } ] ) ;
34
+ deepMapKeys ( [ 1 , { two : 2 , three : 3 , arr : [ 4 , { five : 5 } ] } ] , caps )
35
+ . should . deep . equal ( [ 1 , { TWO : 2 , THREE : 3 , ARR : [ 4 , { FIVE : 5 } ] } ] ) ;
36
+ } ) ;
37
+
38
+ function str2ab ( str : string ) : ArrayBuffer {
39
+ let buf = new ArrayBuffer ( str . length * 2 ) ;
40
+ let bufView = new Uint16Array ( buf ) ;
41
+ for ( let i = 0 , strLen = str . length ; i < strLen ; i ++ ) {
42
+ bufView [ i ] = str . charCodeAt ( i ) ;
43
+ }
44
+ return buf ;
45
+ }
46
+ function ab2str ( buf : ArrayBuffer ) : string {
47
+ return String . fromCharCode . apply ( null , new Uint16Array ( buf ) ) ;
48
+ }
49
+
50
+ it ( 'doesn\'t transform an ArrayBuffer to an empty array' , ( ) => {
51
+ const buffer = str2ab ( 'test' ) ;
52
+ const transformed : any = deepMapKeys ( { two : 2 , three : 3 , arr : buffer } , caps ) ;
53
+ ab2str ( transformed . ARR ) . should . be . equal ( 'test' ) ;
36
54
} ) ;
37
55
38
56
it ( 'transforms an object with circular references' , ( ) => {
39
- let obj = { one : 1 , arr : [ 2 , 3 ] , self : null as any , arr2 : null as any [ ] } ;
57
+ let obj = { one : 1 , arr : [ 2 , 3 ] , self : null as any , arr2 : null as any [ ] } ;
40
58
obj . self = obj ;
41
59
obj . arr2 = obj . arr ;
42
60
43
- let exp = { ONE : 1 , ARR : [ 2 , 3 ] , SELF : null as any , ARR2 : null as any [ ] } ;
61
+ let exp = { ONE : 1 , ARR : [ 2 , 3 ] , SELF : null as any , ARR2 : null as any [ ] } ;
44
62
exp . SELF = exp ;
45
63
exp . ARR2 = exp . ARR ;
46
64
@@ -52,27 +70,27 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
52
70
describe ( '@mapFn(key: string, value: any): string' , ( ) => {
53
71
54
72
it ( 'throws Error if undefined' , ( ) => {
55
- deepMapKeys . bind ( null , { one : 1 } ) . should . throw ( Error ) ;
73
+ deepMapKeys . bind ( null , { one : 1 } ) . should . throw ( Error ) ;
56
74
} ) ;
57
75
58
76
it ( 'throws TypeError if not a function' , ( ) => {
59
- deepMapKeys . bind ( null , { one : 1 } , 42 ) . should . throw ( TypeError ) ;
77
+ deepMapKeys . bind ( null , { one : 1 } , 42 ) . should . throw ( TypeError ) ;
60
78
} ) ;
61
79
62
80
it ( 'is called once per object property' , ( ) => {
63
- deepMapKeys ( { one : 1 , obj : { two : 2 , three : 3 } , arr : [ 4 , 5 ] } , caps ) ;
81
+ deepMapKeys ( { one : 1 , obj : { two : 2 , three : 3 } , arr : [ 4 , 5 ] } , caps ) ;
64
82
caps . should . have . callCount ( 5 ) ;
65
83
} ) ;
66
84
67
85
it ( 'is called with @key as first argument' , ( ) => {
68
- deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
86
+ deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
69
87
caps . should . have . been . calledWith ( 'one' ) ;
70
88
caps . should . have . been . calledWith ( 'arr' ) ;
71
89
} ) ;
72
90
73
91
it ( 'is called with @value as second argument' , ( ) => {
74
- let { any} = sinon . match ;
75
- deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
92
+ let { any } = sinon . match ;
93
+ deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
76
94
caps . should . have . been . calledWith ( any , 1 ) ;
77
95
caps . should . have . been . calledWithMatch ( any , [ 2 , 3 ] ) ;
78
96
} ) ;
@@ -82,18 +100,18 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
82
100
describe ( '@options?' , ( ) => {
83
101
84
102
it ( 'throws TypeError if defined but not an object' , ( ) => {
85
- deepMapKeys . bind ( null , { one : 1 } , caps , 42 ) . should . throw ( TypeError ) ;
103
+ deepMapKeys . bind ( null , { one : 1 } , caps , 42 ) . should . throw ( TypeError ) ;
86
104
} ) ;
87
105
88
106
describe ( 'option: thisArg' , ( ) => {
89
107
90
108
it ( 'sets context within @mapFn' , ( ) => {
91
- deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps , { thisArg : 42 } ) ;
109
+ deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps , { thisArg : 42 } ) ;
92
110
caps . should . have . been . calledOn ( 42 ) ;
93
111
} ) ;
94
112
95
113
it ( 'defaults to undefined' , ( ) => {
96
- deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
114
+ deepMapKeys ( { one : 1 , arr : [ 2 , 3 ] } , caps ) ;
97
115
caps . should . have . been . calledOn ( undefined ) ;
98
116
} ) ;
99
117
0 commit comments