1
1
import { expect } from 'chai' ;
2
2
import { transposeObjectArray } from '../transposeObjectArray' ;
3
3
4
- describe ( 'transposeObjectArray' , ( ) => {
4
+ describe . only ( 'transposeObjectArray' , ( ) => {
5
5
it ( 'should handle empty array' , ( ) => {
6
- const result = transposeObjectArray ( [ ] , [ 'id' , 'name' ] ) ;
6
+ const result = transposeObjectArray ( { objects : [ ] , properties : [ 'id' , 'name' ] } ) ;
7
7
expect ( result ) . to . deep . equal ( { } ) ;
8
8
} ) ;
9
9
10
10
it ( 'should extract multiple properties from array of objects' , ( ) => {
11
- const items = [
11
+ const objects = [
12
12
{ id : 1 , name : 'John' , age : 25 , city : 'NY' } ,
13
13
{ id : 2 , name : 'Jane' , age : 30 , city : 'LA' } ,
14
14
] ;
15
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
15
+ const result = transposeObjectArray ( { objects , properties : [ 'id' , 'name' ] } ) ;
16
16
expect ( result ) . to . deep . equal ( {
17
17
id : [ 1 , 2 ] ,
18
18
name : [ 'John' , 'Jane' ] ,
@@ -24,12 +24,12 @@ describe('transposeObjectArray', () => {
24
24
} ) ;
25
25
26
26
it ( 'should handle objects with missing properties' , ( ) => {
27
- const items = [
27
+ const objects = [
28
28
{ id : 1 , name : 'John' , age : 25 } ,
29
29
{ id : 2 , age : 30 } ,
30
30
{ id : 3 , name : 'Bob' , city : 'LA' } ,
31
31
] ;
32
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
32
+ const result = transposeObjectArray ( { objects , properties : [ 'id' , 'name' ] } ) ;
33
33
expect ( result ) . to . deep . equal ( {
34
34
id : [ 1 , 2 , 3 ] ,
35
35
name : [ 'John' , undefined , 'Bob' ] ,
@@ -38,11 +38,11 @@ describe('transposeObjectArray', () => {
38
38
} ) ;
39
39
40
40
it ( 'should handle different value types' , ( ) => {
41
- const items = [
41
+ const objects = [
42
42
{ id : 1 , active : true , count : 10 , tags : [ 'a' , 'b' ] } ,
43
43
{ id : 2 , active : false , count : 20 , tags : [ 'c' ] } ,
44
44
] ;
45
- const result = transposeObjectArray ( items , [ 'active' , 'tags' ] ) ;
45
+ const result = transposeObjectArray ( { objects , properties : [ 'active' , 'tags' ] } ) ;
46
46
expect ( result ) . to . deep . equal ( {
47
47
active : [ true , false ] ,
48
48
tags : [ [ 'a' , 'b' ] , [ 'c' ] ] ,
@@ -54,11 +54,11 @@ describe('transposeObjectArray', () => {
54
54
} ) ;
55
55
56
56
it ( 'should handle extracting all properties (empty rest)' , ( ) => {
57
- const items = [
57
+ const objects = [
58
58
{ id : 1 , name : 'John' } ,
59
59
{ id : 2 , name : 'Jane' } ,
60
60
] ;
61
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
61
+ const result = transposeObjectArray ( { objects , properties : [ 'id' , 'name' ] } ) ;
62
62
expect ( result ) . to . deep . equal ( {
63
63
id : [ 1 , 2 ] ,
64
64
name : [ 'John' , 'Jane' ] ,
@@ -67,11 +67,11 @@ describe('transposeObjectArray', () => {
67
67
} ) ;
68
68
69
69
it ( 'should handle extracting no properties (everything in rest)' , ( ) => {
70
- const items = [
70
+ const objects = [
71
71
{ id : 1 , name : 'John' } ,
72
72
{ id : 2 , name : 'Jane' } ,
73
73
] ;
74
- const result = transposeObjectArray ( items , [ ] ) ;
74
+ const result = transposeObjectArray ( { objects , properties : [ ] } ) ;
75
75
expect ( result ) . to . deep . equal ( {
76
76
rest : [
77
77
{ id : 1 , name : 'John' } ,
@@ -81,11 +81,11 @@ describe('transposeObjectArray', () => {
81
81
} ) ;
82
82
83
83
it ( 'should handle objects with null or undefined values' , ( ) => {
84
- const items = [
84
+ const objects = [
85
85
{ id : 1 , name : null , age : 25 } ,
86
86
{ id : 2 , name : undefined , age : 30 } ,
87
87
] ;
88
- const result = transposeObjectArray ( items , [ 'id' , 'name' ] ) ;
88
+ const result = transposeObjectArray ( { objects , properties : [ 'id' , 'name' ] } ) ;
89
89
expect ( result ) . to . deep . equal ( {
90
90
id : [ 1 , 2 ] ,
91
91
name : [ null , undefined ] ,
@@ -94,11 +94,11 @@ describe('transposeObjectArray', () => {
94
94
} ) ;
95
95
96
96
it ( 'should handle nested objects' , ( ) => {
97
- const items = [
97
+ const objects = [
98
98
{ id : 1 , user : { name : 'John' , age : 25 } } ,
99
99
{ id : 2 , user : { name : 'Jane' , age : 30 } } ,
100
100
] ;
101
- const result = transposeObjectArray ( items , [ 'id' , 'user' ] ) ;
101
+ const result = transposeObjectArray ( { objects , properties : [ 'id' , 'user' ] } ) ;
102
102
expect ( result ) . to . deep . equal ( {
103
103
id : [ 1 , 2 ] ,
104
104
user : [
@@ -110,11 +110,11 @@ describe('transposeObjectArray', () => {
110
110
} ) ;
111
111
112
112
it ( 'should preserve property order in rest object' , ( ) => {
113
- const items = [
113
+ const objects = [
114
114
{ a : 1 , b : 2 , c : 3 , d : 4 } ,
115
115
{ a : 5 , b : 6 , c : 7 , d : 8 } ,
116
116
] ;
117
- const result = transposeObjectArray ( items , [ 'a' , 'c' ] ) ;
117
+ const result = transposeObjectArray ( { objects , properties : [ 'a' , 'c' ] } ) ;
118
118
expect ( result ) . to . deep . equal ( {
119
119
a : [ 1 , 5 ] ,
120
120
c : [ 3 , 7 ] ,
@@ -124,4 +124,16 @@ describe('transposeObjectArray', () => {
124
124
] ,
125
125
} ) ;
126
126
} ) ;
127
+
128
+ it ( 'should omit rest properties if includeOtherProperties is false' , ( ) => {
129
+ const objects = [
130
+ { id : 1 , name : null , age : 25 } ,
131
+ { id : 2 , name : undefined , age : 30 } ,
132
+ ] ;
133
+ const result = transposeObjectArray ( { objects, properties : [ 'id' , 'name' ] , options : { includeOtherProperties : false } } ) ;
134
+ expect ( result ) . to . deep . equal ( {
135
+ id : [ 1 , 2 ] ,
136
+ name : [ null , undefined ] ,
137
+ } ) ;
138
+ } ) ;
127
139
} ) ;
0 commit comments