Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(math): extract common utils
- Loading branch information
Showing
6 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
import type { ComputedRef } from 'vue-demi' | ||
import { computed } from 'vue-demi' | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
import type { MaybeComputedRefArgs } from '../utils' | ||
import { resolveUnrefArgsFlat } from '../utils' | ||
|
||
export function useMax(array: MaybeComputedRef<MaybeComputedRef<number>[]>): ComputedRef<number> | ||
export function useMax(...args: MaybeComputedRef<number>[]): ComputedRef<number> | ||
|
||
/** | ||
* Reactively get maximum of values. | ||
* | ||
* @see https://vueuse.org/useMax | ||
* @param values | ||
*/ | ||
export function useMax(...values: MaybeComputedRef<number>[]) { | ||
return computed<number>(() => | ||
Math.max(...values.map(value => resolveUnref(value))), | ||
) | ||
export function useMax(...args: MaybeComputedRefArgs<number>) { | ||
return computed<number>(() => { | ||
const array = resolveUnrefArgsFlat(args) | ||
return Math.max(...array) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
|
||
export type MaybeComputedRefArgs<T> = MaybeComputedRef<T>[] | [MaybeComputedRef<MaybeComputedRef<T>[]>] | ||
|
||
export function resolveUnrefArgsFlat<T>(args: MaybeComputedRefArgs<T>): T[] { | ||
return args | ||
.flatMap((i: any) => { | ||
const v = resolveUnref(i) | ||
if (Array.isArray(v)) | ||
return v.map(i => resolveUnref(i)) | ||
return [v] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters