@fajarmaulana/komerce-lp-helper 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Form.js +7 -30
- package/dist/components/LazyBackground.js +2 -3
- package/dist/components/hooks/useLazyBackground.js +8 -26
- package/dist/constants/index.js +1 -1
- package/dist/constants/regex.js +10 -10
- package/dist/hooks/debounce.js +16 -47
- package/dist/hooks/form.js +26 -65
- package/dist/hooks/router.js +49 -89
- package/dist/hooks/sectionObserver.js +7 -25
- package/dist/hooks/slider.js +36 -55
- package/dist/utils/api.js +261 -434
- package/dist/utils/cookie.js +18 -19
- package/dist/utils/error-provider.js +30 -55
- package/dist/utils/file.js +27 -31
- package/dist/utils/general.d.ts +8 -0
- package/dist/utils/general.js +24 -11
- package/dist/utils/local.js +10 -10
- package/dist/utils/useApi.js +188 -315
- package/package.json +11 -1
package/dist/utils/cookie.js
CHANGED
|
@@ -5,18 +5,17 @@
|
|
|
5
5
|
* @param value - The cookie value (will be JSON-stringified).
|
|
6
6
|
* @param maxAge - Optional max age of the cookie in seconds.
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
document.cookie = "".concat(key, "=").concat(serializedValue).concat(maxAge ? "; Max-Age=".concat(maxAge) : '', "; Secure; SameSite=Strict; path=/");
|
|
8
|
+
export const setCookie = ({ key, value, maxAge }) => {
|
|
9
|
+
const serializedValue = JSON.stringify(value);
|
|
10
|
+
document.cookie = `${key}=${serializedValue}${maxAge ? `; Max-Age=${maxAge}` : ''}; Secure; SameSite=Strict; path=/`;
|
|
12
11
|
};
|
|
13
12
|
/**
|
|
14
13
|
* Sets multiple cookies from an array of cookie objects.
|
|
15
14
|
*
|
|
16
15
|
* @param items - Array of cookies to set.
|
|
17
16
|
*/
|
|
18
|
-
export
|
|
19
|
-
items.forEach(
|
|
17
|
+
export const setCookies = (items) => {
|
|
18
|
+
items.forEach(item => setCookie(item));
|
|
20
19
|
};
|
|
21
20
|
/**
|
|
22
21
|
* Gets the value of a cookie by key and parses it from JSON.
|
|
@@ -24,14 +23,14 @@ export var setCookies = function (items) {
|
|
|
24
23
|
* @param key - The key of the cookie to get.
|
|
25
24
|
* @returns The parsed cookie value, or null if not found.
|
|
26
25
|
*/
|
|
27
|
-
export
|
|
28
|
-
|
|
26
|
+
export const getCookie = (key) => {
|
|
27
|
+
const match = document.cookie.match(new RegExp('(^| )' + key + '=([^;]+)'));
|
|
29
28
|
if (!match)
|
|
30
29
|
return null;
|
|
31
30
|
try {
|
|
32
31
|
return JSON.parse(decodeURIComponent(match[2]));
|
|
33
32
|
}
|
|
34
|
-
catch
|
|
33
|
+
catch {
|
|
35
34
|
return decodeURIComponent(match[2]);
|
|
36
35
|
}
|
|
37
36
|
};
|
|
@@ -41,8 +40,8 @@ export var getCookie = function (key) {
|
|
|
41
40
|
* @param keys - Array of keys to retrieve.
|
|
42
41
|
* @returns Object with keys and their corresponding parsed values.
|
|
43
42
|
*/
|
|
44
|
-
export
|
|
45
|
-
return keys.reduce(
|
|
43
|
+
export const getCookies = (keys) => {
|
|
44
|
+
return keys.reduce((acc, key) => {
|
|
46
45
|
acc[key] = getCookie(key);
|
|
47
46
|
return acc;
|
|
48
47
|
}, {});
|
|
@@ -52,24 +51,24 @@ export var getCookies = function (keys) {
|
|
|
52
51
|
*
|
|
53
52
|
* @param key - The key of the cookie to remove.
|
|
54
53
|
*/
|
|
55
|
-
export
|
|
56
|
-
document.cookie =
|
|
54
|
+
export const removeCookie = (key) => {
|
|
55
|
+
document.cookie = `${key}=; Max-Age=0; Secure; SameSite=Strict; path=/`;
|
|
57
56
|
};
|
|
58
57
|
/**
|
|
59
58
|
* Removes multiple cookies by keys.
|
|
60
59
|
*
|
|
61
60
|
* @param keys - Array of keys to remove.
|
|
62
61
|
*/
|
|
63
|
-
export
|
|
64
|
-
keys.forEach(
|
|
62
|
+
export const removeCookies = (keys) => {
|
|
63
|
+
keys.forEach(key => removeCookie(key));
|
|
65
64
|
};
|
|
66
65
|
/**
|
|
67
66
|
* Clears all cookies by setting their max age to 0.
|
|
68
67
|
*/
|
|
69
|
-
export
|
|
70
|
-
document.cookie.split('; ').forEach(
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
export const clearCookies = () => {
|
|
69
|
+
document.cookie.split('; ').forEach(cookie => {
|
|
70
|
+
const eqPos = cookie.indexOf('=');
|
|
71
|
+
const key = eqPos > -1 ? cookie.substring(0, eqPos) : cookie;
|
|
73
72
|
removeCookie(key);
|
|
74
73
|
});
|
|
75
74
|
};
|
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
1
|
import { ANY_LOWERCASE, ANY_NUMBER, ANY_SYMBOL, ANY_UPPERCASE, NO_SPACE } from '../constants/regex';
|
|
18
2
|
import { getByAny } from './general';
|
|
19
3
|
/**
|
|
@@ -46,21 +30,19 @@ import { getByAny } from './general';
|
|
|
46
30
|
* - The function automatically applies a red border style (`!border-error-main`) to the associated `<label>`.
|
|
47
31
|
* - When all rules pass, the error text is cleared and the border style is removed.
|
|
48
32
|
*/
|
|
49
|
-
export
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
Object.entries(rules).forEach(function (_a) {
|
|
54
|
-
var _b = __read(_a, 2), message = _b[0], isError = _b[1];
|
|
33
|
+
export const provideFieldError = ({ field_id, field_value, field_error, rules, }) => {
|
|
34
|
+
let error = false;
|
|
35
|
+
const label = getByAny(`label[for="${field_id}"]`);
|
|
36
|
+
Object.entries(rules).forEach(([message, isError]) => {
|
|
55
37
|
if (isError && (!error || !!field_value)) {
|
|
56
38
|
field_error.textContent = message;
|
|
57
39
|
error = true;
|
|
58
|
-
label
|
|
40
|
+
label?.classList.add('!border-error-main');
|
|
59
41
|
}
|
|
60
42
|
});
|
|
61
43
|
if (!error) {
|
|
62
44
|
field_error.textContent = '';
|
|
63
|
-
label
|
|
45
|
+
label?.classList.remove('!border-error-main');
|
|
64
46
|
}
|
|
65
47
|
};
|
|
66
48
|
/**
|
|
@@ -95,46 +77,39 @@ export var provideFieldError = function (_a) {
|
|
|
95
77
|
* - Automatically applies or removes the red border class (`!border-error-main`) on the associated `<label>`.
|
|
96
78
|
* - Uses predefined regex constants from `@/constants/regex` for validation checks.
|
|
97
79
|
*/
|
|
98
|
-
export
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
var label = getByAny("label[for=\"".concat(field_id, "\"]"));
|
|
104
|
-
var firstRules = {
|
|
80
|
+
export const providePasswordFieldError = ({ field_id, field_value, field_error }) => {
|
|
81
|
+
let error = false;
|
|
82
|
+
const value = field_value;
|
|
83
|
+
const label = getByAny(`label[for="${field_id}"]`);
|
|
84
|
+
const firstRules = {
|
|
105
85
|
'password harus diisi': !value,
|
|
106
86
|
'panjang password minimal adalah 8 karakter': value.length < 8,
|
|
107
87
|
};
|
|
108
|
-
Object.entries(firstRules).forEach(
|
|
109
|
-
var _b = __read(_a, 2), message = _b[0], isError = _b[1];
|
|
88
|
+
Object.entries(firstRules).forEach(([message, isError]) => {
|
|
110
89
|
if (isError && (!error || !!value)) {
|
|
111
90
|
field_error.textContent = message;
|
|
112
91
|
error = true;
|
|
113
|
-
label
|
|
92
|
+
label?.classList.add('!border-error-main');
|
|
114
93
|
}
|
|
115
94
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return rule;
|
|
125
|
-
});
|
|
126
|
-
var nextMessage = 'tambahkan ' +
|
|
95
|
+
const nextRules = {
|
|
96
|
+
['huruf kapital']: !ANY_UPPERCASE.re.test(value),
|
|
97
|
+
['huruf kecil']: !ANY_LOWERCASE.re.test(value),
|
|
98
|
+
['angka']: !ANY_NUMBER.re.test(value),
|
|
99
|
+
['simbol']: !ANY_SYMBOL.re.test(value),
|
|
100
|
+
};
|
|
101
|
+
const nextError = Object.entries(nextRules).filter(([_, rule]) => rule);
|
|
102
|
+
const nextMessage = 'tambahkan ' +
|
|
127
103
|
nextError
|
|
128
|
-
.map(
|
|
129
|
-
|
|
130
|
-
var len = nextError.length;
|
|
104
|
+
.map(([message], idx) => {
|
|
105
|
+
const len = nextError.length;
|
|
131
106
|
if (len > 1 && idx >= len - 1) {
|
|
132
107
|
if (len <= 2)
|
|
133
|
-
return
|
|
134
|
-
return
|
|
108
|
+
return ` dan ${message}`;
|
|
109
|
+
return `, dan ${message}`;
|
|
135
110
|
}
|
|
136
111
|
if (idx > 0)
|
|
137
|
-
return
|
|
112
|
+
return `, ${message}`;
|
|
138
113
|
return message;
|
|
139
114
|
})
|
|
140
115
|
.join('');
|
|
@@ -144,15 +119,15 @@ export var providePasswordFieldError = function (_a) {
|
|
|
144
119
|
field_error.textContent = 'password terlalu lemah. ' + nextMessage + '.';
|
|
145
120
|
}
|
|
146
121
|
error = true;
|
|
147
|
-
label
|
|
122
|
+
label?.classList.add('!border-error-main');
|
|
148
123
|
}
|
|
149
124
|
if (!NO_SPACE.re.test(value) && (!error || !!value)) {
|
|
150
|
-
field_error.textContent =
|
|
125
|
+
field_error.textContent = `password ${NO_SPACE.text}`;
|
|
151
126
|
error = true;
|
|
152
|
-
label
|
|
127
|
+
label?.classList.add('!border-error-main');
|
|
153
128
|
}
|
|
154
129
|
if (!error) {
|
|
155
130
|
field_error.textContent = '';
|
|
156
|
-
label
|
|
131
|
+
label?.classList.remove('!border-error-main');
|
|
157
132
|
}
|
|
158
133
|
};
|
package/dist/utils/file.js
CHANGED
|
@@ -3,24 +3,22 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @param url - The image URL to check.
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
});
|
|
13
|
-
};
|
|
6
|
+
export const checkImage = url => new Promise(resolve => {
|
|
7
|
+
const img = new Image();
|
|
8
|
+
img.src = url;
|
|
9
|
+
img.onload = () => resolve(url);
|
|
10
|
+
img.onerror = () => resolve('');
|
|
11
|
+
});
|
|
14
12
|
/**
|
|
15
13
|
* Converts a Blob object into a Base64-encoded data URL string.
|
|
16
14
|
*
|
|
17
15
|
* @param blob - The Blob object to be converted.
|
|
18
16
|
* @returns - A Promise that resolves to a Base64-encoded data URL string.
|
|
19
17
|
*/
|
|
20
|
-
export
|
|
21
|
-
return new Promise(
|
|
22
|
-
|
|
23
|
-
reader.onload =
|
|
18
|
+
export const convertBlob = blob => {
|
|
19
|
+
return new Promise(resolve => {
|
|
20
|
+
const reader = new FileReader();
|
|
21
|
+
reader.onload = () => resolve(reader.result);
|
|
24
22
|
reader.readAsDataURL(blob);
|
|
25
23
|
});
|
|
26
24
|
};
|
|
@@ -37,8 +35,8 @@ export var convertBlob = function (blob) {
|
|
|
37
35
|
* ```
|
|
38
36
|
*/
|
|
39
37
|
export function getExtension(mimeType) {
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
const cleanMimeType = mimeType.split(';')[0].trim().toLowerCase();
|
|
39
|
+
const mimeMap = {
|
|
42
40
|
'application/pdf': 'pdf',
|
|
43
41
|
'application/msword': 'doc',
|
|
44
42
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
|
|
@@ -113,12 +111,12 @@ export function getExtension(mimeType) {
|
|
|
113
111
|
if (mimeMap[cleanMimeType]) {
|
|
114
112
|
return mimeMap[cleanMimeType];
|
|
115
113
|
}
|
|
116
|
-
|
|
114
|
+
const parts = cleanMimeType.split('/');
|
|
117
115
|
if (parts.length === 2) {
|
|
118
|
-
|
|
116
|
+
let subtype = parts[1];
|
|
119
117
|
subtype = subtype.replace(/^(x-|vnd\.)/, '');
|
|
120
118
|
subtype = subtype.split('+')[0];
|
|
121
|
-
|
|
119
|
+
const dashParts = subtype.split('-');
|
|
122
120
|
subtype = dashParts[dashParts.length - 1];
|
|
123
121
|
if (subtype && /^[a-z0-9]{2,4}$/.test(subtype)) {
|
|
124
122
|
return subtype;
|
|
@@ -140,10 +138,9 @@ export function getExtension(mimeType) {
|
|
|
140
138
|
* console.log(filename) // 'download.pdf'
|
|
141
139
|
* ```
|
|
142
140
|
*/
|
|
143
|
-
export function filenameWithExtension(blob, prefix) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return "".concat(prefix, ".").concat(ext);
|
|
141
|
+
export function filenameWithExtension(blob, prefix = 'download') {
|
|
142
|
+
const ext = getExtension(blob.type);
|
|
143
|
+
return `${prefix}.${ext}`;
|
|
147
144
|
}
|
|
148
145
|
/**
|
|
149
146
|
* Creates an HTMLAnchorElement configured for downloading a Blob.
|
|
@@ -164,16 +161,15 @@ export function filenameWithExtension(blob, prefix) {
|
|
|
164
161
|
* URL.revokeObjectURL(blobUrl)
|
|
165
162
|
* ```
|
|
166
163
|
*/
|
|
167
|
-
export
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
var anchor = document.createElement('a');
|
|
164
|
+
export const createDownloadAnchor = (blob, options) => {
|
|
165
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
166
|
+
const anchor = document.createElement('a');
|
|
171
167
|
anchor.href = blobUrl;
|
|
172
|
-
anchor.target =
|
|
173
|
-
if (options
|
|
168
|
+
anchor.target = options?.target ?? '_blank';
|
|
169
|
+
if (options?.filename) {
|
|
174
170
|
anchor.download = options.filename;
|
|
175
171
|
}
|
|
176
|
-
return { anchor
|
|
172
|
+
return { anchor, blobUrl };
|
|
177
173
|
};
|
|
178
174
|
/**
|
|
179
175
|
* Triggers a download of a Blob by creating a temporary anchor, clicking it, and cleaning up.
|
|
@@ -189,12 +185,12 @@ export var createDownloadAnchor = function (blob, options) {
|
|
|
189
185
|
* // Download starts immediately, cleanup is automatic
|
|
190
186
|
* ```
|
|
191
187
|
*/
|
|
192
|
-
export
|
|
193
|
-
|
|
188
|
+
export const downloadBlob = (blob, filename) => {
|
|
189
|
+
const { anchor, blobUrl } = createDownloadAnchor(blob, { filename });
|
|
194
190
|
document.body.appendChild(anchor);
|
|
195
191
|
anchor.click();
|
|
196
192
|
document.body.removeChild(anchor);
|
|
197
|
-
setTimeout(
|
|
193
|
+
setTimeout(() => {
|
|
198
194
|
URL.revokeObjectURL(blobUrl);
|
|
199
195
|
}, 100);
|
|
200
196
|
};
|
package/dist/utils/general.d.ts
CHANGED
|
@@ -43,3 +43,11 @@ export declare const acronym: (name: string) => string;
|
|
|
43
43
|
* @returns - data type status
|
|
44
44
|
*/
|
|
45
45
|
export declare const isNotPrimitive: (value: unknown) => value is object;
|
|
46
|
+
/**
|
|
47
|
+
* Get WhatsApp url by message and phone number if urlOnly = true, or open the url in new window.
|
|
48
|
+
*
|
|
49
|
+
* @param message - message.
|
|
50
|
+
* @param urlOnly - get WhatsApp if true, open url in new window otherwise.
|
|
51
|
+
* @param phoneNumber - the target number.
|
|
52
|
+
*/
|
|
53
|
+
export declare const toWA: (message?: string, urlOnly?: boolean, phoneNumber?: string) => string | undefined;
|
package/dist/utils/general.js
CHANGED
|
@@ -3,20 +3,20 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @param id - The ID of the element to get.
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
6
|
+
export const getById = (id) => document.getElementById(id);
|
|
7
7
|
/**
|
|
8
8
|
* Get a DOM element by any valid CSS selector.
|
|
9
9
|
*
|
|
10
10
|
* @param str - A string containing a CSS selector to match an element in the document.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
12
|
+
export const getByAny = (str) => document.querySelector(str);
|
|
13
13
|
/**
|
|
14
14
|
* Trigger a click event on an element by its ID.
|
|
15
15
|
*
|
|
16
16
|
* @param id - The ID of the element to click.
|
|
17
17
|
*/
|
|
18
|
-
export
|
|
19
|
-
|
|
18
|
+
export const clickById = (id) => {
|
|
19
|
+
const el = getById(id);
|
|
20
20
|
if (el) {
|
|
21
21
|
el.click();
|
|
22
22
|
}
|
|
@@ -26,8 +26,8 @@ export var clickById = function (id) {
|
|
|
26
26
|
*
|
|
27
27
|
* @param id - The ID of the element to focus.
|
|
28
28
|
*/
|
|
29
|
-
export
|
|
30
|
-
|
|
29
|
+
export const focusById = (id) => {
|
|
30
|
+
const el = getById(id);
|
|
31
31
|
if (el) {
|
|
32
32
|
el.focus();
|
|
33
33
|
}
|
|
@@ -39,7 +39,7 @@ export var focusById = function (id) {
|
|
|
39
39
|
* @param currentHash - Current hash in URL.
|
|
40
40
|
* @param func - Optional function to triggered before main logic.
|
|
41
41
|
*/
|
|
42
|
-
export
|
|
42
|
+
export const handleHashLink = (hash, currentHash, func) => {
|
|
43
43
|
if (func)
|
|
44
44
|
func();
|
|
45
45
|
if (hash === currentHash) {
|
|
@@ -47,7 +47,7 @@ export var handleHashLink = function (hash, currentHash, func) {
|
|
|
47
47
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
const target = getByAny(hash);
|
|
51
51
|
target.scrollIntoView({ behavior: 'smooth' });
|
|
52
52
|
}
|
|
53
53
|
};
|
|
@@ -56,8 +56,8 @@ export var handleHashLink = function (hash, currentHash, func) {
|
|
|
56
56
|
*
|
|
57
57
|
* @param name - Full name string.
|
|
58
58
|
*/
|
|
59
|
-
export
|
|
60
|
-
|
|
59
|
+
export const acronym = (name) => {
|
|
60
|
+
const parts = name.trim().split(/\s+/);
|
|
61
61
|
if (parts.length === 0)
|
|
62
62
|
return '.';
|
|
63
63
|
if (parts.length === 1)
|
|
@@ -70,6 +70,19 @@ export var acronym = function (name) {
|
|
|
70
70
|
* @param value
|
|
71
71
|
* @returns - data type status
|
|
72
72
|
*/
|
|
73
|
-
export
|
|
73
|
+
export const isNotPrimitive = (value) => {
|
|
74
74
|
return typeof value === 'object' && value !== null;
|
|
75
75
|
};
|
|
76
|
+
/**
|
|
77
|
+
* Get WhatsApp url by message and phone number if urlOnly = true, or open the url in new window.
|
|
78
|
+
*
|
|
79
|
+
* @param message - message.
|
|
80
|
+
* @param urlOnly - get WhatsApp if true, open url in new window otherwise.
|
|
81
|
+
* @param phoneNumber - the target number.
|
|
82
|
+
*/
|
|
83
|
+
export const toWA = (message = '', urlOnly = true, phoneNumber = '6283129733705') => {
|
|
84
|
+
const whatsappUrl = `https://wa.me/${phoneNumber}${message ? `?text=${encodeURIComponent(message)}` : ''}`;
|
|
85
|
+
if (urlOnly)
|
|
86
|
+
return whatsappUrl;
|
|
87
|
+
window.open(whatsappUrl, '_blank');
|
|
88
|
+
};
|
package/dist/utils/local.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Clear localStorage.
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export const clearLocals = () => localStorage.clear();
|
|
5
5
|
/**
|
|
6
6
|
* Removes an item from localStorage by key.
|
|
7
7
|
*
|
|
8
8
|
* @param key - The key of the item to remove.
|
|
9
9
|
*/
|
|
10
|
-
export
|
|
10
|
+
export const removeLocal = (key) => localStorage.removeItem(key);
|
|
11
11
|
/**
|
|
12
12
|
* Sets an item in localStorage after serializing it to JSON.
|
|
13
13
|
*
|
|
14
14
|
* @param key - The key under which to store the value.
|
|
15
15
|
* @param value - The value to store (will be JSON-stringified).
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
17
|
+
export const setLocal = (key, value) => localStorage.setItem(key, JSON.stringify(value));
|
|
18
18
|
/**
|
|
19
19
|
* Sets multiple items in localStorage after serializing it to JSON.
|
|
20
20
|
*
|
|
21
21
|
* @param items - Array of local storage items to set.
|
|
22
22
|
*/
|
|
23
|
-
export
|
|
24
|
-
items.forEach(
|
|
23
|
+
export const setLocals = (items) => {
|
|
24
|
+
items.forEach(item => setLocal(item.key, item.value));
|
|
25
25
|
};
|
|
26
26
|
/**
|
|
27
27
|
* Retrieves and parses a JSON item from localStorage by key.
|
|
@@ -29,14 +29,14 @@ export var setLocals = function (items) {
|
|
|
29
29
|
* @param key - The key of the item to retrieve.
|
|
30
30
|
* @returns The parsed value from localStorage, or null if not found.
|
|
31
31
|
*/
|
|
32
|
-
export
|
|
33
|
-
|
|
32
|
+
export const getLocal = (key) => {
|
|
33
|
+
const item = localStorage.getItem(key);
|
|
34
34
|
try {
|
|
35
35
|
if (item)
|
|
36
36
|
return JSON.parse(item);
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
|
-
catch
|
|
39
|
+
catch {
|
|
40
40
|
return item;
|
|
41
41
|
}
|
|
42
42
|
};
|
|
@@ -46,8 +46,8 @@ export var getLocal = function (key) {
|
|
|
46
46
|
* @param keys - The keys of the items to retrieve.
|
|
47
47
|
* @returns The parsed values from localStorage.
|
|
48
48
|
*/
|
|
49
|
-
export
|
|
50
|
-
return keys.reduce(
|
|
49
|
+
export const getLocals = (keys) => {
|
|
50
|
+
return keys.reduce((acc, key) => {
|
|
51
51
|
acc[key] = getLocal(key);
|
|
52
52
|
return acc;
|
|
53
53
|
}, {});
|