1.0.0 • Published 3 years ago

wsg-until v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

安装

npm install wsg-until

导入

const wsg = require('wsg-until')

格式化时间

// 使用带中文的输出格式
const aaa = wsg.nowTimeFormatChinese(new Date())
console.log(aaa);
// 现在是2022年09月22日星期四16时20分51秒

// 默认正常字符的输入格式
console.log(wsg.nowTimeFormat(new Date()))
// 2022-09-22 16:20:51

// 获得一个时间距离当前时间的距离 以及总秒数 返回对象 可自由组合
console.log(wsg.timeGap('2022-09-22 18:30:00'))
{
  times: 7748,
  d: '00',
  h: '02',
  m: '09',
  s: '08',
  demoStr1: '默念7748下',
  demoStr2: '00天02小时09分08秒'
}

转义HTML的对应符号

// html转义成 一般字符串 使得浏览器不渲染
const str3 = '<h1>文本一<span class="solo">文本二</span></h1>'
console.log(wsg.htmlEscape(str3))

// 带转义符号的字符串 转成HTML格式
const str4 = '&lt;h1&gt;文本一&lt;span class="solo"&gt;文本二&lt;/span&gt;&lt;/h1&gt;'
console.log(wsg.escapeHtml(str4))

节流和防抖的工具函数

// 节流 在200ms 内 xx 函数不会被重复执行
function xxx(){

}
wsg.throttle(xxx,200)

// 防抖 200ms 后在执行 xxx 检测到重复执行就往后拖迟
function xxx(){

}
wsg.debounce(xxx,200)

数组去重的工具函数

// 1方法内部通过 filter+indexOf 去重
let shuzu = [2,0,2,2,9,22,18,30,0,0]
console.log(wsg.Urepeat1(shuzu))
// 2方法内部通过 set 数据解构去重
let shuzu = [2,0,2,2,9,22,18,30,0,0]
console.log(wsg.Urepeat2(shuzu))