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

vstores

v0.0.17

Published

cross-platform storage

Downloads

193

Readme

vstores

GitHub CI Coverage Status Minified Size

中文 | English

Ts 类型友好、支持过期时间的、功能强大的、跨平台存储器。

安装

首先确保本地安装了 node 和 npm 环境,在终端环境下执行

$ npm install vstores

or

$ yarn add vstores

使用

基础使用(在 js 或者 ts 中)

import vstores from 'vstores';

type DataType = {
  test: number;
  bar: number;
};
const store = vstores.create<DataType>();
// key 只能是 "test" | “bar”, 有严格的类型提示
store.set('test', 1);

// 类型提示为 number
store.get('test');

设置过期时间

import vstores from 'vstores'
/** 全局配置存值的过期时间*/
const store = vstores.create({
    //可以指定特定时间,如:2024-12-24 14:00过期
    expireAt: "2024-12-24 14:00",
    //可以从当前日期起设置过期天数秒数等,如 60秒后过期或者3天后过期
    expire: 60,  //or [3, "day"];
    //格式化key值,如原始key为 name, 实际存储值为before-name-after
    formatKey: (v)=> `before-${v}-after`,
    //对于key获取值为undefined时,返回对应的默认值
    // defaultValues: {
    //   [key]: value
    // }

})
//或者在设置单个key值设置值, 优先顺序为首先是:单个> 全局, 然后再是 expireAt > expire
store.set("test", 1, {
      //2024-12-24 14:00过期
    expireAt: "2024-12-24 14:00";
    //设置成功key后开始, 36000毫秒后过期 或者 3天后过期
    // expire: 36000 or ;
    //只能读取一次,读取一次后失效自动删除
    //once: true
})
store.get("test")

API

创建实例

  • 直接导出的 vsotes 是一个实例对象
  • 也可以通过vstores.create(config)创建新的实例

实例上的方法

get(key: string): any

获取存储值

const data = vstores.get('key');

set(key: string, value: any, config?: InstanceConfig): vstores

设置存储值及配置项目

vstores.set('key', data);

// or

vstores.set('key', data, {
  expireAt: '2024-02-04 12:24',
  expirt: [2, 'day'] || 3600, //3600 单位是秒
  once: true,
});

del(key: string): void

删除 key 对应的存储值

vstores.del('key');

clear(): void

删除所有存储值

vstores.clear();

配置项

全局配置

import vstores from 'vstores'
/** 全局配置存值的过期时间*/
const store = vstores.create({
    //指定具体的过期时间
    expireAt: "2024-12-24 14:00";
    //指定过期时间距离现在还有多久
    expire: 60  //or [3, "day"];
    //格式化key值,如原始key为 name, 实际存储值为before-name-after
    formatKey: (v)=> `before-${v}-after`,
    //跨平台的存储器
    adapter: webAdapter,
    //异常处理器
    errorHandler: (err)=> console.log(err),
    //对于key获取值为undefined时,返回对应的默认值
    // defaultValues: {
    //   [key]: value
    // }
})

set 方法的配置

vstores.set('key', data, {
  //指定具体的过期时间
  expireAt: '2024-02-04 12:24',
  //指定过期时间距离现在还有多久
  expirt: [2, 'day'] || 3600, //3600 单位是秒
  //读取一次后会删除这个存储
  once: true,
});

跨平台

支持 web 端、微信小程序、支付宝小程序,根据不同平台自动适配,无需额外配置

跨平台适配器

内置了 4 种适配器适用于不同平台,可以手动指定:

web 端:webAdapter(使用 localStorage), sessionAdapter(使用 sessionStorage)

微信小程序:wxAdapter

支付宝小程序: aliAdapter

手动指定适配器方式:

import vstores, { sessionAdapter } from 'vstores';
const store = vstores.create({
  adapter: sessionAdapter,
});

版权

MIT © jaluik