LockerJS - HTML5 Storage Wrapper
Extremely light library that makes HTML5 storage usage easier to the edge. No dependencies, no configuration.
npm install --save lockerjs
yarn add lockerjs
;const locker = windowlocalStorage;// orconst locker = windowsessionStorage;
setItem does the job, although it is quite limited as it accepts only String - String values. By using LockerJS you are no longer limited by value types! Saving object with key 1 is as easy as:
const myObj ='name': 'John''surname': 'Test';locker;
Isn't that easy? You can use any parameter type as your key and value;
Only adds value to specific key if a given key is not already used. Otherwise throws an error.
locker; //OKlocker; //ReferenceError: 'Provided key is already in use'
getItem allows you to retrieve text value from storage by passing key, but wouldn't that be great just to pass any parameter type? Consider this snippet:
Isn't that as easy as one-two-three? There's more! Locker will return you an original value type!
const myArr = 1 2 3;locker;const retrievedArr = locker;typeof retrievedArr; // "array"console; // [1, 2, 3]
Of course, same goes for numbers, objects, ES6 Maps etc.
Simply returns true if a given key has been already used, otherwise false
locker;locker // Truelocker // False
Simply returns true if a given value has been already used, otherwise false.
const mySet = ;mySet;locker;locker; // True
Clear the whole storage
Returns the size of the storage
Pass an array of keys that shall be removed and Locker will remove only those entries
const testObj ='name': 'John'locker;locker;const keysToRemove = '1' testObj;locker; // Empty
As you have noticed you can mix & match all value types to your preferences.
Convert & copy keys and values from ES6 Map into storage.
const sampleMap = ;map;map;lockerclear;locker;locker; // 'First entry'locker; // 'Second entry'
If you would like to get a 'backup' of client's storage you can do so by invoking saveMap(). It will construct ES6 Map from storage.
locker;const backup = locker;
Please raise any issues using a template inside ./github folder.
All PRs are more than welcome! Please run
before submitting a merge request.