-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalStorage.js
More file actions
144 lines (127 loc) · 4.03 KB
/
localStorage.js
File metadata and controls
144 lines (127 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* This module provides a safe, extensible pattern for saving and loading
* application settings from localStorage.
*
* Features:
* - Creates isolated settings handlers bound to a specific localStorage key
* - New keys added to defaults are automatically picked up
* - Missing or invalid stored data is handled safely
* - Internal functions and state are private
* - Public API is exposed via `window.createSettings` for browser compatibility
* - Settings cannot get corrupted. If JSON.parse fails to parse a localStorage key, it will be overwritten by default settings.
*
* Example usage:
* --------------------------------
* // settings.js (this module, loaded via <script>)
*
* const defaultSettings = {
* language: 'english',
* theme: 'dark',
* };
*
* const settings = window.createSettings('app_settings', defaultSettings);
*
* // Load settings (merged with defaults)
* const current = settings.load();
*
* // Save partial updates
* settings.save({ language: 'japanese' });
*
* // Reset to defaults
* settings.reset();
*/
(() => {
'use strict';
/**
* Safely parse JSON from localStorage
* -----------------------------------
* Returns an empty object if:
* - localStorage is unavailable
* - the key does not exist
* - the stored value is invalid JSON
*
* Logs warnings to the console when any of the above occur.
*/
const safeParse = (key) => {
if (typeof window === 'undefined' || !window.localStorage) {
console.warn(`localStorage is not available. Cannot read key "${key}".`);
return {};
}
const raw = localStorage.getItem(key);
if (!raw) {
console.warn(`No data found in localStorage for key "${key}".`);
return {};
}
try {
return JSON.parse(raw);
} catch (error) {
console.warn(
`Failed to parse localStorage key "${key}". The stored value was invalid JSON.`,
error
);
return {};
}
};
/**
* Creates a settings instance bound to a storage key.
*/
const createSettings = (storageKey, defaultSettings) => {
/**
* Load settings from localStorage
* --------------------------------
* - Merges stored values with defaults
* - Ensures newly added default keys are included
* - Writes the merged result back to localStorage
*/
const load = () => {
const stored = safeParse(storageKey);
const merged = {
...defaultSettings,
...stored,
};
localStorage.setItem(storageKey, JSON.stringify(merged));
return merged;
};
/**
* Save settings to localStorage
* --------------------------------
* Accepts a partial settings object and merges it
* with the current stored settings.
*/
const save = (updates) => {
const current = load();
const merged = {
...current,
...updates,
};
localStorage.setItem(storageKey, JSON.stringify(merged));
return merged;
};
/**
* Reset settings back to defaults
* --------------------------------
*/
const reset = () => {
localStorage.setItem(
storageKey,
JSON.stringify(defaultSettings)
);
return { ...defaultSettings };
};
/**
* Public API
*/
return {
load,
save,
reset,
};
};
/**
* Exposed on the `window` object rather than as a module, to maximize compatibility
* with direct file access (file://). This allows users to run the application
* locally by simply opening the HTML file in a browser, without requiring a
* local web server or module bundler.
*/
window.createSettings = createSettings;
})();