@@ -3,16 +3,22 @@ import { transposeObjectArray } from '../transposeObjectArray';
3
3
4
4
describe ( 'transposeObjectArray' , ( ) => {
5
5
it ( 'should handle empty array' , ( ) => {
6
- const result = transposeObjectArray ( [ ] , [ 'id' , 'name' ] ) ;
6
+ const result = transposeObjectArray ( {
7
+ objects : [ ] ,
8
+ properties : [ 'id' , 'name' ] ,
9
+ } ) ;
7
10
expect ( result ) . to . deep . equal ( { } ) ;
8
11
} ) ;
9
12
10
13
it ( 'should extract multiple properties from array of objects' , ( ) => {
11
- const items = [
14
+ const objects = [
12
15
{ id : 1 , name : 'John' , age : 25 , city : 'NY' } ,
13
16
{ id : 2 , name : 'Jane' , age : 30 , city : 'LA' } ,
14
17
] ;
15
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
18
+ const result = transposeObjectArray ( {
19
+ objects,
20
+ properties : [ 'id' , 'name' ] ,
21
+ } ) ;
16
22
expect ( result ) . to . deep . equal ( {
17
23
id : [ 1 , 2 ] ,
18
24
name : [ 'John' , 'Jane' ] ,
@@ -24,12 +30,15 @@ describe('transposeObjectArray', () => {
24
30
} ) ;
25
31
26
32
it ( 'should handle objects with missing properties' , ( ) => {
27
- const items = [
33
+ const objects = [
28
34
{ id : 1 , name : 'John' , age : 25 } ,
29
35
{ id : 2 , age : 30 } ,
30
36
{ id : 3 , name : 'Bob' , city : 'LA' } ,
31
37
] ;
32
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
38
+ const result = transposeObjectArray ( {
39
+ objects,
40
+ properties : [ 'id' , 'name' ] ,
41
+ } ) ;
33
42
expect ( result ) . to . deep . equal ( {
34
43
id : [ 1 , 2 , 3 ] ,
35
44
name : [ 'John' , undefined , 'Bob' ] ,
@@ -38,11 +47,14 @@ describe('transposeObjectArray', () => {
38
47
} ) ;
39
48
40
49
it ( 'should handle different value types' , ( ) => {
41
- const items = [
50
+ const objects = [
42
51
{ id : 1 , active : true , count : 10 , tags : [ 'a' , 'b' ] } ,
43
52
{ id : 2 , active : false , count : 20 , tags : [ 'c' ] } ,
44
53
] ;
45
- const result = transposeObjectArray ( items , [ 'active' , 'tags' ] ) ;
54
+ const result = transposeObjectArray ( {
55
+ objects,
56
+ properties : [ 'active' , 'tags' ] ,
57
+ } ) ;
46
58
expect ( result ) . to . deep . equal ( {
47
59
active : [ true , false ] ,
48
60
tags : [ [ 'a' , 'b' ] , [ 'c' ] ] ,
@@ -54,11 +66,14 @@ describe('transposeObjectArray', () => {
54
66
} ) ;
55
67
56
68
it ( 'should handle extracting all properties (empty rest)' , ( ) => {
57
- const items = [
69
+ const objects = [
58
70
{ id : 1 , name : 'John' } ,
59
71
{ id : 2 , name : 'Jane' } ,
60
72
] ;
61
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
73
+ const result = transposeObjectArray ( {
74
+ objects,
75
+ properties : [ 'id' , 'name' ] ,
76
+ } ) ;
62
77
expect ( result ) . to . deep . equal ( {
63
78
id : [ 1 , 2 ] ,
64
79
name : [ 'John' , 'Jane' ] ,
@@ -67,11 +82,11 @@ describe('transposeObjectArray', () => {
67
82
} ) ;
68
83
69
84
it ( 'should handle extracting no properties (everything in rest)' , ( ) => {
70
- const items = [
85
+ const objects = [
71
86
{ id : 1 , name : 'John' } ,
72
87
{ id : 2 , name : 'Jane' } ,
73
88
] ;
74
- const result = transposeObjectArray ( items , [ ] ) ;
89
+ const result = transposeObjectArray ( { objects , properties : [ ] } ) ;
75
90
expect ( result ) . to . deep . equal ( {
76
91
rest : [
77
92
{ id : 1 , name : 'John' } ,
@@ -81,11 +96,14 @@ describe('transposeObjectArray', () => {
81
96
} ) ;
82
97
83
98
it ( 'should handle objects with null or undefined values' , ( ) => {
84
- const items = [
99
+ const objects = [
85
100
{ id : 1 , name : null , age : 25 } ,
86
101
{ id : 2 , name : undefined , age : 30 } ,
87
102
] ;
88
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
103
+ const result = transposeObjectArray ( {
104
+ objects,
105
+ properties : [ 'id' , 'name' ] ,
106
+ } ) ;
89
107
expect ( result ) . to . deep . equal ( {
90
108
id : [ 1 , 2 ] ,
91
109
name : [ null , undefined ] ,
@@ -94,11 +112,14 @@ describe('transposeObjectArray', () => {
94
112
} ) ;
95
113
96
114
it ( 'should handle nested objects' , ( ) => {
97
- const items = [
115
+ const objects = [
98
116
{ id : 1 , user : { name : 'John' , age : 25 } } ,
99
117
{ id : 2 , user : { name : 'Jane' , age : 30 } } ,
100
118
] ;
101
- const result = transposeObjectArray ( items , [ 'id' , 'user' ] ) ;
119
+ const result = transposeObjectArray ( {
120
+ objects,
121
+ properties : [ 'id' , 'user' ] ,
122
+ } ) ;
102
123
expect ( result ) . to . deep . equal ( {
103
124
id : [ 1 , 2 ] ,
104
125
user : [
@@ -110,11 +131,11 @@ describe('transposeObjectArray', () => {
110
131
} ) ;
111
132
112
133
it ( 'should preserve property order in rest object' , ( ) => {
113
- const items = [
134
+ const objects = [
114
135
{ a : 1 , b : 2 , c : 3 , d : 4 } ,
115
136
{ a : 5 , b : 6 , c : 7 , d : 8 } ,
116
137
] ;
117
- const result = transposeObjectArray ( items , [ 'a' , 'c' ] ) ;
138
+ const result = transposeObjectArray ( { objects , properties : [ 'a' , 'c' ] } ) ;
118
139
expect ( result ) . to . deep . equal ( {
119
140
a : [ 1 , 5 ] ,
120
141
c : [ 3 , 7 ] ,
@@ -124,4 +145,20 @@ describe('transposeObjectArray', () => {
124
145
] ,
125
146
} ) ;
126
147
} ) ;
148
+
149
+ it ( 'should omit rest properties if includeOtherProperties is false' , ( ) => {
150
+ const objects = [
151
+ { id : 1 , name : null , age : 25 } ,
152
+ { id : 2 , name : undefined , age : 30 } ,
153
+ ] ;
154
+ const result = transposeObjectArray ( {
155
+ objects,
156
+ properties : [ 'id' , 'name' ] ,
157
+ options : { includeOtherProperties : false } ,
158
+ } ) ;
159
+ expect ( result ) . to . deep . equal ( {
160
+ id : [ 1 , 2 ] ,
161
+ name : [ null , undefined ] ,
162
+ } ) ;
163
+ } ) ;
127
164
} ) ;
0 commit comments