本次文章为一些JS技巧,方法,可用于公司开发
// 公共方法
// 获取字符串的字符数
const characterCount = (str, char) => str.split(char).length - 1
// 检查对象是否为空
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object
// 等待一定时间后执行
const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
// 获取两个日期之间的天差
const daysBetween = (data1, data2) => Math.ceil(Math.abs(data1 - date2) / (1000 * 60 * 60 * 24))
// 重定向到另一个URL ( 没权限 , 将其重定向 )
const redirect = url => location.href = url // redirect('wwww.baidu.com')
// 检查设备上的触摸支持
const touchSupported = () => ('ontouchstart' in window || DocumentTouch && document instanceof DocumentTouch) // 检查文档是否支持 touchstart 事件
// console.log(touchSupported()); // 返回 true || false
// 在元素后面插入一串 html
const insertHTMLAfter = (html, el) => el.insertAdjacentHTML('afterend', html)
// 计算数组的平均值
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length
// console.log(average([1, 2, 3, 4, 5]));
// 获取随机一个布尔值
const getRandomBoolean = () => Math.random() >= 0.5
// console.log(getRandomBoolean()); // 返回 true
// 打乱数组
const shuffle = arr => arr.sort(() => 0.5 - Math.random())
// console.log(shuffle([1, 2, 3, 4, 5]));
// 万亿数字转大写
function digitUppercase(n) {
const fraction = ["角", "分"];
const digit = [
"零",
"壹",
"贰",
"叁",
"肆",
"伍",
"陆",
"柒",
"捌",
"玖",
];
const unit = [
["元", "万", "亿"],
["", "拾", "佰", "仟"],
];
const head = n < 0 ? "欠" : "";
n = Math.abs(n);
let result = "";
for (let i = 0; i < fraction.length; i++) {
result +=
(digit[Math.floor(n * 10 * Math.pow(10, i)) %!](MISSING) + fraction[i])
.replace(/零./, "");
}
result = result || "整";
n = Math.floor(n);
for (let i = 0; i < unit[0].length && n > 0; i++) {
let num = 0;
let temp = "";
for (let j = 0; j < unit[1].length && n > 0; j++) {
num = n %!;(MISSING)
temp = digit[num] + unit[1][j] + temp;
n = Math.floor(n / 10);
}
result = temp.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + result;
}
return head + result.replace(/(零.)*零元/, "元").replace(/(零.)+/g, "零").replace(/^整$/, "零元整");
}
//console.log(digitUppercase(123456789.32));