Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (46 sloc) 1.71 KB
import { debounce } from 'lodash';
// Create a record
export const createRecord = (key: string, value: string): { error?: string } | void => {
if (!key || !value) { return { error: 'Store to localStorage failed. Invalid key or value.' }; }
localStorage.setItem(key, value);
};
// Read a record
export const readRecord = (key: string): string | null => {
return localStorage.getItem(key);
};
// Update a record
export const updateRecord = (key: string, value: string): { error?: string } | void => {
if (!key || !value) { return { error: 'Store to localStorage failed. Invalid key or value.' }; }
localStorage.setItem(key, value);
};
// Delete a record
export const deleteRecord = (key: string): { error?: string } | void => {
if (!key) { return { error: 'Store to localStorage failed. Invalid key.' }; }
localStorage.removeItem(key)
};
// Clear all
export const clearAll = (): void => localStorage.clear();
// Check for stored items
export const hasStoredItems = (): boolean => localStorage.length > 0;
// Check for support
export const isLocalStorageSupported = (): boolean => !!window.localStorage;
// Store (create or update) record to local storage with some delay (e.g. 1500 ms)
export const storeToLocalStorageDebounced = debounce((key: string, value: string): void => {
if (isLocalStorageSupported()) {
if (readRecord(key)) {
updateRecord(key, value);
} else {
createRecord(key, value);
}
}
}, 1500, {leading: false});
// Store (create or update) record to local storage
export const storeToLocalStorage = (key: string, value: string): void => {
if (isLocalStorageSupported()) {
if (readRecord(key)) {
updateRecord(key, value);
} else {
createRecord(key, value);
}
}
};
You can’t perform that action at this time.