diff --git a/public/data/javascript.json b/public/data/javascript.json index 28bd1e94..9b0048f5 100644 --- a/public/data/javascript.json +++ b/public/data/javascript.json @@ -425,6 +425,34 @@ ], "tags": ["javascript", "localStorage", "storage", "utility"], "author": "dostonnabotov" + }, + { + "title": "Local Storage Utility", + "description": "A class with static methods to interact with localStorage. (set, get, remove). It also supports encoding and decoding values.", + "code": [ + "class LocalStore {", + " static set(key, value, encode = false) {", + " if(!value) return;", + " localStorage.setItem(key, encode ? btoa(JSON.stringify(value)) : JSON.stringify(value));", + " }", + "", + " static get(key, decode = false) {", + " const value = localStorage.getItem(key);", + " return value ? (decode ? JSON.parse(atob(value)) : JSON.parse(value)) : null;", + " }", + "", + " static remove(key) {", + " localStorage.removeItem(key);", + " }", + "}", + "", + "// Usage:", + "LocalStore.set('user', { name: 'John', age: 30 }, true);", + "const user = LocalStore.get('user', true);", + " console.log(user); // Output: { name: 'John', age: 30 }" + ], + "tags": ["javascript", "localStorage", "storage", "utility"], + "author": "marville001" } ] },