CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vueuse--math

Reactive mathematical utility functions and composables for Vue.js applications

Pending
Overview
Eval results
Files

aggregation.mddocs/

Aggregation Functions

Functions for calculating sums, averages, minimums, and maximums from arrays or multiple arguments with full reactivity. All aggregation functions support both array inputs and spread arguments.

Capabilities

useSum

Reactively calculates the sum of numbers from an array or multiple arguments.

/**
 * Get the sum of a set of numbers from an array
 * @param array - Array of reactive numbers or refs
 * @returns ComputedRef containing the sum
 */
function useSum(array: MaybeRefOrGetter<MaybeRefOrGetter<number>[]>): ComputedRef<number>;

/**
 * Get the sum of a set of numbers from multiple arguments
 * @param args - Multiple reactive numbers or refs
 * @returns ComputedRef containing the sum
 */
function useSum(...args: MaybeRefOrGetter<number>[]): ComputedRef<number>;

Usage Examples:

import { ref } from "vue";
import { useSum } from "@vueuse/math";

// Array approach
const numbers = ref([1, 2, 3, 4, 5]);
const sum1 = useSum(numbers);
console.log(sum1.value); // 15

// Spread arguments approach
const a = ref(10);
const b = ref(20);
const c = ref(30);
const sum2 = useSum(a, b, c);
console.log(sum2.value); // 60

// Mixed with plain values
const sum3 = useSum(a, 5, b);
console.log(sum3.value); // 35

// Nested reactive arrays
const nestedArray = ref([ref(1), ref(2), ref(3)]);
const sum4 = useSum(nestedArray);
console.log(sum4.value); // 6

useAverage

Reactively calculates the average (mean) of numbers from an array or multiple arguments.

/**
 * Get the average of a set of numbers from an array
 * @param array - Array of reactive numbers or refs
 * @returns ComputedRef containing the average
 */
function useAverage(array: MaybeRefOrGetter<MaybeRefOrGetter<number>[]>): ComputedRef<number>;

/**
 * Get the average of a set of numbers from multiple arguments
 * @param args - Multiple reactive numbers or refs
 * @returns ComputedRef containing the average
 */
function useAverage(...args: MaybeRefOrGetter<number>[]): ComputedRef<number>;

Usage Examples:

import { ref } from "vue";
import { useAverage } from "@vueuse/math";

// Array approach
const scores = ref([85, 92, 78, 94, 88]);
const average = useAverage(scores);
console.log(average.value); // 87.4

// Arguments approach
const test1 = ref(90);
const test2 = ref(85);
const test3 = ref(95);
const testAverage = useAverage(test1, test2, test3);
console.log(testAverage.value); // 90

// Updates reactively
scores.value.push(100);
console.log(average.value); // New average including 100

useMax

Reactively finds the maximum value from numbers in an array or multiple arguments.

/**
 * Get the maximum value from an array of numbers
 * @param array - Array of reactive numbers or refs
 * @returns ComputedRef containing the maximum value
 */
function useMax(array: MaybeRefOrGetter<MaybeRefOrGetter<number>[]>): ComputedRef<number>;

/**
 * Get the maximum value from multiple number arguments
 * @param args - Multiple reactive numbers or refs
 * @returns ComputedRef containing the maximum value
 */
function useMax(...args: MaybeRefOrGetter<number>[]): ComputedRef<number>;

Usage Examples:

import { ref } from "vue";
import { useMax } from "@vueuse/math";

// Array approach
const temperatures = ref([22, 25, 19, 30, 28]);
const maxTemp = useMax(temperatures);
console.log(maxTemp.value); // 30

// Arguments approach
const price1 = ref(10.99);
const price2 = ref(8.99);
const price3 = ref(12.50);
const maxPrice = useMax(price1, price2, price3);
console.log(maxPrice.value); // 12.5

// With negative numbers
const values = ref([-5, -2, -10, -1]);
const max = useMax(values);
console.log(max.value); // -1

useMin

Reactively finds the minimum value from numbers in an array or multiple arguments.

/**
 * Get the minimum value from an array of numbers
 * @param array - Array of reactive numbers or refs
 * @returns ComputedRef containing the minimum value
 */
function useMin(array: MaybeRefOrGetter<MaybeRefOrGetter<number>[]>): ComputedRef<number>;

/**
 * Get the minimum value from multiple number arguments
 * @param args - Multiple reactive numbers or refs
 * @returns ComputedRef containing the minimum value
 */
function useMin(...args: MaybeRefOrGetter<number>[]): ComputedRef<number>;

Usage Examples:

import { ref } from "vue";
import { useMin } from "@vueuse/math";

// Array approach
const expenses = ref([45.50, 12.30, 89.99, 23.40]);
const minExpense = useMin(expenses);
console.log(minExpense.value); // 12.3

// Arguments approach
const bid1 = ref(1000);
const bid2 = ref(950);
const bid3 = ref(1100);
const minBid = useMin(bid1, bid2, bid3);
console.log(minBid.value); // 950

// Empty array edge case
const empty = ref([]);
const minEmpty = useMin(empty);
console.log(minEmpty.value); // -Infinity (Math.min behavior)

Utility Types

/**
 * Type for functions accepting either spread args or single array arg
 */
type MaybeComputedRefArgs<T> = MaybeRefOrGetter<T>[] | [MaybeRefOrGetter<MaybeRefOrGetter<T>[]>];

/**
 * Utility to flatten and resolve MaybeComputedRefArgs to array of values
 * @param args - The arguments to flatten
 * @returns Flattened array of resolved values
 */
function toValueArgsFlat<T>(args: MaybeComputedRefArgs<T>): T[];

Advanced Usage Patterns

Combining Aggregation Functions

import { ref, computed } from "vue";
import { useSum, useAverage, useMax, useMin } from "@vueuse/math";

const dataset = ref([10, 25, 15, 30, 20]);

const sum = useSum(dataset);
const average = useAverage(dataset);
const max = useMax(dataset);
const min = useMin(dataset);

// Computed statistics
const stats = computed(() => ({
  sum: sum.value,
  average: average.value,
  max: max.value,
  min: min.value,
  range: max.value - min.value,
  count: dataset.value.length
}));

console.log(stats.value);
// { sum: 100, average: 20, max: 30, min: 10, range: 20, count: 5 }

Dynamic Data Updates

import { ref, watch } from "vue";
import { useSum, useAverage } from "@vueuse/math";

const scores = ref([85, 90, 78]);
const sum = useSum(scores);
const average = useAverage(scores);

// Watch for changes
watch([sum, average], ([newSum, newAvg]) => {
  console.log(`Total: ${newSum}, Average: ${newAvg.toFixed(2)}`);
}, { immediate: true });

// Add new scores - watchers will fire automatically
scores.value.push(92, 88);
// Logs: "Total: 433, Average: 86.60"

// Modify existing score
scores.value[0] = 95;
// Logs: "Total: 443, Average: 88.60"

Install with Tessl CLI

npx tessl i tessl/npm-vueuse--math

docs

aggregation.md

basic-math.md

generic-math.md

index.md

logical.md

projection.md

value-control.md

tile.json