1、获取浏览器Cookie的值
const cookie = name => ; ${document.cookie}
.split(; ${name}=
).pop().split(';').shift();
示例:cookie('token');
2、检查日期是否合法
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
示例:isDateValid("December 11, 2022 10:24:00");
3、英文字符串首字母大写
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
示例:capitalize("follow for more")
4、数组去重
const removeDuplicates = (arr) => [...new Set(arr)];
示例:removeDuplicates([1, 2, 3, 3, 4, 4, 5, 5, 6]);
5、时间处理
const timeFromDate = date => date.toTimeString().slice(0, 8);
示例:timeFromDate(new Date(2021, 0, 10, 17, 30, 0));
6、回顶部
const goToTop = () => window.scrollTo(0, 0);
goToTop();
7、字符串反向
const reverse = str => str.split('').reverse().join('');
示例:reverse('hello world');
8、奇偶数检测
const isEven = num => num % 2 === 0;
console.log(isEven(2)); // ture
9、计算两个日期之间相差的天数
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22")) // 366
10、复制文本到剪切板
const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");
网友评论