@carlsebastian/jsu 1.0.34
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/LICENSE +201 -0
- package/package.json +58 -0
- package/src/global.js +7 -0
- package/src/types/api/api.d.ts +147 -0
- package/src/types/api/element.d.ts +55 -0
- package/src/types/custom/custom.d.ts +8 -0
- package/src/types/custom/error/builder/error.builder.d.ts +78 -0
- package/src/types/custom/error/constructor/error.base.d.ts +4 -0
- package/src/types/custom/error/constructor/error.custom.d.ts +34 -0
- package/src/types/custom/error/constructor/error.meta.d.ts +18 -0
- package/src/types/custom/error/error.d.ts +5 -0
- package/src/types/custom/utils/custom.utils.d.ts +42 -0
- package/src/types/custom/utils/generator/generator.d.ts +92 -0
- package/src/types/dom/attr/attr.class.d.ts +81 -0
- package/src/types/dom/attr/attr.id.d.ts +23 -0
- package/src/types/dom/attr/attr.style.d.ts +32 -0
- package/src/types/dom/dom.d.ts +8 -0
- package/src/types/dom/element/create/element.create.d.ts +67 -0
- package/src/types/dom/element/getElementBy/dom.getElementBy.d.ts +71 -0
- package/src/types/dom/element/tag-verifier/verifier.d.ts +16 -0
- package/src/types/global.d.ts +13 -0
- package/src/types/guards/data-types/data-types.d.ts +5 -0
- package/src/types/guards/formats/formats.d.ts +5 -0
- package/src/types/guards/guards.d.ts +8 -0
- package/src/types/primitives/obj/obj.accessor.d.ts +5 -0
- package/src/types/primitives/obj/obj.iterator.d.ts +5 -0
- package/src/types/primitives/primitives.d.ts +8 -0
- package/src/types/primitives/str/str.d.ts +26 -0
- package/src/types/storage/local/storage.local.d.ts +86 -0
- package/src/types/storage/session/storage.session.d.ts +86 -0
- package/src/types/storage/storage.d.ts +8 -0
- package/src/utils/custom/custom.js +20 -0
- package/src/utils/custom/error/builder/error.builder.js +181 -0
- package/src/utils/custom/error/constructor/error.base.js +71 -0
- package/src/utils/custom/error/constructor/error.custom.js +107 -0
- package/src/utils/custom/error/error.js +23 -0
- package/src/utils/custom/utils/custom.utils.js +150 -0
- package/src/utils/custom/utils/generator/generator.js +222 -0
- package/src/utils/dom/attr/attr.class.js +186 -0
- package/src/utils/dom/attr/attr.id.js +64 -0
- package/src/utils/dom/attr/attr.style.js +128 -0
- package/src/utils/dom/dom.js +29 -0
- package/src/utils/dom/element/create/element.create.js +312 -0
- package/src/utils/dom/element/getElementBy/dom.getElementBy.js +171 -0
- package/src/utils/dom/element/query/dom.query.js +75 -0
- package/src/utils/dom/element/tag-verifier/verifier.js +60 -0
- package/src/utils/dom/lifecycle/mount.js +48 -0
- package/src/utils/dom/lifecycle/unmount.js +43 -0
- package/src/utils/guards/data-types/data-types.js +201 -0
- package/src/utils/guards/formats/formats.js +274 -0
- package/src/utils/guards/guards.js +21 -0
- package/src/utils/primitives/obj/obj.accessor.js +242 -0
- package/src/utils/primitives/obj/obj.iterator.js +148 -0
- package/src/utils/primitives/primitives.js +23 -0
- package/src/utils/primitives/str/str.js +52 -0
- package/src/utils/storage/local/storage.local.js +236 -0
- package/src/utils/storage/session/storage.session.js +236 -0
- package/src/utils/storage/storage.js +59 -0
- package/src/utils/variables.js +78 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import Emit from '../../custom/error/builder/error.builder.js';
|
|
2
|
+
|
|
3
|
+
/* -- Helper -- */
|
|
4
|
+
/**
|
|
5
|
+
* Validates the parameter `localKey` format.
|
|
6
|
+
*
|
|
7
|
+
* @param { string } caller - The caller method id that performs this validation.
|
|
8
|
+
* @param { string } localKey - The access key of the storage to validate.
|
|
9
|
+
* @param { string } processId - The process of caller id that is going to be performed.
|
|
10
|
+
* @returns { string | false } The state of validation process result.
|
|
11
|
+
*/
|
|
12
|
+
function ValidateStorageKey(caller, localKey, processId) {
|
|
13
|
+
const Method = typeof caller === "string" && caller.length > 0
|
|
14
|
+
? caller : `STORAGE.LOCAL.(ANONYMOUS)`;
|
|
15
|
+
|
|
16
|
+
if (typeof localKey !== "string")
|
|
17
|
+
Emit._ArgumentError(Method, "localKey", localKey, "String");
|
|
18
|
+
|
|
19
|
+
if (typeof processId !== "string")
|
|
20
|
+
Emit._ArgumentError(Method, "processId", processId, "String");
|
|
21
|
+
|
|
22
|
+
const Key = localKey.trim();
|
|
23
|
+
if (Key.length === 0) {
|
|
24
|
+
console.warn(`${Method}(): Cannot perform the ${processId} process with an empty string of storage access key!`);
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return Key;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A local `storage` API methods for accessing user's built-in browser local storage.
|
|
33
|
+
*/
|
|
34
|
+
const LocalStorageMethods = {
|
|
35
|
+
/**
|
|
36
|
+
* Name of this object.
|
|
37
|
+
*/
|
|
38
|
+
NAME: "LOCAL",
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The total count of existing items at local `storage`.
|
|
42
|
+
*/
|
|
43
|
+
ITEMS_COUNT: localStorage.length,
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The total used storage size (MB) in bytes that is calculated in UTF-16 for every existing data at local `storage`.
|
|
47
|
+
*
|
|
48
|
+
* ***Notes***:
|
|
49
|
+
* - This is not the base for checking on the maximum size in (bytes) of local `storage` that it can hold, but
|
|
50
|
+
* can be used to check on the current used (bytes) size of local `storage`.
|
|
51
|
+
* - The maximum (bytes) size of local `storage` may vary on your or the user's browser environment. But commonly is
|
|
52
|
+
* ~ `5` Megabytes per domain.
|
|
53
|
+
*
|
|
54
|
+
* @returns { number } The current total used (bytes) size in local `storage`.
|
|
55
|
+
*/
|
|
56
|
+
SIZE() {
|
|
57
|
+
let Byte = 0;
|
|
58
|
+
|
|
59
|
+
for (let I = 0; I < this.ITEMS_COUNT; I++) {
|
|
60
|
+
const Key = localStorage.key(I);
|
|
61
|
+
|
|
62
|
+
if (!Key)
|
|
63
|
+
continue;
|
|
64
|
+
|
|
65
|
+
const Data = localStorage.getItem(Key) ?? "";
|
|
66
|
+
|
|
67
|
+
Byte += (Key.length + Data.length) * 2;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return Byte;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A state that indicates whether if the local `storage` API is supported in user's browser version.
|
|
75
|
+
*/
|
|
76
|
+
IS_SUPPORTED: (function () {
|
|
77
|
+
try {
|
|
78
|
+
const Key = "__TEST_LOCAL_KEY__";
|
|
79
|
+
|
|
80
|
+
localStorage.setItem(Key, "__TEST__");
|
|
81
|
+
localStorage.removeItem(Key);
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
})(),
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the data of the specified access key of item at local `storage`.
|
|
91
|
+
*
|
|
92
|
+
* ***Notes***:
|
|
93
|
+
* - If there's no existing item for the specified access key at local `storage`, it will
|
|
94
|
+
* return a default response `object`.
|
|
95
|
+
* - Automatically parsed the data of the item of specified access key at local `storage` into its
|
|
96
|
+
* original type by using `JSON.parse()` parser. (Can be disabled by providing a `false` state as second parameter.)
|
|
97
|
+
*
|
|
98
|
+
* @template { boolean } [B=true]
|
|
99
|
+
* @overload
|
|
100
|
+
* @param { string } localKey - The access key of item at local `storage`.
|
|
101
|
+
* @param { B } [autoParse] - A optional state parameter for automatically parsing the retrieved data.
|
|
102
|
+
* @returns {{
|
|
103
|
+
* State: boolean,
|
|
104
|
+
* Data: B extends true ? unknown : string | undefined,
|
|
105
|
+
* Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED"
|
|
106
|
+
* }} The response object result of retrieval process.
|
|
107
|
+
*/
|
|
108
|
+
GET(localKey, autoParse = true) {
|
|
109
|
+
const Method = "STORAGE.LOCAL.GET", Response = { State: false, Data: undefined };
|
|
110
|
+
|
|
111
|
+
if (!this.IS_SUPPORTED) {
|
|
112
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
113
|
+
return { ...Response, Reason: "NOT_SUPPORTED" };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const Key = ValidateStorageKey(Method, localKey, "retrieval");
|
|
117
|
+
if (Key === false)
|
|
118
|
+
return { ...Response, Reason: "INVALID_KEY" };
|
|
119
|
+
|
|
120
|
+
if (typeof autoParse !== "boolean")
|
|
121
|
+
autoParse = true;
|
|
122
|
+
|
|
123
|
+
if (this.ITEMS_COUNT === 0 || Object.keys(localStorage).indexOf(Key) < 0)
|
|
124
|
+
return { ...Response, Reason: "NO_SUCH_KEY" };
|
|
125
|
+
|
|
126
|
+
const Data = localStorage.getItem(Key);
|
|
127
|
+
|
|
128
|
+
Response.State = true;
|
|
129
|
+
Response.Data = autoParse ? JSON.parse(Data) : Data;
|
|
130
|
+
|
|
131
|
+
return Response;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Store a new local `storage` item with the specified access key and its data at local `storage`.
|
|
136
|
+
*
|
|
137
|
+
* ***Notes***:
|
|
138
|
+
* - Terminates the creation process when there's already existing local item with the same key provided
|
|
139
|
+
* at local `storage`.
|
|
140
|
+
*
|
|
141
|
+
* @param { string } localKey - The access key of item at local `storage`.
|
|
142
|
+
* @param { any } [data] - The data to store at new local item. (Default: undefined)
|
|
143
|
+
* @returns {{ State: boolean, Reason?: "DUPLICATE_KEY_FOUND" | "INVALID_KEY" | "NOT_SUPPORTED" }} The response object result of creation process.
|
|
144
|
+
*/
|
|
145
|
+
NEW(localKey, data) {
|
|
146
|
+
const Method = "STORAGE.LOCAL.NEW", Response = { State: false, Reason: undefined };
|
|
147
|
+
|
|
148
|
+
if (!this.IS_SUPPORTED) {
|
|
149
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
150
|
+
Response.Reason = "NOT_SUPPORTED";
|
|
151
|
+
return Response;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const Key = ValidateStorageKey(Method, localKey, "creation");
|
|
155
|
+
if (Key === false) {
|
|
156
|
+
Response.Reason = "INVALID_KEY";
|
|
157
|
+
return Response;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (this.GET(Key).State) {
|
|
161
|
+
Response.Reason = "DUPLICATE_KEY_FOUND";
|
|
162
|
+
return Response;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
localStorage.setItem(Key, JSON.stringify(data));
|
|
166
|
+
Response.State = true;
|
|
167
|
+
return Response;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Removes an item at local `storage` that matches the specified access key.
|
|
172
|
+
*
|
|
173
|
+
* @param { string } localKey - The access key of item at local `storage`.
|
|
174
|
+
* @returns {{ State: boolean, Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED" }} The response object result of removal process.
|
|
175
|
+
*/
|
|
176
|
+
REMOVE(localKey) {
|
|
177
|
+
const Method = "STORAGE.LOCAL.REMOVE", Response = { State: false, Reason: undefined };
|
|
178
|
+
|
|
179
|
+
if (!this.IS_SUPPORTED) {
|
|
180
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
181
|
+
Response.Reason = "NOT_SUPPORTED";
|
|
182
|
+
return Response;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const Key = ValidateStorageKey(Method, localKey, "removal");
|
|
186
|
+
if (Key === false) {
|
|
187
|
+
Response.Reason = "INVALID_KEY";
|
|
188
|
+
return Response;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!this.GET(Key).State) {
|
|
192
|
+
Response.Reason = "NO_SUCH_KEY";
|
|
193
|
+
return Response;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
localStorage.removeItem(Key);
|
|
197
|
+
Response.State = true;
|
|
198
|
+
return Response;
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Updates the current data with the specified new data of an item at local `storage` that matches the specified access key.
|
|
203
|
+
*
|
|
204
|
+
* @param { string } localKey - The access key of item at local `storage`.
|
|
205
|
+
* @param { any } newData - The data to set as new data of the item of local `storage`.
|
|
206
|
+
* @returns {{ State: boolean, Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED" }} The response object result of update process.
|
|
207
|
+
*/
|
|
208
|
+
UPDATE(localKey, newData) {
|
|
209
|
+
const Method = "STORAGE.LOCAL.UPDATE", Response = { State: false, Reason: undefined };
|
|
210
|
+
|
|
211
|
+
if (!this.IS_SUPPORTED) {
|
|
212
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
213
|
+
Response.Reason = "NOT_SUPPORTED"
|
|
214
|
+
return Response;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const Key = ValidateStorageKey(Method, localKey, "data update");
|
|
218
|
+
if (Key === false) {
|
|
219
|
+
Response.Reason = "INVALID_KEY";
|
|
220
|
+
return Response;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let Local = this.GET(Key);
|
|
224
|
+
if (!Local.State) {
|
|
225
|
+
Response.Reason = "NO_SUCH_KEY";
|
|
226
|
+
return Response;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
Local.Data = JSON.stringify(newData);
|
|
230
|
+
localStorage.setItem(Key, Local.Data);
|
|
231
|
+
Response.State = true;
|
|
232
|
+
return Response;
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export default LocalStorageMethods;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import Emit from '../../custom/error/builder/error.builder.js';
|
|
2
|
+
|
|
3
|
+
/* Helper */
|
|
4
|
+
/**
|
|
5
|
+
* Validates the parameter `sessionKey` format.
|
|
6
|
+
*
|
|
7
|
+
* @param { string } caller - The caller method id that performs this validation.
|
|
8
|
+
* @param { string } sessionKey - The access key of the storage to validate.
|
|
9
|
+
* @param { string } processId - The process of caller id that is going to be performed.
|
|
10
|
+
* @returns { string | false } The state of validation process result.
|
|
11
|
+
*/
|
|
12
|
+
function ValidateStorageKey(caller, sessionKey, processId) {
|
|
13
|
+
const Method = typeof caller === "string" && caller.length > 0
|
|
14
|
+
? caller : `STORAGE.SESSION.(ANONYMOUS)`;
|
|
15
|
+
|
|
16
|
+
if (typeof sessionKey !== "string")
|
|
17
|
+
Emit._ArgumentError(Method, "sessionKey", sessionKey, "String");
|
|
18
|
+
|
|
19
|
+
if (typeof processId !== "string")
|
|
20
|
+
Emit._ArgumentError(Method, "processId", processId, "String");
|
|
21
|
+
|
|
22
|
+
const Key = sessionKey.trim();
|
|
23
|
+
if (Key.length === 0) {
|
|
24
|
+
console.warn(`${Method}(): Cannot perform the ${processId} process with an empty string of storage access key!`);
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return Key;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A session `storage` API methods for accessing user's built-in browser session storage.
|
|
33
|
+
*/
|
|
34
|
+
const SessionStorageMethods = {
|
|
35
|
+
/**
|
|
36
|
+
* The name of this object.
|
|
37
|
+
*/
|
|
38
|
+
NAME: "SESSION",
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The total count of existing items at session `storage`.
|
|
42
|
+
*/
|
|
43
|
+
ITEM_COUNT: sessionStorage.length,
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The total accumulated size of session `storage` API in `bytes` format.
|
|
47
|
+
*
|
|
48
|
+
* ***Notes***:
|
|
49
|
+
* - This is not the base for checking on the maximum size in (bytes) of session `storage` that it can hold, but
|
|
50
|
+
* can be used to check on the current accumulated (bytes) size of session `storage`.
|
|
51
|
+
* - The maximum (bytes) size of session `storage` may vary on your or the user's browser environment. But commonly is
|
|
52
|
+
* ~ `5` Megabytes per domain.
|
|
53
|
+
*
|
|
54
|
+
* @returns { number } The current total used (bytes) size in session `storage`.
|
|
55
|
+
*/
|
|
56
|
+
SIZE() {
|
|
57
|
+
let Byte = 0;
|
|
58
|
+
|
|
59
|
+
for (let I = 0; I < this.ITEM_COUNT; I++) {
|
|
60
|
+
const Key = localStorage.key(I);
|
|
61
|
+
|
|
62
|
+
if (!Key)
|
|
63
|
+
continue;
|
|
64
|
+
|
|
65
|
+
const Data = localStorage.getItem(Key) ?? "";
|
|
66
|
+
|
|
67
|
+
Byte += (Key.length + Data.length) * 2;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return Byte;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A state that indicates whether if the session `storage` API is supported in user's browser version.
|
|
75
|
+
*/
|
|
76
|
+
IS_SUPPORTED: (function () {
|
|
77
|
+
try {
|
|
78
|
+
const Key = "__TEST_SESSION_KEY__";
|
|
79
|
+
|
|
80
|
+
sessionStorage.setItem(Key, "__TEST__");
|
|
81
|
+
sessionStorage.removeItem(Key);
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
})(),
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the data of the specified access key of item at session `storage`.
|
|
91
|
+
*
|
|
92
|
+
* ***Notes***:
|
|
93
|
+
* - If there's no existing item for the specified access key at session `storage`, it will
|
|
94
|
+
* return a default response `object`.
|
|
95
|
+
* - Automatically parsed the data of the item of specified access key at session `storage` into its
|
|
96
|
+
* original type by using `JSON.parse()` parser. (Can be disabled by providing a `false` state as second parameter.)
|
|
97
|
+
*
|
|
98
|
+
* @template { boolean } [B=true]
|
|
99
|
+
* @overload
|
|
100
|
+
* @param { string } sessionKey - The access key of item at session `storage`.
|
|
101
|
+
* @param { B } [autoParse] - A optional state parameter for automatically parsing the retrieved data.
|
|
102
|
+
* @returns {{
|
|
103
|
+
* State: boolean,
|
|
104
|
+
* Data: B extends true ? unknown : string | undefined,
|
|
105
|
+
* Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED"
|
|
106
|
+
* }} The response object result of retrieval process.
|
|
107
|
+
*/
|
|
108
|
+
GET(sessionKey, autoParse = true) {
|
|
109
|
+
const Method = `STORAGE.${this.NAME}.GET`, Response = { State: false, Data: undefined };
|
|
110
|
+
|
|
111
|
+
if (!this.IS_SUPPORTED) {
|
|
112
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
113
|
+
return { ...Response, Reason: "NOT_SUPPORTED" };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const Key = ValidateStorageKey(Method, sessionKey, "retrieval");
|
|
117
|
+
if (!Key)
|
|
118
|
+
return { ...Response, Reason: "INVALID_KEY" };
|
|
119
|
+
|
|
120
|
+
if (typeof autoParse !== "boolean")
|
|
121
|
+
autoParse = true;
|
|
122
|
+
|
|
123
|
+
if (this.ITEM_COUNT === 0 || Object.keys(sessionStorage).indexOf(Key) < 0)
|
|
124
|
+
return { ...Response, Reason: "NO_SUCH_KEY" };
|
|
125
|
+
|
|
126
|
+
const Data = sessionStorage.getItem(Key);
|
|
127
|
+
Response.State = true;
|
|
128
|
+
Response.Data = autoParse ? JSON.parse(Data) : Data;
|
|
129
|
+
|
|
130
|
+
return Response;
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Store a new session `storage` item with the specified access key and its data at session `storage`.
|
|
135
|
+
*
|
|
136
|
+
* ***Notes***:
|
|
137
|
+
* - Terminates the creation process when there's already existing session item with the same key provided
|
|
138
|
+
* at session `storage`.
|
|
139
|
+
*
|
|
140
|
+
* @param { string } sessionKey - The access key of item at session `storage`.
|
|
141
|
+
* @param { any } [data] - The data to store at new session item. (Default: undefined)
|
|
142
|
+
* @returns {{ State: boolean, Reason?: "DUPLICATE_KEY_FOUND" | "INVALID_KEY" | "NOT_SUPPORTED" }} The response object result of creation process.
|
|
143
|
+
*/
|
|
144
|
+
NEW(sessionKey, data) {
|
|
145
|
+
const Method = `STORAGE.${this.NAME}.NEW`, Response = { State: false, Reason: undefined };
|
|
146
|
+
|
|
147
|
+
if (!this.IS_SUPPORTED) {
|
|
148
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
149
|
+
Response.Reason = "NOT_SUPPORTED";
|
|
150
|
+
return Response;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const Key = ValidateStorageKey(Method, sessionKey, "creation");
|
|
154
|
+
if (!Key) {
|
|
155
|
+
Response.Reason = "INVALID_KEY";
|
|
156
|
+
return Response;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (this.GET(Key).State) {
|
|
160
|
+
console.warn(`${Method}(): There's already existing session storage item with an access key of '${Key}'! (Exited)`);
|
|
161
|
+
Response.Reason = "DUPLICATE_KEY_FOUND";
|
|
162
|
+
return Response;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
sessionStorage.setItem(sessionKey, JSON.stringify(data));
|
|
166
|
+
Response.State = true;
|
|
167
|
+
return Response;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Removes an item at session `storage` that matches the specified access key.
|
|
172
|
+
*
|
|
173
|
+
* @param { string } sessionKey - The access key of item at session `storage`.
|
|
174
|
+
* @returns {{ State: boolean, Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED" }} The response object result of removal process.
|
|
175
|
+
*/
|
|
176
|
+
REMOVE(sessionKey) {
|
|
177
|
+
const Method = `STORAGE.${this.NAME}.REMOVE`, Response = { State: false, Reason: undefined };
|
|
178
|
+
|
|
179
|
+
if (!this.IS_SUPPORTED) {
|
|
180
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
181
|
+
Response.Reason = "NOT_SUPPORTED";
|
|
182
|
+
return Response;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const Key = ValidateStorageKey(sessionKey);
|
|
186
|
+
if (!Key) {
|
|
187
|
+
Response.Reason = "INVALID_KEY";
|
|
188
|
+
return Response;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!this.GET(Key).State) {
|
|
192
|
+
Response.Reason = "NO_SUCH_KEY";
|
|
193
|
+
return Response;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
sessionStorage.removeItem(Key);
|
|
197
|
+
Response.State = true;
|
|
198
|
+
return Response;
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Updates the current data with the specified new data of an item at session `storage` that matches the specified access key.
|
|
203
|
+
*
|
|
204
|
+
* @param { string } sessionKey - The access key of item at session `storage`.
|
|
205
|
+
* @param { any } newData - The data to set as new data of the item of session `storage`.
|
|
206
|
+
* @returns {{ State: boolean, Reason?: "INVALID_KEY" | "NO_SUCH_KEY" | "NOT_SUPPORTED" }} The response object result of update process.
|
|
207
|
+
*/
|
|
208
|
+
UPDATE(sessionKey, newData) {
|
|
209
|
+
const Method = `STORAGE.${this.NAME}.UPDATE`, Response = { State: false, Reason: undefined };
|
|
210
|
+
|
|
211
|
+
if (!this.IS_SUPPORTED) {
|
|
212
|
+
console.error(`${Method}(): Your current environment does not supported 'Storage' API! Please upgrade your environment to its latest version.`);
|
|
213
|
+
Response.Reason = "NOT_SUPPORTED";
|
|
214
|
+
return Response;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const Key = ValidateStorageKey(sessionKey);
|
|
218
|
+
if (!Key) {
|
|
219
|
+
Response.Reason = "INVALID_KEY";
|
|
220
|
+
return Response;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const This = this.GET(Key);
|
|
224
|
+
if (!This.State) {
|
|
225
|
+
Response.Reason = This.Reason;
|
|
226
|
+
return Response;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
This.Data = JSON.stringify(newData);
|
|
230
|
+
sessionStorage.setItem(Key, This.Data);
|
|
231
|
+
Response.State = true;
|
|
232
|
+
return Response;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export default SessionStorageMethods;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import Emit from '../custom/error/builder/error.builder.js';
|
|
2
|
+
import { Global } from '../custom/utils/custom.utils.js';
|
|
3
|
+
import { IsNullOrUndefined, IsPropertyAt } from '../guards/data-types/data-types.js';
|
|
4
|
+
import LocalStorageMethods from './local/storage.local.js';
|
|
5
|
+
import SessionStorageMethods from './session/storage.session.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A built-in `Storage` API interfaces for user's browser environment.
|
|
9
|
+
*/
|
|
10
|
+
const STORAGE = {
|
|
11
|
+
/**
|
|
12
|
+
* The name of this object.
|
|
13
|
+
* @readonly
|
|
14
|
+
*/
|
|
15
|
+
NAME: "STORAGE",
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Verifies a state whether if all of available `storage` instances or API are supported in your or user's current environment.
|
|
19
|
+
*
|
|
20
|
+
* ***Notes***:
|
|
21
|
+
* - Does not throw a `NotSupportedError` when there's at least 1 or more of the `storage` instances or API that is supported.
|
|
22
|
+
* - Only throw a `NotSupportedError` when trying to access the not supported `storage` instances or API.
|
|
23
|
+
*
|
|
24
|
+
* @returns { true | void }
|
|
25
|
+
*/
|
|
26
|
+
IS_SUPPORTED() {
|
|
27
|
+
if (!this.LOCAL.IS_SUPPORTED && !this.SESSION.IS_SUPPORTED)
|
|
28
|
+
Emit._NotSupportedError(`${this.NAME}.${this.IS_SUPPORTED.name}`, this.NAME);
|
|
29
|
+
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A local `storage` API for storing user's activity or history permanently which can be used for storing
|
|
35
|
+
* configurations, and many more.
|
|
36
|
+
*
|
|
37
|
+
* ***Notes***:
|
|
38
|
+
* - This local `storage` API has a very limited size to offer, that is could only be around `5` to `10` Megabytes (MB).
|
|
39
|
+
*
|
|
40
|
+
* @readonly
|
|
41
|
+
*/
|
|
42
|
+
LOCAL: LocalStorageMethods,
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A session `storage` API methods for storing user's activity temporarily that would be automatically
|
|
46
|
+
* removed once the page changed or the user's leaved.
|
|
47
|
+
*
|
|
48
|
+
* ***Notes***:
|
|
49
|
+
* - This session `storage` API has a very limited size to offer, that is could only about `5` to `10` Megabytes (MB).
|
|
50
|
+
*
|
|
51
|
+
* @readonly
|
|
52
|
+
*/
|
|
53
|
+
SESSION: SessionStorageMethods,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default STORAGE;
|
|
57
|
+
|
|
58
|
+
if (IsNullOrUndefined(globalThis.STORAGE) || !IsPropertyAt(globalThis, "STORAGE"))
|
|
59
|
+
Global("STORAGE", STORAGE, "soft");
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A collection of HTMLElements tags.
|
|
3
|
+
*
|
|
4
|
+
* ***Source***: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements
|
|
5
|
+
*
|
|
6
|
+
* @type { Map<string, { Generate: () => HTMLElement }> }
|
|
7
|
+
*/
|
|
8
|
+
export const HTMLElementTags = [
|
|
9
|
+
"a", "abbr", "acronym", "address", "address", "area", "article", "aside", "audio", "b", "base", "bdi",
|
|
10
|
+
"bdo", "big", "blockquote", "body", "br", "button", "canvas", "caption", "center", "cite", "code", "col",
|
|
11
|
+
"colgroup", "data", "datalist", "dd", "del", "details", "dfn", "dialog", "dir", "div", "dl", "dt", "em",
|
|
12
|
+
"embed", "fencedframe", "fieldset", "figcaption", "figure", "font", "footer", "form", "frame", "frameset",
|
|
13
|
+
"geolocation", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", "i", "iframe",
|
|
14
|
+
"img", "input", "ins", "kbd", "label", "legend", "li", "link", "main", "map", "mark", "marquee", "menu",
|
|
15
|
+
"meta", "meter", "nav", "nobr", "noembed", "noframes", "noscript", "object", "ol", "optgroup", "option",
|
|
16
|
+
"output", "p", "param", "picture", "plaintext", "pre", "progress", "q", "rb", "rp", "rt", "rtc", "ruby",
|
|
17
|
+
"s", "samp", "script", "search", "section", "select", "selectioncontent", "slot", "small", "source", "span",
|
|
18
|
+
"strike", "strong", "style", "sub", "summary", "sup", "table", "tbody", "td", "template", "textarea", "tfoot",
|
|
19
|
+
"th", "thead", "time", "title", "tr", "track", "tt", "u", "ul", "var", "video", "wbr", "xmp"
|
|
20
|
+
].reduce((Acc, Tag) => {
|
|
21
|
+
const T = Tag.trim().toLowerCase();
|
|
22
|
+
Acc.set(T, { Generate: () => document.createElement(T) });
|
|
23
|
+
return Acc;
|
|
24
|
+
}, new Map([]));
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A collection of SVGElements tags.
|
|
28
|
+
*
|
|
29
|
+
* ***Source***: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element
|
|
30
|
+
*
|
|
31
|
+
* @type { Map<string, { Generate: () => SVGElement}> }
|
|
32
|
+
*/
|
|
33
|
+
export const SVGElementTags = [
|
|
34
|
+
"a", "animate", "animateMotion", "animateTransform", "circle", "clipPath", "defs", "desc", "ellipse",
|
|
35
|
+
"feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting",
|
|
36
|
+
"feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR",
|
|
37
|
+
"feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting",
|
|
38
|
+
"feSpotLight", "feTile", "feTurbulence", "filter", "foreignObject", "g", "image", "line", "linearGradient",
|
|
39
|
+
"marker", "mask", "metadata", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect",
|
|
40
|
+
"script", "set", "stop", "style", "svg", "switch", "symbol", "text", "textPath", "title", "tspan", "use",
|
|
41
|
+
"view"
|
|
42
|
+
].reduce((Acc, Tag) => {
|
|
43
|
+
const T = Tag.trim().toLowerCase();
|
|
44
|
+
Acc.set(T, { Generate: () => document.createElementNS("http://www.w3.org/2000/svg", T) });
|
|
45
|
+
return Acc;
|
|
46
|
+
}, new Map([]));
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A collection of MathMLElements tags.
|
|
50
|
+
*
|
|
51
|
+
* ***Source***: https://developer.mozilla.org/en-US/docs/Web/MathML/Reference/Element
|
|
52
|
+
*
|
|
53
|
+
* @type { Map<string, { Generate: () => MathMLElement}> }
|
|
54
|
+
*/
|
|
55
|
+
export const MathMLElementTags = [
|
|
56
|
+
"math", "maction", "annotation", "annotation-xml", "menclose", "merror", "mfenced", "mfrac", "mi", "mmultiscripts",
|
|
57
|
+
"mn", "mo", "mover", "mpadded", "mphantom", "mpresscripts", "mroot", "mrow", "ms", "semantics", "mspace",
|
|
58
|
+
"msqrt", "mstyle", "msub", "msup", "msubsup", "mtable", "mtd", "mtext", "mtr", "munder", "munderover"
|
|
59
|
+
].reduce((Acc, Tag) => {
|
|
60
|
+
const T = Tag.trim().toLowerCase();
|
|
61
|
+
Acc.set(T, { Generate: () => document.createElementNS("http://www.w3.org/1998/Math/MathML", T) });
|
|
62
|
+
return Acc;
|
|
63
|
+
}, new Map([]));
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A collection of available element's XML namespace.
|
|
67
|
+
*/
|
|
68
|
+
export const XMLNameSpace = ["http://www.w3.org/1998/Math/MathML", "http://www.w3.org/1999/xhtml", "http://www.w3.org/2000/svg"];
|
|
69
|
+
|
|
70
|
+
[
|
|
71
|
+
["HTMLElementTags", HTMLElementTags], ["MathMLElementTags", MathMLElementTags],
|
|
72
|
+
["SVGElementTags", SVGElementTags], ["XMLNameSpace", XMLNameSpace]
|
|
73
|
+
].forEach((key, val) => {
|
|
74
|
+
Object.defineProperty(globalThis, key, {
|
|
75
|
+
value: val,
|
|
76
|
+
writable: false, configurable: true, enumerable: true
|
|
77
|
+
});
|
|
78
|
+
});
|