webdev-power-kit
Version:
A powerful toolkit that simplifies access to browser features like clipboard, notifications, battery, vibration, and more — perfect for modern web developers.
125 lines (124 loc) • 3.32 kB
JavaScript
/**
* @fileoverview A utility to interact safely with localStorage and sessionStorage
* with built-in error handling and JSON support.
*/
/**
* Returns the correct Storage object based on type.
* @param type - 'local' or 'session'
*/
function getStorage(type) {
try {
const storage = type === 'local' ? window.localStorage : window.sessionStorage;
const testKey = '__storage_test__';
storage.setItem(testKey, testKey);
storage.removeItem(testKey);
return storage;
}
catch (err) {
console.error(`[webdev-power-kit][storage] Storage unavailable:`, err);
return null;
}
}
/**
* Sets a value in storage.
* @param key - Storage key
* @param value - Any serializable value
* @param type - 'local' (default) or 'session'
*/
export function setItem(key, value, type = 'local') {
const storage = getStorage(type);
if (!storage)
return;
try {
const json = JSON.stringify(value);
storage.setItem(key, json);
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to set item "${key}":`, err);
}
}
/**
* Gets a value from storage.
* @param key - Storage key
* @param type - 'local' (default) or 'session'
* @returns Parsed value or null
*/
export function getItem(key, type = 'local') {
const storage = getStorage(type);
if (!storage)
return null;
try {
const item = storage.getItem(key);
return item ? JSON.parse(item) : null;
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to get item "${key}":`, err);
return null;
}
}
/**
* Removes a key from storage.
* @param key - Key to remove
* @param type - 'local' (default) or 'session'
*/
export function removeItem(key, type = 'local') {
const storage = getStorage(type);
if (!storage)
return;
try {
storage.removeItem(key);
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to remove item "${key}":`, err);
}
}
/**
* Clears all storage keys.
* @param type - 'local' (default) or 'session'
*/
export function clearStorage(type = 'local') {
const storage = getStorage(type);
if (!storage)
return;
try {
storage.clear();
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to clear ${type}Storage:`, err);
}
}
/**
* Checks if a key exists in storage.
* @param key - Key to check
* @param type - 'local' (default) or 'session'
* @returns true if exists, false otherwise
*/
export function hasItem(key, type = 'local') {
const storage = getStorage(type);
if (!storage)
return false;
try {
return storage.getItem(key) !== null;
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to check item "${key}":`, err);
return false;
}
}
/**
* Returns all keys in storage.
* @param type - 'local' (default) or 'session'
* @returns Array of keys
*/
export function getAllKeys(type = 'local') {
const storage = getStorage(type);
if (!storage)
return [];
try {
return Object.keys(storage);
}
catch (err) {
console.error(`[webdev-power-kit][storage] Failed to get keys:`, err);
return [];
}
}