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

@liaotai/light-storage

v0.0.3

Published

storage tool

Downloads

1

Readme

storage

Usage

Usage 1

import { LightStorage } from '@liaotai/light-storage';

interface User{
    name: string,
    id: number,
}

declare const user:User

const localLightStorage = new LightStorage(localStorage)

localLightStorage.set<User>('user', user);

localLightStorage.get<User>('user')

localLightStorage.remove<User>('user');

localLightStorage.clear();

const listener = (oldValue: User | null, newValue: User | null) => {
    // do something
}
// 监听数据
localLightStorage.listen<User>('user', listener);

// 取消监听
localLightStorage.unListen<User>('user', listener);

localLightStorage.size();

localLightStorage.keys();

localLightStorage.eachKey((key: string) => {
    // do something
});

localLightStorage.each((key: string, value: any) => {
    // do something
});

localLightStorage.getAll<Record<string, any>>();

localLightStorage.setAll<Record<string, any>>({});

Usage 2

import { LightStorage, createGenerator } from '@liaotai/light-storage';

interface User{
    name: string,
    id: number,
}
declare const user:User

const localLightStorage = new LightStorage(localStorage)

const createStorage = createGenerator(localLightStorage)

const userStorage = createStorage<User>('user');

userStorage.set(user)

userStorage.get()

userStorage.remove();

const listener = (oldValue: User | null, newValue: User | null) => {
    // do something
}

userStorage.listen(listener);

userStorage.unListen(listener);

Usage3

import { LightStorage, createFunction } from @liaotai/light-storage';

interface User{
    name: string,
    id: number,
}
declare const user:User

const localLightStorage = new LightStorage(localStorage)

const storage = createFunction(localLightStorage)

// set
storage<User>('user', user)

// get
storage<User>('user')

//remove
storage<User>('user',null)

// getAll
storage<Record<string, any>>()

// setAll
storage<Record<string, any>>({})

//clear
storage<Record<string, any>>(null)

// each
storage((key: string,value: any) => {
    // do something
})

API

LightStorage

type Listener<T> = (oldVal: T | null, newValue: T | null) => void;

export declare class LightStorage {
    
    constructor(_storage: Storage, _config?: LightStorageConfig);
    
    get<T = any>(key: string): T | null;
    
    set<T = any>(key: string, val: T): T;
    
    remove<T = any>(key: string): T | null;
    
    clear(): void;
    
    // 监听数据
    listen<T = any>(key: string, listener: Listener<T>): void;
    
    // 取消监听
    unListen<T = any>(key: string, listener: Listener<T>): void;
    
    // 当前命名空间下的数量
    size(): number;
    
    // 当前命名空间下的key
    keys(): string[];
    
    eachKey(callback: (key: string) => void): void;
    
    each(callback: (key: string, value: any) => void): void;
    
    // 获取当前命名空间下的所有数据
    getAll<T extends Record<string, any>>(): T;
    
    // 同时设置多个数据
    setAll<T extends Record<string, any>>(data: T): T;
}

LightStorageConfig

type Code = (data: string) => string;

export interface LightStorageConfig {
    // 将数据编码后存储
    encode?: Code;
    // 解码数据
    decode?: Code;
    
    // 将key编码后存储
    encodeTitle?: Code;
    
    // 解码 key
    decodeTitle?: Code;
    
    // 命名空间,默认为 ” “, 不可重复, 命名空间可将数据进行隔离,操作互不影响
    namespace?: string;
}

createGenerator

declare function createGenerator(lightStorage: LightStorage): <T = any>(key: string) => LightSimpleStorage<T>;

LightSimpleStorage

type Listener<T> = (oldVal: T | null, newValue: T | null) => void;

export declare class LightSimpleStorage<T> {
    constructor(_key: string, _storage: LightStorage);
    
    get(): T | null;
    
    set(data: T): T;
    
    remove(): T | null;
    
    listen(listener: Listener<T>): void;
    
    unListen(listener: Listener<T>): void;
}
interface StorageFn {
    // getAll
    <T extends Record<string, any>>(): T;
    
    // clear
    (clear: null): void;
    
    // setAll
    <T extends Record<string, any>>(data: T): T;
    
    // each
    (callback: (key: string, value: any) => void): void;
    
    // get
    <T = any>(key: string): T | null;
    
    // set
    <T = any>(key: string, data: T): T;
    
    // remove
    <T = any>(key: string, data: null): T | null;
}

export declare function createFunction(lightStorage: LightStorage): StorageFn;

支持Bigint

由于源码需要编译成es5, 所以无法内部支持Bigint, 如果有需要可以通过如下方式支持

import { LightStorage, LightStorageConfig } from '@liaotai/light-storage';

class BigIntStorage extends LightStorage{
    constructor(storage: Storage, config?: LightStorageConfig) {
        super(storage, config);
    }
    protected _encode(data: any): string {
        if(typeof data === 'bigint') {
            data = data.toString() + 'n'
        }
        return super._encode(data)
    }

    protected _decode(data: string): any {
        if(/^\d+n$/.test(data)){
            return BigInt(data.slice(0, -1))
        }
        return super._decode(data);
    }
}

const testStorage = new BigIntStorage(localStorage)

testStorage.set<BigInt>('bigInt', 99999999999999999999n)