@gateweb/react-utils 1.0.0 → 1.1.1

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.
@@ -262,11 +262,9 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
262
262
  declare const createEnumLikeObject: <T extends Record<string, {
263
263
  value: any;
264
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">; };
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
+ key: keyof T;
267
+ } & T[keyof T])[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
270
268
 
271
269
  /**
272
270
  * simulate a fake api request
@@ -604,6 +602,24 @@ declare const formatAmount: (num: number) => string;
604
602
  * formatString('123456', 1, 4) // '1****6'
605
603
  */
606
604
  declare const formatStarMask: (str: string, n: number, m: number) => string;
605
+ /**
606
+ * format file size to human readable string
607
+ *
608
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
609
+ *
610
+ * @param bytes file size in bytes
611
+ * @param decimals number of decimal places (default is 2)
612
+ *
613
+ * @example
614
+ *
615
+ * ```js
616
+ * formatBytes(0) // 0 Bytes
617
+ * formatBytes(1024) // 1 KB
618
+ * formatBytes(1024, 2) // 1.00 KB
619
+ * formatBytes(1024 * 1024) // 1 MB
620
+ * ```
621
+ */
622
+ declare const formatBytes: (bytes: number, decimals?: number) => string;
607
623
 
608
624
  /**
609
625
  * 檢查稅務編號是否符合正確的編號規則。
@@ -688,4 +704,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
688
704
  */
689
705
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
690
706
 
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 };
707
+ 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;
@@ -21,5 +21,20 @@
21
21
  type TExtractValueType<T, K extends string> = T extends Record<string, {
22
22
  [P in K]: infer R;
23
23
  }> ? R : never;
24
+ /**
25
+ * A utility type that transforms an object type `T` into an array of its entries.
26
+ * Each entry is represented as a tuple where the first element is the key of type `K`
27
+ * and the second element is the corresponding value of type `T[K]`.
28
+ *
29
+ * @template T - The object type to transform into entries.
30
+ *
31
+ * @example
32
+ * type Example = { a: number; b: string };
33
+ * type ExampleEntries = Entries<Example>;
34
+ * // Result: ExampleEntries is [ ["a", number], ["b", string] ]
35
+ */
36
+ type Entries<T> = {
37
+ [K in keyof T]: [K, T[K]];
38
+ }[keyof T][];
24
39
 
25
- export type { TExtractValueType };
40
+ export type { Entries, TExtractValueType };
@@ -262,11 +262,9 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
262
262
  declare const createEnumLikeObject: <T extends Record<string, {
263
263
  value: any;
264
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">; };
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
+ key: keyof T;
267
+ } & T[keyof T])[] : (value: TExtractValueType<T, "value">) => string | TExtractValueType<T, "value">; };
270
268
 
271
269
  /**
272
270
  * simulate a fake api request
@@ -604,6 +602,24 @@ declare const formatAmount: (num: number) => string;
604
602
  * formatString('123456', 1, 4) // '1****6'
605
603
  */
606
604
  declare const formatStarMask: (str: string, n: number, m: number) => string;
605
+ /**
606
+ * format file size to human readable string
607
+ *
608
+ * it will convert bytes to KB, MB, GB, TB, PB, EB, ZB, YB
609
+ *
610
+ * @param bytes file size in bytes
611
+ * @param decimals number of decimal places (default is 2)
612
+ *
613
+ * @example
614
+ *
615
+ * ```js
616
+ * formatBytes(0) // 0 Bytes
617
+ * formatBytes(1024) // 1 KB
618
+ * formatBytes(1024, 2) // 1.00 KB
619
+ * formatBytes(1024 * 1024) // 1 MB
620
+ * ```
621
+ */
622
+ declare const formatBytes: (bytes: number, decimals?: number) => string;
607
623
 
608
624
  /**
609
625
  * 檢查稅務編號是否符合正確的編號規則。
@@ -688,4 +704,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
688
704
  */
689
705
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
690
706
 
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 };
707
+ 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 };
@@ -21,5 +21,20 @@
21
21
  type TExtractValueType<T, K extends string> = T extends Record<string, {
22
22
  [P in K]: infer R;
23
23
  }> ? R : never;
24
+ /**
25
+ * A utility type that transforms an object type `T` into an array of its entries.
26
+ * Each entry is represented as a tuple where the first element is the key of type `K`
27
+ * and the second element is the corresponding value of type `T[K]`.
28
+ *
29
+ * @template T - The object type to transform into entries.
30
+ *
31
+ * @example
32
+ * type Example = { a: number; b: string };
33
+ * type ExampleEntries = Entries<Example>;
34
+ * // Result: ExampleEntries is [ ["a", number], ["b", string] ]
35
+ */
36
+ type Entries<T> = {
37
+ [K in keyof T]: [K, T[K]];
38
+ }[keyof T][];
24
39
 
25
- export type { TExtractValueType };
40
+ export type { Entries, TExtractValueType };
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.1",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [