npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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 })