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

commonstorage

v1.0.2

Published

A Universal LocalStorage

Downloads

8

Readme

commonstorage.js

对本地storage的通用处理。支持 命名空间、过期时间、监听当前页面localStorage发生变化.

Installation

$ npm install commonstorage --save

命名空间

可通过传入命名空间,来防止localStorage被污染

import commonStorage from 'commonstorage';
const namespace = 'my'; // 可传入命名空间
const storage = new commonStorage(namespace);
storage.setItem('name', '张三'); // 此时存储的key为 my-name
storage.clear(); // 此时仅会清空 my- 开头的所有localStorage, 无命名空间则清空所有

过期时间

支持传入过期时间, 来保证localStorage的有效性。time 支持 m分钟 h小时 d天的时间格式, 如果直接传入数字格式 默认为 h, 不传或者格式不对默认永久。

import commonStorage from 'commonstorage';
const namespace = 'my'; // 可传入命名空间
const storage = new commonStorage(namespace);
storage.setItem('name', '张三'); // 过期时间永久
storage.setItem('name', '李四', 6); // 过期时间 6h
storage.setItem('name', '王五', '3d'); // 过期时间3天
storage.setItem('name', '老六', '3t'); // 格式错误过期时间永久

监听当前页面Storage变化

import commonStorage from 'commonstorage';
const namespace = 'my'; // 可传入命名空间
const storage = new commonStorage(namespace);
// 监听变化, 可通过e.detail获取 key val time
document.addEventListener('changeStorage', (e)=> {
    console.log("发生变化", e.detail);
})

Quick Start

import commonStorage from 'commonstorage';
const namespace = 'my'; // 可传入命名空间
const storage = new commonStorage(namespace);
storage.setItem('name', '张三', '6d')
storage.setItem('user', JSON.stringify({age: 18}))
storage.setItem('path', {path: 'xxxxx'}, '3m')
storage.getItem('name'); // '张三'
storage.getItem('user'); // '{"age":18}'
storage.getItem('path'); // {path: 'xxxxx'}
// storage.clear(); // 清空 有命名空间时清空指定的缓存 没有的时候清空所有
// storage.removeItem('user')