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
New @vueuse/math
package
#1812
Comments
For some of the single parameter Example
|
@LittleSound Yes we could. But how should we handle this case?: const value = ref(0.5)
const round = useRound(value)
watchSyncEffect(() => console.log(value)) // log: 1
round.value = 0.1 // log: 0 // value?
round.value = 2.5 // log: 3 |
@antfu 😂 I made a typo, it should be I fixed it |
No problem with the typo. My question is, how should we handle the write back if the value is provided as a ref? |
It seems to me that "write-back" should probably be disabled, or needs to be turned on via options If "write-back" is enabled, it should be the result of the |
For associative binary operations (i.e. e.g. function min(arr: MaybeComputedRef<MaybeComputedRef<number>[]>): ComputedRef<number>
function min(...args: MaybeComputedRef<number>[]): ComputedRef<number>
function min(...args: [MaybeComputedRef<MaybeComputedRef<number>[]>] | MaybeComputedRef<number>[]): ComputedRef<number> {
let xs: Ref<MaybeComputedRef<number>[]>
if (args.length === 1 && Array.isArray(resolveUnref(args[0])))
xs = resolveRef(args[0]) as typeof xs
else
xs = resolveRef(args) as typeof xs
return computed(() => xs.value.reduce<number>((a, b) => Math.min(resolveUnref(a), resolveUnref(b)), Infinity))
}
min(ref(1), ref(2), 3) // works
min([1, 2, 3]) // works
min([ref(1), ref(2), 3]) // works
min(ref([1, 2, 3])) // works |
@zojize I'm trying to achieve a similar effect in test('reactive list', () => {
const list = reactive([10, 20])
const sum = useSum(list)
expect(sum.value).toBe(30)
list[0] = 21
expect(sum.value).toBe(41)
list.push(5)
expect(sum.value).toBe(46)
}) |
@LittleSound I saw your implementation of useSum vueuse/packages/math/useSum/index.ts Lines 19 to 35 in fe25afe
Maybe we can have some more general array utilities, like And then implement Another question is should we optimize |
Yes I already planned to have them in |
@yjl9903 Do you have any ideas about how to implement this kind of optimization? |
Maybe something like this export function useSum(nums: Array<MaybeComputedRef<number>>) {
const sum = ref(0)
for (const num of nums) {
sum.value += resolveUnref(num)
watch(() => resolveUnref(num), (newNum, prevNum) => {
sum.value += newNum - prevNum
})
}
return sum
} And I am not sure how to implement when wrapper array is also a ref. Another problem is that the optimized one has different semantic from the origin. Counter example is const r1 = ref(Number.MAX_VALUE)
const r2 = ref(Number.MAX_VALUE)
const sum = useSum([r1, r2]) // Inf
r1.value = 0
r2.value = 0
// NaN (Inf - Inf = NaN) |
|
We plan to ship a new
@vueuse/math
package in v9.0, where you can see it in thenext
branch already.We planned to adding those new functions:
useMax
useMin
useRound
useFloor
useCeil
useSum
Open for contributions
The text was updated successfully, but these errors were encountered: