Code Monkey home page Code Monkey logo

nano-storage's Introduction

nano-storage

nano storage 是一个简化的 localStorage 及 sessionStorage 方法

Feature

  • gzip 0.2kb, 仅仅是一个工具方法
  • 直接下标取值,取值直接走对象缓存
  • set 内容使用 Object.assign
  • 自动持久化
  • 支持 Typescript
  • 支持版本规划

Use

NanoStorage 第一个参数是 storage 的 key,第二个参数是初始化对象,NanoStorage 会以初始化对象为范型作为后续的对象提示,为了项目对象更好的理解,建议所有使用的对象都有一个初始化的值

import { NanoStorage } from "nano-storage";
const store = NanoStorage('user-data', {
  name:'dog';
  age: 20,
});

console.log(store.name); // "dog"
console.log(store.age); // 20

// 更新内容
store.set({name:'fish'});
console.log(store.name); // "fish"

// 还原初始化内容
store.reinit();

使用 sessionStorage

nano-storage 默认使用 localstorage, 若要使用 sessionStorage 可以设置 storage 为 'sessionStorage':

const store = NanoStorage('user-data', {
  name:'dog';
  age: 20,
}, {
  storage: 'sessionStorage',
});

统一设置版本

所有 NanoStorage 的 key 等于 key + version

NanoStorage.version = "_0.0.1";

个别对象单独版本

NanoStorage.version 是所有 对象都设置的版本号,若有个别对象希望不一样的版本号,可以设置 version 对象

const store = NanoStorage('user-data', {
  name:'dog';
  age: 20,
}, {
  version: '0.0.2'
});

nano-storage's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.