@gateweb/react-utils 0.0.5 → 1.1.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 CHANGED
@@ -1,2 +1,74 @@
1
1
  # react-utils
2
- React Utils for GateWeb
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.
@@ -268,6 +268,23 @@ declare const createEnumLikeObject: <T extends Record<string, {
268
268
  key: string;
269
269
  }[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
270
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
+
271
288
  /**
272
289
  * 檢查檔案是否為合法的 MIME 類型
273
290
  *
@@ -587,6 +604,24 @@ declare const formatAmount: (num: number) => string;
587
604
  * formatString('123456', 1, 4) // '1****6'
588
605
  */
589
606
  declare const formatStarMask: (str: string, n: number, m: number) => string;
607
+ /**
608
+ * format file size to human readable string
609
+ *
610
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
611
+ *
612
+ * @param bytes file size in bytes
613
+ * @param decimals number of decimal places (default is 2)
614
+ *
615
+ * @example
616
+ *
617
+ * ```js
618
+ * formatBytes(0) // 0 Bytes
619
+ * formatBytes(1024) // 1 KB
620
+ * formatBytes(1024, 2) // 1.00 KB
621
+ * formatBytes(1024 * 1024) // 1 MB
622
+ * ```
623
+ */
624
+ declare const formatBytes: (bytes: number, decimals?: number) => string;
590
625
 
591
626
  /**
592
627
  * 檢查稅務編號是否符合正確的編號規則。
@@ -632,6 +667,12 @@ declare const validTaxId: (taxId: string) => boolean;
632
667
  */
633
668
  declare const validateDateString: (dateString: string, format: string) => boolean;
634
669
 
670
+ /**
671
+ * Wait for a given amount of time
672
+ * @param ms time to wait in milliseconds
673
+ */
674
+ declare const wait: (ms: number) => void;
675
+
635
676
  /**
636
677
  * Downloads a file from a given source.
637
678
  *
@@ -665,4 +706,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
665
706
  */
666
707
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
667
708
 
668
- export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, 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 };
709
+ export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, 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
@@ -379,6 +379,26 @@ const transformObjectKey = (obj, transformFunName)=>{
379
379
  };
380
380
  };
381
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
+
382
402
  /**
383
403
  * 檢查檔案是否為合法的 MIME 類型
384
404
  *
@@ -745,6 +765,45 @@ message) {
745
765
  *
746
766
  * formatString('123456', 1, 4) // '1****6'
747
767
  */ const formatStarMask = (str, n, m)=>str.slice(0, n) + '*'.repeat(m - n + 1) + str.slice(m + 1);
768
+ /**
769
+ * format file size to human readable string
770
+ *
771
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
772
+ *
773
+ * @param bytes file size in bytes
774
+ * @param decimals number of decimal places (default is 2)
775
+ *
776
+ * @example
777
+ *
778
+ * ```js
779
+ * formatBytes(0) // 0 Bytes
780
+ * formatBytes(1024) // 1 KB
781
+ * formatBytes(1024, 2) // 1.00 KB
782
+ * formatBytes(1024 * 1024) // 1 MB
783
+ * ```
784
+ */ const formatBytes = (bytes, decimals = 0)=>{
785
+ if (bytes === 0) return '0 Bytes';
786
+ const k = 1024;
787
+ const sizes = [
788
+ 'Bytes',
789
+ 'KB',
790
+ 'MB',
791
+ 'GB',
792
+ 'TB',
793
+ 'PB',
794
+ 'EB',
795
+ 'ZB',
796
+ 'YB'
797
+ ];
798
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
799
+ return `${(bytes / k ** i).toFixed(decimals)} ${sizes[i]}`;
800
+ };
801
+
802
+ /**
803
+ * Wait for a given amount of time
804
+ * @param ms time to wait in milliseconds
805
+ */ const wait = (ms)=>{
806
+ };
748
807
 
749
808
  exports.useCountdown = useCountdownClient.useCountdown;
750
809
  exports.downloadFile = downloadClient.downloadFile;
@@ -758,7 +817,9 @@ exports.camelString2SnakeString = camelString2SnakeString;
758
817
  exports.createEnumLikeObject = createEnumLikeObject;
759
818
  exports.debounce = debounce;
760
819
  exports.extractEnumLikeObject = extractEnumLikeObject;
820
+ exports.fakeApi = fakeApi;
761
821
  exports.formatAmount = formatAmount;
822
+ exports.formatBytes = formatBytes;
762
823
  exports.formatStarMask = formatStarMask;
763
824
  exports.generatePeriodArray = generatePeriodArray;
764
825
  exports.getCurrentPeriod = getCurrentPeriod;
@@ -793,3 +854,4 @@ exports.throttle = throttle;
793
854
  exports.validTaxId = validTaxId;
794
855
  exports.validateDateString = validateDateString;
795
856
  exports.validateFileType = validateFileType;
857
+ exports.wait = wait;
@@ -268,6 +268,23 @@ declare const createEnumLikeObject: <T extends Record<string, {
268
268
  key: string;
269
269
  }[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
270
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
+
271
288
  /**
272
289
  * 檢查檔案是否為合法的 MIME 類型
273
290
  *
@@ -587,6 +604,24 @@ declare const formatAmount: (num: number) => string;
587
604
  * formatString('123456', 1, 4) // '1****6'
588
605
  */
589
606
  declare const formatStarMask: (str: string, n: number, m: number) => string;
607
+ /**
608
+ * format file size to human readable string
609
+ *
610
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
611
+ *
612
+ * @param bytes file size in bytes
613
+ * @param decimals number of decimal places (default is 2)
614
+ *
615
+ * @example
616
+ *
617
+ * ```js
618
+ * formatBytes(0) // 0 Bytes
619
+ * formatBytes(1024) // 1 KB
620
+ * formatBytes(1024, 2) // 1.00 KB
621
+ * formatBytes(1024 * 1024) // 1 MB
622
+ * ```
623
+ */
624
+ declare const formatBytes: (bytes: number, decimals?: number) => string;
590
625
 
591
626
  /**
592
627
  * 檢查稅務編號是否符合正確的編號規則。
@@ -632,6 +667,12 @@ declare const validTaxId: (taxId: string) => boolean;
632
667
  */
633
668
  declare const validateDateString: (dateString: string, format: string) => boolean;
634
669
 
670
+ /**
671
+ * Wait for a given amount of time
672
+ * @param ms time to wait in milliseconds
673
+ */
674
+ declare const wait: (ms: number) => void;
675
+
635
676
  /**
636
677
  * Downloads a file from a given source.
637
678
  *
@@ -665,4 +706,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
665
706
  */
666
707
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
667
708
 
668
- export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, 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 };
709
+ export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, 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
@@ -373,6 +373,26 @@ const transformObjectKey = (obj, transformFunName)=>{
373
373
  };
374
374
  };
375
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
+
376
396
  /**
377
397
  * 檢查檔案是否為合法的 MIME 類型
378
398
  *
@@ -739,5 +759,44 @@ message) {
739
759
  *
740
760
  * formatString('123456', 1, 4) // '1****6'
741
761
  */ const formatStarMask = (str, n, m)=>str.slice(0, n) + '*'.repeat(m - n + 1) + str.slice(m + 1);
762
+ /**
763
+ * format file size to human readable string
764
+ *
765
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
766
+ *
767
+ * @param bytes file size in bytes
768
+ * @param decimals number of decimal places (default is 2)
769
+ *
770
+ * @example
771
+ *
772
+ * ```js
773
+ * formatBytes(0) // 0 Bytes
774
+ * formatBytes(1024) // 1 KB
775
+ * formatBytes(1024, 2) // 1.00 KB
776
+ * formatBytes(1024 * 1024) // 1 MB
777
+ * ```
778
+ */ const formatBytes = (bytes, decimals = 0)=>{
779
+ if (bytes === 0) return '0 Bytes';
780
+ const k = 1024;
781
+ const sizes = [
782
+ 'Bytes',
783
+ 'KB',
784
+ 'MB',
785
+ 'GB',
786
+ 'TB',
787
+ 'PB',
788
+ 'EB',
789
+ 'ZB',
790
+ 'YB'
791
+ ];
792
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
793
+ return `${(bytes / k ** i).toFixed(decimals)} ${sizes[i]}`;
794
+ };
795
+
796
+ /**
797
+ * Wait for a given amount of time
798
+ * @param ms time to wait in milliseconds
799
+ */ const wait = (ms)=>{
800
+ };
742
801
 
743
- export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, extractEnumLikeObject, 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 };
802
+ export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gateweb/react-utils",
3
- "version": "0.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [