All files index.ts

100% Statements 40/40
75% Branches 3/4
100% Functions 3/3
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x
import { useState } from 'react'
import { isNumber } from 'asura-eye'
 
export type Options = {
	min?: number,
	max?: number
}
 
function getTargetValue(val: number, options: Options = {}) {
	const { min = 0, max = Infinity } = options;
	let target = val;
	if (isNumber(max)) {
		target = Math.min(max, target)
	}
	if (isNumber(min)) {
		target = Math.max(min, target)
	}
	return target;
}
 
/**
 * @title useCount 
 * @description 计数
 * @param initialState {number=0}
 * @param options {min?:number,max?:number}
 * @returns [number, (value?:number)=>void]
 */
export function useCount(initialState = 0, options: Options = {})
	: readonly [number, (value?: number) => void] {
 
	const [state, setStateTemp] = useState<number>(getTargetValue(initialState, options))
 
	const setState = (value: number = state) => {
		const result = getTargetValue(value + 1, options)
		if (result === state) return;
		setStateTemp(result)
	}
 
	return [state, setState] as const
}