@gateweb/react-utils 1.14.3 → 1.15.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/dist/cjs/index.d.ts +32 -17
- package/dist/cjs/index.js +32 -10
- package/dist/cjs/types.d.ts +66 -1
- package/dist/es/index.d.mts +32 -17
- package/dist/es/index.mjs +26 -5
- package/dist/es/types.d.mts +66 -1
- package/package.json +13 -11
- /package/dist/cjs/{download-client-DKxkL92w.js → download-12s-DKxkL92w.js} +0 -0
- /package/dist/cjs/{queryStore-client-q_SLGgYH.js → queryStore-12s-q_SLGgYH.js} +0 -0
- /package/dist/cjs/{useCountdown-client-uiqhgllY.js → useCountdown-12s-uiqhgllY.js} +0 -0
- /package/dist/cjs/{webStorage-client-DHr9PcPl.js → webStorage-12s-DHr9PcPl.js} +0 -0
- /package/dist/es/{download-client-CnaJ0p_f.mjs → download-12s-CnaJ0p_f.mjs} +0 -0
- /package/dist/es/{queryStore-client-CFQTVwrg.mjs → queryStore-12s-CFQTVwrg.mjs} +0 -0
- /package/dist/es/{useCountdown-client-t52WIHfq.mjs → useCountdown-12s-t52WIHfq.mjs} +0 -0
- /package/dist/es/{webStorage-client-W1DItzhS.mjs → webStorage-12s-W1DItzhS.mjs} +0 -0
package/dist/cjs/index.d.ts
CHANGED
|
@@ -381,29 +381,30 @@ declare const isEqual: (value1: unknown, value2: unknown) => boolean;
|
|
|
381
381
|
* // => { A: 'a', B: 'b' }
|
|
382
382
|
*/
|
|
383
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
|
|
385
|
-
[K in keyof T]: T[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
386
|
};
|
|
387
387
|
type NormalList<T extends Record<string, any>> = Array<{
|
|
388
388
|
key: string;
|
|
389
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;
|
|
390
391
|
type ScenesList<T extends Record<string, {
|
|
391
392
|
scenes: Record<string, Record<string, any>>;
|
|
392
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
393
|
+
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = Array<{
|
|
393
394
|
key: string;
|
|
394
|
-
value: T
|
|
395
|
+
value: ValueAtKey<T, VK>;
|
|
395
396
|
} & T[keyof T]['scenes'][Scene]>;
|
|
396
|
-
type NormalReturn<T extends Record<string, any>, VK extends
|
|
397
|
+
type NormalReturn<T extends Record<string, any>, VK extends string> = {
|
|
397
398
|
Enum: EnumMap<T, VK>;
|
|
398
399
|
List: NormalList<T>;
|
|
399
|
-
getLabel: (value: T
|
|
400
|
+
getLabel: (value: ValueAtKey<T, VK>) => string;
|
|
400
401
|
};
|
|
401
402
|
type ScenesReturn<T extends Record<string, {
|
|
402
403
|
scenes: Record<string, Record<string, any>>;
|
|
403
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
404
|
+
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = {
|
|
404
405
|
Enum: EnumMap<T, VK>;
|
|
405
406
|
List: ScenesList<T, Scene, VK>;
|
|
406
|
-
getLabel: (value: T
|
|
407
|
+
getLabel: (value: ValueAtKey<T, VK>) => string;
|
|
407
408
|
};
|
|
408
409
|
type AsNamed<R, N extends string> = {
|
|
409
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];
|
|
@@ -504,18 +505,18 @@ type ScenesOptions<Scene extends string, VK extends string> = SceneOption<Scene>
|
|
|
504
505
|
* ```
|
|
505
506
|
*
|
|
506
507
|
*/
|
|
507
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
508
|
+
declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
|
|
508
509
|
scenes: Record<string, Record<string, any>>;
|
|
509
|
-
}>, N extends string, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
510
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
511
512
|
scenes: Record<string, Record<string, any>>;
|
|
512
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
513
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
514
515
|
label: string;
|
|
515
|
-
}>, N extends string, VK extends
|
|
516
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
517
518
|
label: string;
|
|
518
|
-
}>, VK extends
|
|
519
|
+
}>, VK extends string = 'value'>(obj: T, options?: NormalOptions<VK>): NormalReturn<T, VK>;
|
|
519
520
|
|
|
520
521
|
/**
|
|
521
522
|
* simulate a fake api request
|
|
@@ -763,6 +764,20 @@ type PartialBy<T, K extends keyof T, RemoveUndefined extends boolean = false> =
|
|
|
763
764
|
*
|
|
764
765
|
*/
|
|
765
766
|
declare const deepClone: <T>(obj: T) => T;
|
|
767
|
+
/**
|
|
768
|
+
* A utility function to rename a key in an object.
|
|
769
|
+
*
|
|
770
|
+
* @param obj - The object to modify.
|
|
771
|
+
* @param oldKey - The key to rename.
|
|
772
|
+
* @param newKey - The new key name.
|
|
773
|
+
*
|
|
774
|
+
* @example
|
|
775
|
+
*
|
|
776
|
+
* const obj = { a: 1, b: 2 };
|
|
777
|
+
* const newObj = renameKey(obj, 'a', 'c');
|
|
778
|
+
* console.log(newObj); // { c: 1, b: 2 }
|
|
779
|
+
*/
|
|
780
|
+
declare const renameKey: <T extends object, K extends keyof T, NK extends string>(obj: T, oldKey: K, newKey: NK) => Omit<T, K> & Record<NK, T[K]>;
|
|
766
781
|
|
|
767
782
|
/**
|
|
768
783
|
* debounce function
|
|
@@ -1320,5 +1335,5 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
1320
1335
|
*/
|
|
1321
1336
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
1322
1337
|
|
|
1323
|
-
export { ByteSize, MimeTypeMap, OtherMimeType, QueryProvider, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1338
|
+
export { ByteSize, MimeTypeMap, OtherMimeType, QueryProvider, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, renameKey, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1324
1339
|
export type { MimeTypeExtension, MimeTypeValue, PartialBy, RequiredBy, TCountdownActions };
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
|
|
3
3
|
var dayjs = require('dayjs');
|
|
4
|
-
var
|
|
4
|
+
var queryStore12s = require('./queryStore-12s-q_SLGgYH.js');
|
|
5
5
|
var React = require('react');
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
6
|
+
var useCountdown12s = require('./useCountdown-12s-uiqhgllY.js');
|
|
7
|
+
var download12s = require('./download-12s-DKxkL92w.js');
|
|
8
|
+
var webStorage12s = require('./webStorage-12s-DHr9PcPl.js');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
|
|
@@ -539,6 +539,27 @@ const isObject = (value)=>value !== null && typeof value === 'object';
|
|
|
539
539
|
}, {});
|
|
540
540
|
return cloned;
|
|
541
541
|
};
|
|
542
|
+
/**
|
|
543
|
+
* A utility function to rename a key in an object.
|
|
544
|
+
*
|
|
545
|
+
* @param obj - The object to modify.
|
|
546
|
+
* @param oldKey - The key to rename.
|
|
547
|
+
* @param newKey - The new key name.
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
*
|
|
551
|
+
* const obj = { a: 1, b: 2 };
|
|
552
|
+
* const newObj = renameKey(obj, 'a', 'c');
|
|
553
|
+
* console.log(newObj); // { c: 1, b: 2 }
|
|
554
|
+
*/ const renameKey = (obj, oldKey, newKey)=>{
|
|
555
|
+
// 建立一個淺拷貝,避免修改原始物件
|
|
556
|
+
const { [oldKey]: oldValue, ...rest } = obj;
|
|
557
|
+
// 回傳新的物件:用新 key 存舊值,其他 key 保留
|
|
558
|
+
return {
|
|
559
|
+
...rest,
|
|
560
|
+
[newKey]: oldValue
|
|
561
|
+
};
|
|
562
|
+
};
|
|
542
563
|
|
|
543
564
|
/**
|
|
544
565
|
* 將嵌套物件的所有屬性設為指定的布林值
|
|
@@ -1399,12 +1420,12 @@ function mergeRefs(refs) {
|
|
|
1399
1420
|
return dayjs__default.default(endMonth, 'YYYYMM').subtract(1911, 'year').format('YYYYMM').substring(1);
|
|
1400
1421
|
};
|
|
1401
1422
|
|
|
1402
|
-
exports.QueryProvider =
|
|
1403
|
-
exports.useQueryContext =
|
|
1404
|
-
exports.useCountdown =
|
|
1405
|
-
exports.downloadFile =
|
|
1406
|
-
exports.getLocalStorage =
|
|
1407
|
-
exports.setLocalStorage =
|
|
1423
|
+
exports.QueryProvider = queryStore12s.QueryProvider;
|
|
1424
|
+
exports.useQueryContext = queryStore12s.useQueryContext;
|
|
1425
|
+
exports.useCountdown = useCountdown12s.useCountdown;
|
|
1426
|
+
exports.downloadFile = download12s.downloadFile;
|
|
1427
|
+
exports.getLocalStorage = webStorage12s.getLocalStorage;
|
|
1428
|
+
exports.setLocalStorage = webStorage12s.setLocalStorage;
|
|
1408
1429
|
exports.ByteSize = ByteSize;
|
|
1409
1430
|
exports.MimeTypeMap = MimeTypeMap;
|
|
1410
1431
|
exports.OtherMimeType = OtherMimeType;
|
|
@@ -1459,6 +1480,7 @@ exports.pascalString2CamelString = pascalString2CamelString;
|
|
|
1459
1480
|
exports.pascalString2SnakeString = pascalString2SnakeString;
|
|
1460
1481
|
exports.pick = pick;
|
|
1461
1482
|
exports.pickByValue = pickByValue;
|
|
1483
|
+
exports.renameKey = renameKey;
|
|
1462
1484
|
exports.rocEraToAd = rocEraToAd;
|
|
1463
1485
|
exports.searchParamsToObject = searchParamsToObject;
|
|
1464
1486
|
exports.snakeCase2CamelCase = snakeCase2CamelCase;
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -200,5 +200,70 @@ type MapToType<T extends Record<any, any>, A = any> = {
|
|
|
200
200
|
type OverrideProps<T, R extends {
|
|
201
201
|
[K in keyof R]: any;
|
|
202
202
|
}> = Omit<T, keyof R> & R;
|
|
203
|
+
/**
|
|
204
|
+
* A utility type that makes a specific property in the generic type `T` required while keeping the rest optional.
|
|
205
|
+
*
|
|
206
|
+
* @template T - The object type to modify.
|
|
207
|
+
* @template K - The key of the property to make required.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* type User = { id?: number; name?: string }
|
|
211
|
+
* type FormUser = WithRequired<User, 'id'> // { id: number; name?: string }
|
|
212
|
+
*/
|
|
213
|
+
type WithRequired<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;
|
|
214
|
+
/**
|
|
215
|
+
* A utility type that makes a specific property in the generic type `T` optional while keeping the rest required.
|
|
216
|
+
*
|
|
217
|
+
* @template T - The object type to modify.
|
|
218
|
+
* @template K - The key of the property to make optional.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* type User = { id: number; name: string }
|
|
222
|
+
* type FormUser = WithOptional<User, 'id'> // { id?: number; name: string }
|
|
223
|
+
*/
|
|
224
|
+
type WithOptional<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
|
|
225
|
+
/**
|
|
226
|
+
* 產生從 1 到 N 的「數字字面量聯集」型別。
|
|
227
|
+
*
|
|
228
|
+
* @template N - 終止數字(必須是正整數)
|
|
229
|
+
* @template A - 用於遞迴計數的輔助 tuple,預設為 [any] 代表從 1 開始
|
|
230
|
+
* @template R - 暫存累積結果的 union,預設為 1
|
|
231
|
+
*
|
|
232
|
+
* @returns 1 | 2 | ... | N
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* type T1 = Num1ToN<3>;
|
|
236
|
+
* // ^? 1 | 2 | 3
|
|
237
|
+
*
|
|
238
|
+
* type T2 = Num1ToN<5>;
|
|
239
|
+
* // ^? 1 | 2 | 3 | 4 | 5
|
|
240
|
+
*
|
|
241
|
+
* @warning
|
|
242
|
+
* ⚠ **效能注意事項**:
|
|
243
|
+
* - 遞迴深度等於 `N`,因此 `N` 過大會影響 TypeScript 編譯效能
|
|
244
|
+
* - 建議 **N ≤ 100**,否則 VS Code IntelliSense 可能變慢
|
|
245
|
+
*/
|
|
246
|
+
type Num1ToN<N extends number, A extends any[] = [], R extends number = never> = A['length'] extends N ? R | N : Num1ToN<N, [...A, any], R | (A['length'] extends 0 ? never : A['length'])>;
|
|
247
|
+
/**
|
|
248
|
+
* 根據給定的前綴字串與數量,從陣列型別生成一個物件型別。
|
|
249
|
+
*
|
|
250
|
+
* @template T - 陣列型別,例如 `string[]`
|
|
251
|
+
* @template Prefix - 屬性鍵的前綴字串,例如 `'url'`
|
|
252
|
+
* @template N - 欄位數量,會生成 1..N 的鍵名
|
|
253
|
+
*
|
|
254
|
+
* @returns 一個物件型別,鍵為 `${Prefix}${1..N}`,值為 `T[number]`
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* type List = string[];
|
|
258
|
+
*
|
|
259
|
+
* type Obj = BuildPrefixedObject<List, 'url', 3>;
|
|
260
|
+
* // ^? { url1: string; url2: string; url3: string }
|
|
261
|
+
*
|
|
262
|
+
* type Obj2 = BuildPrefixedObject<List, 'name', 2>;
|
|
263
|
+
* // ^? { name1: string; name2: string }
|
|
264
|
+
*/
|
|
265
|
+
type BuildPrefixedObject<T extends readonly unknown[], Prefix extends string, N extends number> = {
|
|
266
|
+
[K in Num1ToN<N> as `${Prefix}${K & number}`]: T[number];
|
|
267
|
+
};
|
|
203
268
|
|
|
204
|
-
export type { AtLeastOne, DeepPartial, Entries, MapToString, MapToType, OnlyOne, OverrideProps, PopArgs, PushArgs, ShiftArgs, TChangeKeyType, TExtractValueType, UnshiftArgs };
|
|
269
|
+
export type { AtLeastOne, BuildPrefixedObject, DeepPartial, Entries, MapToString, MapToType, Num1ToN, OnlyOne, OverrideProps, PopArgs, PushArgs, ShiftArgs, TChangeKeyType, TExtractValueType, UnshiftArgs, WithOptional, WithRequired };
|
package/dist/es/index.d.mts
CHANGED
|
@@ -381,29 +381,30 @@ declare const isEqual: (value1: unknown, value2: unknown) => boolean;
|
|
|
381
381
|
* // => { A: 'a', B: 'b' }
|
|
382
382
|
*/
|
|
383
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
|
|
385
|
-
[K in keyof T]: T[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
386
|
};
|
|
387
387
|
type NormalList<T extends Record<string, any>> = Array<{
|
|
388
388
|
key: string;
|
|
389
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;
|
|
390
391
|
type ScenesList<T extends Record<string, {
|
|
391
392
|
scenes: Record<string, Record<string, any>>;
|
|
392
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
393
|
+
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = Array<{
|
|
393
394
|
key: string;
|
|
394
|
-
value: T
|
|
395
|
+
value: ValueAtKey<T, VK>;
|
|
395
396
|
} & T[keyof T]['scenes'][Scene]>;
|
|
396
|
-
type NormalReturn<T extends Record<string, any>, VK extends
|
|
397
|
+
type NormalReturn<T extends Record<string, any>, VK extends string> = {
|
|
397
398
|
Enum: EnumMap<T, VK>;
|
|
398
399
|
List: NormalList<T>;
|
|
399
|
-
getLabel: (value: T
|
|
400
|
+
getLabel: (value: ValueAtKey<T, VK>) => string;
|
|
400
401
|
};
|
|
401
402
|
type ScenesReturn<T extends Record<string, {
|
|
402
403
|
scenes: Record<string, Record<string, any>>;
|
|
403
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
404
|
+
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends string> = {
|
|
404
405
|
Enum: EnumMap<T, VK>;
|
|
405
406
|
List: ScenesList<T, Scene, VK>;
|
|
406
|
-
getLabel: (value: T
|
|
407
|
+
getLabel: (value: ValueAtKey<T, VK>) => string;
|
|
407
408
|
};
|
|
408
409
|
type AsNamed<R, N extends string> = {
|
|
409
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];
|
|
@@ -504,18 +505,18 @@ type ScenesOptions<Scene extends string, VK extends string> = SceneOption<Scene>
|
|
|
504
505
|
* ```
|
|
505
506
|
*
|
|
506
507
|
*/
|
|
507
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
508
|
+
declare function createEnumLikeObject<T extends Record<string, Record<string, any> & {
|
|
508
509
|
scenes: Record<string, Record<string, any>>;
|
|
509
|
-
}>, N extends string, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
510
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
511
512
|
scenes: Record<string, Record<string, any>>;
|
|
512
|
-
}>, Scene extends keyof T[keyof T]['scenes'] & string, VK extends
|
|
513
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
514
515
|
label: string;
|
|
515
|
-
}>, N extends string, VK extends
|
|
516
|
-
declare function createEnumLikeObject<T extends Record<string, Record<
|
|
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> & {
|
|
517
518
|
label: string;
|
|
518
|
-
}>, VK extends
|
|
519
|
+
}>, VK extends string = 'value'>(obj: T, options?: NormalOptions<VK>): NormalReturn<T, VK>;
|
|
519
520
|
|
|
520
521
|
/**
|
|
521
522
|
* simulate a fake api request
|
|
@@ -763,6 +764,20 @@ type PartialBy<T, K extends keyof T, RemoveUndefined extends boolean = false> =
|
|
|
763
764
|
*
|
|
764
765
|
*/
|
|
765
766
|
declare const deepClone: <T>(obj: T) => T;
|
|
767
|
+
/**
|
|
768
|
+
* A utility function to rename a key in an object.
|
|
769
|
+
*
|
|
770
|
+
* @param obj - The object to modify.
|
|
771
|
+
* @param oldKey - The key to rename.
|
|
772
|
+
* @param newKey - The new key name.
|
|
773
|
+
*
|
|
774
|
+
* @example
|
|
775
|
+
*
|
|
776
|
+
* const obj = { a: 1, b: 2 };
|
|
777
|
+
* const newObj = renameKey(obj, 'a', 'c');
|
|
778
|
+
* console.log(newObj); // { c: 1, b: 2 }
|
|
779
|
+
*/
|
|
780
|
+
declare const renameKey: <T extends object, K extends keyof T, NK extends string>(obj: T, oldKey: K, newKey: NK) => Omit<T, K> & Record<NK, T[K]>;
|
|
766
781
|
|
|
767
782
|
/**
|
|
768
783
|
* debounce function
|
|
@@ -1320,5 +1335,5 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
1320
1335
|
*/
|
|
1321
1336
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
1322
1337
|
|
|
1323
|
-
export { ByteSize, MimeTypeMap, OtherMimeType, QueryProvider, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1338
|
+
export { ByteSize, MimeTypeMap, OtherMimeType, QueryProvider, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, renameKey, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1324
1339
|
export type { MimeTypeExtension, MimeTypeValue, PartialBy, RequiredBy, TCountdownActions };
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import dayjs from 'dayjs';
|
|
2
|
-
export { Q as QueryProvider, u as useQueryContext } from './queryStore-
|
|
2
|
+
export { Q as QueryProvider, u as useQueryContext } from './queryStore-12s-CFQTVwrg.mjs';
|
|
3
3
|
import React, { useMemo, createContext, useContext, useState, useCallback } from 'react';
|
|
4
|
-
export { u as useCountdown } from './useCountdown-
|
|
5
|
-
export { d as downloadFile } from './download-
|
|
6
|
-
export { g as getLocalStorage, s as setLocalStorage } from './webStorage-
|
|
4
|
+
export { u as useCountdown } from './useCountdown-12s-t52WIHfq.mjs';
|
|
5
|
+
export { d as downloadFile } from './download-12s-CnaJ0p_f.mjs';
|
|
6
|
+
export { g as getLocalStorage, s as setLocalStorage } from './webStorage-12s-W1DItzhS.mjs';
|
|
7
7
|
|
|
8
8
|
const FILE_SIZE_UNITS$1 = [
|
|
9
9
|
'Bytes',
|
|
@@ -532,6 +532,27 @@ const isObject = (value)=>value !== null && typeof value === 'object';
|
|
|
532
532
|
}, {});
|
|
533
533
|
return cloned;
|
|
534
534
|
};
|
|
535
|
+
/**
|
|
536
|
+
* A utility function to rename a key in an object.
|
|
537
|
+
*
|
|
538
|
+
* @param obj - The object to modify.
|
|
539
|
+
* @param oldKey - The key to rename.
|
|
540
|
+
* @param newKey - The new key name.
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
*
|
|
544
|
+
* const obj = { a: 1, b: 2 };
|
|
545
|
+
* const newObj = renameKey(obj, 'a', 'c');
|
|
546
|
+
* console.log(newObj); // { c: 1, b: 2 }
|
|
547
|
+
*/ const renameKey = (obj, oldKey, newKey)=>{
|
|
548
|
+
// 建立一個淺拷貝,避免修改原始物件
|
|
549
|
+
const { [oldKey]: oldValue, ...rest } = obj;
|
|
550
|
+
// 回傳新的物件:用新 key 存舊值,其他 key 保留
|
|
551
|
+
return {
|
|
552
|
+
...rest,
|
|
553
|
+
[newKey]: oldValue
|
|
554
|
+
};
|
|
555
|
+
};
|
|
535
556
|
|
|
536
557
|
/**
|
|
537
558
|
* 將嵌套物件的所有屬性設為指定的布林值
|
|
@@ -1392,4 +1413,4 @@ function mergeRefs(refs) {
|
|
|
1392
1413
|
return dayjs(endMonth, 'YYYYMM').subtract(1911, 'year').format('YYYYMM').substring(1);
|
|
1393
1414
|
};
|
|
1394
1415
|
|
|
1395
|
-
export { ByteSize, MimeTypeMap, OtherMimeType, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1416
|
+
export { ByteSize, MimeTypeMap, OtherMimeType, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepClone, deepMerge, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, renameKey, rocEraToAd, searchParamsToObject, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useValue, validTaxId, validateDateString, validateFileType, wait };
|
package/dist/es/types.d.mts
CHANGED
|
@@ -200,5 +200,70 @@ type MapToType<T extends Record<any, any>, A = any> = {
|
|
|
200
200
|
type OverrideProps<T, R extends {
|
|
201
201
|
[K in keyof R]: any;
|
|
202
202
|
}> = Omit<T, keyof R> & R;
|
|
203
|
+
/**
|
|
204
|
+
* A utility type that makes a specific property in the generic type `T` required while keeping the rest optional.
|
|
205
|
+
*
|
|
206
|
+
* @template T - The object type to modify.
|
|
207
|
+
* @template K - The key of the property to make required.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* type User = { id?: number; name?: string }
|
|
211
|
+
* type FormUser = WithRequired<User, 'id'> // { id: number; name?: string }
|
|
212
|
+
*/
|
|
213
|
+
type WithRequired<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;
|
|
214
|
+
/**
|
|
215
|
+
* A utility type that makes a specific property in the generic type `T` optional while keeping the rest required.
|
|
216
|
+
*
|
|
217
|
+
* @template T - The object type to modify.
|
|
218
|
+
* @template K - The key of the property to make optional.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* type User = { id: number; name: string }
|
|
222
|
+
* type FormUser = WithOptional<User, 'id'> // { id?: number; name: string }
|
|
223
|
+
*/
|
|
224
|
+
type WithOptional<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
|
|
225
|
+
/**
|
|
226
|
+
* 產生從 1 到 N 的「數字字面量聯集」型別。
|
|
227
|
+
*
|
|
228
|
+
* @template N - 終止數字(必須是正整數)
|
|
229
|
+
* @template A - 用於遞迴計數的輔助 tuple,預設為 [any] 代表從 1 開始
|
|
230
|
+
* @template R - 暫存累積結果的 union,預設為 1
|
|
231
|
+
*
|
|
232
|
+
* @returns 1 | 2 | ... | N
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* type T1 = Num1ToN<3>;
|
|
236
|
+
* // ^? 1 | 2 | 3
|
|
237
|
+
*
|
|
238
|
+
* type T2 = Num1ToN<5>;
|
|
239
|
+
* // ^? 1 | 2 | 3 | 4 | 5
|
|
240
|
+
*
|
|
241
|
+
* @warning
|
|
242
|
+
* ⚠ **效能注意事項**:
|
|
243
|
+
* - 遞迴深度等於 `N`,因此 `N` 過大會影響 TypeScript 編譯效能
|
|
244
|
+
* - 建議 **N ≤ 100**,否則 VS Code IntelliSense 可能變慢
|
|
245
|
+
*/
|
|
246
|
+
type Num1ToN<N extends number, A extends any[] = [], R extends number = never> = A['length'] extends N ? R | N : Num1ToN<N, [...A, any], R | (A['length'] extends 0 ? never : A['length'])>;
|
|
247
|
+
/**
|
|
248
|
+
* 根據給定的前綴字串與數量,從陣列型別生成一個物件型別。
|
|
249
|
+
*
|
|
250
|
+
* @template T - 陣列型別,例如 `string[]`
|
|
251
|
+
* @template Prefix - 屬性鍵的前綴字串,例如 `'url'`
|
|
252
|
+
* @template N - 欄位數量,會生成 1..N 的鍵名
|
|
253
|
+
*
|
|
254
|
+
* @returns 一個物件型別,鍵為 `${Prefix}${1..N}`,值為 `T[number]`
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* type List = string[];
|
|
258
|
+
*
|
|
259
|
+
* type Obj = BuildPrefixedObject<List, 'url', 3>;
|
|
260
|
+
* // ^? { url1: string; url2: string; url3: string }
|
|
261
|
+
*
|
|
262
|
+
* type Obj2 = BuildPrefixedObject<List, 'name', 2>;
|
|
263
|
+
* // ^? { name1: string; name2: string }
|
|
264
|
+
*/
|
|
265
|
+
type BuildPrefixedObject<T extends readonly unknown[], Prefix extends string, N extends number> = {
|
|
266
|
+
[K in Num1ToN<N> as `${Prefix}${K & number}`]: T[number];
|
|
267
|
+
};
|
|
203
268
|
|
|
204
|
-
export type { AtLeastOne, DeepPartial, Entries, MapToString, MapToType, OnlyOne, OverrideProps, PopArgs, PushArgs, ShiftArgs, TChangeKeyType, TExtractValueType, UnshiftArgs };
|
|
269
|
+
export type { AtLeastOne, BuildPrefixedObject, DeepPartial, Entries, MapToString, MapToType, Num1ToN, OnlyOne, OverrideProps, PopArgs, PushArgs, ShiftArgs, TChangeKeyType, TExtractValueType, UnshiftArgs, WithOptional, WithRequired };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gateweb/react-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "React Utils for GateWeb",
|
|
5
5
|
"homepage": "https://github.com/GatewebSolutions/react-utils",
|
|
6
6
|
"files": [
|
|
@@ -31,6 +31,16 @@
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"prepare": "husky",
|
|
36
|
+
"run-code": "ts-node src/period.ts",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:watch": "vitest",
|
|
39
|
+
"test:coverage": "vitest run --coverage",
|
|
40
|
+
"test:ui": "vitest --ui --coverage.enabled=true",
|
|
41
|
+
"build": "bunchee",
|
|
42
|
+
"build:prepare": "bunchee --prepare"
|
|
43
|
+
},
|
|
34
44
|
"publishConfig": {
|
|
35
45
|
"access": "public"
|
|
36
46
|
},
|
|
@@ -52,7 +62,7 @@
|
|
|
52
62
|
"@types/react": "^19.1.8",
|
|
53
63
|
"@vitest/coverage-v8": "^3.2.4",
|
|
54
64
|
"@vitest/ui": "3.2.4",
|
|
55
|
-
"bunchee": "^
|
|
65
|
+
"bunchee": "^6.6.0",
|
|
56
66
|
"eslint": "^9.31.0",
|
|
57
67
|
"husky": "^9.1.7",
|
|
58
68
|
"jest": "^30.0.5",
|
|
@@ -69,13 +79,5 @@
|
|
|
69
79
|
"@commitlint/config-conventional"
|
|
70
80
|
]
|
|
71
81
|
},
|
|
72
|
-
"
|
|
73
|
-
"run-code": "ts-node src/period.ts",
|
|
74
|
-
"test": "vitest run",
|
|
75
|
-
"test:watch": "vitest",
|
|
76
|
-
"test:coverage": "vitest run --coverage",
|
|
77
|
-
"test:ui": "vitest --ui --coverage.enabled=true",
|
|
78
|
-
"build": "bunchee",
|
|
79
|
-
"build:prepare": "bunchee --prepare"
|
|
80
|
-
}
|
|
82
|
+
"packageManager": "pnpm@10.11.0"
|
|
81
83
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|