ls-map-wrap

1.1.0 • Public • Published

ls-map-wrap

Build Status Coverage dependencies dev dependencies License: MIT XO code style

A wrapper around localstorage, so it can be used as a Map

Getting Started

Install it via npm:

npm install ls-map-wrap

And include in your project:

// Using ESM
import lsMapWrap from 'ls-map-wrap';
// Or using CJS
const lsMapWrap = require('ls-map-wrap');

Usage

import lsMapWrap from 'ls-map-wrap';
 
lsMapWrap.set('key', {ultimateAnswer: 42});
lsMapWrap.get('key'); // {ultimateAnswer: 42}
lsMapWrap.has('key'); // true
 
// works with values not typically handled by JSON.stringify/parse
lsMapWrap.set('nan', NaN);
Number.isNaN(lsMapWrap.get('nan')); // true
 
// works with functions too
lsMapWrap.set('simpleFn', () => console.log('hello'));
const fn = lsMapWrap.get('simpleFn');
fn(); // hello is logged to the console
 
lsMapWrap.delete('key');
lsMapWrap.has('key'); // false
 
lsMapWrap.clear();
lsMapWrap.has('nan'); // false

API

Implements all of the functions nessecary to drop it in place for an in-memory map

get(key)

Returns the value for the given key

key

Type: string

set(key, value)

Saves the value in localstorage for the given key

key

Type: string

value

Type: any

has(key)

Returns true if the map (localStorage) contains this value, false otherwise

key

Type: string

delete(key)

Removes the value from the map with the given key

key

Type: string

clear()

Removes all values from the map (localStorage)

Readme

Keywords

none

Package Sidebar

Install

npm i ls-map-wrap

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

147 kB

Total Files

7

Last publish

Collaborators

  • mdvorscak