@gateweb/react-utils 0.0.1 → 0.0.3
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 +27 -20
- package/dist/cjs/index.js +27 -22
- package/dist/es/index.d.mts +27 -20
- package/dist/es/index.mjs +27 -22
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -263,6 +263,8 @@ type TCountdownActions = {
|
|
|
263
263
|
*/
|
|
264
264
|
declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
|
|
265
265
|
|
|
266
|
+
declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
|
|
267
|
+
|
|
266
268
|
/**
|
|
267
269
|
* 判斷執行環境是否為 Server(node.js) 端
|
|
268
270
|
*/
|
|
@@ -290,25 +292,6 @@ declare const omit: <T extends object, K extends [...(keyof T)[]]>(object: T, ..
|
|
|
290
292
|
* const b = omitByValue(a, undefined, null); // { c: 3, d: 4 }
|
|
291
293
|
*/
|
|
292
294
|
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]>;
|
|
293
|
-
/**
|
|
294
|
-
* 將指定格式的物件轉換成類似 enum 的物件
|
|
295
|
-
*
|
|
296
|
-
* @param enumObject enum 物件
|
|
297
|
-
* @param valueKey value 的 key
|
|
298
|
-
*
|
|
299
|
-
* @example
|
|
300
|
-
*
|
|
301
|
-
* ```js
|
|
302
|
-
* const myObj = {
|
|
303
|
-
* A: { value: 'a' },
|
|
304
|
-
* B: { value: 'b', other: 'other' },
|
|
305
|
-
* }
|
|
306
|
-
*
|
|
307
|
-
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
308
|
-
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
309
|
-
* ```
|
|
310
|
-
*/
|
|
311
|
-
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]; };
|
|
312
295
|
|
|
313
296
|
/**
|
|
314
297
|
* debounce function
|
|
@@ -617,4 +600,28 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
617
600
|
*/
|
|
618
601
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
619
602
|
|
|
620
|
-
|
|
603
|
+
/**
|
|
604
|
+
* 從對象類型中提取特定屬性的值類型。
|
|
605
|
+
*
|
|
606
|
+
* @template T 要處理的對象類型
|
|
607
|
+
* @template K 要提取的屬性名(字符串字面量類型)
|
|
608
|
+
*
|
|
609
|
+
* @description
|
|
610
|
+
* 這個類型用於從一個對象類型 T 中提取指定屬性 K 的值類型。
|
|
611
|
+
* T 應該是一個對象,其每個屬性都是另一個包含 K 屬性的對象。
|
|
612
|
+
* 如果 T 不符合這個結構,或者指定的屬性 K 不存在,則返回 never。
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* type Obj = {
|
|
616
|
+
* a: { value: string },
|
|
617
|
+
* b: { value: number }
|
|
618
|
+
* };
|
|
619
|
+
* type Result = ExtractValueType<Obj, 'value'>; // string | number
|
|
620
|
+
*
|
|
621
|
+
* @returns 返回 T 中所有 K 屬性值的聯合類型,如果不符合條件則返回 never
|
|
622
|
+
*/
|
|
623
|
+
type TExtractValueType<T, K extends string> = T extends Record<string, {
|
|
624
|
+
[P in K]: infer R;
|
|
625
|
+
}> ? R : never;
|
|
626
|
+
|
|
627
|
+
export { type TCountdownActions, type TExtractValueType, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce, downloadFile, 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 };
|
package/dist/cjs/index.js
CHANGED
|
@@ -376,6 +376,32 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
376
376
|
}
|
|
377
377
|
};
|
|
378
378
|
|
|
379
|
+
// const isProduction: boolean = process.env.NODE_ENV === 'production';
|
|
380
|
+
const prefix = 'Invariant failed';
|
|
381
|
+
// Throw an error if the condition fails
|
|
382
|
+
// Strip out error messages for production
|
|
383
|
+
// > Not providing an inline default argument for message as the result is smaller
|
|
384
|
+
function invariant(condition, // Can provide a string, or a function that returns a string for cases where
|
|
385
|
+
// the message takes a fair amount of effort to compute
|
|
386
|
+
message) {
|
|
387
|
+
if (condition) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
// Condition not passed
|
|
391
|
+
// In production we strip the message but still throw
|
|
392
|
+
if (process.env.NODE_ENV === 'production') {
|
|
393
|
+
throw new Error(prefix);
|
|
394
|
+
}
|
|
395
|
+
// When not in production we allow the message to pass through
|
|
396
|
+
// *This block will be removed in production builds*
|
|
397
|
+
const provided = typeof message === 'function' ? message() : message;
|
|
398
|
+
// Options:
|
|
399
|
+
// 1. message provided: `${prefix}: ${provided}`
|
|
400
|
+
// 2. message not provided: prefix
|
|
401
|
+
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
402
|
+
throw new Error(value);
|
|
403
|
+
}
|
|
404
|
+
|
|
379
405
|
/**
|
|
380
406
|
* 判斷執行環境是否為 Server(node.js) 端
|
|
381
407
|
*/ const isServer = ()=>typeof window === 'undefined';
|
|
@@ -416,27 +442,6 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
416
442
|
[key]: value
|
|
417
443
|
};
|
|
418
444
|
}, {});
|
|
419
|
-
/**
|
|
420
|
-
* 將指定格式的物件轉換成類似 enum 的物件
|
|
421
|
-
*
|
|
422
|
-
* @param enumObject enum 物件
|
|
423
|
-
* @param valueKey value 的 key
|
|
424
|
-
*
|
|
425
|
-
* @example
|
|
426
|
-
*
|
|
427
|
-
* ```js
|
|
428
|
-
* const myObj = {
|
|
429
|
-
* A: { value: 'a' },
|
|
430
|
-
* B: { value: 'b', other: 'other' },
|
|
431
|
-
* }
|
|
432
|
-
*
|
|
433
|
-
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
434
|
-
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
435
|
-
* ```
|
|
436
|
-
*/ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
|
|
437
|
-
...acc,
|
|
438
|
-
[key]: value[valueKey]
|
|
439
|
-
}), {});
|
|
440
445
|
|
|
441
446
|
/**
|
|
442
447
|
* debounce function
|
|
@@ -679,12 +684,12 @@ exports.camelCase2SnakeCase = camelCase2SnakeCase;
|
|
|
679
684
|
exports.camelString2PascalString = camelString2PascalString;
|
|
680
685
|
exports.camelString2SnakeString = camelString2SnakeString;
|
|
681
686
|
exports.debounce = debounce;
|
|
682
|
-
exports.extractEnumLikeObject = extractEnumLikeObject;
|
|
683
687
|
exports.formatAmount = formatAmount;
|
|
684
688
|
exports.formatStarMask = formatStarMask;
|
|
685
689
|
exports.generatePeriodArray = generatePeriodArray;
|
|
686
690
|
exports.getCurrentPeriod = getCurrentPeriod;
|
|
687
691
|
exports.getMimeType = getMimeType;
|
|
692
|
+
exports.invariant = invariant;
|
|
688
693
|
exports.isChinese = isChinese;
|
|
689
694
|
exports.isDateString = isDateString;
|
|
690
695
|
exports.isDateTimeString = isDateTimeString;
|
package/dist/es/index.d.mts
CHANGED
|
@@ -263,6 +263,8 @@ type TCountdownActions = {
|
|
|
263
263
|
*/
|
|
264
264
|
declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
|
|
265
265
|
|
|
266
|
+
declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
|
|
267
|
+
|
|
266
268
|
/**
|
|
267
269
|
* 判斷執行環境是否為 Server(node.js) 端
|
|
268
270
|
*/
|
|
@@ -290,25 +292,6 @@ declare const omit: <T extends object, K extends [...(keyof T)[]]>(object: T, ..
|
|
|
290
292
|
* const b = omitByValue(a, undefined, null); // { c: 3, d: 4 }
|
|
291
293
|
*/
|
|
292
294
|
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]>;
|
|
293
|
-
/**
|
|
294
|
-
* 將指定格式的物件轉換成類似 enum 的物件
|
|
295
|
-
*
|
|
296
|
-
* @param enumObject enum 物件
|
|
297
|
-
* @param valueKey value 的 key
|
|
298
|
-
*
|
|
299
|
-
* @example
|
|
300
|
-
*
|
|
301
|
-
* ```js
|
|
302
|
-
* const myObj = {
|
|
303
|
-
* A: { value: 'a' },
|
|
304
|
-
* B: { value: 'b', other: 'other' },
|
|
305
|
-
* }
|
|
306
|
-
*
|
|
307
|
-
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
308
|
-
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
309
|
-
* ```
|
|
310
|
-
*/
|
|
311
|
-
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]; };
|
|
312
295
|
|
|
313
296
|
/**
|
|
314
297
|
* debounce function
|
|
@@ -617,4 +600,28 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
617
600
|
*/
|
|
618
601
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
619
602
|
|
|
620
|
-
|
|
603
|
+
/**
|
|
604
|
+
* 從對象類型中提取特定屬性的值類型。
|
|
605
|
+
*
|
|
606
|
+
* @template T 要處理的對象類型
|
|
607
|
+
* @template K 要提取的屬性名(字符串字面量類型)
|
|
608
|
+
*
|
|
609
|
+
* @description
|
|
610
|
+
* 這個類型用於從一個對象類型 T 中提取指定屬性 K 的值類型。
|
|
611
|
+
* T 應該是一個對象,其每個屬性都是另一個包含 K 屬性的對象。
|
|
612
|
+
* 如果 T 不符合這個結構,或者指定的屬性 K 不存在,則返回 never。
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* type Obj = {
|
|
616
|
+
* a: { value: string },
|
|
617
|
+
* b: { value: number }
|
|
618
|
+
* };
|
|
619
|
+
* type Result = ExtractValueType<Obj, 'value'>; // string | number
|
|
620
|
+
*
|
|
621
|
+
* @returns 返回 T 中所有 K 屬性值的聯合類型,如果不符合條件則返回 never
|
|
622
|
+
*/
|
|
623
|
+
type TExtractValueType<T, K extends string> = T extends Record<string, {
|
|
624
|
+
[P in K]: infer R;
|
|
625
|
+
}> ? R : never;
|
|
626
|
+
|
|
627
|
+
export { type TCountdownActions, type TExtractValueType, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce, downloadFile, 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 };
|
package/dist/es/index.mjs
CHANGED
|
@@ -370,6 +370,32 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
370
370
|
}
|
|
371
371
|
};
|
|
372
372
|
|
|
373
|
+
// const isProduction: boolean = process.env.NODE_ENV === 'production';
|
|
374
|
+
const prefix = 'Invariant failed';
|
|
375
|
+
// Throw an error if the condition fails
|
|
376
|
+
// Strip out error messages for production
|
|
377
|
+
// > Not providing an inline default argument for message as the result is smaller
|
|
378
|
+
function invariant(condition, // Can provide a string, or a function that returns a string for cases where
|
|
379
|
+
// the message takes a fair amount of effort to compute
|
|
380
|
+
message) {
|
|
381
|
+
if (condition) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
// Condition not passed
|
|
385
|
+
// In production we strip the message but still throw
|
|
386
|
+
if (process.env.NODE_ENV === 'production') {
|
|
387
|
+
throw new Error(prefix);
|
|
388
|
+
}
|
|
389
|
+
// When not in production we allow the message to pass through
|
|
390
|
+
// *This block will be removed in production builds*
|
|
391
|
+
const provided = typeof message === 'function' ? message() : message;
|
|
392
|
+
// Options:
|
|
393
|
+
// 1. message provided: `${prefix}: ${provided}`
|
|
394
|
+
// 2. message not provided: prefix
|
|
395
|
+
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
396
|
+
throw new Error(value);
|
|
397
|
+
}
|
|
398
|
+
|
|
373
399
|
/**
|
|
374
400
|
* 判斷執行環境是否為 Server(node.js) 端
|
|
375
401
|
*/ const isServer = ()=>typeof window === 'undefined';
|
|
@@ -410,27 +436,6 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
410
436
|
[key]: value
|
|
411
437
|
};
|
|
412
438
|
}, {});
|
|
413
|
-
/**
|
|
414
|
-
* 將指定格式的物件轉換成類似 enum 的物件
|
|
415
|
-
*
|
|
416
|
-
* @param enumObject enum 物件
|
|
417
|
-
* @param valueKey value 的 key
|
|
418
|
-
*
|
|
419
|
-
* @example
|
|
420
|
-
*
|
|
421
|
-
* ```js
|
|
422
|
-
* const myObj = {
|
|
423
|
-
* A: { value: 'a' },
|
|
424
|
-
* B: { value: 'b', other: 'other' },
|
|
425
|
-
* }
|
|
426
|
-
*
|
|
427
|
-
* const enumCode = extractEnumLikeObject(myObj, 'value');
|
|
428
|
-
* console.log(enumCode); // { A: 'a', B: 'b' }
|
|
429
|
-
* ```
|
|
430
|
-
*/ const extractEnumLikeObject = (enumObject, valueKey)=>Object.entries(enumObject).reduce((acc, [key, value])=>({
|
|
431
|
-
...acc,
|
|
432
|
-
[key]: value[valueKey]
|
|
433
|
-
}), {});
|
|
434
439
|
|
|
435
440
|
/**
|
|
436
441
|
* debounce function
|
|
@@ -663,4 +668,4 @@ const transformObjectKey = (obj, transformFunName)=>{
|
|
|
663
668
|
* formatString('123456', 1, 4) // '1****6'
|
|
664
669
|
*/ const formatStarMask = (str, n, m)=>str.slice(0, n) + '*'.repeat(m - n + 1) + str.slice(m + 1);
|
|
665
670
|
|
|
666
|
-
export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce,
|
|
671
|
+
export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, debounce, 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 };
|