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

@shencom/utils-storage

v1.5.0

Published

Storage工具

Downloads

59

Readme

@shencom/utils-storage

Storage 工具,兼容小程序,Uniapp,Browser

Install

pnpm add @shencom/utils

# or

pnpm add @shencom/utils-storage

Basic Usage

const storage = new ScStorageBase(StorageOption);

Option

interface StorageOption {
  scid: string;
  get: (key: string) => string | null;
  set: (key: string, value: string) => void;
  remove: (key: string) => void;
  clear: () => void;
  keys: () => string[];
}

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | ------ | ---------------------- | -------- | ------ | ------ | | scid | 唯一标识符 | String | - | - | | get | 获取本地存储数据 | Function | - | - | | set | 设置本地存储数据 | Function | - | - | | remove | 移除指定本地存储数据 | Function | - | - | | clear | 清空本地存储数据 | Function | - | - | | keys | 获取本地存储所有的 key | Function | - | - |

Methods

get

  • 说明: 获取本地存储数据
  • 类型: get<T = any>(key: string): T | null
  • 示例:
    const data = storage.get(key);

set

  • 说明: 设置本地存储数据
  • 类型: set(key: string, data: any, time?: number): void
  • 参数:
    • key: 存储的 key
    • data: 存储的值
    • time: 存储时间(单位为分钟),默认: 永久
  • 示例:
    storage.set(key, data, 10); // 10分钟
    storage.set(key, data); // 永久

remove

  • 说明: 移除指定本地存储数据
  • 类型: remove(key: string): void
  • 参数:
    • key: 存储的 key
  • 示例:
    storage.remove(key);

clear

  • 说明: 清空本地存储数据,默认保留 lasting 前缀字段数据
  • 类型: clear(isAll?: boolean): void
  • 参数:
    • isAll: 是否全部清除,默认: false
  • 示例:
    storage.clear(); // 保留 lasting 前缀字段数据
    storage.clear(true); // 全部清除

keys

  • 说明: 获取本地存储所有的 key
  • 类型: keys(): string[]
  • 示例:
    const keys = storage.keys();

getUser

  • 说明: 获取 user_ 开头的数据
  • 类型: getUser<T = any>(key: string): T | null
  • 参数:
    • key: 存储的 key
  • 示例:
    const data = storage.getUser(key);

setUser

  • 说明: 设置 user_ 开头的数据
  • 类型: setUser(key: string, data: any, time?: number): void
  • 参数:
    • key: 存储的 key
    • data: 存储的值
    • time: 存储时间(单位为分钟),默认: 永久
  • 示例:
    storage.setUser(key, data, 10); // 10分钟
    storage.setUser(key, data); // 永久

removeUser

  • 说明: 移除以 user_ 开头的数据
  • 类型: removeUser(key: string): void
  • 参数:
    • key: 存储的 key
  • 示例:
    storage.removeUser(key);

clearUser

  • 说明: 清除所有以 user_ 开头的数据
  • 类型: clearUser(): void
  • 示例:
    storage.clearUser();

getData

  • 说明: 获取 data_ 开头的数据
  • 类型: getData<T = any>(key: string): T | null
  • 参数:
    • key: 存储的 key
  • 示例:
    const data = storage.getData(key);

setData

  • 说明: 设置 data_ 开头的数据
  • 类型: setData(key: string, data: any, time?: number): void
  • 参数:
    • key: 存储的 key
    • data: 存储的值
    • time: 存储时间(单位为分钟),默认: 永久
  • 示例:
    storage.setData(key, data, 10); // 10分钟
    storage.setData(key, data); // 永久

removeData

  • 说明: 移除以 data_ 开头的数据
  • 类型: removeData(key: string): void
  • 参数:
    • key: 存储的 key
  • 示例:
    storage.removeData(key);

clearData

  • 说明: 清除所有以 data_ 开头的数据
  • 类型: clearData(): void
  • 示例:
    storage.clearData();

getLasting

  • 说明: 获取 lasting_ 开头的数据
  • 类型: getLasting<T = any>(key: string): T | null
  • 参数:
    • key: 存储的 key
  • 示例:
    const data = storage.getLasting(key);

setLasting

  • 说明: 设置 lasting_ 开头的数据,永久存储,使用 clear 方法默认是不清空这个数据
  • 类型: setLasting(key: string, data: any): void
  • 参数:
    • key: 存储的 key
    • data: 存储的值
  • 示例:
    storage.setLasting(key, data);

removeLasting

  • 说明: 移除以 lasting_ 开头的数据
  • 类型: removeLasting(key: string): void
  • 参数:
    • key: 存储的 key
  • 示例:
    storage.removeLasting(key);

clearLasting

  • 说明: 清除所有以 lasting_ 开头的数据
  • 类型: clearLasting(): void
  • 示例:
    storage.clearLasting();

Example

Browser

import { ScStorageBase } from '@shencom/utils';
// import ScStorageBase from '@shencom/utils-storage';

const scid = 'xxx';

const Storages = new ScStorageBase({
  scid,
  get: window.localStorage.getItem.bind(window.localStorage),
  set: window.localStorage.setItem.bind(window.localStorage),
  remove: window.localStorage.removeItem.bind(window.localStorage),
  clear: window.localStorage.clear.bind(window.localStorage),
  keys: () => Object.keys(window.localStorage),
});

export default Storages;

UniApp

import { ScStorageBase } from '@shencom/utils';
// import ScStorageBase from '@shencom/utils-storage';

const scid = 'xxx';

const Storages = new ScStorageBase({
  scid,
  get: uni.getStorageSync,
  set: uni.setStorageSync,
  clear: uni.clearStorageSync,
  remove: uni.removeStorageSync,
  keys: () => uni.getStorageInfoSync().keys,
});

export default Storages;