Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useArrayReduce): new function #1919

Merged
merged 3 commits into from Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/shared/useArrayReduce/index.md
@@ -0,0 +1,38 @@
---
category: Utilities
---

# useArrayReduce

Reactively array reduce.

## Usage

```js
import { useArrayReduce } from '@vueuse/core'

const sum = useArrayReduce([ref(1), ref(2), ref(3)], (sum, val) => sum + val)
// sum.value: 6
```

### Use with reactive array

```js
import { useArrayReduce } from '@vueuse/core'

const list = reactive([1, 2])
const sum = useArrayReduce(list, (sum, val) => sum + val)

list.push(3)
// sum.value: 6
```

### Use with initialValue

```js
import { useArrayReduce } from '@vueuse/core'

const list = reactive([{ num: 1 }, { num: 2 }])
const sum = useArrayReduce(list, (sum, val) => sum + val.num, 0)
// sum.value: 3
```
44 changes: 44 additions & 0 deletions packages/shared/useArrayReduce/index.test.ts
@@ -0,0 +1,44 @@
import { reactive, ref } from 'vue-demi'
import { useSetup } from '../../.test'
import { useArrayReduce } from '../useArrayReduce'

describe('useArrayReduce', () => {
it('should be defined', () => {
expect(useArrayReduce).toBeDefined()
})

it('should calulate the array sum', () => {
useSetup(() => {
const item1 = ref(1)
const item2 = ref(2)
const sum = useArrayReduce([item1, item2, 3], (a, b) => a + b)
expect(sum.value).toBe(6)

item1.value = 4
expect(sum.value).toBe(9)

item2.value = 3
expect(sum.value).toBe(10)
})
})

it('should work with reactive array', () => {
useSetup(() => {
const list = reactive([1, 2])
const sum = useArrayReduce(list, (a, b) => a + b)
expect(sum.value).toBe(3)

list.push(3)
expect(sum.value).toBe(6)
})
})

it('should work with initialValue', () => {
const list = reactive([{ num: 1 }, { num: 2 }])
const sum = useArrayReduce(list, (sum, val) => sum + val.num, 0)
expect(sum.value).toBe(3)

list.push({ num: 3 })
expect(sum.value).toBe(6)
})
})
35 changes: 35 additions & 0 deletions packages/shared/useArrayReduce/index.ts
@@ -0,0 +1,35 @@
import type { MaybeComputedRef } from '@vueuse/shared'
import type { ComputedRef } from 'vue-demi'
import { resolveUnref } from '@vueuse/shared'
import { computed } from 'vue-demi'

export type UseArrayReducer<PV, CV, R> = (previousValue: PV, currentValue: CV, currentIndex: number) => R

export function useArrayReduce<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
reducer: UseArrayReducer<T, T, T>,
): ComputedRef<T>

export function useArrayReduce<T, U>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
reducer: UseArrayReducer<U, T, U>,
initialValue: U,
): ComputedRef<U>

export function useArrayReduce<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
reducer: ((...p: any[]) => any),
...args: any[]
): ComputedRef<T> {
return computed(() => {
const reduce = Array.prototype.reduce.bind(resolveUnref(list))

const reduceCallback = (sum: any, value: any, index: number) => reducer(resolveUnref(sum), resolveUnref(value), index)

// Depending on the behavior of reduce, undefined is also a valid initialization value,
// and this code will distinguish the behavior between them.
return args.length
? reduce(reduceCallback, args[0])
: reduce(reduceCallback)
})
}