Open gracelinmumu opened 1 year ago
// 你的答案 function useCounter(initialValue = 0, options: UseCounterOptions = {}) { const count = ref(initialValue); const min = options.hasOwnProperty('min') ? options.min : -Infinity; const max = options.hasOwnProperty('max') ? options.max : -Infinity; const getCount = (newValue: number) => { return Math.max(Math.min(max, newValue), min); } const inc = () => { count.value = getCount(count.value + 1); } const dec = () => { count.value = getCount(count.value - 1); } const reset = () => { count.value = initialValue; } return { inc, dec, count, reset, } }
const min = options.hasOwnProperty('min') ? options.min : -Infinity; - const max = options.hasOwnProperty('max') ? options.max : -Infinity; + const max = options.hasOwnProperty('max') ? options.max : Infinity;