Loading

一行代码的工具方法

获取字符串中的字符数

export const characterCount = (str, char) => str.split(char).length - 1

检查对象是否为空

export const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object

等待一定时间后执行

export const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));

获取两个日期之间的天差

export const daysBetween = (date1, date2) => Math.ceil(Math.abs(date1 - date2) / (1000 * 60 * 60 * 24))

检查设备上的触摸支持

export const touchSupported = () => ('ontouchstart' in window || DocumentTouch && document instanceof DocumentTouch)

在元素后插入一串 HTML

export const insertHTMLAfter = (html, el) => el.insertAdjacentHTML('afterend', html)

打乱数组

export const shuffle = arr => arr.sort(() => 0.5 - Math.random())

在网页上获取选定的文本

export const getSelectedText = () => window.getSelection().toString()

获取一个随机布尔值

export const getRandomBoolean = () => Math.random() >= 0.5

计算数组的平均值

export const average = (arr) => arr.reduce((a, b) => a + b) / arr.length
posted @ 2022-02-28 23:52  Frank-Link  阅读(77)  评论(0编辑  收藏  举报