@gateweb/react-utils 1.14.2 → 1.14.4

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.
@@ -373,18 +373,76 @@ declare const isEqual: (value1: unknown, value2: unknown) => boolean;
373
373
  * @param valueKey value 的 key
374
374
  *
375
375
  * @example
376
- *
377
- * ```js
378
376
  * const myObj = {
379
- * A: { value: 'a' },
380
- * B: { value: 'b', other: 'other' },
381
- * }
382
- *
377
+ * A: { value: 'a' },
378
+ * B: { value: 'b', other: 'other' },
379
+ * } as const;
383
380
  * const enumCode = extractEnumLikeObject(myObj, 'value');
384
- * console.log(enumCode); // { A: 'a', B: 'b' }
385
- * ```
381
+ * // => { A: 'a', B: 'b' }
386
382
  */
387
383
  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]; };
384
+ type EnumMap<T extends Record<string, any>, VK extends string> = {
385
+ [K in keyof T]: T[K] extends Record<VK, infer R> ? R : never;
386
+ };
387
+ type NormalList<T extends Record<string, any>> = Array<{
388
+ key: string;
389
+ } & T[keyof T]>;
390
+ type ValueAtKey<T extends Record<string, any>, K extends string> = T[keyof T] extends infer U ? U extends Record<K, infer R> ? R : never : never;
391
+ type ScenesList<T extends Record<string, {
392
+ scenes: Record<string, Record<string, any>>;
393
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = Array<{
394
+ key: string;
395
+ value: ValueAtKey<T, VK>;
396
+ } & T[keyof T]['scenes'][Scene]>;
397
+ type NormalReturn<T extends Record<string, any>, VK extends string> = {
398
+ Enum: EnumMap<T, VK>;
399
+ List: NormalList<T>;
400
+ getLabel: (value: ValueAtKey<T, VK>) => string;
401
+ };
402
+ type ScenesReturn<T extends Record<string, {
403
+ scenes: Record<string, Record<string, any>>;
404
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = {
405
+ Enum: EnumMap<T, VK>;
406
+ List: ScenesList<T, Scene, VK>;
407
+ getLabel: (value: ValueAtKey<T, VK>) => string;
408
+ };
409
+ type AsNamed<R, N extends string> = {
410
+ [K in keyof R as K extends 'Enum' ? `Enum${N}` : K extends 'List' ? `${N}List` : K extends 'getLabel' ? `get${N}Label` : never]: R[K];
411
+ };
412
+ /**
413
+ * 指定 value 欄位的鍵名選項。
414
+ * @template VK value 對應的鍵名型別
415
+ */
416
+ type ValueKeyOption<VK extends string> = {
417
+ /**
418
+ * 指定 value 欄位的鍵名,當輸入物件的值欄位不是 `value` 時使用,預設為 `'value'`。
419
+ */
420
+ valueKey?: VK;
421
+ };
422
+ /**
423
+ * 命名選項,用於產生 `Enum${name}`、`${name}List`、`get${name}Label`。
424
+ * @template N 自訂名稱型別
425
+ */
426
+ type NamedOption<N extends string> = {
427
+ /**
428
+ * 自訂輸出鍵名的名稱,用於產生 `Enum${name}`、`${name}List`、`get${name}Label`。
429
+ */
430
+ name: N;
431
+ };
432
+ /**
433
+ * 場景選項,指定要取用的場景 key。
434
+ * @template Scene 場景鍵名型別
435
+ */
436
+ type SceneOption<Scene extends string> = {
437
+ /**
438
+ * 指定要取用的場景 key,會從 `scenes[scene]` 中取出 label 與其餘屬性。
439
+ */
440
+ scene: Scene;
441
+ };
442
+ type NormalNamedOptions<N extends string, VK extends string> = NamedOption<N> & ValueKeyOption<VK>;
443
+ type NormalOptions<VK extends string> = ValueKeyOption<VK>;
444
+ type ScenesNamedOptions<N extends string, Scene extends string, VK extends string> = NamedOption<N> & SceneOption<Scene> & ValueKeyOption<VK>;
445
+ type ScenesOptions<Scene extends string, VK extends string> = SceneOption<Scene> & ValueKeyOption<VK>;
388
446
  /**
389
447
  * 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
390
448
  *
@@ -447,119 +505,18 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
447
505
  * ```
448
506
  *
449
507
  */
450
- declare function createEnumLikeObject<T extends Record<string, {
451
- value: any;
452
- label: string;
453
- }>, N extends string>(obj: T, options: {
454
- name: N;
455
- }): {
456
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
457
- [key in keyof T]: T[key]['value'];
458
- } : K extends `${N}List` ? Array<{
459
- key: string;
460
- } & T[keyof T]> : (value: T[keyof T]['value']) => string;
461
- };
462
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
463
- label: string;
464
- }>, N extends string, VK extends keyof T[keyof T] & string>(obj: T, options: {
465
- name: N;
466
- valueKey: VK;
467
- }): {
468
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
469
- [key in keyof T]: T[key][VK];
470
- } : K extends `${N}List` ? Array<{
471
- key: string;
472
- } & Omit<T[keyof T], 'value'> & {
473
- value: T[keyof T][VK];
474
- }> : (value: T[keyof T][VK]) => string;
475
- };
476
- declare function createEnumLikeObject<T extends Record<string, {
477
- value: any;
478
- label: string;
479
- }>>(obj: T, options?: {
480
- name?: undefined;
481
- }): {
482
- Enum: {
483
- [key in keyof T]: T[key]['value'];
484
- };
485
- List: Array<{
486
- key: string;
487
- } & T[keyof T]>;
488
- getLabel: (value: T[keyof T]['value']) => string;
489
- };
490
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
491
- label: string;
492
- }>, VK extends keyof T[keyof T] & string>(obj: T, options: {
493
- valueKey: VK;
494
- }): {
495
- Enum: {
496
- [key in keyof T]: T[key][VK];
497
- };
498
- List: Array<{
499
- key: string;
500
- } & Omit<T[keyof T], 'value'> & {
501
- value: T[keyof T][VK];
502
- }>;
503
- getLabel: (value: T[keyof T][VK]) => string;
504
- };
505
- declare function createEnumLikeObject<T extends Record<string, {
506
- value: any;
507
- scenes: Record<string, Record<string, any>>;
508
- }>, N extends string, Scene extends keyof T[keyof T]['scenes']>(obj: T, options: {
509
- name: N;
510
- scene: Scene;
511
- }): {
512
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
513
- [key in keyof T]: T[key]['value'];
514
- } : K extends `${N}List` ? Array<{
515
- key: string;
516
- value: T[keyof T]['value'];
517
- } & T[keyof T]['scenes'][Scene]> : (value: T[keyof T]['value']) => string;
518
- };
519
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
508
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
520
509
  scenes: Record<string, Record<string, any>>;
521
- }>, N extends string, Scene extends keyof T[keyof T]['scenes'], VK extends keyof T[keyof T] & string>(obj: T, options: {
522
- name: N;
523
- scene: Scene;
524
- valueKey: VK;
525
- }): {
526
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
527
- [key in keyof T]: T[key][VK];
528
- } : K extends `${N}List` ? Array<{
529
- key: string;
530
- value: T[keyof T][VK];
531
- } & T[keyof T]['scenes'][Scene]> : (value: T[keyof T][VK]) => string;
532
- };
533
- declare function createEnumLikeObject<T extends Record<string, {
534
- value: any;
510
+ }>, N extends string, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string = 'value'>(obj: T, options: ScenesNamedOptions<N, Scene, VK>): AsNamed<ScenesReturn<T, Scene, VK>, N>;
511
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
535
512
  scenes: Record<string, Record<string, any>>;
536
- }>, Scene extends keyof T[keyof T]['scenes']>(obj: T, options: {
537
- scene: Scene;
538
- }): {
539
- Enum: {
540
- [key in keyof T]: T[key]['value'];
541
- };
542
- List: Array<{
543
- key: string;
544
- value: T[keyof T]['value'];
545
- } & T[keyof T]['scenes'][Scene]>;
546
- getLabel: (value: T[keyof T]['value']) => string;
547
- };
548
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
549
- scenes: Record<string, Record<string, any>>;
550
- }>, Scene extends keyof T[keyof T]['scenes'], VK extends keyof T[keyof T] & string>(obj: T, options: {
551
- scene: Scene;
552
- valueKey: VK;
553
- }): {
554
- Enum: {
555
- [key in keyof T]: T[key][VK];
556
- };
557
- List: Array<{
558
- key: string;
559
- value: T[keyof T][VK];
560
- } & T[keyof T]['scenes'][Scene]>;
561
- getLabel: (value: T[keyof T][VK]) => string;
562
- };
513
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string = 'value'>(obj: T, options: ScenesOptions<Scene, VK>): ScenesReturn<T, Scene, VK>;
514
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
515
+ label: string;
516
+ }>, N extends string, VK extends string = 'value'>(obj: T, options: NormalNamedOptions<N, VK>): AsNamed<NormalReturn<T, VK>, N>;
517
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
518
+ label: string;
519
+ }>, VK extends string = 'value'>(obj: T, options?: NormalOptions<VK>): NormalReturn<T, VK>;
563
520
 
564
521
  /**
565
522
  * simulate a fake api request
package/dist/cjs/index.js CHANGED
@@ -164,16 +164,12 @@ const FILE_SIZE_UNITS$1 = [
164
164
  * @param valueKey value 的 key
165
165
  *
166
166
  * @example
167
- *
168
- * ```js
169
167
  * const myObj = {
170
- * A: { value: 'a' },
171
- * B: { value: 'b', other: 'other' },
172
- * }
173
- *
168
+ * A: { value: 'a' },
169
+ * B: { value: 'b', other: 'other' },
170
+ * } as const;
174
171
  * const enumCode = extractEnumLikeObject(myObj, 'value');
175
- * console.log(enumCode); // { A: 'a', B: 'b' }
176
- * ```
172
+ * // => { A: 'a', B: 'b' }
177
173
  */ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
178
174
  ...acc,
179
175
  [key]: value[valueKey]
@@ -373,18 +373,76 @@ declare const isEqual: (value1: unknown, value2: unknown) => boolean;
373
373
  * @param valueKey value 的 key
374
374
  *
375
375
  * @example
376
- *
377
- * ```js
378
376
  * const myObj = {
379
- * A: { value: 'a' },
380
- * B: { value: 'b', other: 'other' },
381
- * }
382
- *
377
+ * A: { value: 'a' },
378
+ * B: { value: 'b', other: 'other' },
379
+ * } as const;
383
380
  * const enumCode = extractEnumLikeObject(myObj, 'value');
384
- * console.log(enumCode); // { A: 'a', B: 'b' }
385
- * ```
381
+ * // => { A: 'a', B: 'b' }
386
382
  */
387
383
  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]; };
384
+ type EnumMap<T extends Record<string, any>, VK extends string> = {
385
+ [K in keyof T]: T[K] extends Record<VK, infer R> ? R : never;
386
+ };
387
+ type NormalList<T extends Record<string, any>> = Array<{
388
+ key: string;
389
+ } & T[keyof T]>;
390
+ type ValueAtKey<T extends Record<string, any>, K extends string> = T[keyof T] extends infer U ? U extends Record<K, infer R> ? R : never : never;
391
+ type ScenesList<T extends Record<string, {
392
+ scenes: Record<string, Record<string, any>>;
393
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = Array<{
394
+ key: string;
395
+ value: ValueAtKey<T, VK>;
396
+ } & T[keyof T]['scenes'][Scene]>;
397
+ type NormalReturn<T extends Record<string, any>, VK extends string> = {
398
+ Enum: EnumMap<T, VK>;
399
+ List: NormalList<T>;
400
+ getLabel: (value: ValueAtKey<T, VK>) => string;
401
+ };
402
+ type ScenesReturn<T extends Record<string, {
403
+ scenes: Record<string, Record<string, any>>;
404
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = {
405
+ Enum: EnumMap<T, VK>;
406
+ List: ScenesList<T, Scene, VK>;
407
+ getLabel: (value: ValueAtKey<T, VK>) => string;
408
+ };
409
+ type AsNamed<R, N extends string> = {
410
+ [K in keyof R as K extends 'Enum' ? `Enum${N}` : K extends 'List' ? `${N}List` : K extends 'getLabel' ? `get${N}Label` : never]: R[K];
411
+ };
412
+ /**
413
+ * 指定 value 欄位的鍵名選項。
414
+ * @template VK value 對應的鍵名型別
415
+ */
416
+ type ValueKeyOption<VK extends string> = {
417
+ /**
418
+ * 指定 value 欄位的鍵名,當輸入物件的值欄位不是 `value` 時使用,預設為 `'value'`。
419
+ */
420
+ valueKey?: VK;
421
+ };
422
+ /**
423
+ * 命名選項,用於產生 `Enum${name}`、`${name}List`、`get${name}Label`。
424
+ * @template N 自訂名稱型別
425
+ */
426
+ type NamedOption<N extends string> = {
427
+ /**
428
+ * 自訂輸出鍵名的名稱,用於產生 `Enum${name}`、`${name}List`、`get${name}Label`。
429
+ */
430
+ name: N;
431
+ };
432
+ /**
433
+ * 場景選項,指定要取用的場景 key。
434
+ * @template Scene 場景鍵名型別
435
+ */
436
+ type SceneOption<Scene extends string> = {
437
+ /**
438
+ * 指定要取用的場景 key,會從 `scenes[scene]` 中取出 label 與其餘屬性。
439
+ */
440
+ scene: Scene;
441
+ };
442
+ type NormalNamedOptions<N extends string, VK extends string> = NamedOption<N> & ValueKeyOption<VK>;
443
+ type NormalOptions<VK extends string> = ValueKeyOption<VK>;
444
+ type ScenesNamedOptions<N extends string, Scene extends string, VK extends string> = NamedOption<N> & SceneOption<Scene> & ValueKeyOption<VK>;
445
+ type ScenesOptions<Scene extends string, VK extends string> = SceneOption<Scene> & ValueKeyOption<VK>;
388
446
  /**
389
447
  * 將指定格式的物件生成 enum 物件、enum 列表、取得 label 的方法
390
448
  *
@@ -447,119 +505,18 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
447
505
  * ```
448
506
  *
449
507
  */
450
- declare function createEnumLikeObject<T extends Record<string, {
451
- value: any;
452
- label: string;
453
- }>, N extends string>(obj: T, options: {
454
- name: N;
455
- }): {
456
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
457
- [key in keyof T]: T[key]['value'];
458
- } : K extends `${N}List` ? Array<{
459
- key: string;
460
- } & T[keyof T]> : (value: T[keyof T]['value']) => string;
461
- };
462
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
463
- label: string;
464
- }>, N extends string, VK extends keyof T[keyof T] & string>(obj: T, options: {
465
- name: N;
466
- valueKey: VK;
467
- }): {
468
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
469
- [key in keyof T]: T[key][VK];
470
- } : K extends `${N}List` ? Array<{
471
- key: string;
472
- } & Omit<T[keyof T], 'value'> & {
473
- value: T[keyof T][VK];
474
- }> : (value: T[keyof T][VK]) => string;
475
- };
476
- declare function createEnumLikeObject<T extends Record<string, {
477
- value: any;
478
- label: string;
479
- }>>(obj: T, options?: {
480
- name?: undefined;
481
- }): {
482
- Enum: {
483
- [key in keyof T]: T[key]['value'];
484
- };
485
- List: Array<{
486
- key: string;
487
- } & T[keyof T]>;
488
- getLabel: (value: T[keyof T]['value']) => string;
489
- };
490
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
491
- label: string;
492
- }>, VK extends keyof T[keyof T] & string>(obj: T, options: {
493
- valueKey: VK;
494
- }): {
495
- Enum: {
496
- [key in keyof T]: T[key][VK];
497
- };
498
- List: Array<{
499
- key: string;
500
- } & Omit<T[keyof T], 'value'> & {
501
- value: T[keyof T][VK];
502
- }>;
503
- getLabel: (value: T[keyof T][VK]) => string;
504
- };
505
- declare function createEnumLikeObject<T extends Record<string, {
506
- value: any;
507
- scenes: Record<string, Record<string, any>>;
508
- }>, N extends string, Scene extends keyof T[keyof T]['scenes']>(obj: T, options: {
509
- name: N;
510
- scene: Scene;
511
- }): {
512
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
513
- [key in keyof T]: T[key]['value'];
514
- } : K extends `${N}List` ? Array<{
515
- key: string;
516
- value: T[keyof T]['value'];
517
- } & T[keyof T]['scenes'][Scene]> : (value: T[keyof T]['value']) => string;
518
- };
519
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
508
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
520
509
  scenes: Record<string, Record<string, any>>;
