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(useSum): reactively adds the values together #1831

Closed
wants to merge 10 commits into from

Conversation

LittleSound
Copy link
Contributor

@LittleSound LittleSound commented Jul 10, 2022

Description

import { useSum } from '@vueuse/math'

const value1 = ref(10)
const value2 = ref(20)

const sum = useSum([value1, value2])
// sum.value: 30
import { useSum } from '@vueuse/math'

const list = reactive([10, 20])

const sum = useSum(list)
// sum.value: 30
const sum = useSum([ref(10), ref(20)])
const sum = useSum(ref([10, 20]))
const sum = useSum(reactive([10, 20]))
// custom adder
const sum = useSum(ref([10, 20]), (sum, value) => sum * value)
// initialization value
const sum = useSum(ref([{ a: 1 }, { a: 2 }]), (sum, value) => sum + value.a, 0)

Additional context

issues #1812


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@LittleSound LittleSound changed the title feat(useSum): reactively adds the values together feat(useSum): [WIP] reactively adds the values together Jul 10, 2022
@LittleSound LittleSound mentioned this pull request Jul 10, 2022
6 tasks
@LittleSound LittleSound marked this pull request as ready for review July 10, 2022 13:47
@LittleSound LittleSound changed the title feat(useSum): [WIP] reactively adds the values together feat(useSum): reactively adds the values together Jul 10, 2022
@LittleSound LittleSound changed the title feat(useSum): reactively adds the values together feat(useSum): reactively adds the values together. Jul 10, 2022
@LittleSound LittleSound changed the title feat(useSum): reactively adds the values together. feat(useSum): reactively adds the values together Jul 10, 2022
@antfu
Copy link
Member

antfu commented Jul 12, 2022

Interesting, but I think this might be better to introduce a more general useArrayReduce in @vueuse/share first and reuse it.

@LittleSound
Copy link
Contributor Author

LittleSound commented Jul 14, 2022

Interesting, but I think this might be better to introduce a more general useArrayReduce in @vueuse/share first and reuse it.

I see that someone is already working on useArrayReduce and has a merged useSum method.

  • Does this PR need to be turned off?
  • Is there anything else I can do or contribute now?

@antfu
Copy link
Member

antfu commented Jul 14, 2022

Thanks for the contribution. I think #1874 is based on your implementation.

@antfu antfu closed this Jul 14, 2022
@yjl9903 yjl9903 mentioned this pull request Jul 14, 2022
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants