@gateweb/react-utils 1.1.1 → 1.2.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.
@@ -372,6 +372,30 @@ declare const omit: <T extends object, K extends [...(keyof T)[]]>(object: T, ..
372
372
  * const b = omitByValue(a, undefined, null); // { c: 3, d: 4 }
373
373
  */
374
374
  declare const omitByValue: <T extends object, K extends any[]>(object: T, ...values: K) => Pick<T, { [K2 in keyof T]: T[K2] extends K[number] ? never : K2; }[keyof T]>;
375
+ /**
376
+ * extract the object fields by the given keys
377
+ *
378
+ * @param object - the object to pick fields from
379
+ * @param keys - the keys to pick
380
+ *
381
+ * @example
382
+ *
383
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
384
+ * const b = pick(a, 'a', 'b'); // { a: 1, b: 2 }
385
+ */
386
+ declare const pick: <T extends object, K extends [...(keyof T)[]]>(object: T, ...keys: K) => { [K2 in K[number]]: T[K2]; };
387
+ /**
388
+ * extract the object fields by the given values
389
+ *
390
+ * @param object - the object to pick fields from
391
+ * @param values - the values to pick
392
+ *
393
+ * @example
394
+ *
395
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
396
+ * const b = pickByValue(a, 1, 2); // { a: 1, b: 2 }
397
+ */
398
+ declare const pickByValue: <T extends object, K extends any[]>(object: T, ...values: K) => Pick<T, { [K2 in keyof T]: T[K2] extends K[number] ? K2 : never; }[keyof T]>;
375
399
 
376
400
  /**
377
401
  * debounce function
@@ -491,6 +515,61 @@ declare const isDateTimeString: (separator?: string) => RegExp;
491
515
  * isTimeString().test('12:00:00.123') // false
492
516
  */
493
517
  declare const isTimeString: () => RegExp;
518
+ /**
519
+ * 密碼複雜度檢查
520
+ *
521
+ * 預設密碼長度為 8 位,且包含大寫英文、小寫英文、數字、特殊字元
522
+ *
523
+ * @param options - 選項
524
+ *
525
+ * @example
526
+ *
527
+ * isValidPassword().test('123') // false
528
+ * isValidPassword().test('12345678') // false
529
+ * isValidPassword().test('1234567A') // false
530
+ * isValidPassword().test('123456Aa') // false
531
+ * isValidPassword().test('12345Aa!') // true
532
+ * isValidPassword({ minLength: 6 }).test('12345Aa!') // true
533
+ * isValidPassword({ minLength: 6, uppercase: false }).test('12345a!') // true
534
+ * isValidPassword({ minLength: 6, lowercase: false }).test('12345A!') // true
535
+ * isValidPassword({ minLength: 6, number: false }).test('ABCDEa!') // true
536
+ * isValidPassword({ minLength: 6, special: false }).test('ABCDEa1') // true
537
+ */
538
+ declare const isValidPassword: (options?: {
539
+ /**
540
+ * 包含大寫英文
541
+ *
542
+ * @default true
543
+ */
544
+ uppercase?: boolean;
545
+ /**
546
+ * 包含小寫英文
547
+ *
548
+ * @default true
549
+ */
550
+ lowercase?: boolean;
551
+ /**
552
+ * 包含數字
553
+ *
554
+ * @default true
555
+ *
556
+ */
557
+ number?: boolean;
558
+ /**
559
+ * 包含特殊字元
560
+ *
561
+ * @default true
562
+ *
563
+ */
564
+ special?: boolean;
565
+ /**
566
+ * 最小長度
567
+ *
568
+ * @default 8
569
+ *
570
+ */
571
+ minLength?: number;
572
+ }) => RegExp;
494
573
 
495
574
  /**
496
575
  * 數字
@@ -704,4 +783,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
704
783
  */
705
784
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
706
785
 
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 };
786
+ 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, isValidPassword, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
package/dist/cjs/index.js CHANGED
@@ -534,6 +534,39 @@ message) {
534
534
  [key]: value
535
535
  };
536
536
  }, {});
537
+ /**
538
+ * extract the object fields by the given keys
539
+ *
540
+ * @param object - the object to pick fields from
541
+ * @param keys - the keys to pick
542
+ *
543
+ * @example
544
+ *
545
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
546
+ * const b = pick(a, 'a', 'b'); // { a: 1, b: 2 }
547
+ */ const pick = (object, ...keys)=>keys.reduce((acc, cur)=>({
548
+ ...acc,
549
+ [cur]: object[cur]
550
+ }), {});
551
+ /**
552
+ * extract the object fields by the given values
553
+ *
554
+ * @param object - the object to pick fields from
555
+ * @param values - the values to pick
556
+ *
557
+ * @example
558
+ *
559
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
560
+ * const b = pickByValue(a, 1, 2); // { a: 1, b: 2 }
561
+ */ const pickByValue = (object, ...values)=>Object.entries(object).reduce((acc, [key, value])=>{
562
+ if (values.includes(value)) {
563
+ return {
564
+ ...acc,
565
+ [key]: value
566
+ };
567
+ }
568
+ return acc;
569
+ }, {});
537
570
 
538
571
  /**
539
572
  * debounce function
@@ -660,6 +693,26 @@ message) {
660
693
  * isTimeString().test('12:00:00') // true
661
694
  * isTimeString().test('12:00:00.123') // false
662
695
  */ const isTimeString = ()=>/^\d{2}:\d{2}:\d{2}$/;
696
+ /**
697
+ * 密碼複雜度檢查
698
+ *
699
+ * 預設密碼長度為 8 位,且包含大寫英文、小寫英文、數字、特殊字元
700
+ *
701
+ * @param options - 選項
702
+ *
703
+ * @example
704
+ *
705
+ * isValidPassword().test('123') // false
706
+ * isValidPassword().test('12345678') // false
707
+ * isValidPassword().test('1234567A') // false
708
+ * isValidPassword().test('123456Aa') // false
709
+ * isValidPassword().test('12345Aa!') // true
710
+ * isValidPassword({ minLength: 6 }).test('12345Aa!') // true
711
+ * isValidPassword({ minLength: 6, uppercase: false }).test('12345a!') // true
712
+ * isValidPassword({ minLength: 6, lowercase: false }).test('12345A!') // true
713
+ * isValidPassword({ minLength: 6, number: false }).test('ABCDEa!') // true
714
+ * isValidPassword({ minLength: 6, special: false }).test('ABCDEa1') // true
715
+ */ const isValidPassword = (options)=>new RegExp(`^${options?.uppercase ?? true ? '(?=.*[A-Z])' : ''}${options?.lowercase ?? true ? '(?=.*[a-z])' : ''}${options?.number ?? true ? '(?=.*\\d)' : ''}${options?.special ?? true ? '(?=.*[\\p{P}\\p{S}])' : ''}${options?.minLength ? `.{${options.minLength},}` : '.{8,}$'}`, 'u');
663
716
 
664
717
  /**
665
718
  * 數字
@@ -839,12 +892,15 @@ exports.isServer = isServer;
839
892
  exports.isTWMobile = isTWMobile;
840
893
  exports.isTWPhone = isTWPhone;
841
894
  exports.isTimeString = isTimeString;
895
+ exports.isValidPassword = isValidPassword;
842
896
  exports.omit = omit;
843
897
  exports.omitByValue = omitByValue;
844
898
  exports.pascalCase2CamelCase = pascalCase2CamelCase;
845
899
  exports.pascalCase2SnakeCase = pascalCase2SnakeCase;
846
900
  exports.pascalString2CamelString = pascalString2CamelString;
847
901
  exports.pascalString2SnakeString = pascalString2SnakeString;
902
+ exports.pick = pick;
903
+ exports.pickByValue = pickByValue;
848
904
  exports.rocEraToAd = rocEraToAd;
849
905
  exports.snakeCase2CamelCase = snakeCase2CamelCase;
850
906
  exports.snakeCase2PascalCase = snakeCase2PascalCase;
@@ -36,5 +36,19 @@ type TExtractValueType<T, K extends string> = T extends Record<string, {
36
36
  type Entries<T> = {
37
37
  [K in keyof T]: [K, T[K]];
38
38
  }[keyof T][];
39
+ /**
40
+ * A utility type that transforms an object type `T` into another object type where each value type is a string.
41
+ *
42
+ * @template T - The object type to transform into keys.
43
+ *
44
+ * @example
45
+ *
46
+ * type Example = { a: number; b: string };
47
+ * type ExampleToString = MapToString<Example>;
48
+ * // Result: ExampleToString is { a: string; b: string; }
49
+ */
50
+ type MapToString<T> = {
51
+ [K in keyof T]: string;
52
+ };
39
53
 
40
- export type { Entries, TExtractValueType };
54
+ export type { Entries, MapToString, TExtractValueType };
@@ -372,6 +372,30 @@ declare const omit: <T extends object, K extends [...(keyof T)[]]>(object: T, ..
372
372
  * const b = omitByValue(a, undefined, null); // { c: 3, d: 4 }
373
373
  */
374
374
  declare const omitByValue: <T extends object, K extends any[]>(object: T, ...values: K) => Pick<T, { [K2 in keyof T]: T[K2] extends K[number] ? never : K2; }[keyof T]>;
375
+ /**
376
+ * extract the object fields by the given keys
377
+ *
378
+ * @param object - the object to pick fields from
379
+ * @param keys - the keys to pick
380
+ *
381
+ * @example
382
+ *
383
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
384
+ * const b = pick(a, 'a', 'b'); // { a: 1, b: 2 }
385
+ */
386
+ declare const pick: <T extends object, K extends [...(keyof T)[]]>(object: T, ...keys: K) => { [K2 in K[number]]: T[K2]; };
387
+ /**
388
+ * extract the object fields by the given values
389
+ *
390
+ * @param object - the object to pick fields from
391
+ * @param values - the values to pick
392
+ *
393
+ * @example
394
+ *
395
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
396
+ * const b = pickByValue(a, 1, 2); // { a: 1, b: 2 }
397
+ */
398
+ declare const pickByValue: <T extends object, K extends any[]>(object: T, ...values: K) => Pick<T, { [K2 in keyof T]: T[K2] extends K[number] ? K2 : never; }[keyof T]>;
375
399
 
376
400
  /**
377
401
  * debounce function
@@ -491,6 +515,61 @@ declare const isDateTimeString: (separator?: string) => RegExp;
491
515
  * isTimeString().test('12:00:00.123') // false
492
516
  */
493
517
  declare const isTimeString: () => RegExp;
518
+ /**
519
+ * 密碼複雜度檢查
520
+ *
521
+ * 預設密碼長度為 8 位,且包含大寫英文、小寫英文、數字、特殊字元
522
+ *
523
+ * @param options - 選項
524
+ *
525
+ * @example
526
+ *
527
+ * isValidPassword().test('123') // false
528
+ * isValidPassword().test('12345678') // false
529
+ * isValidPassword().test('1234567A') // false
530
+ * isValidPassword().test('123456Aa') // false
531
+ * isValidPassword().test('12345Aa!') // true
532
+ * isValidPassword({ minLength: 6 }).test('12345Aa!') // true
533
+ * isValidPassword({ minLength: 6, uppercase: false }).test('12345a!') // true
534
+ * isValidPassword({ minLength: 6, lowercase: false }).test('12345A!') // true
535
+ * isValidPassword({ minLength: 6, number: false }).test('ABCDEa!') // true
536
+ * isValidPassword({ minLength: 6, special: false }).test('ABCDEa1') // true
537
+ */
538
+ declare const isValidPassword: (options?: {
539
+ /**
540
+ * 包含大寫英文
541
+ *
542
+ * @default true
543
+ */
544
+ uppercase?: boolean;
545
+ /**
546
+ * 包含小寫英文
547
+ *
548
+ * @default true
549
+ */
550
+ lowercase?: boolean;
551
+ /**
552
+ * 包含數字
553
+ *
554
+ * @default true
555
+ *
556
+ */
557
+ number?: boolean;
558
+ /**
559
+ * 包含特殊字元
560
+ *
561
+ * @default true
562
+ *
563
+ */
564
+ special?: boolean;
565
+ /**
566
+ * 最小長度
567
+ *
568
+ * @default 8
569
+ *
570
+ */
571
+ minLength?: number;
572
+ }) => RegExp;
494
573
 
495
574
  /**
496
575
  * 數字
@@ -704,4 +783,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
704
783
  */
705
784
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
706
785
 
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 };
786
+ 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, isValidPassword, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
package/dist/es/index.mjs CHANGED
@@ -528,6 +528,39 @@ message) {
528
528
  [key]: value
529
529
  };
530
530
  }, {});
531
+ /**
532
+ * extract the object fields by the given keys
533
+ *
534
+ * @param object - the object to pick fields from
535
+ * @param keys - the keys to pick
536
+ *
537
+ * @example
538
+ *
539
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
540
+ * const b = pick(a, 'a', 'b'); // { a: 1, b: 2 }
541
+ */ const pick = (object, ...keys)=>keys.reduce((acc, cur)=>({
542
+ ...acc,
543
+ [cur]: object[cur]
544
+ }), {});
545
+ /**
546
+ * extract the object fields by the given values
547
+ *
548
+ * @param object - the object to pick fields from
549
+ * @param values - the values to pick
550
+ *
551
+ * @example
552
+ *
553
+ * const a = { a: 1, b: 2, c: 3, d: 4 };
554
+ * const b = pickByValue(a, 1, 2); // { a: 1, b: 2 }
555
+ */ const pickByValue = (object, ...values)=>Object.entries(object).reduce((acc, [key, value])=>{
556
+ if (values.includes(value)) {
557
+ return {
558
+ ...acc,
559
+ [key]: value
560
+ };
561
+ }
562
+ return acc;
563
+ }, {});
531
564
 
532
565
  /**
533
566
  * debounce function
@@ -654,6 +687,26 @@ message) {
654
687
  * isTimeString().test('12:00:00') // true
655
688
  * isTimeString().test('12:00:00.123') // false
656
689
  */ const isTimeString = ()=>/^\d{2}:\d{2}:\d{2}$/;
690
+ /**
691
+ * 密碼複雜度檢查
692
+ *
693
+ * 預設密碼長度為 8 位,且包含大寫英文、小寫英文、數字、特殊字元
694
+ *
695
+ * @param options - 選項
696
+ *
697
+ * @example
698
+ *
699
+ * isValidPassword().test('123') // false
700
+ * isValidPassword().test('12345678') // false
701
+ * isValidPassword().test('1234567A') // false
702
+ * isValidPassword().test('123456Aa') // false
703
+ * isValidPassword().test('12345Aa!') // true
704
+ * isValidPassword({ minLength: 6 }).test('12345Aa!') // true
705
+ * isValidPassword({ minLength: 6, uppercase: false }).test('12345a!') // true
706
+ * isValidPassword({ minLength: 6, lowercase: false }).test('12345A!') // true
707
+ * isValidPassword({ minLength: 6, number: false }).test('ABCDEa!') // true
708
+ * isValidPassword({ minLength: 6, special: false }).test('ABCDEa1') // true
709
+ */ const isValidPassword = (options)=>new RegExp(`^${options?.uppercase ?? true ? '(?=.*[A-Z])' : ''}${options?.lowercase ?? true ? '(?=.*[a-z])' : ''}${options?.number ?? true ? '(?=.*\\d)' : ''}${options?.special ?? true ? '(?=.*[\\p{P}\\p{S}])' : ''}${options?.minLength ? `.{${options.minLength},}` : '.{8,}$'}`, 'u');
657
710
 
658
711
  /**
659
712
  * 數字
@@ -799,4 +852,4 @@ message) {
799
852
  */ const wait = (ms)=>{
800
853
  };
801
854
 
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 };
855
+ 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, isValidPassword, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, validTaxId, validateDateString, validateFileType, wait };
@@ -36,5 +36,19 @@ type TExtractValueType<T, K extends string> = T extends Record<string, {
36
36
  type Entries<T> = {
37
37
  [K in keyof T]: [K, T[K]];
38
38
  }[keyof T][];
39
+ /**
40
+ * A utility type that transforms an object type `T` into another object type where each value type is a string.
41
+ *
42
+ * @template T - The object type to transform into keys.
43
+ *
44
+ * @example
45
+ *
46
+ * type Example = { a: number; b: string };
47
+ * type ExampleToString = MapToString<Example>;
48
+ * // Result: ExampleToString is { a: string; b: string; }
49
+ */
50
+ type MapToString<T> = {
51
+ [K in keyof T]: string;
52
+ };
39
53
 
40
- export type { Entries, TExtractValueType };
54
+ export type { Entries, MapToString, TExtractValueType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gateweb/react-utils",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [