@gateweb/react-utils 1.17.0 → 2.0.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/cjs/index.js CHANGED
@@ -1,17 +1,837 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
- var webStorage12s = require('./webStorage-12s-P8HpaTRP.js');
4
3
  var dayjs = require('dayjs');
5
- var queryStore12s = require('./queryStore-12s-q_SLGgYH.js');
6
- var React = require('react');
7
- var useCountdown12s = require('./useCountdown-12s-uiqhgllY.js');
8
- var useDisclosure12s = require('./useDisclosure-12s-SZtbSE4A.js');
9
- var download12s = require('./download-12s-DKxkL92w.js');
10
4
 
11
5
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
6
 
13
7
  var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
14
- var React__default = /*#__PURE__*/_interopDefault(React);
8
+
9
+ /* eslint-disable no-bitwise */ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10
+ /**
11
+ * 將位元組陣列編碼為 base64 字串
12
+ *
13
+ * @param bytes 要編碼的位元組陣列
14
+ * @returns base64 編碼的字串
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const bytes = new TextEncoder().encode('Hello World');
19
+ * const encoded = encodeBase64(bytes);
20
+ * console.log(encoded); // 'SGVsbG8gV29ybGQ='
21
+ * ```
22
+ */ const encodeBase64 = (bytes)=>{
23
+ let out = '';
24
+ for(let i = 0; i < bytes.length; i += 3){
25
+ const n = bytes[i] << 16 | (bytes[i + 1] || 0) << 8 | (bytes[i + 2] || 0);
26
+ out += chars[n >> 18 & 63] + chars[n >> 12 & 63] + (i + 1 < bytes.length ? chars[n >> 6 & 63] : '=') + (i + 2 < bytes.length ? chars[n & 63] : '=');
27
+ }
28
+ return out;
29
+ };
30
+ /**
31
+ * 將 base64 字串解碼為位元組陣列
32
+ *
33
+ * @param base64 要解碼的 base64 字串(支援 base64url 格式)
34
+ * @returns 解碼後的位元組陣列
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const decoded = decodeBase64('SGVsbG8gV29ybGQ=');
39
+ * const text = new TextDecoder().decode(decoded);
40
+ * console.log(text); // 'Hello World'
41
+ * ```
42
+ */ const decodeBase64 = (base64)=>{
43
+ let out = base64.replace(/-/g, '+').replace(/_/g, '/'); // 支援 base64url
44
+ while(out.length % 4)out += '='; // 自動補 "="
45
+ const buffer = [];
46
+ for(let i = 0; i < out.length; i += 4){
47
+ const n = chars.indexOf(out[i]) << 18 | chars.indexOf(out[i + 1]) << 12 | (chars.indexOf(out[i + 2]) & 63) << 6 | chars.indexOf(out[i + 3]) & 63;
48
+ buffer.push(n >> 16 & 255);
49
+ if (out[i + 2] !== '=') buffer.push(n >> 8 & 255);
50
+ if (out[i + 3] !== '=') buffer.push(n & 255);
51
+ }
52
+ return new Uint8Array(buffer);
53
+ };
54
+ /**
55
+ * 將 JSON 資料編碼為 base64 字串
56
+ *
57
+ * @param data 要編碼的 JSON 可序列化資料
58
+ * @returns base64 編碼的字串
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const data = { name: 'John', age: 30 };
63
+ * const encoded = encodeJson(data);
64
+ * console.log(encoded); // 'eyJuYW1lIjoiSm9obiIsImFnZSI6MzB9'
65
+ *
66
+ * const array = [1, 2, 3];
67
+ * const encodedArray = encodeJson(array);
68
+ * ```
69
+ */ const encodeJson = (data)=>{
70
+ const json = JSON.stringify(data);
71
+ return encodeBase64(new TextEncoder().encode(json));
72
+ };
73
+ /**
74
+ * 將 base64 字串解碼為 JSON 資料
75
+ *
76
+ * @param b64 要解碼的 base64 字串
77
+ * @returns 解碼後的資料
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const encoded = 'eyJuYW1lIjoiSm9obiIsImFnZSI6MzB9';
82
+ * const decoded = decodeJson<{ name: string; age: number }>(encoded);
83
+ * console.log(decoded); // { name: 'John', age: 30 }
84
+ *
85
+ * const decodedArray = decodeJson<number[]>(encodedArray);
86
+ * ```
87
+ */ const decodeJson = (b64)=>{
88
+ const json = new TextDecoder().decode(decodeBase64(b64));
89
+ return JSON.parse(json);
90
+ };
91
+
92
+ const FILE_SIZE_UNITS$1 = [
93
+ 'Bytes',
94
+ 'KB',
95
+ 'MB',
96
+ 'GB',
97
+ 'TB',
98
+ 'PB',
99
+ 'EB',
100
+ 'ZB',
101
+ 'YB'
102
+ ];
103
+ /**
104
+ * 代表字節大小的類,提供各種格式化和轉換方法
105
+ */ class ByteSize {
106
+ /**
107
+ * 建立一個新的 ByteSize 實例
108
+ * @param bytes 位元組數值
109
+ */ constructor(bytes){
110
+ this.bytes = bytes;
111
+ }
112
+ /**
113
+ * 取得原始位元組數值
114
+ */ get value() {
115
+ return this.bytes;
116
+ }
117
+ /**
118
+ * 將位元組轉換為指定單位的數值
119
+ *
120
+ * @param unit 目標單位 (例如 'KB', 'MB', 'GB')
121
+ * @returns 轉換後的數值
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * const size = createByteSize(1024);
126
+ * size.to('KB'); // 1
127
+ * size.to('MB'); // 0.0009765625
128
+ * ```
129
+ */ to(unit) {
130
+ if (!FILE_SIZE_UNITS$1.includes(unit)) {
131
+ console.warn(`Invalid unit: ${unit}. Valid units are: ${FILE_SIZE_UNITS$1.join(', ')}`);
132
+ return this.bytes;
133
+ }
134
+ const k = 1024;
135
+ const i = FILE_SIZE_UNITS$1.indexOf(unit);
136
+ return this.bytes / k ** i;
137
+ }
138
+ /**
139
+ * 轉換為適當單位的可讀字符串
140
+ *
141
+ * @param unit 指定單位 (例如 'KB', 'MB', 'GB'),不指定則自動選擇最適合的單位
142
+ * @param decimals 小數點位數 (預設為 2)
143
+ * @returns 格式化後的字符串
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * const size = createByteSize(1024);
148
+ * size.format(); // '1.00 KB'
149
+ * size.format('KB'); // '1.00 KB'
150
+ * size.format('MB', 3); // '0.001 MB'
151
+ * ```
152
+ */ format(unit, decimals = 2) {
153
+ if (this.bytes === 0) return `0 ${unit || 'Bytes'}`;
154
+ const k = 1024;
155
+ // 如果指定了有效單位,則使用;否則,計算最適合的單位
156
+ const i = unit && FILE_SIZE_UNITS$1.includes(unit) ? FILE_SIZE_UNITS$1.indexOf(unit) : Math.floor(Math.log(this.bytes) / Math.log(k));
157
+ return `${(this.bytes / k ** i).toFixed(decimals)} ${FILE_SIZE_UNITS$1[i]}`;
158
+ }
159
+ /**
160
+ * 比較兩個 ByteSize 實例
161
+ *
162
+ * @param other 要比較的另一個 ByteSize 實例
163
+ * @returns 比較結果:-1 表示小於,0 表示等於,1 表示大於
164
+ */ compareTo(other) {
165
+ if (this.bytes < other.value) return -1;
166
+ if (this.bytes > other.value) return 1;
167
+ return 0;
168
+ }
169
+ /**
170
+ * 轉換為字符串表示
171
+ * @returns 預設格式化的字符串
172
+ */ toString() {
173
+ return this.format();
174
+ }
175
+ }
176
+ /**
177
+ * 將位元組轉換為可讀字符串 (保留舊的 API 以確保向後兼容)
178
+ *
179
+ * @param bytes 位元組數值
180
+ * @param unit 目標單位 (例如 'KB', 'MB', 'GB'),不指定則自動選擇最適合的單位
181
+ * @param decimals 小數點位數 (預設為 2)
182
+ * @returns 格式化後的字符串
183
+ *
184
+ * @example
185
+ * ```ts
186
+ * convertBytes(0) // '0 Bytes'
187
+ * convertBytes(1024, 'KB') // '1 KB'
188
+ * convertBytes(1024, 'KB', 2) // '1.00 KB'
189
+ * convertBytes(1024 * 1024, 'MB') // '1 MB'
190
+ * convertBytes(1024 * 1024, 'KB') // '1024 KB'
191
+ * ```
192
+ */ const convertBytes = (bytes, unit, decimals = 2)=>new ByteSize(bytes).format(unit, decimals);
193
+
194
+ /**
195
+ * 檢查兩個值是否相等(包含陣列等引用型別)
196
+ * - 支援基本型別的直接比較
197
+ * - 特別處理空陣列比較
198
+ * - 支援非空陣列的深度比較
199
+ *
200
+ * @param value1 - 第一個值
201
+ * @param value2 - 第二個值
202
+ * @returns 如果值相等則返回 true
203
+ *
204
+ * @example
205
+ * // 基本型別比較
206
+ * isEqual(1, 1); // true
207
+ * isEqual('abc', 'abc'); // true
208
+ * isEqual(null, null); // true
209
+ *
210
+ * // 陣列比較
211
+ * isEqual([], []); // true
212
+ * isEqual([1, 2], [1, 2]); // true
213
+ * isEqual([1, 2], [1, 3]); // false
214
+ */ const isEqual = (value1, value2)=>{
215
+ // 處理基本型別
216
+ if (value1 === value2) {
217
+ return true;
218
+ }
219
+ // 處理空陣列
220
+ if (Array.isArray(value1) && Array.isArray(value2) && value1.length === 0 && value2.length === 0) {
221
+ return true;
222
+ }
223
+ // 兩者都是非 null 的物件(包含陣列)
224
+ if (value1 !== null && value2 !== null && typeof value1 === 'object' && typeof value2 === 'object') {
225
+ // 處理非空陣列
226
+ if (Array.isArray(value1) && Array.isArray(value2)) {
227
+ if (value1.length !== value2.length) {
228
+ return false;
229
+ }
230
+ return value1.every((item, index)=>isEqual(item, value2[index]));
231
+ }
232
+ // 未來可以擴展這裡,增加其他物件型別的深度比較
233
+ }
234
+ return false;
235
+ };
236
+
237
+ /**
238
+ * 將指定格式的物件轉換成類似 enum 的物件
239
+ *
240
+ * @param enumObject enum 物件
241
+ * @param valueKey value 的 key
242
+ *
243
+ * @example
244
+ * const myObj = {
245
+ * A: { value: 'a' },
246
+ * B: { value: 'b', other: 'other' },
247
+ * } as const;
248
+ * const enumCode = extractEnumLikeObject(myObj, 'value');
249
+ * // => { A: 'a', B: 'b' }
250
+ */ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
251
+ ...acc,
252
+ [key]: value[valueKey]
253
+ }), {});
254
+ // 實作
255
+ function createEnumLikeObject(obj, options) {
256
+ const name = options?.name;
257
+ const scene = options?.scene;
258
+ const valueKey = options?.valueKey ?? 'value';
259
+ const Enum = extractEnumLikeObject(obj, valueKey);
260
+ // List:根據有無 scene 做不同處理
261
+ let list;
262
+ if (scene) {
263
+ // scenes 版本:解構指定 scene 的 label
264
+ list = Object.entries(obj).map(([key, item])=>({
265
+ key,
266
+ value: item?.[valueKey],
267
+ ...item.scenes?.[scene] ?? {}
268
+ }));
269
+ } else {
270
+ // label only 版本
271
+ list = Object.entries(obj).map(([key, item])=>({
272
+ key,
273
+ value: item?.[valueKey],
274
+ ...item
275
+ }));
276
+ }
277
+ function getLabel(value) {
278
+ const targetItem = list.find((item)=>// 如果 list 項目內含有 valueKey 對應欄位,優先以該欄位比對;否則以 value 欄位比對
279
+ valueKey in item ? item[valueKey] === value : item.value === value);
280
+ if (!targetItem) return String(value);
281
+ if ('label' in targetItem) return targetItem.label;
282
+ return String(value);
283
+ }
284
+ if (name) {
285
+ // 命名版:使用 Enum${name}, ${name}List, get${name}Label 鍵名
286
+ return {
287
+ [`Enum${name}`]: Enum,
288
+ [`${name}List`]: list,
289
+ [`get${name}Label`]: getLabel
290
+ };
291
+ }
292
+ // 未命名:提供預設鍵名 Enum, List, getLabel
293
+ return {
294
+ Enum,
295
+ List: list,
296
+ getLabel
297
+ };
298
+ }
299
+
300
+ /**
301
+ * simulate a fake api request
302
+ *
303
+ * @param returnValue the value to return
304
+ * @param result the result of the request
305
+ * @param time the time to wait before resolving
306
+ *
307
+ * @example
308
+ *
309
+ * const result = await fakeApi({ foo: 'bar' });
310
+ * console.log(result); // { result: true, data: { foo: 'bar' } }
311
+ */ const fakeApi = (returnValue, result = true, time = 1000)=>new Promise((resolve)=>{
312
+ setTimeout(()=>{
313
+ if (result) resolve({
314
+ result,
315
+ data: returnValue
316
+ });
317
+ else resolve({
318
+ result,
319
+ message: 'error'
320
+ });
321
+ }, time);
322
+ });
323
+
324
+ const MimeTypeMap = {
325
+ jpeg: 'image/jpeg',
326
+ jpg: 'image/jpeg',
327
+ gif: 'image/gif',
328
+ png: 'image/png',
329
+ pdf: 'application/pdf',
330
+ zip: 'application/zip',
331
+ csv: 'text/csv',
332
+ ppt: 'application/vnd.ms-powerpoint',
333
+ pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
334
+ xls: 'application/vnd.ms-excel',
335
+ xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
336
+ doc: 'application/msword',
337
+ docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
338
+ txt: 'text/plain'
339
+ };
340
+ const OtherMimeType = 'application/octet-stream';
341
+
342
+ /**
343
+ * 檢查檔案是否為合法的檔案類型
344
+ *
345
+ * `accepts` 可同時接受副檔名以及 MIME 類型
346
+ *
347
+ * @param file 檔案
348
+ * @param accepts 允許的類型
349
+ *
350
+ * @example
351
+ *
352
+ * ```js
353
+ * validateFileType({ type: 'image/png' }, ['image/png', 'image/jpeg']) // true
354
+ * validateFileType({ type: 'image/png' }, ['image/jpeg']) // false
355
+ * validateFileType({ type: 'image/png' }, ['image/*']) // true
356
+ * validateFileType({ name: '圖片.png', type: 'image/png' }, ['.png']) // true
357
+ * ```
358
+ */ const validateFileType = (file, accepts)=>{
359
+ if (accepts.length === 0) return true;
360
+ // 獲取文件的MIME類型
361
+ const fileMimeType = file.type;
362
+ // 提取副檔名(含 .,且轉小寫)
363
+ const fileExt = file.name.includes('.') ? `.${file.name.split('.').pop().toLowerCase()}` : '';
364
+ return accepts.some((accept)=>{
365
+ if (accept.startsWith('.')) {
366
+ // 以副檔名檢查,忽略大小寫
367
+ return fileExt === accept.toLowerCase();
368
+ }
369
+ if (accept === fileMimeType) {
370
+ return true;
371
+ }
372
+ if (accept.endsWith('/*')) {
373
+ return accept.split('/')[0] === fileMimeType.split('/')[0];
374
+ }
375
+ return false;
376
+ });
377
+ };
378
+ /**
379
+ * 根據檔案副檔名取得對應的 MIME Type
380
+ *
381
+ * 目前支援的副檔名有:pdf, csv, jpeg, jpg, png, zip, txt
382
+ * 除此之外的副檔名皆回傳 application/octet-stream
383
+ *
384
+ * @param fileExtension 檔案副檔名
385
+ *
386
+ * @example
387
+ *
388
+ * getMimeType('pdf') // 'application/pdf'
389
+ * getMimeType('csv') // 'text/csv'
390
+ * getMimeType('jpeg') // 'image/jpeg'
391
+ * getMimeType('jpg') // 'image/jpeg'
392
+ * getMimeType('png') // 'image/png'
393
+ * getMimeType('txt') // 'text/plain'
394
+ * getMimeType('zip') // 'application/zip'
395
+ * getMimeType('mp4') // 'application/octet-stream'
396
+ * getMimeType('xls') // 'application/vnd.ms-excel'
397
+ * getMimeType('xlsx') // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
398
+ * getMimeType('doc') // 'application/msword'
399
+ * getMimeType('docx') // 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
400
+ *
401
+ * @deprecated use `parseFileInfoFromFilename` instead
402
+ */ const getMimeType = (fileName)=>{
403
+ const ext = (fileName.split('.').pop() || '').toLowerCase();
404
+ return MimeTypeMap[ext] ?? OtherMimeType;
405
+ };
406
+ /**
407
+ * 用來解析後端在 response header content-disposition 的內容
408
+ *
409
+ * 一般來說格式會有以下兩種
410
+ *
411
+ * - Content-Disposition: attachment; filename="file name.jpg"
412
+ * - Content-Disposition: attachment; filename*=UTF-8''file%20name2.jpg
413
+ *
414
+ * 如果格式正確就會取得檔案名稱(包含副檔名),優先取 filename* 內的內容
415
+ *
416
+ * @param disposition Content-Disposition
417
+ *
418
+ * @example
419
+ *
420
+ * parseFilenameFromDisposition('attachment; filename="file name1.jpg') // file name.jpg
421
+ * parseFilenameFromDisposition('attachment; filename*=UTF-8''file%20name2.jpg') // file name2.jpg
422
+ * parseFilenameFromDisposition('attachment; filename="file name.jpg; filename*=UTF-8''file%20name2.jpg') // file name2.jpg
423
+ */ const parseFilenameFromDisposition = (disposition)=>{
424
+ // 1. 先找 filename*
425
+ const filenameStarMatch = disposition.match(/filename\*\s*=\s*(?:UTF-8'')?([^;]+)/i);
426
+ if (filenameStarMatch && filenameStarMatch[1]) {
427
+ // 依 RFC 5987 格式(UTF-8''URL-ENCODED),要先 decode
428
+ try {
429
+ return decodeURIComponent(filenameStarMatch[1].replace(/(^['"]|['"]$)/g, ''));
430
+ } catch {
431
+ // fallback,如果 decode 失敗,直接傳回原值
432
+ return filenameStarMatch[1];
433
+ }
434
+ }
435
+ // 2. 沒有 filename*,再找 filename
436
+ const filenameMatch = disposition.match(/filename\s*=\s*("?)([^";]+)\1/i);
437
+ if (filenameMatch && filenameMatch[2]) {
438
+ return filenameMatch[2];
439
+ }
440
+ // 3. 都沒有則 undefined
441
+ return undefined;
442
+ };
443
+ /**
444
+ * 解析 `filename` 回傳檔名、副檔名、MIME type
445
+ *
446
+ * @param filename 檔案名稱
447
+ *
448
+ * @example
449
+ *
450
+ * parseFileInfoFromFilename('image.jpg') // ['image', 'jpg', 'image/jpeg']
451
+ * parseFileInfoFromFilename('image') // ['image', '', 'application/octet-stream']
452
+ */ const parseFileInfoFromFilename = (filename)=>{
453
+ const lastDot = filename.lastIndexOf('.');
454
+ if (lastDot === -1) return [
455
+ filename,
456
+ '',
457
+ OtherMimeType
458
+ ]; // 沒有副檔名
459
+ return [
460
+ filename.slice(0, lastDot),
461
+ filename.slice(lastDot + 1),
462
+ MimeTypeMap[filename.slice(lastDot + 1)] ?? OtherMimeType
463
+ ];
464
+ };
465
+
466
+ // const isProduction: boolean = process.env.NODE_ENV === 'production';
467
+ const prefix = 'Invariant failed';
468
+ // Throw an error if the condition fails
469
+ // Strip out error messages for production
470
+ // > Not providing an inline default argument for message as the result is smaller
471
+ function invariant(condition, // Can provide a string, or a function that returns a string for cases where
472
+ // the message takes a fair amount of effort to compute
473
+ message) {
474
+ if (condition) {
475
+ return;
476
+ }
477
+ // Condition not passed
478
+ // In production we strip the message but still throw
479
+ if (process.env.NODE_ENV === 'production') {
480
+ throw new Error(prefix);
481
+ }
482
+ // When not in production we allow the message to pass through
483
+ // *This block will be removed in production builds*
484
+ const provided = typeof message === 'function' ? message() : message;
485
+ // Options:
486
+ // 1. message provided: `${prefix}: ${provided}`
487
+ // 2. message not provided: prefix
488
+ const value = provided ? `${prefix}: ${provided}` : prefix;
489
+ throw new Error(value);
490
+ }
491
+
492
+ /**
493
+ * 判斷執行環境是否為 Server(node.js) 端
494
+ */ const isServer = ()=>typeof window === 'undefined';
495
+
496
+ /**
497
+ * 將物件中的某些 key 排除
498
+ *
499
+ * @param object 原始物件
500
+ * @param keys 要排除的 key
501
+ *
502
+ * @example
503
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
504
+ * const b = omit(a, 'a', 'b'); // { c: 3, d: 4 }
505
+ */ const omit = (object, ...keys)=>Object.keys(object).reduce((acc, cur)=>{
506
+ if (keys.includes(cur)) {
507
+ return acc;
508
+ }
509
+ return {
510
+ ...acc,
511
+ [cur]: object[cur]
512
+ };
513
+ }, {});
514
+ /**
515
+ * 將物件中的某些 value 排除
516
+ *
517
+ * @param object 原始物件
518
+ * @param values 要排除的 value
519
+ *
520
+ * @example
521
+ * const a = { a: undefined, b: null, c: 3, d: 4, e: [] };
522
+ * const b = omitByValue(a, undefined, null, []); // { c: 3, d: 4 }
523
+ */ const omitByValue = (object, ...values)=>Object.entries(object).reduce((acc, [key, value])=>{
524
+ // 使用深度比較檢查值是否應該被排除
525
+ const shouldOmit = values.some((excludeValue)=>isEqual(value, excludeValue));
526
+ if (shouldOmit) {
527
+ return acc;
528
+ }
529
+ return {
530
+ ...acc,
531
+ [key]: value
532
+ };
533
+ }, {});
534
+ /**
535
+ * extract the object fields by the given keys
536
+ *
537
+ * @param object - the object to pick fields from
538
+ * @param keys - the keys to pick
539
+ *
540
+ * @example
541
+ *
542
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
543
+ * const b = pick(a, 'a', 'b'); // { a: 1, b: 2 }
544
+ */ const pick = (object, ...keys)=>keys.reduce((acc, cur)=>({
545
+ ...acc,
546
+ [cur]: object[cur]
547
+ }), {});
548
+ /**
549
+ * extract the object fields by the given values
550
+ *
551
+ * @param object - the object to pick fields from
552
+ * @param values - the values to pick
553
+ *
554
+ * @example
555
+ *
556
+ * const a = { a: 1, b: 2, c: 3, d: 4, e: [] };
557
+ * const b = pickByValue(a, 1, 2, []); // { a: 1, b: 2, e: [] }
558
+ */ const pickByValue = (object, ...values)=>Object.entries(object).reduce((acc, [key, value])=>{
559
+ // 使用深度比較檢查值是否應該被選擇
560
+ const shouldPick = values.some((pickValue)=>isEqual(value, pickValue));
561
+ if (shouldPick) {
562
+ return {
563
+ ...acc,
564
+ [key]: value
565
+ };
566
+ }
567
+ return acc;
568
+ }, {});
569
+ const isObject = (value)=>value !== null && typeof value === 'object';
570
+ /**
571
+ * merge two objects deeply
572
+ *
573
+ * @param target - the target object
574
+ * @param source - the source object
575
+ *
576
+ * @example
577
+ *
578
+ * const obja = { a: { a1: { a11: 'value 1', a12: 'value 2' }, a2: 'value 3' }, b: 'value 4' };
579
+ * const objb = { a: { a1: { a13: 'value 5', a14: 'value 6' }, a3: 'value 7' }};
580
+ *
581
+ * const mergeResult = deepMerge(obja, objb); // { a: { a1: { a11: 'value 1', a12: 'value 2', a13: 'value 5', a14: 'value 6' }, a2: 'value 3', a3: 'value 7' }, b: 'value 4' }
582
+ *
583
+ */ const deepMerge = (target, source)=>Object.entries(source).reduce((acc, [key, value])=>{
584
+ if (isObject(value)) {
585
+ acc[key] = key in target && isObject(target[key]) ? deepMerge(target[key], value) : value;
586
+ } else {
587
+ acc[key] = value;
588
+ }
589
+ return acc;
590
+ }, {
591
+ ...target
592
+ });
593
+ /**
594
+ * A utility function to deeply clone an object.
595
+ * @param obj - The object to clone.
596
+ *
597
+ * @example
598
+ *
599
+ * const original = { a: 1, b: { c: 2 } };
600
+ * const cloned = deepClone(original);
601
+ * console.log(cloned); // { a: 1, b: { c: 2 } }
602
+ *
603
+ */ const deepClone = (obj)=>{
604
+ if (obj === null || typeof obj !== 'object') {
605
+ return obj;
606
+ }
607
+ if (obj instanceof Date) {
608
+ return new Date(obj.getTime());
609
+ }
610
+ if (Array.isArray(obj)) {
611
+ return obj.map((item)=>deepClone(item));
612
+ }
613
+ const cloned = Object.keys(obj).reduce((acc, key)=>{
614
+ acc[key] = deepClone(obj[key]);
615
+ return acc;
616
+ }, {});
617
+ return cloned;
618
+ };
619
+ /**
620
+ * A utility function to rename a key in an object.
621
+ *
622
+ * @param obj - The object to modify.
623
+ * @param oldKey - The key to rename.
624
+ * @param newKey - The new key name.
625
+ *
626
+ * @example
627
+ *
628
+ * const obj = { a: 1, b: 2 };
629
+ * const newObj = renameKey(obj, 'a', 'c');
630
+ * console.log(newObj); // { c: 1, b: 2 }
631
+ */ const renameKey = (obj, oldKey, newKey)=>{
632
+ // 建立一個淺拷貝,避免修改原始物件
633
+ const { [oldKey]: oldValue, ...rest } = obj;
634
+ // 回傳新的物件:用新 key 存舊值,其他 key 保留
635
+ return {
636
+ ...rest,
637
+ [newKey]: oldValue
638
+ };
639
+ };
640
+
641
+ /**
642
+ * 將嵌套物件的所有屬性設為指定的布林值
643
+ * @param obj - 目標物件
644
+ * @param boolValue - 布林值
645
+ *
646
+ * @example
647
+ * const obj = { a: { b: 1, c: 2 }, d: 3 };
648
+ * const result = setBooleanToNestedObject(obj, true);
649
+ * console.log(result); // { a: { b: true, c: true }, d: true }
650
+ */ const setBooleanToNestedObject = (obj, boolValue)=>Object.entries(obj).reduce((acc, [key, value])=>{
651
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
652
+ // 如果是嵌套物件,遞歸處理
653
+ acc[key] = setBooleanToNestedObject(value, boolValue);
654
+ } else {
655
+ // 如果是基本類型,設為布林值
656
+ acc[key] = boolValue;
657
+ }
658
+ return acc;
659
+ }, {});
660
+ /**
661
+ * 深度合併配置物件的通用工具
662
+ *
663
+ * @param defaultConfig - 預設配置
664
+ * @param customConfig - 自訂配置
665
+ *
666
+ * @example
667
+ * const defaultConfig = { a: true, b: { b1: true, b2: true, b3: false }, c: true, d: false };
668
+ * mergeConfig(defaultConfig, { b: { b1: false }, d: true }); // { a: true, b: { b1: false, b2: true, b3: false }, c: true, d: true }
669
+ * mergeConfig(defaultConfig, { b: false }); // { a: true, b: { b1: false, b2: false, b3: false }, c: true, d: false }
670
+ * mergeConfig(defaultConfig, { b: true }); // { a: true, b: { b1: true, b2: true, b3: true }, c: true, d: false }
671
+ */ const mergeConfig = (defaultConfig, customConfig = {})=>{
672
+ // 深拷貝預設配置以避免修改原始物件
673
+ const result = deepClone(defaultConfig);
674
+ return Object.entries(customConfig).reduce((acc, [key, sourceValue])=>{
675
+ const targetValue = acc[key];
676
+ // 如果來源值為 null 或 undefined,跳過
677
+ if (sourceValue === null || sourceValue === undefined) {
678
+ return acc;
679
+ }
680
+ // 如果目標物件中對應的值是物件,且來源值是布林值
681
+ // 這是特殊情況:用布林值覆蓋整個嵌套物件
682
+ if (typeof targetValue === 'object' && targetValue !== null && !Array.isArray(targetValue) && typeof sourceValue === 'boolean') {
683
+ // 將嵌套物件的所有屬性設為該布林值
684
+ acc[key] = setBooleanToNestedObject(targetValue, sourceValue);
685
+ } else if (typeof targetValue === 'object' && targetValue !== null && !Array.isArray(targetValue) && typeof sourceValue === 'object' && sourceValue !== null && !Array.isArray(sourceValue)) {
686
+ acc[key] = mergeConfig(targetValue, sourceValue);
687
+ } else {
688
+ acc[key] = sourceValue;
689
+ }
690
+ return acc;
691
+ }, result);
692
+ };
693
+
694
+ /**
695
+ * debounce function
696
+ *
697
+ * @param {Function} fn function to be executed
698
+ * @param {number} delay time to wait before executing the function
699
+ *
700
+ * @example
701
+ * const debouncedFunction = debounce((a: number, b: number) => console.log(a + b), 1000);
702
+ * debouncedFunction(1, 2);
703
+ * debouncedFunction(3, 4);
704
+ * // after 1 second
705
+ * // 7
706
+ */ const debounce = (fn, delay)=>{
707
+ let timeout;
708
+ // if (isAsync) {
709
+ // return (...args: P[]) =>
710
+ // new Promise((resolve) => {
711
+ // if (timeout) {
712
+ // clearTimeout(timeout);
713
+ // }
714
+ // timeout = setTimeout(() => {
715
+ // resolve(fn(...args));
716
+ // }, delay);
717
+ // });
718
+ // }
719
+ return (...args)=>{
720
+ if (timeout) {
721
+ clearTimeout(timeout);
722
+ }
723
+ timeout = setTimeout(()=>{
724
+ fn(...args);
725
+ }, delay);
726
+ };
727
+ };
728
+ /**
729
+ * throttle function
730
+ *
731
+ * @param {Function} fn function to be executed
732
+ * @param {number} delay time to wait before executing the function
733
+ *
734
+ * @example
735
+ * const throttledFunction = throttle((a: number, b: number) => a + b, 1000);
736
+ * throttledFunction(1, 2); // 3
737
+ * throttledFunction(3, 4); // undefined
738
+ * setTimeout(() => {
739
+ * throttledFunction(5, 6); // 11
740
+ * }, 2000);
741
+ */ const throttle = (fn, delay)=>{
742
+ let wait = false;
743
+ return (...args)=>{
744
+ if (wait) return undefined;
745
+ const val = fn(...args);
746
+ wait = true;
747
+ setTimeout(()=>{
748
+ wait = false;
749
+ }, delay);
750
+ return val;
751
+ };
752
+ };
753
+
754
+ /**
755
+ * Wait for a given amount of time
756
+ * @param ms time to wait in milliseconds
757
+ */ const wait = (ms)=>{
758
+ };
759
+
760
+ /**
761
+ * 將單層物件轉換為 URLSearchParams 物件
762
+ * - 會自動排除 null、undefined、空字串和空陣列的值
763
+ * - 陣列會以逗號連接為字串
764
+ *
765
+ * @param obj - 要轉換的物件,只支援單層物件,其中值可以是 string、number、boolean、null、undefined 或字串陣列
766
+ * @returns URLSearchParams 實例
767
+ *
768
+ * @example
769
+ * const params = { a: '1', b: 123, c: false, d: ['1','2'], e: null, f: undefined, g: '', h: [] };
770
+ * objectToSearchParams(params).toString(); // 'a=1&b=123&c=false&d=1%2C2'
771
+ */ const objectToSearchParams = (obj)=>{
772
+ const searchParams = new URLSearchParams();
773
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) {
774
+ return searchParams;
775
+ }
776
+ Object.entries(omitByValue(obj, undefined, null, '', [])).forEach(([key, value])=>{
777
+ // 如果是陣列,則以逗號連接
778
+ const paramValue = Array.isArray(value) ? value.join(',') : String(value);
779
+ searchParams.append(key, paramValue);
780
+ });
781
+ return searchParams;
782
+ };
783
+ /**
784
+ * 將字串值轉換為指定的型別
785
+ *
786
+ * @param value - 要轉換的字串值
787
+ * @param targetType - 目標型別
788
+ * @returns 轉換後的值
789
+ */ const convertValueByType = (value, targetType)=>{
790
+ switch(targetType){
791
+ case 'number':
792
+ return value ? Number(value) : 0;
793
+ case 'boolean':
794
+ return value === 'true' || value === '1';
795
+ case 'array':
796
+ return value ? value.split(',') : [];
797
+ case 'string':
798
+ default:
799
+ // 預設為字串,不需要轉換
800
+ return value;
801
+ }
802
+ };
803
+ /**
804
+ * 將 URLSearchParams 或字串轉換為物件
805
+ *
806
+ * @param searchParams - 要轉換的搜尋參數字串或 URLSearchParams 實例
807
+ * @param typeMap - 可選的型別轉換映射表,用於指定每個參數的目標型別
808
+ * @returns
809
+ *
810
+ * @example
811
+ * const queryString = 'a=1&b=123&c=true&d=1,2,3&e=false';
812
+ * const result = searchParamsToObject(queryString);
813
+ * // result: { a: '1', b: '123', c: 'true', d: '1,2,3', e: 'false' }
814
+ * const result = searchParamsToObject(queryString, {
815
+ * a: 'string',
816
+ * b: 'number',
817
+ * c: 'boolean',
818
+ * d: 'array',
819
+ * e: 'boolean',
820
+ * });
821
+ * // result: { a: '1', b: 123, c: true, d: ['1', '2', '3'], e: false }
822
+ */ const searchParamsToObject = (searchParams, typeMap = {})=>{
823
+ const searchParamsInstance = typeof searchParams === 'string' ? new URLSearchParams(searchParams) : searchParams;
824
+ const result = {};
825
+ // 從 URLSearchParams 取得所有參數
826
+ searchParamsInstance.forEach((value, key)=>{
827
+ if (!key) {
828
+ return;
829
+ }
830
+ const targetType = typeMap[key];
831
+ result[key] = convertValueByType(value, targetType);
832
+ });
833
+ return result;
834
+ };
15
835
 
16
836
  /**
17
837
  * convert CamelCase string to PascalCase string
@@ -502,100 +1322,6 @@ const FILE_SIZE_UNITS = [
502
1322
  * }
503
1323
  */ const isNil = (value)=>value === null || value === undefined;
504
1324
 
505
- /**
506
- * Creates a strongly-typed React Context and Provider pair for data sharing.
507
- *
508
- * This utility helps you avoid prop drilling by providing a reusable way to define
509
- * context with strict type inference. It returns a custom hook for consuming the context
510
- * and a Provider component for supplying context values.
511
- *
512
- * @template T - The value type for the context.
513
- * @returns {object} An object containing:
514
- * - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
515
- * - DataProvider: A Provider component to wrap your component tree and supply the context value.
516
- *
517
- * @example
518
- * // Example usage:
519
- * const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
520
- *
521
- * function Counter() {
522
- * const { count } = useDataContext();
523
- * return <span>{count}</span>;
524
- * }
525
- *
526
- * function App() {
527
- * return (
528
- * <DataProvider value={{ count: 42 }}>
529
- * <Counter />
530
- * </DataProvider>
531
- * );
532
- * }
533
- */ const createDataContext = ()=>{
534
- const Context = /*#__PURE__*/ React.createContext(undefined);
535
- const useDataContext = ()=>{
536
- const context = React.useContext(Context);
537
- if (context === undefined) {
538
- throw new Error(`useDataContext must be used within a DataProvider`);
539
- }
540
- return context;
541
- };
542
- const DataProvider = ({ children, value })=>{
543
- const memoized = React.useMemo(()=>value, [
544
- value
545
- ]);
546
- return /*#__PURE__*/ React__default.default.createElement(Context.Provider, {
547
- value: memoized
548
- }, children);
549
- };
550
- return {
551
- useDataContext,
552
- DataProvider
553
- };
554
- };
555
-
556
- /**
557
- * A hook to manage a value.
558
- *
559
- * @example
560
- *
561
- * ```tsx
562
- * const MyComponent = ({ value }: { value?: number }) => {
563
- * const [currentValue, setCurrentValue] = useValue({ value });
564
- * };
565
- * ```
566
- */ const useValue = ({ value, defaultValue })=>{
567
- if (value === undefined && defaultValue === undefined) {
568
- throw new Error('Either `value` or `defaultValue` must be provided.');
569
- }
570
- const isControlled = value !== undefined;
571
- const [internalValue, setInternalValue] = React.useState(defaultValue);
572
- const setValue = React.useCallback((newValue)=>{
573
- if (!isControlled) {
574
- setInternalValue(newValue);
575
- }
576
- }, [
577
- isControlled
578
- ]);
579
- const currentValue = isControlled ? value : internalValue;
580
- return [
581
- currentValue,
582
- setValue
583
- ];
584
- };
585
-
586
- function mergeRefs(refs) {
587
- return (value)=>{
588
- refs.forEach((ref)=>{
589
- if (typeof ref === 'function') {
590
- ref(value);
591
- } else if (ref != null) {
592
- // eslint-disable-next-line no-param-reassign
593
- ref.current = value;
594
- }
595
- });
596
- };
597
- }
598
-
599
1325
  /**
600
1326
  * 民國年轉西元年
601
1327
  * @param dateString 日期字串
@@ -677,81 +1403,71 @@ function mergeRefs(refs) {
677
1403
  return dayjs__default.default(endMonth, 'YYYYMM').subtract(1911, 'year').format('YYYYMM').substring(1);
678
1404
  };
679
1405
 
680
- exports.ByteSize = webStorage12s.ByteSize;
681
- exports.MimeTypeMap = webStorage12s.MimeTypeMap;
682
- exports.OtherMimeType = webStorage12s.OtherMimeType;
683
- exports.convertBytes = webStorage12s.convertBytes;
684
- exports.createEnumLikeObject = webStorage12s.createEnumLikeObject;
685
- exports.debounce = webStorage12s.debounce;
686
- exports.decodeBase64 = webStorage12s.decodeBase64;
687
- exports.decodeJson = webStorage12s.decodeJson;
688
- exports.deepClone = webStorage12s.deepClone;
689
- exports.deepMerge = webStorage12s.deepMerge;
690
- exports.encodeBase64 = webStorage12s.encodeBase64;
691
- exports.encodeJson = webStorage12s.encodeJson;
692
- exports.extractEnumLikeObject = webStorage12s.extractEnumLikeObject;
693
- exports.fakeApi = webStorage12s.fakeApi;
694
- exports.getLocalStorage = webStorage12s.getLocalStorage;
695
- exports.getMimeType = webStorage12s.getMimeType;
696
- exports.invariant = webStorage12s.invariant;
697
- exports.isEqual = webStorage12s.isEqual;
698
- exports.isServer = webStorage12s.isServer;
699
- exports.mergeConfig = webStorage12s.mergeConfig;
700
- exports.objectToSearchParams = webStorage12s.objectToSearchParams;
701
- exports.omit = webStorage12s.omit;
702
- exports.omitByValue = webStorage12s.omitByValue;
703
- exports.parseFileInfoFromFilename = webStorage12s.parseFileInfoFromFilename;
704
- exports.parseFilenameFromDisposition = webStorage12s.parseFilenameFromDisposition;
705
- exports.pick = webStorage12s.pick;
706
- exports.pickByValue = webStorage12s.pickByValue;
707
- exports.renameKey = webStorage12s.renameKey;
708
- exports.searchParamsToObject = webStorage12s.searchParamsToObject;
709
- exports.setLocalStorage = webStorage12s.setLocalStorage;
710
- exports.throttle = webStorage12s.throttle;
711
- exports.validateFileType = webStorage12s.validateFileType;
712
- exports.wait = webStorage12s.wait;
713
- exports.QueryProvider = queryStore12s.QueryProvider;
714
- exports.useQueryContext = queryStore12s.useQueryContext;
715
- exports.useCountdown = useCountdown12s.useCountdown;
716
- exports.useDisclosure = useDisclosure12s.useDisclosure;
717
- exports.downloadFile = download12s.downloadFile;
1406
+ exports.ByteSize = ByteSize;
1407
+ exports.MimeTypeMap = MimeTypeMap;
1408
+ exports.OtherMimeType = OtherMimeType;
718
1409
  exports.adToRocEra = adToRocEra;
719
1410
  exports.camelCase2PascalCase = camelCase2PascalCase;
720
1411
  exports.camelCase2SnakeCase = camelCase2SnakeCase;
721
1412
  exports.camelString2PascalString = camelString2PascalString;
722
1413
  exports.camelString2SnakeString = camelString2SnakeString;
723
- exports.createDataContext = createDataContext;
1414
+ exports.convertBytes = convertBytes;
1415
+ exports.createEnumLikeObject = createEnumLikeObject;
1416
+ exports.debounce = debounce;
1417
+ exports.decodeBase64 = decodeBase64;
1418
+ exports.decodeJson = decodeJson;
1419
+ exports.deepClone = deepClone;
1420
+ exports.deepMerge = deepMerge;
1421
+ exports.encodeBase64 = encodeBase64;
1422
+ exports.encodeJson = encodeJson;
1423
+ exports.extractEnumLikeObject = extractEnumLikeObject;
1424
+ exports.fakeApi = fakeApi;
724
1425
  exports.formatAmount = formatAmount;
725
1426
  exports.formatBytes = formatBytes;
726
1427
  exports.formatStarMask = formatStarMask;
727
1428
  exports.generatePeriodArray = generatePeriodArray;
728
1429
  exports.getCurrentPeriod = getCurrentPeriod;
1430
+ exports.getMimeType = getMimeType;
1431
+ exports.invariant = invariant;
729
1432
  exports.isChinese = isChinese;
730
1433
  exports.isDateString = isDateString;
731
1434
  exports.isDateTimeString = isDateTimeString;
732
1435
  exports.isEmail = isEmail;
733
1436
  exports.isEnglish = isEnglish;
1437
+ exports.isEqual = isEqual;
734
1438
  exports.isNil = isNil;
735
1439
  exports.isNonZeroStart = isNonZeroStart;
736
1440
  exports.isNumber = isNumber;
737
1441
  exports.isNumberAtLeastN = isNumberAtLeastN;
738
1442
  exports.isNumberN = isNumberN;
739
1443
  exports.isNumberNM = isNumberNM;
1444
+ exports.isServer = isServer;
740
1445
  exports.isTWMobile = isTWMobile;
741
1446
  exports.isTWPhone = isTWPhone;
742
1447
  exports.isTimeString = isTimeString;
743
1448
  exports.isValidPassword = isValidPassword;
744
1449
  exports.maskString = maskString;
745
- exports.mergeRefs = mergeRefs;
1450
+ exports.mergeConfig = mergeConfig;
1451
+ exports.objectToSearchParams = objectToSearchParams;
1452
+ exports.omit = omit;
1453
+ exports.omitByValue = omitByValue;
1454
+ exports.parseFileInfoFromFilename = parseFileInfoFromFilename;
1455
+ exports.parseFilenameFromDisposition = parseFilenameFromDisposition;
746
1456
  exports.pascalCase2CamelCase = pascalCase2CamelCase;
747
1457
  exports.pascalCase2SnakeCase = pascalCase2SnakeCase;
748
1458
  exports.pascalString2CamelString = pascalString2CamelString;
749
1459
  exports.pascalString2SnakeString = pascalString2SnakeString;
1460
+ exports.pick = pick;
1461
+ exports.pickByValue = pickByValue;
1462
+ exports.renameKey = renameKey;
750
1463
  exports.rocEraToAd = rocEraToAd;
1464
+ exports.searchParamsToObject = searchParamsToObject;
751
1465
  exports.snakeCase2CamelCase = snakeCase2CamelCase;
752
1466
  exports.snakeCase2PascalCase = snakeCase2PascalCase;
753
1467
  exports.snakeString2CamelString = snakeString2CamelString;
754
1468
  exports.snakeString2PascalString = snakeString2PascalString;
755
- exports.useValue = useValue;
1469
+ exports.throttle = throttle;
756
1470
  exports.validTaxId = validTaxId;
757
1471
  exports.validateDateString = validateDateString;
1472
+ exports.validateFileType = validateFileType;
1473
+ exports.wait = wait;