storagedotjs
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Storagedotjs

A simple, lightweight JavaScript api for accessing Local Storage.

Installation

npm

npm install storagedotjs

pnpm

pnpm install storagedotjs

yarn

yarn install storagedotjs

Usage

Storing data

import { setItem } from 'storagedotjs';

setItem('key', 'value');

Data expiry

Data stored in localstorage can last forever unless cleared by the user. To set preferred expiry date or time, pass a third argument.

Using strings
// 30 seconds
setItem('key', 'value', '30s');
// 15 minutes
setItem('key', 'value', '15min');
// 6 hours
setItem('key', 'value', '15h');
// 5 days
setItem('key', 'value', '5d');
// 2 months
setItem('key', 'value', '2m');
// 7 years
setItem('key', 'value', '7y');
Using a date object
// 15 minutes
const date = new Date();
date.setDate(date.getDate() + 7)
setItem('key', 'value', date);
Using a timestamp
// Sunday, February 26, 2023 12:41:41 AM GMT+01:00
setItem('key', 'value', 1677368501000);

Objects and arrays are automatically stringified into json

// Object
const user = { firstName: 'Antonio', lastName: 'Okoro' };
setItem('my-user', user);

// Array
const pets = ['dog', 'cat', 'bunny', 'goldfish'];
setItem('my-pets', pets);

Getting data from storage

import { getItem } from 'storagedotjs';

const user = getItem('my-user');

Using type annotations for return values

import { getItem } from 'storagedotjs';

interface User {
  firstName: string;
  lastName: string;
}

const user = getItem<User>('my-user');

Checking if data exists in the storage

import { hasItem } from 'storagedotjs';

if (hasItem('key')) {
  // code...
}

Removing data

import { removeItem } from 'storagedotjs';

const logout = () => removeItem('key');

Package Sidebar

Install

npm i storagedotjs

Weekly Downloads

1

Version

1.1.1

License

ISC

Unpacked Size

5.91 kB

Total Files

4

Last publish

Collaborators

  • cheezytony