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

browser-used

v0.3.4

Published

Used.js是一个轻量级小巧的可运行在浏览器中的JavaScript函数库

Downloads

19

Readme

Build Status

Used.js是一个轻量级小巧的可运行在浏览器中的JavaScript函数库,除个别外大部分的使用方式与Node.js API保持一致。

  • 更轻量小巧
  • 可按需载入
  • 丰富的API
  • 提高开发效率
  • 全浏览器兼容

编译

$ yarn
$ npm run build

安装

  • NPM
$ npm i browser-used --save

开始

文档说明以NPM的方式来使用进行

import Used from 'browser-used' // 全引用
import Time from 'browser-used/lib/time' // 仅引用Time模块
import querystring from 'browser-used/lib/querystring' // 仅引用querystring模块

大部分的模块主要针对lodash.js未提供但又常用到的一些函数,并且支持按需载入,个别模块如Time的使用和 Moment.js 的API,用法相同, (保证和热门库使用体验一致) 无学习成本。

API

若有不当之处,请指出;

Time

初始化一个Time支持多种传入参数的方式:

  • 无参数,将得到一个包含当前时间和日期的time对象
  • 标准的ISO 8601时间字符串,如: time('2018-05-04')
  • Unix 时间戳,如: time(1525793309344)
  • Date对象,如: time(new Date())

Time Property

  • y
  • m
  • d
  • w 星期几
  • h 小时
  • hm 分钟
  • hms
  • hmss 毫秒

Time Method

  • isValid return Boolean

isValid 检查当前的Time对象是否是一个有效的时间对象。

time().isValid()
  • clone return Time Object

clone 返回一个包含当前对象的拷贝

time().clone()
  • unix return number

unix 返回Unix时间戳(秒)

  • valueOf return number

valueOf 返回Unix时间戳(毫秒)

  • format return string

format 格式化日期

time().format('YYYY-MM')
time().format('hh:mm:ss')

转换格式如下:

| Format | Output | Description | | ------ | ------ | ----------- | | YYYY | 2018 | 四位数的年份 | | MM | 01-12 | 月份,数字前面加上0 | | DD | 01-31 | 月份里的一天,数字前面加上0 | | hh | 00-23 | 小时,数字前面加上0 | | mm | 00-59 | 分钟,数字前面加上0 | | ss | 00-59 | 秒,数字前面加上0 | | a | 'am' 'pm' | 12时制转换日期格式小写 | | A | 'AM' 'PM' | 12时制转换日期格式大写 |

  • isBefore return Boolean

isBefore 检查一个Time对象是否在另一个Time对象的时间之前

time().isBefore(time())
  • isAfter return Boolean

isAfter 检查一个Time对象是否在另一个Time对象的时间之后

time().isAfter(time())
  • isSame return Boolean

isSame 检查一个Time对象是否与另外一个Time对象时间相同

  • isLeapYear return Boolean

isLeapYear 判断闰年

  • daysInMonth return number

daysInMonth 返回某年某月有多少天

  • add return new Time()

add 提供了操作时间的方式

const M = t.add(2019,'Y').format()
console.log(M);

单位格式:

| 单位 | Output | | ------ | ------ | | Y | 年 | | M | 月 | | w | 周 | | d | 天 | | h | 小时 | | m | 分钟 |

url

支持解析和format一个符合URL规则的URL

parse

将一个URL的查询字符串序列化成JSON对象,并且支持获取特定key的value。

const websiteUrl = 'https://github.com/icepy?id=1234&name=你好&name=你好吧'
const _query = url.parse(websiteUrl)
console.log(_query)
const id = url.parse(websiteUrl, 'id')
console.log(id)

format

将一组JSON对象反序列化成URL字符串

const _websiteUrl = url.format('https://github.com/icepy',{
  id: 1234,
  name: '你好'
})
console.log(_websiteUrl)

querystring

处理查询字符串解析和反序列化成字符串,所有的value支持编码解码

parse

使用parse将一个符合标准的URL查询字符串,序列化成JSON对象

const search = 'id=1234&name=你好&name=你好';
const _wu = querystring.parse(search)
console.log(_wu)

stringify

使用stringify将一组JSON对象,反序列化成URL查询字符串

const query = {
  id: 1234,
  name: ['你好', '你好']
}
const wu = querystring.stringify(query)
console.log(wu)

log

良好的日志系统可以在排错方面给予效率,log提供了良好的区分以及格式化输出。

  const logger = log.log;
  const LogType = log.LogType;

  logger(['123456'])
  logger(['error'], LogType.ERROR)
  logger(['waring'], LogType.WARNING)
  logger(['info'], LogType.INFO)
  • log 函数
  • LogType 常量定义了LOG ERROR WARNING INFO 四个等级

Cookie

创建一个cookie应用于整个网站:

cookie.set('name', 'icepy')

创建一个从现在起7天过期的cookie应用于整个网站:

cookie.set('name', 'icepy', { expires:7 })

获取一个key=name的cookie:

cookie.get('name')

获取所有的cookie:

cookie.get()

删除一个key=name的cookie:

cookie.remove('name')

⚠️注意:删除不存在的cookie不会引发任何异常,也不会有返回值,在删除cookie时最正确的处理方式是将设置cookie时完整的路径和域属性都传递进来。

env

用于程序本身判断自己所运行的环境,主要支持BrowserNode.jsWeex,全部的常量返回一个Boolean

  • inBrowser 是否在浏览器中
  • inWeex 是否在Weex中
  • inNodeJS 是否在Node.js中
  • isIE 是否是IE
  • isIE9 是否是IE9
  • isEdge 是否是Edge
  • isAndroid 是否是Android(浏览器或Weex)
  • isiOS 是否是iOS(浏览器或Weex)
  • isChrome 是否是Chrome

parseUrlToLocation

用于解析字符串URL,如:https://github.com/icepy?id=1234/#hash

{
  "url":"https://github.com/icepy?id=1234/#hash",
  "scheme":"https",
  "slash":"//",
  "host":"github.com",
  "path":"icepy",
  "query":"id=1234/",
  "hash":"hash"
}