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

url-storage

v0.2.3

Published

Store data in `window.location`.

Downloads

3

Readme

url-storage

使用 URL 参数来存取数据,用来解决分享 URL 的需求。

NPM

完全采用 TypeScript 开发,并且有 100% 的测试覆盖率。

urlStorage 实现了 Storage 接口,意味着它的 API 和 localStorage 以及 sessionStorage 是一致的。

安装与引入

使用 npm:

npm install url-storage

使用 yarn:

yarn add url-storage

引入:

import urlStorage from 'url-storage'

获取参数

假设当前 URL 为 http://example.com/?id=1&arr[0]=apple&arr[1]=banana

urlStorage.getItem('id') // "1"
urlStorage.getItem('fruits') // ["apple", "banana"]
// 或者直接这么写:
urlStorage.id // "1"
urlStorage.arr // ["apple", "banana"]

设置参数

urlStorage.setItem('id', '2')
urlStorage.setItem('arr', ['cherry', 'durian'])
// 或者直接这么写:
urlStorage.id = '2'
urlStorage.arr = ['cherry', 'durian']
// URL 现在为 `http://example.com/?id=2&arr[0]=cherry&arr[1]=durian`

删除参数

urlStorage.removeItem('arr')
// 或者直接这么写:
delete urlStorage.arr
// URL 现在为 `http://example.com/?id=2

清空参数

urlStorage.clear()
// URL 现在为 `http://example.com/

遍历参数

// 假设 URL 仍然为 `http://example.com/?id=1&arr[0]=apple&arr[1]=banana`。
for (let i = 0, n = urlStorage.length; i < n; i++) {
  console.log(urlStorage.key(i))
}
// "id"
// "arr"

以上便是所有的 API。

注意事项

值得注意的是,当使用简便语法来设置或者删除参数时,参数名不可为 urlStorage 上已存在的属性,请使用 setItemremoveItem 代替。

这种用法会抛出运行时错误。如果使用 TypeScript 的话,会在编译时给出提示。

urlStorage.key = 'zxcvbn'
//         ^^^
//         [ts] 无法分配到“key”,因为它是常数或只读属性。
//
// Error: Can't set `key` directly, use `urlStorage.setItem('key', <value>)` instead

delete urlStorage.length
//     ^^^^^^^^^^^^^^^^^
//     [ts] 删除运算符的操作数不能是只读属性。
//
// Error: Can't delete `length` directly, use `urlStorage.removeItem('length')` instead

自定义序列化方法

本项目默认采用 qs 包来实现参数序列化和反序列化功能,你也可以使用自定义方法来覆盖。默认的实现为:

import qs from 'qs'
import { Core } from 'url-storage'

Core.parse = search => {
  return qs.parse(search, { ignoreQueryPrefix: true })
}

Core.stringify = parsed => {
  const search = qs.stringify(parsed)
  return search ? `?${search}` : ''
}