@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
140 lines (139 loc) • 3.68 kB
JavaScript
/**
* @fileoverview Type-safe local storage utility functions
* Provides methods for managing data in localStorage with proper error handling
*/
/**
* Gets an item from localStorage with type safety
* @param key - Storage key
* @returns Parsed value or null if not found
*/
export function getLocalStorageItem(key) {
if (typeof window === 'undefined')
return null;
try {
const item = localStorage.getItem(key);
if (!item)
return null;
return JSON.parse(item);
}
catch (error) {
console.error(`Error getting localStorage item ${key}:`, error);
return null;
}
}
/**
* Sets multiple items in localStorage
* @param items - Array of key-value pairs to store
*/
export function setLocalStorageItems(items) {
if (typeof window === 'undefined')
return;
try {
items.forEach(({ key, value }) => {
localStorage.setItem(key, typeof value === 'string' ? value : JSON.stringify(value));
});
}
catch (error) {
console.error('Error setting localStorage items:', error);
}
}
/**
* Removes multiple items from localStorage
* @param keys - Array of keys to remove
*/
export function removeLocalStorageItems(keys) {
if (typeof window === 'undefined')
return;
try {
keys.forEach((key) => localStorage.removeItem(key));
}
catch (error) {
console.error('Error removing localStorage items:', error);
}
}
/**
* Clears all items from localStorage
*/
export function clearLocalStorage() {
if (typeof window === 'undefined')
return;
try {
localStorage.clear();
}
catch (error) {
console.error('Error clearing localStorage:', error);
}
}
/**
* Gets the size of localStorage in bytes
* @returns Size in bytes or 0 if not available
*/
export function getLocalStorageSize() {
if (typeof window === 'undefined')
return 0;
try {
let total = 0;
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key) {
const value = localStorage.getItem(key);
if (value) {
total += key.length + value.length;
}
}
}
return total * 2; // Multiply by 2 since characters are 2 bytes each
}
catch (error) {
console.error('Error calculating localStorage size:', error);
return 0;
}
}
/**
* Checks if localStorage is available
* @returns boolean indicating if localStorage is available
*/
export function isLocalStorageAvailable() {
if (typeof window === 'undefined')
return false;
try {
const testKey = '__storage_test__';
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
}
catch (error) {
return false;
}
}
/**
* Gets all keys in localStorage
* @returns Array of localStorage keys
*/
export function getLocalStorageKeys() {
if (typeof window === 'undefined')
return [];
try {
return Object.keys(localStorage);
}
catch (error) {
console.error('Error getting localStorage keys:', error);
return [];
}
}
/**
* Checks if a key exists in localStorage
* @param key - Key to check
* @returns boolean indicating if key exists
*/
export function hasLocalStorageItem(key) {
if (typeof window === 'undefined')
return false;
try {
return key in localStorage;
}
catch (error) {
console.error(`Error checking localStorage for key ${key}:`, error);
return false;
}
}