File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed
packages/shared/useArrayReduce Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -37,4 +37,15 @@ describe('useArrayReduce', () => {
3737 list . push ( { num : 3 } )
3838 expect ( sum . value ) . toBe ( 6 )
3939 } )
40+ it . skipIf ( isVue2 ) ( 'should work with initialValue being a function' , ( ) => {
41+ const list = reactive ( [ { num : 1 } , { num : 2 } ] )
42+ const sum = useArrayReduce ( list , ( prev , val ) => {
43+ prev . push ( val . num )
44+ return prev
45+ } , ( ( ) => [ ] ) as unknown as number [ ] )
46+ expect ( sum . value ) . toEqual ( [ 1 , 2 ] )
47+
48+ list . push ( { num : 3 } )
49+ expect ( sum . value ) . toEqual ( [ 1 , 2 , 3 ] )
50+ } )
4051} )
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ export function useArrayReduce<T>(
5757 // Depending on the behavior of reduce, undefined is also a valid initialization value,
5858 // and this code will distinguish the behavior between them.
5959 return args . length
60- ? resolved . reduce ( reduceCallback , toValue ( args [ 0 ] ) )
60+ ? resolved . reduce ( reduceCallback , typeof args [ 0 ] === 'function' ? toValue ( args [ 0 ] ( ) ) : toValue ( args [ 0 ] ) )
6161 : resolved . reduce ( reduceCallback )
6262 } )
6363}
You can’t perform that action at this time.
0 commit comments