Local Storage Manager
A simple package used for creating, storing and deleting data from local storage.
- Enter key and data into local storage
- Get the data for a given key
- Remove all data for a given key
npm install local-storage-manager --save
var localStorageManager =// Creating a key and entering a string into local storagelocalStorageManager;// Creating a key and entering an array into local storagelocalStorageManager;// Creating a key and entering an object into local storagelocalStorageManager;// Getting data bound to a keylocalStorageManager;returns: 'data'// Removing a key and all data bound to itlocalStorageManager;// Example usagevar data = localStorageManager;if data === 'data'return true;
Using the transform to storage function.
In some cases, strings may not be able to be put into local storage as is. For example, a string such as '23/53453458124234' will return the error "Unexpected token /" because this interrupts the stringify function.
To get around this, the transformToStorage function can replace the slash with a suitable word or phrase. Likewise, the transformFromStorage function can remove the word we introduced.
// Use the localStorageManager.set function as you normally would, but use transform// as the second argumentvar keyName = 'stringWithToken';var value = '23/53453458124234';//Arguments: string to search, characters to find in string, characters to replace found string with.var transformedValue = ;localStorageManager;console
If keys in your local storage are undefined it may result in a crash. The setIfEmpty function eliminates this issue by checking if keys are undefined, and setting a default value if they are. See below for usage.
// define your default values in the format of localStorageKeyName: defaultValuevar defaultValues =string: ''array:object:;// pass your object of default values into the functionlocalStorageManager;
Running the Tests
To run the test included in this package, open the
test.html and open the developer console.
Note that if the environment does not have local storage, the functions will not run.
For changelog, view changelog markdown