@gateweb/react-utils 1.0.0 → 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.
@@ -604,6 +604,24 @@ declare const formatAmount: (num: number) => string;
604
604
  * formatString('123456', 1, 4) // '1****6'
605
605
  */
606
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;
607
625
 
608
626
  /**
609
627
  * 檢查稅務編號是否符合正確的編號規則。
@@ -688,4 +706,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
688
706
  */
689
707
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
690
708
 
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 };
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
@@ -765,6 +765,39 @@ message) {
765
765
  *
766
766
  * formatString('123456', 1, 4) // '1****6'
767
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
+ };
768
801
 
769
802
  /**
770
803
  * Wait for a given amount of time
@@ -786,6 +819,7 @@ exports.debounce = debounce;
786
819
  exports.extractEnumLikeObject = extractEnumLikeObject;
787
820
  exports.fakeApi = fakeApi;
788
821
  exports.formatAmount = formatAmount;
822
+ exports.formatBytes = formatBytes;
789
823
  exports.formatStarMask = formatStarMask;
790
824
  exports.generatePeriodArray = generatePeriodArray;
791
825
  exports.getCurrentPeriod = getCurrentPeriod;
@@ -604,6 +604,24 @@ declare const formatAmount: (num: number) => string;
604
604
  * formatString('123456', 1, 4) // '1****6'
605
605
  */
606
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;
607
625
 
608
626
  /**
609
627
  * 檢查稅務編號是否符合正確的編號規則。
@@ -688,4 +706,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
688
706
  */
689
707
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
690
708
 
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 };
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
@@ -759,6 +759,39 @@ message) {
759
759
  *
760
760
  * formatString('123456', 1, 4) // '1****6'
761
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
+ };
762
795
 
763
796
  /**
764
797
  * Wait for a given amount of time
@@ -766,4 +799,4 @@ message) {
766
799
  */ const wait = (ms)=>{
767
800
  };
768
801
 
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 };
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": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [