@qy_better_lib/core 0.1.2 → 0.1.4

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.
@@ -1,19 +1,164 @@
1
- function u(e, n = 200) {
2
- let t;
3
- return function(...o) {
4
- clearTimeout(t), t = setTimeout(() => {
5
- e.apply(this, o);
6
- }, n);
1
+ function debounce(fn, delay = 200, immediate = false) {
2
+ let timer;
3
+ return function(...args) {
4
+ const context = this;
5
+ if (timer) clearTimeout(timer);
6
+ if (immediate) {
7
+ const callNow = !timer;
8
+ timer = setTimeout(() => {
9
+ timer = void 0;
10
+ }, delay);
11
+ if (callNow) {
12
+ return fn.apply(context, args);
13
+ }
14
+ } else {
15
+ timer = setTimeout(() => {
16
+ fn.apply(context, args);
17
+ }, delay);
18
+ }
19
+ return void 0;
7
20
  };
8
21
  }
9
- function r(e, n = 200) {
10
- let t = 0;
11
- return function(...o) {
12
- const i = Date.now();
13
- i - t >= n && (e.apply(this, o), t = i);
22
+ function throttle(fn, interval = 200, options = {}) {
23
+ const { leading = true, trailing = true } = options;
24
+ let lastCallTime = 0;
25
+ let timer;
26
+ let lastArgs;
27
+ let lastContext;
28
+ return function(...args) {
29
+ const context = this;
30
+ const now = Date.now();
31
+ if (lastCallTime === 0) {
32
+ if (leading) {
33
+ fn.apply(context, args);
34
+ }
35
+ lastCallTime = now;
36
+ return void 0;
37
+ }
38
+ lastArgs = args;
39
+ lastContext = context;
40
+ const elapsed = now - lastCallTime;
41
+ if (elapsed >= interval) {
42
+ if (timer) {
43
+ clearTimeout(timer);
44
+ timer = void 0;
45
+ }
46
+ fn.apply(context, args);
47
+ lastCallTime = now;
48
+ return void 0;
49
+ }
50
+ if (!timer && trailing) {
51
+ timer = setTimeout(() => {
52
+ if (lastArgs) {
53
+ fn.apply(lastContext, lastArgs);
54
+ lastCallTime = Date.now();
55
+ }
56
+ timer = void 0;
57
+ lastArgs = void 0;
58
+ lastContext = void 0;
59
+ }, interval - elapsed);
60
+ }
61
+ return void 0;
14
62
  };
15
63
  }
64
+ function curry(fn) {
65
+ const arity = fn.length;
66
+ function curried(...args) {
67
+ if (args.length >= arity) {
68
+ return fn.apply(this, args);
69
+ }
70
+ return function(...moreArgs) {
71
+ return curried.apply(this, [...args, ...moreArgs]);
72
+ };
73
+ }
74
+ return curried;
75
+ }
76
+ function compose(...fns) {
77
+ return function(initialValue) {
78
+ return fns.reduceRight((accumulator, fn) => {
79
+ return fn(accumulator);
80
+ }, initialValue);
81
+ };
82
+ }
83
+ function pipe(...fns) {
84
+ return function(initialValue) {
85
+ return fns.reduce((accumulator, fn) => {
86
+ return fn(accumulator);
87
+ }, initialValue);
88
+ };
89
+ }
90
+ function delay_execution(fn, delay = 1e3) {
91
+ return new Promise((resolve) => {
92
+ setTimeout(() => {
93
+ resolve(fn());
94
+ }, delay);
95
+ });
96
+ }
97
+ async function retry(fn, maxRetries = 3, delay = 1e3) {
98
+ try {
99
+ return await fn();
100
+ } catch (error) {
101
+ if (maxRetries <= 0) {
102
+ throw error;
103
+ }
104
+ await new Promise((resolve) => setTimeout(resolve, delay));
105
+ return retry(fn, maxRetries - 1, delay);
106
+ }
107
+ }
108
+ function once(fn) {
109
+ let called = false;
110
+ let result;
111
+ return function(...args) {
112
+ if (!called) {
113
+ called = true;
114
+ result = fn.apply(this, args);
115
+ }
116
+ return result;
117
+ };
118
+ }
119
+ function memoize(fn) {
120
+ const cache = /* @__PURE__ */ new Map();
121
+ return function(...args) {
122
+ const key = JSON.stringify(args);
123
+ if (cache.has(key)) {
124
+ return cache.get(key);
125
+ }
126
+ const result = fn.apply(this, args);
127
+ cache.set(key, result);
128
+ return result;
129
+ };
130
+ }
131
+ async function batch_execution(fns, parallel = true) {
132
+ if (parallel) {
133
+ return Promise.all(fns.map((fn) => fn()));
134
+ } else {
135
+ const results = [];
136
+ for (const fn of fns) {
137
+ results.push(await fn());
138
+ }
139
+ return results;
140
+ }
141
+ }
142
+ function timeout(fn, timeout2 = 5e3, timeoutMessage = "Operation timed out") {
143
+ return Promise.race([
144
+ Promise.resolve(fn()),
145
+ new Promise((_, reject) => {
146
+ setTimeout(() => {
147
+ reject(new Error(timeoutMessage));
148
+ }, timeout2);
149
+ })
150
+ ]);
151
+ }
16
152
  export {
17
- u as debounce,
18
- r as throttle
153
+ batch_execution,
154
+ compose,
155
+ curry,
156
+ debounce,
157
+ delay_execution,
158
+ memoize,
159
+ once,
160
+ pipe,
161
+ retry,
162
+ throttle,
163
+ timeout
19
164
  };
@@ -1,35 +1,124 @@
1
+ /**
2
+ * 存储工具函数
3
+ */
4
+ /**
5
+ * 存储类型
6
+ */
7
+ export type StorageType = 'local' | 'session';
8
+ /**
9
+ * 带过期时间的存储数据结构
10
+ */
11
+ export interface StorageWithExpiry {
12
+ value: any;
13
+ expiry: number;
14
+ }
1
15
  /**
2
16
  * 获取用户token,包含过期时间判断
17
+ * @returns token值或null
3
18
  */
4
- export declare function getToken(): any | null;
19
+ export declare function get_token(): any | null;
5
20
  /**
6
21
  * 设置用户token,有效期默认七天
7
- * @param {*} value
22
+ * @param value token值
23
+ * @param expires 过期时间(秒),默认7天
8
24
  */
9
- export declare function setToken(value: any, expires?: number): void;
25
+ export declare function set_token(value: any, expires?: number): void;
10
26
  /**
11
27
  * 移除token
12
28
  */
13
- export declare function removeToken(): void;
29
+ export declare function remove_token(): void;
14
30
  /**
15
31
  * 设置缓存
16
- * @param key key
17
- * @param value
32
+ * @param key 缓存键
33
+ * @param value 缓存值
34
+ * @param type 存储类型,默认local
18
35
  */
19
- export declare function setStorage(key: string, value: any, tpye?: "local" | "session"): void;
36
+ export declare function set_storage(key: string, value: any, type?: StorageType): void;
20
37
  /**
21
38
  * 获取缓存
22
- * @param key
23
- * @returns
39
+ * @param key 缓存键
40
+ * @param type 存储类型,默认local
41
+ * @returns 缓存值或null
24
42
  */
25
- export declare function getStorage(key: string, tpye?: "local" | "session"): any;
43
+ export declare function get_storage(key: string, type?: StorageType): any;
26
44
  /**
27
45
  * 删除指定key的缓存
28
- * @param key
46
+ * @param key 缓存键
47
+ * @param type 存储类型,默认local
29
48
  */
30
- export declare function removeStorage(key: string, tpye?: "local" | "session"): void;
49
+ export declare function remove_storage(key: string, type?: StorageType): void;
31
50
  /**
32
51
  * 清除所有缓存
33
- * @param tpye
52
+ * @param type 存储类型,默认local
53
+ */
54
+ export declare function clear_storage(type?: StorageType): void;
55
+ /**
56
+ * 设置带过期时间的缓存
57
+ * @param key 缓存键
58
+ * @param value 缓存值
59
+ * @param expiry 过期时间(秒)
60
+ * @param type 存储类型,默认local
61
+ */
62
+ export declare function set_storage_with_expiry(key: string, value: any, expiry: number, type?: StorageType): void;
63
+ /**
64
+ * 获取带过期时间的缓存
65
+ * @param key 缓存键
66
+ * @param type 存储类型,默认local
67
+ * @returns 缓存值或null
68
+ */
69
+ export declare function get_storage_with_expiry(key: string, type?: StorageType): any;
70
+ /**
71
+ * 检查缓存是否存在
72
+ * @param key 缓存键
73
+ * @param type 存储类型,默认local
74
+ * @returns 是否存在
75
+ */
76
+ export declare function has_storage(key: string, type?: StorageType): boolean;
77
+ /**
78
+ * 获取所有缓存键
79
+ * @param type 存储类型,默认local
80
+ * @returns 缓存键数组
81
+ */
82
+ export declare function get_all_storage_keys(type?: StorageType): string[];
83
+ /**
84
+ * 清除过期的缓存
85
+ * @param type 存储类型,默认local
86
+ * @returns 清除的缓存数量
87
+ */
88
+ export declare function clear_expired_storage(type?: StorageType): number;
89
+ /**
90
+ * 加密存储敏感数据
91
+ * @param key 缓存键
92
+ * @param value 缓存值
93
+ * @param type 存储类型,默认local
94
+ */
95
+ export declare function encrypt_storage(key: string, value: any, type?: StorageType): void;
96
+ /**
97
+ * 解密获取敏感数据
98
+ * @param key 缓存键
99
+ * @param type 存储类型,默认local
100
+ * @returns 解密后的数据或null
101
+ */
102
+ export declare function decrypt_storage(key: string, type?: StorageType): any;
103
+ /**
104
+ * 批量设置缓存
105
+ * @param items 缓存项数组
106
+ * @param type 存储类型,默认local
107
+ */
108
+ export declare function set_batch_storage(items: Array<{
109
+ key: string;
110
+ value: any;
111
+ }>, type?: StorageType): void;
112
+ /**
113
+ * 批量获取缓存
114
+ * @param keys 缓存键数组
115
+ * @param type 存储类型,默认local
116
+ * @returns 缓存值对象
117
+ */
118
+ export declare function get_batch_storage(keys: string[], type?: StorageType): Record<string, any>;
119
+ /**
120
+ * 批量删除缓存
121
+ * @param keys 缓存键数组
122
+ * @param type 存储类型,默认local
34
123
  */
35
- export declare function clearStorage(tpye?: "local" | "session"): void;
124
+ export declare function remove_batch_storage(keys: string[], type?: StorageType): void;
@@ -1,43 +1,181 @@
1
- function n() {
2
- const o = localStorage.getItem("token");
3
- if (!o)
4
- return;
5
- const e = JSON.parse(o);
6
- if (e !== null)
7
- if (e.expirse != null && e.expirse < (/* @__PURE__ */ new Date()).getTime())
1
+ function get_storage_instance(type = "local") {
2
+ return type === "local" ? localStorage : sessionStorage;
3
+ }
4
+ function json_parse(data, defaultValue = null) {
5
+ try {
6
+ return JSON.parse(data);
7
+ } catch (error) {
8
+ console.error("JSON解析错误:", error);
9
+ return defaultValue;
10
+ }
11
+ }
12
+ function get_token() {
13
+ const session = localStorage.getItem("token");
14
+ if (!session) {
15
+ return null;
16
+ }
17
+ const data = json_parse(session);
18
+ if (data !== null) {
19
+ if (data.expiry != null && data.expiry < Date.now()) {
8
20
  localStorage.removeItem("token");
9
- else
10
- return e.value;
21
+ return null;
22
+ } else {
23
+ return data.value;
24
+ }
25
+ }
11
26
  return null;
12
27
  }
13
- function r(o, e) {
14
- !e && (e = 10080 * 60 * 1e3) || (e = e * 1e3);
15
- const t = { value: o, expirse: (/* @__PURE__ */ new Date()).getTime() + e };
16
- localStorage.setItem("token", JSON.stringify(t));
28
+ function set_token(value, expires = 7 * 24 * 60 * 60) {
29
+ const expiry = Date.now() + expires * 1e3;
30
+ const data = { value, expiry };
31
+ localStorage.setItem("token", JSON.stringify(data));
17
32
  }
18
- function l() {
33
+ function remove_token() {
19
34
  localStorage.removeItem("token");
20
35
  }
21
- function s(o, e, t = "local") {
22
- const a = t == "local" ? localStorage : sessionStorage;
23
- e != null && a.setItem(o, JSON.stringify(e));
36
+ function set_storage(key, value, type = "local") {
37
+ const storage = get_storage_instance(type);
38
+ if (value !== void 0) {
39
+ storage.setItem(key, JSON.stringify(value));
40
+ }
41
+ }
42
+ function get_storage(key, type = "local") {
43
+ const storage = get_storage_instance(type);
44
+ const item = storage.getItem(key);
45
+ if (item) {
46
+ return json_parse(item);
47
+ }
48
+ return null;
49
+ }
50
+ function remove_storage(key, type = "local") {
51
+ const storage = get_storage_instance(type);
52
+ storage.removeItem(key);
53
+ }
54
+ function clear_storage(type = "local") {
55
+ const storage = get_storage_instance(type);
56
+ storage.clear();
57
+ }
58
+ function set_storage_with_expiry(key, value, expiry, type = "local") {
59
+ const storage = get_storage_instance(type);
60
+ const item = {
61
+ value,
62
+ expiry: Date.now() + expiry * 1e3
63
+ };
64
+ storage.setItem(key, JSON.stringify(item));
65
+ }
66
+ function get_storage_with_expiry(key, type = "local") {
67
+ const storage = get_storage_instance(type);
68
+ const itemStr = storage.getItem(key);
69
+ if (!itemStr) {
70
+ return null;
71
+ }
72
+ const item = json_parse(itemStr);
73
+ if (item.expiry && item.expiry < Date.now()) {
74
+ storage.removeItem(key);
75
+ return null;
76
+ }
77
+ return item.value;
78
+ }
79
+ function has_storage(key, type = "local") {
80
+ const storage = get_storage_instance(type);
81
+ return storage.getItem(key) !== null;
82
+ }
83
+ function get_all_storage_keys(type = "local") {
84
+ const storage = get_storage_instance(type);
85
+ const keys = [];
86
+ for (let i = 0; i < storage.length; i++) {
87
+ const key = storage.key(i);
88
+ if (key) {
89
+ keys.push(key);
90
+ }
91
+ }
92
+ return keys;
93
+ }
94
+ function clear_expired_storage(type = "local") {
95
+ const storage = get_storage_instance(type);
96
+ const keys = get_all_storage_keys(type);
97
+ let cleared = 0;
98
+ keys.forEach((key) => {
99
+ const itemStr = storage.getItem(key);
100
+ if (itemStr) {
101
+ const item = json_parse(itemStr);
102
+ if (item.expiry && item.expiry < Date.now()) {
103
+ storage.removeItem(key);
104
+ cleared++;
105
+ }
106
+ }
107
+ });
108
+ return cleared;
109
+ }
110
+ function simple_encrypt(text) {
111
+ try {
112
+ return btoa(unescape(encodeURIComponent(text)));
113
+ } catch (error) {
114
+ console.error("加密错误:", error);
115
+ return text;
116
+ }
117
+ }
118
+ function simple_decrypt(encryptedText) {
119
+ try {
120
+ return decodeURIComponent(escape(atob(encryptedText)));
121
+ } catch (error) {
122
+ console.error("解密错误:", error);
123
+ return encryptedText;
124
+ }
125
+ }
126
+ function encrypt_storage(key, value, type = "local") {
127
+ const storage = get_storage_instance(type);
128
+ const jsonValue = JSON.stringify(value);
129
+ const encryptedValue = simple_encrypt(jsonValue);
130
+ storage.setItem(key, encryptedValue);
131
+ }
132
+ function decrypt_storage(key, type = "local") {
133
+ const storage = get_storage_instance(type);
134
+ const encryptedValue = storage.getItem(key);
135
+ if (!encryptedValue) {
136
+ return null;
137
+ }
138
+ try {
139
+ const decryptedValue = simple_decrypt(encryptedValue);
140
+ return JSON.parse(decryptedValue);
141
+ } catch (error) {
142
+ console.error("解密解析错误:", error);
143
+ return null;
144
+ }
24
145
  }
25
- function c(o, e = "local") {
26
- let a = (e == "local" ? localStorage : sessionStorage).getItem(o);
27
- return a && JSON.parse(a || "");
146
+ function set_batch_storage(items, type = "local") {
147
+ items.forEach((item) => {
148
+ set_storage(item.key, item.value, type);
149
+ });
28
150
  }
29
- function g(o, e = "local") {
30
- (e == "local" ? localStorage : sessionStorage).removeItem(o);
151
+ function get_batch_storage(keys, type = "local") {
152
+ const result = {};
153
+ keys.forEach((key) => {
154
+ result[key] = get_storage(key, type);
155
+ });
156
+ return result;
31
157
  }
32
- function i(o = "local") {
33
- (o == "local" ? localStorage : sessionStorage).clear();
158
+ function remove_batch_storage(keys, type = "local") {
159
+ keys.forEach((key) => {
160
+ remove_storage(key, type);
161
+ });
34
162
  }
35
163
  export {
36
- i as clearStorage,
37
- c as getStorage,
38
- n as getToken,
39
- g as removeStorage,
40
- l as removeToken,
41
- s as setStorage,
42
- r as setToken
164
+ clear_expired_storage,
165
+ clear_storage,
166
+ decrypt_storage,
167
+ encrypt_storage,
168
+ get_all_storage_keys,
169
+ get_batch_storage,
170
+ get_storage,
171
+ get_storage_with_expiry,
172
+ get_token,
173
+ has_storage,
174
+ remove_batch_storage,
175
+ remove_storage,
176
+ remove_token,
177
+ set_batch_storage,
178
+ set_storage,
179
+ set_storage_with_expiry,
180
+ set_token
43
181
  };