A fully-featured, safe, and extensible wrapper around the browser's localStorage API. This class makes working with localStorage as intuitive as working with arrays or objects in JavaScript — with familiar methods like .map(), .filter(), .find(), .reduce(), .keys(), and more.
- ✅ Safe JSON parsing
- ✅
.set(),.get(),.delete(),.clear()– standard CRUD operations - ✅ Array-like methods:
map,filter,reduce,find,forEach,some,every, etc. - ✅ Utility helpers:
includes,includesKey,includesValue,entries,length,print - ✅ Handles invalid JSON gracefully
- ✅ Fully documented with JSDoc annotations
Clone this repository
git clone https://github.com/brianaraby/LocalStorage.gitconst iStorage = new LocalStorage();
iStorage.clear();
iStorage.set('KeyOne', 'ValueOne');
iStorage.set('WAAuthToken', '388ad1c312a488ee9e12998fe097f2258fa8d5ee');
iStorage.print();
iStorage.map((key, value) => console.log(`${key} → ${value}`));
const Filter = iStorage.filter((key, value) => key.startsWith('KeyOne'));
console.log(Filter);
const Found = iStorage.find((key, value) => value === 1);
console.log(Found)<script src="LocalStorage.js"></script>
<script>
const iStorage = new LocalStorage();
iStorage.clear();
iStorage.set('KeyOne', 'ValueOne');
iStorage.set('WAAuthToken', '388ad1c312a488ee9e12998fe097f2258fa8d5ee');
iStorage.print();
iStorage.forEach((key, value) => console.log(`${key} → ${value}`))
</script>| Function | Description |
|---|---|
set(key, value) |
Save a value. |
get(key) |
Get a parsed value. |
delete(key) |
Remove a key. |
has(key) |
Check if key exists. |
clear() |
Remove all keys. |
length |
Get total count of keys. |
keys() |
Get all keys. |
values() |
Get all values. |
entries() |
Get all key/value pairs. |
map(callback) |
Map over entries and return a new array. |
filter(callback) |
Filter entries based on a callback. |
reduce(callback, initial) |
Reduce entries to a single value. |
forEach(callback) |
Execute callback for each entry. |
some(callback) |
Check if some entries pass the test. |
every(callback) |
Check if every entry passes the test. |
find(callback) |
Find the first matching value. |
findIndex(callback) |
Find index of the first matching entry. |
findLast(callback) |
Find the last matching value. |
findLastIndex(callback) |
Find index of the last matching entry. |
includes(value) |
Check if a value exists. |
includesKey(key) |
Check if a key exists. |
includesValue(value) |
Check if a value exists. |
print() |
Output all key/value pairs in a table. |
MIT License © 2025 Araby