Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions public/data/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down