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

@just4/storage

v2.0.0

Published

Just for making localStorage and sessionStorage easier to use.

Downloads

5

Readme

@just4/storage

对 Web 存储对象(localStorage、sessionStorage)进行封装,使其接口更易用。封装后提供以下接口:

| 接口 | 说明 | | --- | --- | | get(key) | 即存储对象的 getItem 方法 | | getAsJSON(key) | 对 get 的结果执行 JSON.parse 后返回 | | set(key, value) | 调用存储对象的 setItem 方法时做了 try...catch 处理 | | setAsJSON(key, value, options) | 对 value 执行 JSON.stringify 后再调用 set | | remove(key) | 即存储对象的 removeItem 方法 |

安装

npm i @just4/storage

调用示例

import { local, session } from '@just4/storage';
// local 即 localStorage 的封装
// session 即 sessionStorage 的封装
// 这两个对象没有任何插件(插件的说明见下文)

const user = { name: 'Tom', pet: 'cat' };

session.setAsJSON('user', user);
typeof session.get('user'); // 'string'
typeof session.getAsJSON('user'); // 'object'

local.setAsJSON('user', user);
local.getAsJSON('user');
typeof local.get('user'); // 'string'
typeof local.getAsJSON('user'); // 'object'

注意

当存储空间已满,或浏览器开启了隐私模式(无痕模式)时,set 操作可能无法往存储空间写入内容,但该方法不会抛出异常。开发者需要在业务代码中要做好数据无法被存储的预案。

插件机制

本程序包在 1.1.0 这个版本中引入了插件机制。该机制的实质就是在设置存储项的值时,以更多的关联存储项去配置存储属性。

举个例子:在设置 token 存储项的值为 abcde 时,以另外一个名叫 __expires_of_token 的关联存储项来存储过期时间。在获取 token 的值时,如果当前时间已经超过 __expires_of_token 所存储的过期时间,则返回 null,否则返回 abcde

所有的插件都继承于 StorageWrap,并按需覆盖 _doGetting_doSetting 以及 _doRemoving 三个方法。

注意,本程序包提供的 localsession 两个 StorageWrap 对象都不带有任何插件。

过期时间插件

本程序包提供了过期时间插件,调用示例如下:

const storage = new StorageWrap('local', {
  plugins: [
    new ExpiresPlugin('local')
  ]
});
storage.set('test-01', '1', { expires: '1 month' });

相关文档

Changelog

v2.0.0

  • StorageWrap 的构造函数参数变更为 storageTypeoptions,不兼容 v1.x 的调用方式。
  • StorageWrap 新增 available 属性表示当前存储是否可用。
  • StorageWrapset 方法不再返回操作是否成功。
  • StorageWrap 进行了重构使其能支持插件机制。
  • 新增过期时间插件 ExpiresPlugin,可以指定存储项的过期时间。