@gateweb/react-utils 0.0.4 → 1.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/README.md +73 -1
- package/dist/cjs/index.d.ts +87 -1
- package/dist/cjs/index.js +102 -0
- package/dist/es/index.d.mts +87 -1
- package/dist/es/index.mjs +99 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,74 @@
|
|
|
1
1
|
# react-utils
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
**_react-utils_** is a collection of utility functions for GateWeb's React projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @gateweb/react-utils
|
|
9
|
+
# or
|
|
10
|
+
yarn add @gateweb/react-utils
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @gateweb/react-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { formatAmount } from '@gateweb/react-utils';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How to Release a New Version
|
|
22
|
+
|
|
23
|
+
### Manually Bump the Version
|
|
24
|
+
|
|
25
|
+
1. Update the version in `package.json`
|
|
26
|
+
|
|
27
|
+
```diff
|
|
28
|
+
# e.g. bump the version from 0.1.0 to 0.1.1
|
|
29
|
+
{
|
|
30
|
+
"name": "@gateweb/react-utils",
|
|
31
|
+
- "version": "0.1.0"
|
|
32
|
+
+ "version": "0.1.1"
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
2. Install the dependencies and build the package
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
pnpm install
|
|
40
|
+
pnpm build
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
3. login to npm
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
# make sure you have an npm account
|
|
47
|
+
pnpm login
|
|
48
|
+
# npm notice Log in on https://registry.npmjs.org/
|
|
49
|
+
# Login at:
|
|
50
|
+
# https://www.npmjs.com/login?next=/login/cli/390e514d-7aa5-4ee7-a13a-b17e6dd64518
|
|
51
|
+
# Press ENTER to open in the browser...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
4. Publish the package
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
pnpm publish --access public --no-git-checks
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
5. Check the published package on npmjs.com
|
|
61
|
+
|
|
62
|
+
[@gateweb/react-utils](https://www.npmjs.com/package/@gateweb/react-utils)
|
|
63
|
+
|
|
64
|
+
### Automatically Bump the Version
|
|
65
|
+
|
|
66
|
+
- When you merge a pull request to the `main` branch, the GitHub Action will automatically bump the version and publish the package to npm.
|
|
67
|
+
|
|
68
|
+
- The version will follow the [Semantic Versioning](https://semver.org/) rules.
|
|
69
|
+
|
|
70
|
+
## Notes
|
|
71
|
+
|
|
72
|
+
- You should use `pnpm` to develop this package.
|
|
73
|
+
- This package is written in TypeScript and supports TypeScript out of the box.
|
|
74
|
+
- This package is built with [bunchee](https://github.com/huozhi/bunchee) which is a zero-config build tool for TypeScript packages based on Rollup.
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TExtractValueType } from './types.js';
|
|
1
2
|
export * from './types.js';
|
|
2
3
|
|
|
3
4
|
type CamelToPascal<S extends string> = S extends `${infer Head}${infer Tail}` ? `${Uppercase<Head>}${Tail}` : S;
|
|
@@ -205,6 +206,85 @@ declare const rocEraToAd: (dateString: string, format?: string) => string;
|
|
|
205
206
|
*/
|
|
206
207
|
declare const adToRocEra: (dateString: string, format?: string) => string;
|
|
207
208
|
|
|
209
|
+
/**
|
|
210
|
+
* 將指定格式的物件轉換成類似 enum 的物件
|
|
211
|
+
*
|
|
212
|
+
* @param enumObject enum 物件
|
|
213
|
+
* @param valueKey value 的 key
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
*
|
|
217
|
+
* ```js
|
|
218
|
+
* const myObj = {
|
|
219
|
+
* A: { value: 'a' },
|
|
220
|
+
* B: { value: 'b', other: 'other' },
|
|
221
|
+
* }
|
|
222
|
+
*
|
|
223
|
+
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
224
|
+
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any; }>, K extends string>(enumObject: T, valueKey: K) => { [key in keyof T]: T[key][K]; };
|
|
228
|
+
/**
|
|
229
|
+
* 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
|
|
230
|
+
*
|
|
231
|
+
* 輸入的物件格式為
|
|
232
|
+
*
|
|
233
|
+
* ```ts
|
|
234
|
+
* { key: { value: string, label: string }, key2: { value: string, label: string }, ... }
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* 會生成以下三個物件
|
|
238
|
+
*
|
|
239
|
+
* enum 物件 - 根據 value 生成的 enum 物件
|
|
240
|
+
*
|
|
241
|
+
* enum 列表 - 根據 value 與 label 生成的列表
|
|
242
|
+
*
|
|
243
|
+
* 取得 label 的方法 - 傳入 value 可以取得對應的 label
|
|
244
|
+
*
|
|
245
|
+
* @param obj 要處理的物件
|
|
246
|
+
* @param name 生成的 enum 名稱
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
*
|
|
250
|
+
* ```ts
|
|
251
|
+
* const Status = { Enabled: { value: 'enabled', label: '啟用' }, Disabled: { value: 'disabled', label: '停用' } };
|
|
252
|
+
*
|
|
253
|
+
* const { EnumStatus, StatusList, getStatusLabel } = createEnumLikeObject(Status, 'Status');
|
|
254
|
+
*
|
|
255
|
+
* console.log(EnumStatus); // { Enabled: 'enabled', Disabled: 'disabled' }
|
|
256
|
+
*
|
|
257
|
+
* console.log(StatusList); // [ { key: 'Enabled', value: 'enabled', label: '啟用' }, { key: 'Disabled', value: 'disabled', label: '停用' } ]
|
|
258
|
+
*
|
|
259
|
+
* console.log(getStatusLabel('enabled')); // '啟用'
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
declare const createEnumLikeObject: <T extends Record<string, {
|
|
263
|
+
value: any;
|
|
264
|
+
label: string;
|
|
265
|
+
}>, N extends string>(obj: T, name: N) => { [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? { [key in keyof T]: T[key]["value"]; } : K extends `${N}List` ? {
|
|
266
|
+
value: any;
|
|
267
|
+
label: string;
|
|
268
|
+
key: string;
|
|
269
|
+
}[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* simulate a fake api request
|
|
273
|
+
*
|
|
274
|
+
* @param returnValue the value to return
|
|
275
|
+
* @param result the result of the request
|
|
276
|
+
* @param time the time to wait before resolving
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
*
|
|
280
|
+
* const result = await fakeApi({ foo: 'bar' });
|
|
281
|
+
* console.log(result); // { result: true, data: { foo: 'bar' } }
|
|
282
|
+
*/
|
|
283
|
+
declare const fakeApi: <T>(returnValue: T, result?: boolean, time?: number) => Promise<{
|
|
284
|
+
result: boolean;
|
|
285
|
+
data: T;
|
|
286
|
+
}>;
|
|
287
|
+
|
|
208
288
|
/**
|
|
209
289
|
* 檢查檔案是否為合法的 MIME 類型
|
|
210
290
|
*
|
|
@@ -569,6 +649,12 @@ declare const validTaxId: (taxId: string) => boolean;
|
|
|
569
649
|
*/
|
|
570
650
|
declare const validateDateString: (dateString: string, format: string) => boolean;
|
|
571
651
|
|
|
652
|
+
/**
|
|
653
|
+
* Wait for a given amount of time
|
|
654
|
+
* @param ms time to wait in milliseconds
|
|
655
|
+
*/
|
|
656
|
+
declare const wait: (ms: number) => void;
|
|
657
|
+
|
|
572
658
|
/**
|
|
573
659
|
* Downloads a file from a given source.
|
|
574
660
|
*
|
|
@@ -602,4 +688,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
602
688
|
*/
|
|
603
689
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
604
690
|
|
|
605
|
-
export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce, downloadFile, formatAmount, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType };
|
|
691
|
+
export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
|
package/dist/cjs/index.js
CHANGED
|
@@ -307,6 +307,98 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
307
307
|
return dayjs__default.default(dateString, format).add(-1911, 'year').format(format).substring(1);
|
|
308
308
|
};
|
|
309
309
|
|
|
310
|
+
/**
|
|
311
|
+
* 將指定格式的物件轉換成類似 enum 的物件
|
|
312
|
+
*
|
|
313
|
+
* @param enumObject enum 物件
|
|
314
|
+
* @param valueKey value 的 key
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
*
|
|
318
|
+
* ```js
|
|
319
|
+
* const myObj = {
|
|
320
|
+
* A: { value: 'a' },
|
|
321
|
+
* B: { value: 'b', other: 'other' },
|
|
322
|
+
* }
|
|
323
|
+
*
|
|
324
|
+
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
325
|
+
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
326
|
+
* ```
|
|
327
|
+
*/ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
|
|
328
|
+
...acc,
|
|
329
|
+
[key]: value[valueKey]
|
|
330
|
+
}), {});
|
|
331
|
+
/**
|
|
332
|
+
* 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
|
|
333
|
+
*
|
|
334
|
+
* 輸入的物件格式為
|
|
335
|
+
*
|
|
336
|
+
* ```ts
|
|
337
|
+
* { key: { value: string, label: string }, key2: { value: string, label: string }, ... }
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
340
|
+
* 會生成以下三個物件
|
|
341
|
+
*
|
|
342
|
+
* enum 物件 - 根據 value 生成的 enum 物件
|
|
343
|
+
*
|
|
344
|
+
* enum 列表 - 根據 value 與 label 生成的列表
|
|
345
|
+
*
|
|
346
|
+
* 取得 label 的方法 - 傳入 value 可以取得對應的 label
|
|
347
|
+
*
|
|
348
|
+
* @param obj 要處理的物件
|
|
349
|
+
* @param name 生成的 enum 名稱
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
*
|
|
353
|
+
* ```ts
|
|
354
|
+
* const Status = { Enabled: { value: 'enabled', label: '啟用' }, Disabled: { value: 'disabled', label: '停用' } };
|
|
355
|
+
*
|
|
356
|
+
* const { EnumStatus, StatusList, getStatusLabel } = createEnumLikeObject(Status, 'Status');
|
|
357
|
+
*
|
|
358
|
+
* console.log(EnumStatus); // { Enabled: 'enabled', Disabled: 'disabled' }
|
|
359
|
+
*
|
|
360
|
+
* console.log(StatusList); // [ { key: 'Enabled', value: 'enabled', label: '啟用' }, { key: 'Disabled', value: 'disabled', label: '停用' } ]
|
|
361
|
+
*
|
|
362
|
+
* console.log(getStatusLabel('enabled')); // '啟用'
|
|
363
|
+
* ```
|
|
364
|
+
*/ const createEnumLikeObject = (obj, name)=>{
|
|
365
|
+
const Enum = extractEnumLikeObject(obj, 'value');
|
|
366
|
+
const list = Object.entries(obj).map(([key, item])=>({
|
|
367
|
+
key,
|
|
368
|
+
...item
|
|
369
|
+
}));
|
|
370
|
+
const getLabel = (value)=>{
|
|
371
|
+
const targetItem = list.find((item)=>item.value === value);
|
|
372
|
+
if (!targetItem) return value;
|
|
373
|
+
return targetItem.label;
|
|
374
|
+
};
|
|
375
|
+
return {
|
|
376
|
+
[`Enum${name}`]: Enum,
|
|
377
|
+
[`${name}List`]: list,
|
|
378
|
+
[`get${name}Label`]: getLabel
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* simulate a fake api request
|
|
384
|
+
*
|
|
385
|
+
* @param returnValue the value to return
|
|
386
|
+
* @param result the result of the request
|
|
387
|
+
* @param time the time to wait before resolving
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
*
|
|
391
|
+
* const result = await fakeApi({ foo: 'bar' });
|
|
392
|
+
* console.log(result); // { result: true, data: { foo: 'bar' } }
|
|
393
|
+
*/ const fakeApi = (returnValue, result = true, time = 1000)=>new Promise((resolve)=>{
|
|
394
|
+
setTimeout(()=>{
|
|
395
|
+
resolve({
|
|
396
|
+
result,
|
|
397
|
+
data: returnValue
|
|
398
|
+
});
|
|
399
|
+
}, time);
|
|
400
|
+
});
|
|
401
|
+
|
|
310
402
|
/**
|
|
311
403
|
* 檢查檔案是否為合法的 MIME 類型
|
|
312
404
|
*
|
|
@@ -674,6 +766,12 @@ message) {
|
|
|
674
766
|
* formatString('123456', 1, 4) // '1****6'
|
|
675
767
|
*/ const formatStarMask = (str, n, m)=>str.slice(0, n) + '*'.repeat(m - n + 1) + str.slice(m + 1);
|
|
676
768
|
|
|
769
|
+
/**
|
|
770
|
+
* Wait for a given amount of time
|
|
771
|
+
* @param ms time to wait in milliseconds
|
|
772
|
+
*/ const wait = (ms)=>{
|
|
773
|
+
};
|
|
774
|
+
|
|
677
775
|
exports.useCountdown = useCountdownClient.useCountdown;
|
|
678
776
|
exports.downloadFile = downloadClient.downloadFile;
|
|
679
777
|
exports.getLocalStorage = webStorageClient.getLocalStorage;
|
|
@@ -683,7 +781,10 @@ exports.camelCase2PascalCase = camelCase2PascalCase;
|
|
|
683
781
|
exports.camelCase2SnakeCase = camelCase2SnakeCase;
|
|
684
782
|
exports.camelString2PascalString = camelString2PascalString;
|
|
685
783
|
exports.camelString2SnakeString = camelString2SnakeString;
|
|
784
|
+
exports.createEnumLikeObject = createEnumLikeObject;
|
|
686
785
|
exports.debounce = debounce;
|
|
786
|
+
exports.extractEnumLikeObject = extractEnumLikeObject;
|
|
787
|
+
exports.fakeApi = fakeApi;
|
|
687
788
|
exports.formatAmount = formatAmount;
|
|
688
789
|
exports.formatStarMask = formatStarMask;
|
|
689
790
|
exports.generatePeriodArray = generatePeriodArray;
|
|
@@ -719,3 +820,4 @@ exports.throttle = throttle;
|
|
|
719
820
|
exports.validTaxId = validTaxId;
|
|
720
821
|
exports.validateDateString = validateDateString;
|
|
721
822
|
exports.validateFileType = validateFileType;
|
|
823
|
+
exports.wait = wait;
|
package/dist/es/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TExtractValueType } from './types.mjs';
|
|
1
2
|
export * from './types.mjs';
|
|
2
3
|
|
|
3
4
|
type CamelToPascal<S extends string> = S extends `${infer Head}${infer Tail}` ? `${Uppercase<Head>}${Tail}` : S;
|
|
@@ -205,6 +206,85 @@ declare const rocEraToAd: (dateString: string, format?: string) => string;
|
|
|
205
206
|
*/
|
|
206
207
|
declare const adToRocEra: (dateString: string, format?: string) => string;
|
|
207
208
|
|
|
209
|
+
/**
|
|
210
|
+
* 將指定格式的物件轉換成類似 enum 的物件
|
|
211
|
+
*
|
|
212
|
+
* @param enumObject enum 物件
|
|
213
|
+
* @param valueKey value 的 key
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
*
|
|
217
|
+
* ```js
|
|
218
|
+
* const myObj = {
|
|
219
|
+
* A: { value: 'a' },
|
|
220
|
+
* B: { value: 'b', other: 'other' },
|
|
221
|
+
* }
|
|
222
|
+
*
|
|
223
|
+
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
224
|
+
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any; }>, K extends string>(enumObject: T, valueKey: K) => { [key in keyof T]: T[key][K]; };
|
|
228
|
+
/**
|
|
229
|
+
* 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
|
|
230
|
+
*
|
|
231
|
+
* 輸入的物件格式為
|
|
232
|
+
*
|
|
233
|
+
* ```ts
|
|
234
|
+
* { key: { value: string, label: string }, key2: { value: string, label: string }, ... }
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* 會生成以下三個物件
|
|
238
|
+
*
|
|
239
|
+
* enum 物件 - 根據 value 生成的 enum 物件
|
|
240
|
+
*
|
|
241
|
+
* enum 列表 - 根據 value 與 label 生成的列表
|
|
242
|
+
*
|
|
243
|
+
* 取得 label 的方法 - 傳入 value 可以取得對應的 label
|
|
244
|
+
*
|
|
245
|
+
* @param obj 要處理的物件
|
|
246
|
+
* @param name 生成的 enum 名稱
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
*
|
|
250
|
+
* ```ts
|
|
251
|
+
* const Status = { Enabled: { value: 'enabled', label: '啟用' }, Disabled: { value: 'disabled', label: '停用' } };
|
|
252
|
+
*
|
|
253
|
+
* const { EnumStatus, StatusList, getStatusLabel } = createEnumLikeObject(Status, 'Status');
|
|
254
|
+
*
|
|
255
|
+
* console.log(EnumStatus); // { Enabled: 'enabled', Disabled: 'disabled' }
|
|
256
|
+
*
|
|
257
|
+
* console.log(StatusList); // [ { key: 'Enabled', value: 'enabled', label: '啟用' }, { key: 'Disabled', value: 'disabled', label: '停用' } ]
|
|
258
|
+
*
|
|
259
|
+
* console.log(getStatusLabel('enabled')); // '啟用'
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
declare const createEnumLikeObject: <T extends Record<string, {
|
|
263
|
+
value: any;
|
|
264
|
+
label: string;
|
|
265
|
+
}>, N extends string>(obj: T, name: N) => { [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? { [key in keyof T]: T[key]["value"]; } : K extends `${N}List` ? {
|
|
266
|
+
value: any;
|
|
267
|
+
label: string;
|
|
268
|
+
key: string;
|
|
269
|
+
}[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* simulate a fake api request
|
|
273
|
+
*
|
|
274
|
+
* @param returnValue the value to return
|
|
275
|
+
* @param result the result of the request
|
|
276
|
+
* @param time the time to wait before resolving
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
*
|
|
280
|
+
* const result = await fakeApi({ foo: 'bar' });
|
|
281
|
+
* console.log(result); // { result: true, data: { foo: 'bar' } }
|
|
282
|
+
*/
|
|
283
|
+
declare const fakeApi: <T>(returnValue: T, result?: boolean, time?: number) => Promise<{
|
|
284
|
+
result: boolean;
|
|
285
|
+
data: T;
|
|
286
|
+
}>;
|
|
287
|
+
|
|
208
288
|
/**
|
|
209
289
|
* 檢查檔案是否為合法的 MIME 類型
|
|
210
290
|
*
|
|
@@ -569,6 +649,12 @@ declare const validTaxId: (taxId: string) => boolean;
|
|
|
569
649
|
*/
|
|
570
650
|
declare const validateDateString: (dateString: string, format: string) => boolean;
|
|
571
651
|
|
|
652
|
+
/**
|
|
653
|
+
* Wait for a given amount of time
|
|
654
|
+
* @param ms time to wait in milliseconds
|
|
655
|
+
*/
|
|
656
|
+
declare const wait: (ms: number) => void;
|
|
657
|
+
|
|
572
658
|
/**
|
|
573
659
|
* Downloads a file from a given source.
|
|
574
660
|
*
|
|
@@ -602,4 +688,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
602
688
|
*/
|
|
603
689
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
604
690
|
|
|
605
|
-
export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce, downloadFile, formatAmount, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType };
|
|
691
|
+
export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
|
package/dist/es/index.mjs
CHANGED
|
@@ -301,6 +301,98 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
301
301
|
return dayjs(dateString, format).add(-1911, 'year').format(format).substring(1);
|
|
302
302
|
};
|
|
303
303
|
|
|
304
|
+
/**
|
|
305
|
+
* 將指定格式的物件轉換成類似 enum 的物件
|
|
306
|
+
*
|
|
307
|
+
* @param enumObject enum 物件
|
|
308
|
+
* @param valueKey value 的 key
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
*
|
|
312
|
+
* ```js
|
|
313
|
+
* const myObj = {
|
|
314
|
+
* A: { value: 'a' },
|
|
315
|
+
* B: { value: 'b', other: 'other' },
|
|
316
|
+
* }
|
|
317
|
+
*
|
|
318
|
+
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
319
|
+
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
320
|
+
* ```
|
|
321
|
+
*/ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
|
|
322
|
+
...acc,
|
|
323
|
+
[key]: value[valueKey]
|
|
324
|
+
}), {});
|
|
325
|
+
/**
|
|
326
|
+
* 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
|
|
327
|
+
*
|
|
328
|
+
* 輸入的物件格式為
|
|
329
|
+
*
|
|
330
|
+
* ```ts
|
|
331
|
+
* { key: { value: string, label: string }, key2: { value: string, label: string }, ... }
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* 會生成以下三個物件
|
|
335
|
+
*
|
|
336
|
+
* enum 物件 - 根據 value 生成的 enum 物件
|
|
337
|
+
*
|
|
338
|
+
* enum 列表 - 根據 value 與 label 生成的列表
|
|
339
|
+
*
|
|
340
|
+
* 取得 label 的方法 - 傳入 value 可以取得對應的 label
|
|
341
|
+
*
|
|
342
|
+
* @param obj 要處理的物件
|
|
343
|
+
* @param name 生成的 enum 名稱
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
*
|
|
347
|
+
* ```ts
|
|
348
|
+
* const Status = { Enabled: { value: 'enabled', label: '啟用' }, Disabled: { value: 'disabled', label: '停用' } };
|
|
349
|
+
*
|
|
350
|
+
* const { EnumStatus, StatusList, getStatusLabel } = createEnumLikeObject(Status, 'Status');
|
|
351
|
+
*
|
|
352
|
+
* console.log(EnumStatus); // { Enabled: 'enabled', Disabled: 'disabled' }
|
|
353
|
+
*
|
|
354
|
+
* console.log(StatusList); // [ { key: 'Enabled', value: 'enabled', label: '啟用' }, { key: 'Disabled', value: 'disabled', label: '停用' } ]
|
|
355
|
+
*
|
|
356
|
+
* console.log(getStatusLabel('enabled')); // '啟用'
|
|
357
|
+
* ```
|
|
358
|
+
*/ const createEnumLikeObject = (obj, name)=>{
|
|
359
|
+
const Enum = extractEnumLikeObject(obj, 'value');
|
|
360
|
+
const list = Object.entries(obj).map(([key, item])=>({
|
|
361
|
+
key,
|
|
362
|
+
...item
|
|
363
|
+
}));
|
|
364
|
+
const getLabel = (value)=>{
|
|
365
|
+
const targetItem = list.find((item)=>item.value === value);
|
|
366
|
+
if (!targetItem) return value;
|
|
367
|
+
return targetItem.label;
|
|
368
|
+
};
|
|
369
|
+
return {
|
|
370
|
+
[`Enum${name}`]: Enum,
|
|
371
|
+
[`${name}List`]: list,
|
|
372
|
+
[`get${name}Label`]: getLabel
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* simulate a fake api request
|
|
378
|
+
*
|
|
379
|
+
* @param returnValue the value to return
|
|
380
|
+
* @param result the result of the request
|
|
381
|
+
* @param time the time to wait before resolving
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
*
|
|
385
|
+
* const result = await fakeApi({ foo: 'bar' });
|
|
386
|
+
* console.log(result); // { result: true, data: { foo: 'bar' } }
|
|
387
|
+
*/ const fakeApi = (returnValue, result = true, time = 1000)=>new Promise((resolve)=>{
|
|
388
|
+
setTimeout(()=>{
|
|
389
|
+
resolve({
|
|
390
|
+
result,
|
|
391
|
+
data: returnValue
|
|
392
|
+
});
|
|
393
|
+
}, time);
|
|
394
|
+
});
|
|
395
|
+
|
|
304
396
|
/**
|
|
305
397
|
* 檢查檔案是否為合法的 MIME 類型
|
|
306
398
|
*
|
|
@@ -668,4 +760,10 @@ message) {
|
|
|
668
760
|
* formatString('123456', 1, 4) // '1****6'
|
|
669
761
|
*/ const formatStarMask = (str, n, m)=>str.slice(0, n) + '*'.repeat(m - n + 1) + str.slice(m + 1);
|
|
670
762
|
|
|
671
|
-
|
|
763
|
+
/**
|
|
764
|
+
* Wait for a given amount of time
|
|
765
|
+
* @param ms time to wait in milliseconds
|
|
766
|
+
*/ const wait = (ms)=>{
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, extractEnumLikeObject, fakeApi, formatAmount, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, rocEraToAd, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, validTaxId, validateDateString, validateFileType, wait };
|