localstorage-with-expires
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

localstorage-with-expires

Tiny localStorage api with data auto-expired, auto JSON stringlify, etc.

Usage

npm i localstorage-with-expires

Example

import * as ls from 'localstorage-with-expires';

ls.set('user-loginInfo', {
  userName: 'blablabla',
  age: 18
}, 24 * 60 * 60 * 1000); // userinfo will be expired in 24 hours

ls.get('user-loginInfo'); // 24 hours later you will get 'null' here, expired data will be remove from localStorage

Typescript supported

import ls, { get, set } from 'localstorage-with-expires';

interface UserInfo {
  userName: string;
  age: number;
}

get<UserInfo>('user-loginInfo');
set<UserInfo>('user-loginInfo', {
  userName: 'blablabla',
  age: 18
});

// factory function `ls`
const loginStorage = ls<UserInfo>('user-loginInfo', 24 * 60 * 60 * 1000);

const login = (loginInfo: UserInfo) => {
  loginStorage.set(loginInfo);
}

const getUserInfo = () => loginStorage.get();

const logout = () => loginStorage.remove(); 

Readme

Keywords

none

Package Sidebar

Install

npm i localstorage-with-expires

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

5.83 kB

Total Files

6

Last publish

Collaborators

  • xiaoyuze88