1
- import './index' ;
2
- import { filter , map , flatMap , distinct , find , some , every , none } from './operators' ;
1
+ import '../src /index' ;
2
+ import { filter , map , flatMap , distinct , find , some , every , none , reduceToEvery , reduceToSome , reduceToNone } from '../src /operators' ;
3
3
4
4
describe ( 'pipe' , ( ) => {
5
5
@@ -32,7 +32,7 @@ describe('pipe', () => {
32
32
} ) ;
33
33
34
34
it ( 'should filter' , ( ) => {
35
- const result : Array < string > = [ 0 , 1 , 1 , 2 , 3 , 4 , 2 , 5 , 6 , 7 , 2 , 8 , 9 ]
35
+ const result : Array < number > = [ 0 , 1 , 1 , 2 , 3 , 4 , 2 , 5 , 6 , 7 , 2 , 8 , 9 ]
36
36
. pipe (
37
37
filter ( ( n : number ) => n % 2 == 0 )
38
38
) ;
@@ -72,7 +72,7 @@ describe('pipe', () => {
72
72
} ) ;
73
73
74
74
it ( 'should pipe and return true because some elements match criteria' , ( ) => {
75
- const result : number = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ]
75
+ const result : boolean = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ]
76
76
. pipe (
77
77
map ( ( n : string ) => parseInt ( n ) ) ,
78
78
some ( ( n : number ) => n > 5 && n < 10 )
@@ -82,7 +82,7 @@ describe('pipe', () => {
82
82
} ) ;
83
83
84
84
it ( 'should pipe and return false because no element matches criteria' , ( ) => {
85
- const result : number = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ]
85
+ const result : boolean = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ]
86
86
. pipe (
87
87
map ( ( n : string ) => parseInt ( n ) ) ,
88
88
some ( ( n : number ) => n >= 10 )
@@ -92,7 +92,7 @@ describe('pipe', () => {
92
92
} ) ;
93
93
94
94
it ( 'should pipe and return true because all elements match criteria' , ( ) => {
95
- const result : number = [ '2' , '4' , '6' , '8' , '10' , '12' , '14' , '16' , '18' , '20' ]
95
+ const result : boolean = [ '2' , '4' , '6' , '8' , '10' , '12' , '14' , '16' , '18' , '20' ]
96
96
. pipe (
97
97
map ( ( n : string ) => parseInt ( n ) ) ,
98
98
every ( ( n : number ) => n % 2 === 0 )
@@ -102,7 +102,7 @@ describe('pipe', () => {
102
102
} ) ;
103
103
104
104
it ( 'should pipe and return false because not all elements match criteria' , ( ) => {
105
- const result : number = [ '2' , '4' , '6' , '8' , '11' , '12' , '14' , '16' , '18' , '20' ]
105
+ const result : boolean = [ '2' , '4' , '6' , '8' , '11' , '12' , '14' , '16' , '18' , '20' ]
106
106
. pipe (
107
107
map ( ( n : string ) => parseInt ( n ) ) ,
108
108
every ( ( n : number ) => n % 2 === 0 )
@@ -112,7 +112,7 @@ describe('pipe', () => {
112
112
} ) ;
113
113
114
114
it ( 'should pipe and return true because all elements do not match criteria' , ( ) => {
115
- const result : number = [ '3' , '5' , '7' , '9' , '11' , '13' , '15' , '17' , '19' , '21' ]
115
+ const result : boolean = [ '3' , '5' , '7' , '9' , '11' , '13' , '15' , '17' , '19' , '21' ]
116
116
. pipe (
117
117
map ( ( n : string ) => parseInt ( n ) ) ,
118
118
none ( ( n : number ) => n % 2 === 0 )
@@ -122,7 +122,7 @@ describe('pipe', () => {
122
122
} ) ;
123
123
124
124
it ( 'should pipe and return false because not all elements match criteria' , ( ) => {
125
- const result : number = [ '3' , '5' , '7' , '9' , '12' , '13' , '15' , '17' , '19' , '21' ]
125
+ const result : boolean = [ '3' , '5' , '7' , '9' , '12' , '13' , '15' , '17' , '19' , '21' ]
126
126
. pipe (
127
127
map ( ( n : string ) => parseInt ( n ) ) ,
128
128
none ( ( n : number ) => n % 2 === 0 )
@@ -151,7 +151,7 @@ describe('pipe', () => {
151
151
152
152
describe ( 'flatMap' , ( ) => {
153
153
it ( 'should pipe with flatMap as last operator' , ( ) => {
154
- const result : number = [ '1' , '3' , '5' , '7' , '9' ]
154
+ const result : Array < number > = [ '1' , '3' , '5' , '7' , '9' ]
155
155
. pipe (
156
156
map ( ( s : string ) => parseInt ( s ) ) ,
157
157
flatMap ( ( n : number ) => [ n , n + 1 ] )
@@ -161,7 +161,7 @@ describe('pipe', () => {
161
161
} ) ;
162
162
163
163
it ( 'should pipe with flatMap as intermediate operator' , ( ) => {
164
- const result : number = [ '1' , '3' , '5' , '7' , '9' ]
164
+ const result : Array < number > = [ '1' , '3' , '5' , '7' , '9' ]
165
165
. pipe (
166
166
map ( ( s : string ) => parseInt ( s ) ) ,
167
167
flatMap ( ( n : number ) => [ n , n + 1 ] ) ,
@@ -184,7 +184,7 @@ describe('pipe', () => {
184
184
} ) ;
185
185
186
186
it ( 'should pipe with flatMap as intermediate operator and filter in the end' , ( ) => {
187
- const result : boolean = [ '1' , '3' , '5' , '7' , '9' ]
187
+ const result : Array < number > = [ '1' , '3' , '5' , '7' , '9' ]
188
188
. pipe (
189
189
map ( ( s : string ) => parseInt ( s ) ) ,
190
190
flatMap ( ( n : number ) => [ n , n + 1 ] ) ,
@@ -196,6 +196,70 @@ describe('pipe', () => {
196
196
197
197
} ) ;
198
198
199
+ describe ( 'reducer' , ( ) => {
200
+
201
+ it ( 'should successfully reduce to every' , ( ) => {
202
+ const result : boolean = [ '1' , '3' , '5' , '7' , '9' ]
203
+ . pipe (
204
+ map ( ( s : string ) => parseInt ( s ) ) ,
205
+ reduceToEvery ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
206
+ ) ;
207
+
208
+ expect ( result ) . toBeTruthy ( ) ;
209
+ } ) ;
210
+
211
+ it ( 'should fail to reduce to every' , ( ) => {
212
+ const result : boolean = [ '1' , '3' , '5' , '6' , '9' ]
213
+ . pipe (
214
+ map ( ( s : string ) => parseInt ( s ) ) ,
215
+ reduceToEvery ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
216
+ ) ;
217
+
218
+ expect ( result ) . toBeFalsy ( ) ;
219
+ } ) ;
220
+
221
+ it ( 'should successfully reduce to some' , ( ) => {
222
+ const result : boolean = [ '1' , '2' , '5' , '7' , '9' ]
223
+ . pipe (
224
+ map ( ( s : string ) => parseInt ( s ) ) ,
225
+ reduceToSome ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
226
+ ) ;
227
+
228
+ expect ( result ) . toBeTruthy ( ) ;
229
+ } ) ;
230
+
231
+ it ( 'should fail to reduce to some' , ( ) => {
232
+ const result : boolean = [ '1' , '2' , '5' , '6' , '9' ]
233
+ . pipe (
234
+ map ( ( s : string ) => parseInt ( s ) ) ,
235
+ reduceToSome ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
236
+ ) ;
237
+
238
+ expect ( result ) . toBeFalsy ( ) ;
239
+ } ) ;
240
+
241
+ it ( 'should successfully reduce to none' , ( ) => {
242
+ const result : boolean = [ '1' , '2' , '5' , '6' , '9' ]
243
+ . pipe (
244
+ map ( ( s : string ) => parseInt ( s ) ) ,
245
+ reduceToNone ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
246
+ ) ;
247
+
248
+ expect ( result ) . toBeTruthy ( ) ;
249
+ } ) ;
250
+
251
+ it ( 'should fail to reduce to none' , ( ) => {
252
+ const result : boolean = [ '1' , '2' , '5' , '7' , '9' ]
253
+ . pipe (
254
+ map ( ( s : string ) => parseInt ( s ) ) ,
255
+ reduceToNone ( ( n1 : number , n2 : number ) => ( n1 + n2 ) % 2 === 0 )
256
+ ) ;
257
+
258
+ expect ( result ) . toBeFalsy ( ) ;
259
+ } ) ;
260
+
261
+ } )
262
+
199
263
} ) ;
200
264
201
265
} )
0 commit comments