Skip to content

⬅️ Back to Table of Contents

📄 useCounter

📊 Analysis Summary

Metric Count
🔧 Functions 6
📦 Imports 5
📐 Interfaces 2

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/useCounter/index.ts

📦 Imports

Name Source
MaybeRef vue
Ref vue
shallowReadonly vue
shallowRef vue
unref vue

Functions

useCounter(initialValue: MaybeRef<number>, options: UseCounterOptions): { count: any; inc: (delta?: number) => number; dec: (delta?…

Basic counter with utility functions.

Parameters:

  • initialValue any (optional): No description
  • options any: No description

See: https://vueuse.org/useCounter

Raw JSDoc
/**
 * Basic counter with utility functions.
 *
 * @see https://vueuse.org/useCounter
 * @param [initialValue]
 * @param options
 */

Calls:

  • unref (from vue)
  • shallowRef (from vue)
  • Math.max
  • Math.min
  • set
  • shallowReadonly (from vue)
Code
export function useCounter(initialValue: MaybeRef<number> = 0, options: UseCounterOptions = {}) {
  let _initialValue = unref(initialValue)
  const count = shallowRef(initialValue)

  const {
    max = Number.POSITIVE_INFINITY,
    min = Number.NEGATIVE_INFINITY,
  } = options

  const inc = (delta = 1) => count.value = Math.max(Math.min(max, count.value + delta), min)
  const dec = (delta = 1) => count.value = Math.min(Math.max(min, count.value - delta), max)
  const get = () => count.value
  const set = (val: number) => (count.value = Math.max(min, Math.min(max, val)))
  const reset = (val = _initialValue) => {
    _initialValue = val
    return set(val)
  }

  return { count: shallowReadonly(count), inc, dec, get, set, reset }
}

Internal helpers

Declared inside another function in this file.

inc(delta: number): number

Parameters:

  • delta number

Returns: number

Code
(delta = 1) => count.value = Math.max(Math.min(max, count.value + delta), min)

dec(delta: number): number

Parameters:

  • delta number

Returns: number

Code
(delta = 1) => count.value = Math.min(Math.max(min, count.value - delta), max)

get(): any

Returns: any

Code
() => count.value

set(val: number): number

Parameters:

  • val number

Returns: number

Code
(val: number) => (count.value = Math.max(min, Math.min(max, val)))

reset(val: any): number

Parameters:

  • val any

Returns: number

Calls:

  • set
Code
(val = _initialValue) => {
    _initialValue = val
    return set(val)
  }

Interfaces

UseCounterOptions

Interface Code
export interface UseCounterOptions {
  min?: number
  max?: number
}

Properties

Name Type Optional Description
min number not shown
max number not shown

UseCounterReturn

Interface Code
export interface UseCounterReturn {
  /**
   * The current value of the counter.
   */
  readonly count: Readonly<Ref<number>>
  /**
   * Increment the counter.
   *
   * @param {number} [delta=1] The number to increment.
   */
  inc: (delta?: number) => void
  /**
   * Decrement the counter.
   *
   * @param {number} [delta=1] The number to decrement.
   */
  dec: (delta?: number) => void
  /**
   * Get the current value of the counter.
   */
  get: () => number
  /**
   * Set the counter to a new value.
   *
   * @param val The new value of the counter.
   */
  set: (val: number) => void
  /**
   * Reset the counter to an initial value.
   */
  reset: (val?: number) => number
}

Properties

Name Type Optional Description
count Readonly<Ref<number>> not shown
inc (delta?: number) => void not shown
dec (delta?: number) => void not shown
get () => number not shown
set (val: number) => void not shown
reset (val?: number) => number not shown

Generated by Syntax Scribe