@ipoa/utils
v0.1.9
Published
可以在浏览器新标签页中打开它。或者你也可以创建一个 .html 文件,然后通过如下方式引入 utils
Downloads
4
Readme
@ipoa/utils
可以在浏览器新标签页中打开它。或者你也可以创建一个 .html 文件,然后通过如下方式引入 utils
<script src="https://cdn.jsdelivr.net/npm/@ipoa/utils@latest/dist/utils.global.legacy.js"></script>
使用 npm 安装
npm i @ipoa/utils --save
Example Usage
Date.prototype.format()
format()
方法根据提供格式,格式化时间
let date = new Date()
console.log(date.format('yyyy-MM-dd hh:mm:ss'))
// expected output: 2022-01-07 21:48:47
Date.prototype.diffTime()
| String.prototype.diffTime()
| Number.prototype.diffTime()
diffTime()
方法根据字符串或数字 返回time1 和time2 之间相差的毫秒数 (time1 - time2)
let date1 = new Date();
console.log(date1.diffTime('1641556498000'))
// expected output: 8027643
console.log(date1.diffTime('1641556498000', true))
// expected output: -8027643
console.log(date1.diffTime(1641556498000))
// expected output: 8027643
console.log(date1.diffTime('2022-01-07 20:44:58', true))
// expected output: -8027643
console.log(date1.diffTime('2022-01-07 20:44:58', false))
// expected output: 8027643
let date2 = '1641571546140';
console.log(date2.diffTime('1641559498000'))
// expected output: 12048140
console.log(date2.diffTime('1641559498000', true))
// expected output: -12048140
console.log(date2.diffTime(1641559498000, false))
// expected output: 12048140
console.log(date2.diffTime('2022-01-07 20:44:58', true))
// expected output: -12048140
console.log(date2.diffTime('2022-01-07 20:44:58'))
// expected output: 12048140
let date3 = '2022-01-08 00:05:46'
console.log(date3.diffTime('2022-01-07 20:44:58'))
// expected output: 12048000
String.prototype.toDate()
| Number.prototype.toDate()
toDate()
方法根据string
or number
返回JavaScript Date
实例
let date1 = '2022-01-07 20:44:58'
console.log(date1.toDate())
// expected output: Fri Jan 07 2022 20:44:58 GMT+0800 (中国标准时间)
let date2 = '1641556498000'
console.log(date2.toDate())
// expected output: Fri Jan 07 2022 20:44:58 GMT+0800 (中国标准时间)
let date3 = 1641556498000
console.log(date3.toDate())
// expected output: Fri Jan 07 2022 20:44:58 GMT+0800 (中国标准时间)
utils.appendWebviewSDK()
appendWebviewSDK() 方法根据不同的小程序场场景插入相应的jsSDK的引用,也可以添加参数,扩展适应不同浏览器。
// 微信小程序
utils.appendWebviewSDK()
// <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
// 修改微信小程序引用 url
// 参数:pattern 用于匹配 navigator.userAgent 的返回值
// 参数: target 需要引用的 url
utils.appendWebviewSDK({
wechat: {
pattern: /miniProgram|MicroMessenger/i, target: 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
}
})
// 扩展适应不同浏览器支持
// 引用 uni.webview.js
utils.appendWebviewSDK({
uni: {
target: 'https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js'
}
})
utils.URL
new URL() 方法支持IE浏览器的url转换方法。 使用方法查看 Web APIs URL()
const urlStr = 'https://root:passwd@localhost:8080/pages/article/details/107051177?_t=12345678901&token=wga'
const url1 = new utils.URL(urlStr)
console.log(url1.protocol)
// expected output: https
console.log(url1.searchParams.get('_t'))
// expected output: 12345678901
url1.searchParams.set('_t', '098765431')
console.log(url1.searchParams.get('_t'))
// expected output: 098765431
// utils.url('')
const url2 = utils.url(urlStr)
console.log(url2.protocol)
// expected output: https
console.log(url2.searchParams.get('_t'))
// expected output: 12345678901
url2.searchParams.set('_t', '098765431')
console.log(url2.searchParams.get('_t'))
// expected output: 098765431
url2.searchParams.delete('token')
console.log(url2.href)
// expected output: https://root:passwd@localhost:8080/pages/article/details/107051177?_t=098765431
url2.searchParams.append('event', 'load')
console.log(url2.href)
// expected output: https://root:passwd@localhost:8080/pages/article/details/107051177?_t=098765431&event=load
const searchParams1 = new URLSearchParams({ foo: 1 })