-
Notifications
You must be signed in to change notification settings - Fork 1
/
getStorage.mjs
94 lines (92 loc) · 4.3 KB
/
getStorage.mjs
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
import { Lodash as _, Storage } from "./polyfill/index.js";
import { log } from "./lib/index.js";
/**
* Get Storage Variables
* @link https://github.com/NanoCat-Me/utils/blob/main/getStorage.mjs
* @author VirgilClyne
* @param {String} key - Persistent Store Key
* @param {Array} names - Platform Names
* @param {Object} database - Default Database
* @return {Object} { Settings, Caches, Configs }
*/
export function getStorage(key, names, database) {
//log("☑️ getStorage, Get Environment Variables", "");
/***************** Default *****************/
const Store = { Settings: database?.Default?.Settings || {}, Configs: database?.Default?.Configs || {}, Caches: {} };
//log("🚧 getStorage, Get Environment Variables, Default", `Store.Settings类型: ${typeof Store.Settings}`, `Store.Settings: ${JSON.stringify(Store.Settings)}`, "");
/***************** Database *****************/
[names].flat(Number.POSITIVE_INFINITY).forEach(name => {
Store.Settings = { ...Store.Settings, ...database?.[name]?.Settings };
Store.Configs = { ...Store.Configs, ...database?.[name]?.Configs };
});
//log("🚧 getStorage, Get Environment Variables, Database", `Store.Settings类型: ${typeof Store.Settings}`, `Store.Settings: ${JSON.stringify(Store.Settings)}`, "");
/***************** Argument *****************/
switch (typeof $argument) {
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "string":
$argument = Object.fromEntries($argument.split("&").map(item => item.split("=", 2).map(i => i.replace(/\"/g, ""))));
case "object": {
const argument = {};
Object.keys($argument).forEach(key => _.set(argument, key, $argument[key]));
//log(`✅ getStorage, Get Environment Variables`, `argument: ${JSON.stringify(argument)}`, "");
Store.Settings = { ...Store.Settings, ...argument };
break;
}
case "undefined":
break;
}
//log("🚧 getStorage, Get Environment Variables, $argument", `Store.Settings类型: ${typeof Store.Settings}`, `Store.Settings: ${JSON.stringify(Store.Settings)}`, "");
/***************** BoxJs *****************/
// 包装为局部变量,用完释放内存
// BoxJs的清空操作返回假值空字符串, 逻辑或操作符会在左侧操作数为假值时返回右侧操作数。
const BoxJs = Storage.getItem(key, database);
//log(`🚧 getStorage, Get Environment Variables`, `BoxJs类型: ${typeof BoxJs}`, `BoxJs内容: ${JSON.stringify(BoxJs || {})}`, "");
[names].flat(Number.POSITIVE_INFINITY).forEach(name => {
switch (typeof BoxJs?.[name]?.Settings) {
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "string":
BoxJs[name].Settings = JSON.parse(BoxJs[name].Settings || "{}");
case "object":
Store.Settings = { ...Store.Settings, ...BoxJs[name].Settings };
break;
case "undefined":
break;
}
switch (typeof BoxJs?.[name]?.Caches) {
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "string":
BoxJs[name].Caches = JSON.parse(BoxJs[name].Caches || "{}");
case "object":
Store.Caches = { ...Store.Caches, ...BoxJs[name].Caches };
break;
case "undefined":
break;
}
});
//log("🚧 getStorage, Get Environment Variables, BoxJs", `Store.Settings类型: ${typeof Store.Settings}`, `Store.Settings: ${JSON.stringify(Store.Settings)}`, "");
/***************** traverseObject *****************/
traverseObject(Store.Settings, (key, value) => {
//log(`🚧 getStorage, traverseObject`, `${key}: ${typeof value}`, `${key}: ${JSON.stringify(value)}`, "");
if (value === "true" || value === "false")
value = JSON.parse(value); // 字符串转Boolean
else if (typeof value === "string") {
if (value.includes(","))
value = value.split(",").map(item => string2number(item)); // 字符串转数组转数字
else value = string2number(value); // 字符串转数字
}
return value;
});
//log("✅ getStorage, Get Environment Variables, traverseObject", `Store.Settings类型: ${typeof Store.Settings}`, `Store.Settings: ${JSON.stringify(Store.Settings)}`, "");
return Store;
}
function traverseObject(o, c) {
for (const t in o) {
const n = o[t];
o[t] = "object" === typeof n && null !== n ? traverseObject(n, c) : c(t, n);
}
return o;
}
function string2number(string) {
if (/^\d+$/.test(string)) string = Number.parseInt(string, 10);
return string;
}