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

usedjs

v1.0.3

Published

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

Downloads

161

Readme

Used.js

Build Status img img

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

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

编译

$ yarn
$ npm run build

安装

  • NPM
$ npm i usedjs --save

开始

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

import usedjs from 'usedjs' // 全引用
import querystring from 'usedjs/lib/querystring' // 仅引用querystring模块

大部分的模块主要针对 lodash.js 未提供但又常用到的一些函数并且支持按需载入, (与热门库使用体验一致) 简单易用无学习成本。

API

若有不当之处,请指出;

parseURLString

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

const result = parseURLString('https://github.com/icepy/used?id=1234#1')
{
  url: 'https://github.com/icepy/used?id=1234#1',
  scheme: 'https',
  slash: '//',
  host: 'github.com',
  port: undefined,
  path: 'icepy/used',
  query: 'id=1234',
  hash: '1',
}

querystring

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

  • parse

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

const search = 'id=1234&name=你好&name=你好';
const _wu = querystring.parse(search)
console.log(_wu)
querystring.parse:  { id: '1234', name: [ '你好', '你好' ] }
  • stringify

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

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

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)
url.parse:  { id: '1234', name: [ '你好', '你好吧' ] }
url.parse id:  1234
  • format

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

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

loggers

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

  • logger 函数
  • LoggerType 常量定义了LOG ERROR WARNING INFO DIR TABLE 六个等级
const loggers = require('../lib/index');
const logger = loggers.default;
const LoggerType = loggers.LoggerType;

logger('default log');
logger({ default: 'log'});
logger(['default', 'log']);
logger([{ default: 'log', name: 'icepy'}], LoggerType.TABLE);

loggers.dir(document.querySelector('body'));
loggers.info('default log');
loggers.warning('default log');
loggers.error('default log');
loggers.table(['default', 'log']);
loggers.table({ default: 'log', name: 'icepy'});
loggers.table([{ default: 'log', name: 'icepy'}], ['name']);

env

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

  • Browser 是否在浏览器中
  • Weex 是否在Weex中
  • NodeJS 是否在Node.js中
  • IE 是否是IE
  • IE9 是否是IE9
  • Edge 是否是Edge
  • Android 是否是Android(浏览器或Weex)
  • iOS 是否是iOS(浏览器或Weex)
  • Chrome 是否是Chrome
{
  Browser: false,
  Weex: false,
  weexPlatform: false,
  IE: false,
  IE9: false,
  Edge: false,
  Android: false,
  iOS: false,
  Chrome: false,
  NodeJS: true
}

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时完整的路径和域属性都传递进来。

createUUID

创建一个 uuid:

const uuid = createUUID();
console.log('createUUID: ', uuid);
createUUID:  49bafb3b-4b3f-0aec-66fc-1b13479720a4

compareVersion

版本对比 x.y.z ,旧版本和新版本比对的逻辑是 大于或等于为 true。

const cv = compareVersion('1.0.0', '1.0.1');
console.log('oldVersion: 1.0.0 newVersion: 1.0.1', cv);

const cv1 = compareVersion('1.0.0', '1.0.0');
console.log('oldVersion: 1.0.0 newVersion: 1.0.0', cv1);

const cv2 = compareVersion('1.0.0', '0.0.9');
console.log('oldVersion: 1.0.0 newVersion: 0.0.9', cv2);
oldVersion: 1.0.0 newVersion: 1.0.1 true
oldVersion: 1.0.0 newVersion: 1.0.0 true
oldVersion: 1.0.0 newVersion: 0.0.9 false