521
- }>, N extends string, Scene extends keyof T[keyof T]['scenes'], VK extends keyof T[keyof T] & string>(obj: T, options: {
522
- name: N;
523
- scene: Scene;
524
- valueKey: VK;
525
- }): {
526
- [K in `Enum${N}` | `${N}List` | `get${N}Label`]: K extends `Enum${N}` ? {
527
- [key in keyof T]: T[key][VK];
528
- } : K extends `${N}List` ? Array<{
529
- key: string;
530
- value: T[keyof T][VK];
531
- } & T[keyof T]['scenes'][Scene]> : (value: T[keyof T][VK]) => string;
532
- };
533
- declare function createEnumLikeObject<T extends Record<string, {
534
- value: any;
510
+ }>, N extends string, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string = 'value'>(obj: T, options: ScenesNamedOptions<N, Scene, VK>): AsNamed<ScenesReturn<T, Scene, VK>, N>;
511
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
535
512
  scenes: Record<string, Record<string, any>>;
536
- }>, Scene extends keyof T[keyof T]['scenes']>(obj: T, options: {
537
- scene: Scene;
538
- }): {
539
- Enum: {
540
- [key in keyof T]: T[key]['value'];
541
- };
542
- List: Array<{
543
- key: string;
544
- value: T[keyof T]['value'];
545
- } & T[keyof T]['scenes'][Scene]>;
546
- getLabel: (value: T[keyof T]['value']) => string;
547
- };
548
- declare function createEnumLikeObject<T extends Record<string, Record<VK, any> & {
549
- scenes: Record<string, Record<string, any>>;
550
- }>, Scene extends keyof T[keyof T]['scenes'], VK extends keyof T[keyof T] & string>(obj: T, options: {
551
- scene: Scene;
552
- valueKey: VK;
553
- }): {
554
- Enum: {
555
- [key in keyof T]: T[key][VK];
556
- };
557
- List: Array<{
558
- key: string;
559
- value: T[keyof T][VK];
560
- } & T[keyof T]['scenes'][Scene]>;
561
- getLabel: (value: T[keyof T][VK]) => string;
562
- };
513
+ }>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string = 'value'>(obj: T, options: ScenesOptions<Scene, VK>): ScenesReturn<T, Scene, VK>;
514
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
515
+ label: string;
516
+ }>, N extends string, VK extends string = 'value'>(obj: T, options: NormalNamedOptions<N, VK>): AsNamed<NormalReturn<T, VK>, N>;
517
+ declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
518
+ label: string;
519
+ }>, VK extends string = 'value'>(obj: T, options?: NormalOptions<VK>): NormalReturn<T, VK>;
563
520
 
564
521
  /**
565
522
  * simulate a fake api request
package/dist/es/index.mjs CHANGED
@@ -157,16 +157,12 @@ const FILE_SIZE_UNITS$1 = [
157
157
  * @param valueKey value 的 key
158
158
  *
159
159
  * @example
160
- *
161
- * ```js
162
160
  * const myObj = {
163
- * A: { value: 'a' },
164
- * B: { value: 'b', other: 'other' },
165
- * }
166
- *
161
+ * A: { value: 'a' },
162
+ * B: { value: 'b', other: 'other' },
163
+ * } as const;
167
164
  * const enumCode = extractEnumLikeObject(myObj, 'value');
168
- * console.log(enumCode); // { A: 'a', B: 'b' }
169
- * ```
165
+ * // => { A: 'a', B: 'b' }
170
166
  */ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
171
167
  ...acc,
172
168
  [key]: value[valueKey]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gateweb/react-utils",
3
- "version": "1.14.2",
3
+ "version": "1.14.4",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [