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', () => {
37
37
list . push ( { num : 3 } )
38
38
expect ( sum . value ) . toBe ( 6 )
39
39
} )
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
+ } )
40
51
} )
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ export function useArrayReduce<T>(
57
57
// Depending on the behavior of reduce, undefined is also a valid initialization value,
58
58
// and this code will distinguish the behavior between them.
59
59
return args . length
60
- ? resolved . reduce ( reduceCallback , toValue ( args [ 0 ] ) )
60
+ ? resolved . reduce ( reduceCallback , typeof args [ 0 ] === 'function' ? toValue ( args [ 0 ] ( ) ) : toValue ( args [ 0 ] ) )
61
61
: resolved . reduce ( reduceCallback )
62
62
} )
63
63
}
You can’t perform that action at this time.
0 commit comments