• Stars
    star
    178
  • Rank 214,438 (Top 5 %)
  • Language
    TypeScript
  • License
    Other
  • Created almost 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Typed localStorage and sessionStorage utility with data structure and prefix support.

๐Ÿ”’ @ultimate/vault

1KB typed localStorage and sessionStorage utility with data structure and prefix support.

Installation

Install via npm i @ultimate/vault.

Documentation

ESModule: Import Vault into your TypeScript or JavaScript project and create a new instance:

import { Vault } from '@ultimate/vault';

const localStorage = new Vault();

Global: Access window.Vault if you are not using a module system:

<script src="vault.min.js"></script>
<script>
  // implicitly uses localStorage until specified
const localStorage = new Vault();
</script>

Local or Session Storage

By default new Vault() will use localStorage. You may specify the type of storage:

const localStorage = new Vault({ type: 'local' });
const sessionStorage = new Vault({ type: 'session' });

As Vault is a class each instance works independently.

Key Prefixes

Create a prefix for each Vault instance:

const localStorage = new Vault({ prefix: 'x9ea45' });

All keys set into storage via this instance will be stored as x9ea45-<key>.

isSupported property

Browser support is IE8+ so this shouldn't be wildly needed, but it's there anyway:

const localStorage = new Vault();

if (localStorage.isSupported) {
  // initialize...
}

set<T>(key: string, value: T): void

Set a key and value into storage using the typed set method:

// TypeScript
const localStorage = new Vault();

interface User {
  name: string
}

localStorage.set<User>('user', { name: 'Todd Motto' });

All methods are available to use without TypeScript:

const localStorage = new Vault();

localStorage.set('user', { name: 'Todd Motto' });

get<T>(key: string): T | undefined

Get a value from storage using the typed get method:

const localStorage = new Vault();

interface User {
  name: string
}

localStorage.get<User>('user');

remove(key: string): void

Remove an item from storage using the remove method:

const localStorage = new Vault();

localStorage.remove('user');

removeAll(): void

Remove all items from storage:

const localStorage = new Vault();

localStorage.removeAll();

onChange(key: string, fn: (e: StorageEvent) => void): () => void

Listen to the storage change event from another tab, which is emitted when any storage value is changed. Here we can specify to only listen to specific property changes:

const localStorage = new Vault();

const unsubscribe = localStorage.onChange('user', (e: StorageEvent) => {
  // `user` was changed in another tab
  // we could use this new data to sync our UI
  console.log(e);
});

// remove the event listener when you're ready
unsubscribe();

Get all values

Obtain all storage values by accessing the value getter:

const localStorage = new Vault();

console.log(localStorage.value); // { "user": "Todd Motto", ... }

Returns an object with all keys and values. Values will remain a string type and will need parsing with JSON.parse() if you need to access the value.

Length of Storage

Access how many items are currently in storage with length:

const localStorage = new Vault();

console.log(localStorage.length); // 3

More Repositories

1

ngx-errors

A declarative validation errors module for reactive forms.
TypeScript
470
star
2

angular-pro-src

Source code for the "Angular Pro" course.
TypeScript
335
star
3

angular-fundamentals-src

Source code for the "Angular Fundamentals" course.
TypeScript
252
star
4

redux-store

Vanilla TypeScript example of a Redux Store
TypeScript
169
star
5

angular-fundamentals-seed

Angular Fundamentals seed project
JavaScript
143
star
6

angular-pro-app-seed

Seed project for Angular Pro final application
JavaScript
107
star
7

angular-tesla-range-calculator

Building Tesla's battery range calculator with Angular 2+ reactive forms
TypeScript
96
star
8

javascript-basics

๐Ÿ† Starter project for JavaScript Basics
CSS
83
star
9

ultimate-angular-master-seed

Seed project for the "Ultimate Angular 1.x Pro" course.
JavaScript
73
star
10

typescript-masterclass-src

TypeScript Masterclass. Advanced TypeScript, comprehensively covered in real-world digestable chapters.
TypeScript
72
star
11

angular-lazy-load-demo

Lazy loading: code-splitting with @โ€‹NgModules demo
TypeScript
69
star
12

ultimate-angular-master-src

Source code for "Ultimate Angular 1.x Pro" course material
JavaScript
62
star
13

typescript-basics-seed

Seed project for TypeScript Basics course
HTML
56
star
14

ultimate-angular-starter-src

Source code for "Ultimate Angular 1.x Fundamentals" course:
HTML
55
star
15

typescript-basics-src

TypeScript Basics. Our comprehensive introduction to static types in JavaScript.
JavaScript
53
star
16

lite-store

Reactive Stores with entities and frozen state, without the headache
TypeScript
49
star
17

ultimate-angular-starter-seed

The starter project for the "Ultimate Angular 1.x Fundamentals" course
CSS
38
star
18

ngx-fullscreen

Angular Directive that implements the Fullscreen API.
TypeScript
35
star
19

node-express-typescript

A minimal starter project to write your Node.js apps in TypeScript, complete with local and production build scripts.
TypeScript
35
star
20

angular-1-performance-src

Source code for AngularJS Performance course
JavaScript
30
star
21

javascript-masterclass

๐Ÿ† Starter project for JavaScript Masterclass
CSS
24
star
22

javascript-html5-apis

๐Ÿ† Starter project for JavaScript HTML5 APIs
JavaScript
20
star
23

javascript-dom

๐Ÿ† Starter project for the JavaScript DOM course
CSS
18
star
24

ngx-pagevisibility

TypeScript
15
star
25

angular-basics-src

TypeScript
15
star
26

javascript-dom-project

๐Ÿ† Starter project for the JavaScript DOM course project build
CSS
14
star
27

react-basics

๐Ÿ† Starter project for React Basics [Create React App]
JavaScript
14
star
28

angular-basics-seed

Angular Basics (v15) Course Seed Project
TypeScript
12
star
29

typescript-masterclass-seed

Seed project for TypeScript Masterclass course
JavaScript
10
star
30

html-css-basics

๐Ÿ† Starter project for the HTML + CSS Basics course
CSS
10
star
31

ultimate-react-icecream

UltimateCourses demo React app
JavaScript
10
star
32

rxjs-basics

๐Ÿ† Starter project for RxJS Basics and Masterclass courses
CSS
9
star
33

react-router-src

JavaScript
7
star
34

react-state-management-course

JavaScript
6
star
35

react-router-seed

Learn React Router v6 ๐Ÿงญ
JavaScript
5
star
36

react-basics-src

Source code for React Basics
JavaScript
4
star