/** |
|
* @description 金额运算 |
|
* @param { '+' | '-' | '*' | '/' } method 计算方法 |
|
* @param { string | number} args 需要参与计算的数值或字符串 |
|
* @return { number } 计算后的值 |
|
*/ |
|
const operation = (method, ...args) => { |
|
const arr = args.map((item) => parseInt((item *= 100))); |
|
let num = 0; |
|
switch (method) { |
|
case '+': |
|
num = arr.reduce((total, item) => total + item) / 100; |
|
break; |
|
case '-': |
|
num = arr.reduce((total, item) => total - item) / 100; |
|
break; |
|
case '*': |
|
num = arr.reduce((total, item) => (total * item) / 100, 1); |
|
break; |
|
case '/': |
|
num = arr.reduce((total, item) => (total * 100) / item) / 100; |
|
break; |
|
} |
|
return num; |
|
}; |
|
|
|
/** |
|
* @description 防抖 |
|
* @param { function } handler 触发的函数 |
|
* @param { number } delay 时间 |
|
*/ |
|
const debounce = (handler, delay) => { |
|
let timer; |
|
return function () { |
|
let _this = this; |
|
clearTimeout(timer); |
|
timer = setTimeout(function () { |
|
handler.apply(_this, arguments); |
|
}, delay); |
|
}; |
|
}; |