mazey 5.1.1 → 5.3.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/README.md +156 -0
- package/lib/index.cjs.js +2 -2
- package/lib/index.d.ts +479 -145
- package/lib/index.esm.js +2 -2
- package/lib/mazey.min.js +2 -2
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* ZH: 计算两个字符串的最长公共子串。
|
|
2
|
+
* Compute the length of the longest common substring of two strings.
|
|
5
3
|
*
|
|
6
4
|
* Usage:
|
|
7
5
|
*
|
|
@@ -31,9 +29,7 @@ declare function longestComSubstring(aStr: string, bStr: string): number;
|
|
|
31
29
|
*/
|
|
32
30
|
declare function calLongestCommonSubstring(aStr: string, bStr: string): number;
|
|
33
31
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* ZH: 计算两个字符串的最长公共子序列。
|
|
32
|
+
* Compute the length of the longest common subsequence of two strings.
|
|
37
33
|
*
|
|
38
34
|
* Usage:
|
|
39
35
|
*
|
|
@@ -50,9 +46,9 @@ declare function calLongestCommonSubstring(aStr: string, bStr: string): number;
|
|
|
50
46
|
* 4
|
|
51
47
|
* ```
|
|
52
48
|
*
|
|
53
|
-
* @param {string} aStr
|
|
54
|
-
* @param {string} bStr
|
|
55
|
-
* @returns {number}
|
|
49
|
+
* @param {string} aStr The first string.
|
|
50
|
+
* @param {string} bStr The second string.
|
|
51
|
+
* @returns {number} The length of the longest common subsequence.
|
|
56
52
|
* @category Calculate and Formula
|
|
57
53
|
*/
|
|
58
54
|
declare function longestComSubsequence(aStr: string, bStr: string): number;
|
|
@@ -63,16 +59,14 @@ declare function longestComSubsequence(aStr: string, bStr: string): number;
|
|
|
63
59
|
*/
|
|
64
60
|
declare function calLongestCommonSubsequence(aStr: string, bStr: string): number;
|
|
65
61
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* ZH: 百分位概率。
|
|
62
|
+
* Return whether a random value falls within the given probability.
|
|
69
63
|
*
|
|
70
64
|
* Usage:
|
|
71
65
|
*
|
|
72
66
|
* ```javascript
|
|
73
67
|
* import { isHit } from "mazey";
|
|
74
68
|
*
|
|
75
|
-
* const ret = isHit(0.5); //
|
|
69
|
+
* const ret = isHit(0.5); // A 50% chance of returning true.
|
|
76
70
|
* console.log(ret);
|
|
77
71
|
* ```
|
|
78
72
|
*
|
|
@@ -103,8 +97,8 @@ declare function calLongestCommonSubsequence(aStr: string, bStr: string): number
|
|
|
103
97
|
* 499994 500006
|
|
104
98
|
* ```
|
|
105
99
|
*
|
|
106
|
-
* @param {number} rate
|
|
107
|
-
* @returns {boolean}
|
|
100
|
+
* @param {number} rate Probability expressed as a value from 0 to 1.
|
|
101
|
+
* @returns {boolean} Whether the random value is less than the probability.
|
|
108
102
|
* @category Calculate and Formula
|
|
109
103
|
*/
|
|
110
104
|
declare function isHit(rate: number): boolean;
|
|
@@ -467,6 +461,24 @@ declare function isJSONString(str: string): boolean;
|
|
|
467
461
|
* @hidden
|
|
468
462
|
*/
|
|
469
463
|
declare function isJsonString(str: string): boolean;
|
|
464
|
+
/**
|
|
465
|
+
* Parse a JSON string and return a caller-defined fallback when parsing fails.
|
|
466
|
+
*
|
|
467
|
+
* Usage:
|
|
468
|
+
*
|
|
469
|
+
* ```javascript
|
|
470
|
+
* import { parseJsonSafe } from "mazey";
|
|
471
|
+
*
|
|
472
|
+
* const data = parseJsonSafe('{"enabled":true}');
|
|
473
|
+
* const fallback = parseJsonSafe("invalid", {});
|
|
474
|
+
* ```
|
|
475
|
+
*
|
|
476
|
+
* @param value JSON string to parse.
|
|
477
|
+
* @param fallback Value returned when parsing fails. Defaults to `null`.
|
|
478
|
+
* @returns The parsed JSON value or the supplied fallback.
|
|
479
|
+
* @category Util
|
|
480
|
+
*/
|
|
481
|
+
declare function parseJsonSafe<T, F = null>(value: string, fallback?: F): T | F;
|
|
470
482
|
/**
|
|
471
483
|
* Generate a random string of number, `genRndNumString(7)` => "7658495".
|
|
472
484
|
*
|
|
@@ -500,9 +512,8 @@ declare function genRndNumString(n?: number): string;
|
|
|
500
512
|
*/
|
|
501
513
|
declare function generateRndNum(n?: number): string;
|
|
502
514
|
/**
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
* ZH: 根据时间生成唯一标志的数字:`genUniqueNumString()` => `1538324722364123`。
|
|
515
|
+
* Generate a numeric identifier by combining the current timestamp with a
|
|
516
|
+
* random numeric suffix.
|
|
506
517
|
*
|
|
507
518
|
* Usage:
|
|
508
519
|
*
|
|
@@ -522,7 +533,8 @@ declare function generateRndNum(n?: number): string;
|
|
|
522
533
|
* 1538324722364123
|
|
523
534
|
* ```
|
|
524
535
|
*
|
|
525
|
-
* @param {number} n
|
|
536
|
+
* @param {number} n Length of the random numeric suffix.
|
|
537
|
+
* @returns {string} A timestamp-based numeric identifier.
|
|
526
538
|
* @category Util
|
|
527
539
|
*/
|
|
528
540
|
declare function genUniqueNumString(n?: number): string;
|
|
@@ -533,9 +545,7 @@ declare function genUniqueNumString(n?: number): string;
|
|
|
533
545
|
*/
|
|
534
546
|
declare function generateUniqueNum(n?: number): string;
|
|
535
547
|
/**
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
* ZH: 获取时间戳。
|
|
548
|
+
* Get the current timestamp in milliseconds.
|
|
539
549
|
*
|
|
540
550
|
* Usage:
|
|
541
551
|
*
|
|
@@ -552,13 +562,12 @@ declare function generateUniqueNum(n?: number): string;
|
|
|
552
562
|
* 1585325367122
|
|
553
563
|
* ```
|
|
554
564
|
*
|
|
565
|
+
* @returns {number} The current timestamp in milliseconds.
|
|
555
566
|
* @category Util
|
|
556
567
|
*/
|
|
557
568
|
declare function mNow(): number;
|
|
558
569
|
/**
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
* ZH: 浮点数转为百分比 0.2 => 20%。
|
|
570
|
+
* Convert a floating-point ratio to a percentage string.
|
|
562
571
|
*
|
|
563
572
|
* Usage:
|
|
564
573
|
*
|
|
@@ -578,15 +587,14 @@ declare function mNow(): number;
|
|
|
578
587
|
* 20.00%
|
|
579
588
|
* ```
|
|
580
589
|
*
|
|
581
|
-
* @param {number} num
|
|
582
|
-
* @param {number} fixSize
|
|
590
|
+
* @param {number} num Floating-point ratio to convert.
|
|
591
|
+
* @param {number} fixSize Number of decimal places in the percentage.
|
|
592
|
+
* @returns {string} The percentage string.
|
|
583
593
|
* @category Util
|
|
584
594
|
*/
|
|
585
595
|
declare function floatToPercent(num: number, fixSize?: number): string;
|
|
586
596
|
/**
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
* ZH: 浮点数保留指定位。
|
|
597
|
+
* Format a number with a fixed number of decimal places.
|
|
590
598
|
*
|
|
591
599
|
* Usage:
|
|
592
600
|
*
|
|
@@ -606,13 +614,14 @@ declare function floatToPercent(num: number, fixSize?: number): string;
|
|
|
606
614
|
* 0.20
|
|
607
615
|
* ```
|
|
608
616
|
*
|
|
617
|
+
* @param num Number or numeric string to format.
|
|
618
|
+
* @param size Number of decimal places.
|
|
619
|
+
* @returns The fixed-point string.
|
|
609
620
|
* @category Util
|
|
610
621
|
*/
|
|
611
622
|
declare function floatFixed(num: number | string, size?: number): string;
|
|
612
623
|
/**
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
* ZH: 节流,用于限制函数在一段时间内的执行频率。
|
|
624
|
+
* Limit how frequently a function can be invoked over time.
|
|
616
625
|
*
|
|
617
626
|
* Usage:
|
|
618
627
|
*
|
|
@@ -626,6 +635,11 @@ declare function floatFixed(num: number | string, size?: number): string;
|
|
|
626
635
|
*
|
|
627
636
|
* Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
|
|
628
637
|
*
|
|
638
|
+
* @param func Function to throttle.
|
|
639
|
+
* @param wait Minimum interval between invocations, in milliseconds.
|
|
640
|
+
* @param options.leading Whether to invoke on the leading edge.
|
|
641
|
+
* @param options.trailing Whether to invoke on the trailing edge.
|
|
642
|
+
* @returns The throttled function.
|
|
629
643
|
* @category Util
|
|
630
644
|
*/
|
|
631
645
|
declare function throttle<T extends (...args: MazeyFnParams) => MazeyFnReturn>(func: T, wait: number, options?: {
|
|
@@ -633,9 +647,8 @@ declare function throttle<T extends (...args: MazeyFnParams) => MazeyFnReturn>(f
|
|
|
633
647
|
trailing?: boolean;
|
|
634
648
|
}): ThrottleFunc<T>;
|
|
635
649
|
/**
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
* ZH: 防抖,用于在最后一次调用后的指定时间内延迟函数的执行。
|
|
650
|
+
* Delay function execution until the specified time has passed since the last
|
|
651
|
+
* invocation.
|
|
639
652
|
*
|
|
640
653
|
* Usage:
|
|
641
654
|
*
|
|
@@ -647,6 +660,10 @@ declare function throttle<T extends (...args: MazeyFnParams) => MazeyFnReturn>(f
|
|
|
647
660
|
* }, 1000, true);
|
|
648
661
|
* ```
|
|
649
662
|
*
|
|
663
|
+
* @param func Function to debounce.
|
|
664
|
+
* @param wait Delay after the last invocation, in milliseconds.
|
|
665
|
+
* @param immediate Whether to invoke on the leading edge instead.
|
|
666
|
+
* @returns The debounced function.
|
|
650
667
|
* @category Util
|
|
651
668
|
*/
|
|
652
669
|
declare function debounce<T extends (...args: MazeyFnParams) => MazeyFnReturn>(func: T, wait: number, immediate?: boolean): DebounceFunc<T>;
|
|
@@ -727,9 +744,7 @@ declare function getFriendlyInterval(start?: number | string | Date, end?: numbe
|
|
|
727
744
|
*/
|
|
728
745
|
declare function formatDurationFromMs(durationMs: number): string;
|
|
729
746
|
/**
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
* ZH: 判断是否有效数字。
|
|
747
|
+
* Check whether a value is a number allowed by the supplied options.
|
|
733
748
|
*
|
|
734
749
|
* Usage:
|
|
735
750
|
*
|
|
@@ -752,16 +767,14 @@ declare function formatDurationFromMs(durationMs: number): string;
|
|
|
752
767
|
* true false false true false true
|
|
753
768
|
* ```
|
|
754
769
|
*
|
|
755
|
-
* @param {*} num
|
|
770
|
+
* @param {*} num Value to check.
|
|
756
771
|
* @param options Controls whether `NaN`, `Infinity`, or other non-finite values count as numbers.
|
|
757
|
-
* @returns {boolean}
|
|
772
|
+
* @returns {boolean} Whether the value is an allowed number.
|
|
758
773
|
* @category Util
|
|
759
774
|
*/
|
|
760
775
|
declare function isNumber(num: unknown, options?: IsNumberOptions): boolean;
|
|
761
776
|
/**
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
* ZH: 执行有效函数。
|
|
777
|
+
* Invoke a value only when it is a function.
|
|
765
778
|
*
|
|
766
779
|
* Usage:
|
|
767
780
|
*
|
|
@@ -773,7 +786,9 @@ declare function isNumber(num: unknown, options?: IsNumberOptions): boolean;
|
|
|
773
786
|
* });
|
|
774
787
|
* ```
|
|
775
788
|
*
|
|
776
|
-
* @param {function} fn
|
|
789
|
+
* @param {function} fn Potential function to invoke.
|
|
790
|
+
* @param params Arguments passed to the function.
|
|
791
|
+
* @returns The function result, or `null` when `fn` is not callable.
|
|
777
792
|
* @category Util
|
|
778
793
|
*/
|
|
779
794
|
declare function invokeFn(fn: MazeyFunction | null | undefined, ...params: Parameters<MazeyFunction>): ReturnType<MazeyFunction> | null;
|
|
@@ -1037,8 +1052,8 @@ declare function newLine(str: string): string;
|
|
|
1037
1052
|
* hello world
|
|
1038
1053
|
* ```
|
|
1039
1054
|
*
|
|
1040
|
-
* @param {string} str
|
|
1041
|
-
* @returns {string}
|
|
1055
|
+
* @param {string} str A string that may contain HTML tags.
|
|
1056
|
+
* @returns {string} The string with HTML tags removed.
|
|
1042
1057
|
* @category Util
|
|
1043
1058
|
*/
|
|
1044
1059
|
declare function removeHTML(str: string, options?: {
|
|
@@ -1121,9 +1136,8 @@ declare function unsanitizeInput(input: string): string;
|
|
|
1121
1136
|
*/
|
|
1122
1137
|
declare function unsanitize(str: string): string;
|
|
1123
1138
|
/**
|
|
1124
|
-
*
|
|
1125
|
-
*
|
|
1126
|
-
* ZH: 截取字符串,中文算 2 个字节。
|
|
1139
|
+
* Truncate a string by weighted length, counting non-ASCII characters as two
|
|
1140
|
+
* units.
|
|
1127
1141
|
*
|
|
1128
1142
|
* Usage:
|
|
1129
1143
|
*
|
|
@@ -1140,11 +1154,11 @@ declare function unsanitize(str: string): string;
|
|
|
1140
1154
|
* hello
|
|
1141
1155
|
* ```
|
|
1142
1156
|
*
|
|
1143
|
-
* @param {string} str
|
|
1144
|
-
* @param {number} len
|
|
1145
|
-
* @param {boolean} options.hasDot
|
|
1146
|
-
* @param {string} options.dotText
|
|
1147
|
-
* @returns {string}
|
|
1157
|
+
* @param {string} str String to truncate.
|
|
1158
|
+
* @param {number} len Maximum weighted length.
|
|
1159
|
+
* @param {boolean} options.hasDot Whether to append truncation text.
|
|
1160
|
+
* @param {string} options.dotText Text appended when truncation occurs.
|
|
1161
|
+
* @returns {string} The truncated string.
|
|
1148
1162
|
* @category Util
|
|
1149
1163
|
*/
|
|
1150
1164
|
declare function cutZHString(str: string | null | undefined, len: number, options?: {
|
|
@@ -1167,11 +1181,12 @@ declare function cutZHString(str: string | null | undefined, len: number, option
|
|
|
1167
1181
|
*
|
|
1168
1182
|
* ```text
|
|
1169
1183
|
* hello
|
|
1184
|
+
* ```
|
|
1170
1185
|
*
|
|
1171
|
-
* @param {string} str
|
|
1172
|
-
* @param {number} len
|
|
1173
|
-
* @param {boolean} hasDot
|
|
1174
|
-
* @returns {string}
|
|
1186
|
+
* @param {string} str String to truncate.
|
|
1187
|
+
* @param {number} len Maximum weighted length.
|
|
1188
|
+
* @param {boolean} hasDot Whether to append the default truncation text.
|
|
1189
|
+
* @returns {string} The truncated string.
|
|
1175
1190
|
* @hidden
|
|
1176
1191
|
*/
|
|
1177
1192
|
declare function truncateZHString(str: string | null | undefined, len: number, hasDot?: boolean): string;
|
|
@@ -1229,9 +1244,7 @@ declare function zAxiosIsValidRes(res: ZResResponse | undefined, options?: ZResI
|
|
|
1229
1244
|
*/
|
|
1230
1245
|
declare function isValidData(data: MazeyObject, attributes: string[], validValue: SimpleType): boolean;
|
|
1231
1246
|
/**
|
|
1232
|
-
*
|
|
1233
|
-
*
|
|
1234
|
-
* ZH: 语义化文件大小,把字节转换成正常文件大小。
|
|
1247
|
+
* Convert a byte count to a human-readable file size.
|
|
1235
1248
|
*
|
|
1236
1249
|
* Usage:
|
|
1237
1250
|
*
|
|
@@ -1248,6 +1261,8 @@ declare function isValidData(data: MazeyObject, attributes: string[], validValue
|
|
|
1248
1261
|
* 1 KB
|
|
1249
1262
|
* ```
|
|
1250
1263
|
*
|
|
1264
|
+
* @param size File size in bytes.
|
|
1265
|
+
* @returns A rounded file-size string, or an empty string for a non-positive or non-finite value.
|
|
1251
1266
|
* @category Util
|
|
1252
1267
|
*/
|
|
1253
1268
|
declare function getFileSize(size: number): string;
|
|
@@ -1274,6 +1289,31 @@ declare function getFileSize(size: number): string;
|
|
|
1274
1289
|
* @category Util
|
|
1275
1290
|
*/
|
|
1276
1291
|
declare function genHashCode(str: string): number;
|
|
1292
|
+
/**
|
|
1293
|
+
* Generate a lowercase SHA-256 hexadecimal digest with the Web Crypto API.
|
|
1294
|
+
*
|
|
1295
|
+
* Usage:
|
|
1296
|
+
*
|
|
1297
|
+
* ```javascript
|
|
1298
|
+
* import { sha256Hex } from "mazey";
|
|
1299
|
+
*
|
|
1300
|
+
* const digest = await sha256Hex("hello world");
|
|
1301
|
+
* console.log(digest);
|
|
1302
|
+
* ```
|
|
1303
|
+
*
|
|
1304
|
+
* Output:
|
|
1305
|
+
*
|
|
1306
|
+
* ```text
|
|
1307
|
+
* b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
|
1308
|
+
* ```
|
|
1309
|
+
*
|
|
1310
|
+
* @remarks Requires Web Crypto. String input also requires `TextEncoder`.
|
|
1311
|
+
* @param input Text or binary data to hash.
|
|
1312
|
+
* @returns A promise that resolves to the lowercase hexadecimal digest.
|
|
1313
|
+
* @throws When the required platform API is unavailable. Digest failures are propagated.
|
|
1314
|
+
* @category Util
|
|
1315
|
+
*/
|
|
1316
|
+
declare function sha256Hex(input: string | BufferSource): Promise<string>;
|
|
1277
1317
|
/**
|
|
1278
1318
|
* Check whether an unknown value represents a valid date.
|
|
1279
1319
|
*
|
|
@@ -1310,6 +1350,150 @@ declare function genHashCode(str: string): number;
|
|
|
1310
1350
|
* @category Util
|
|
1311
1351
|
*/
|
|
1312
1352
|
declare function isValidDate(value: unknown): boolean;
|
|
1353
|
+
/**
|
|
1354
|
+
* Check whether a date is today in the runtime's local timezone.
|
|
1355
|
+
*
|
|
1356
|
+
* Usage:
|
|
1357
|
+
*
|
|
1358
|
+
* ```javascript
|
|
1359
|
+
* import { isToday } from "mazey";
|
|
1360
|
+
*
|
|
1361
|
+
* const ret = isToday(new Date());
|
|
1362
|
+
* console.log(ret);
|
|
1363
|
+
* ```
|
|
1364
|
+
*
|
|
1365
|
+
* Output:
|
|
1366
|
+
*
|
|
1367
|
+
* ```text
|
|
1368
|
+
* true
|
|
1369
|
+
* ```
|
|
1370
|
+
*
|
|
1371
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1372
|
+
* @returns Whether the value has the current local year, month, and day. Invalid input returns `false`.
|
|
1373
|
+
* @remarks Hours, minutes, seconds, and milliseconds are ignored. Results depend on the runtime's local timezone.
|
|
1374
|
+
* @category Util
|
|
1375
|
+
*/
|
|
1376
|
+
declare function isToday(date: MazeyDate): boolean;
|
|
1377
|
+
/**
|
|
1378
|
+
* Check whether a date is in the current local calendar year.
|
|
1379
|
+
*
|
|
1380
|
+
* Usage:
|
|
1381
|
+
*
|
|
1382
|
+
* ```javascript
|
|
1383
|
+
* import { isThisYear } from "mazey";
|
|
1384
|
+
*
|
|
1385
|
+
* const ret = isThisYear(new Date());
|
|
1386
|
+
* console.log(ret);
|
|
1387
|
+
* ```
|
|
1388
|
+
*
|
|
1389
|
+
* Output:
|
|
1390
|
+
*
|
|
1391
|
+
* ```text
|
|
1392
|
+
* true
|
|
1393
|
+
* ```
|
|
1394
|
+
*
|
|
1395
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1396
|
+
* @returns Whether the value has the current local year. Invalid input returns `false`.
|
|
1397
|
+
* @remarks Results depend on the runtime's local timezone.
|
|
1398
|
+
* @category Util
|
|
1399
|
+
*/
|
|
1400
|
+
declare function isThisYear(date: MazeyDate): boolean;
|
|
1401
|
+
/**
|
|
1402
|
+
* Check whether a date is in the current local calendar month and year.
|
|
1403
|
+
*
|
|
1404
|
+
* Usage:
|
|
1405
|
+
*
|
|
1406
|
+
* ```javascript
|
|
1407
|
+
* import { isThisMonth } from "mazey";
|
|
1408
|
+
*
|
|
1409
|
+
* const ret = isThisMonth(new Date());
|
|
1410
|
+
* console.log(ret);
|
|
1411
|
+
* ```
|
|
1412
|
+
*
|
|
1413
|
+
* Output:
|
|
1414
|
+
*
|
|
1415
|
+
* ```text
|
|
1416
|
+
* true
|
|
1417
|
+
* ```
|
|
1418
|
+
*
|
|
1419
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1420
|
+
* @returns Whether the value has the current local year and month. Invalid input returns `false`.
|
|
1421
|
+
* @remarks Results depend on the runtime's local timezone.
|
|
1422
|
+
* @category Util
|
|
1423
|
+
*/
|
|
1424
|
+
declare function isThisMonth(date: MazeyDate): boolean;
|
|
1425
|
+
/**
|
|
1426
|
+
* Check whether a date is in the current Monday-first local calendar week.
|
|
1427
|
+
*
|
|
1428
|
+
* Usage:
|
|
1429
|
+
*
|
|
1430
|
+
* ```javascript
|
|
1431
|
+
* import { isThisWeek } from "mazey";
|
|
1432
|
+
*
|
|
1433
|
+
* const ret = isThisWeek(new Date());
|
|
1434
|
+
* console.log(ret);
|
|
1435
|
+
* ```
|
|
1436
|
+
*
|
|
1437
|
+
* Output:
|
|
1438
|
+
*
|
|
1439
|
+
* ```text
|
|
1440
|
+
* true
|
|
1441
|
+
* ```
|
|
1442
|
+
*
|
|
1443
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1444
|
+
* @returns Whether the value is in the current local week. Invalid input returns `false`.
|
|
1445
|
+
* @remarks The week begins on Monday and ends before the following Monday. Boundaries use local time and a half-open range.
|
|
1446
|
+
* @category Util
|
|
1447
|
+
*/
|
|
1448
|
+
declare function isThisWeek(date: MazeyDate): boolean;
|
|
1449
|
+
/**
|
|
1450
|
+
* Check whether a date is within the current local clock hour.
|
|
1451
|
+
*
|
|
1452
|
+
* Usage:
|
|
1453
|
+
*
|
|
1454
|
+
* ```javascript
|
|
1455
|
+
* import { isThisHour } from "mazey";
|
|
1456
|
+
*
|
|
1457
|
+
* const ret = isThisHour(new Date());
|
|
1458
|
+
* console.log(ret);
|
|
1459
|
+
* ```
|
|
1460
|
+
*
|
|
1461
|
+
* Output:
|
|
1462
|
+
*
|
|
1463
|
+
* ```text
|
|
1464
|
+
* true
|
|
1465
|
+
* ```
|
|
1466
|
+
*
|
|
1467
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1468
|
+
* @returns Whether the value has the current local year, month, day, and hour. Invalid input returns `false`.
|
|
1469
|
+
* @remarks Minutes, seconds, and milliseconds are ignored. Results depend on the runtime's local timezone.
|
|
1470
|
+
* @category Util
|
|
1471
|
+
*/
|
|
1472
|
+
declare function isThisHour(date: MazeyDate): boolean;
|
|
1473
|
+
/**
|
|
1474
|
+
* Format the absolute distance from a date to now in concise English words.
|
|
1475
|
+
*
|
|
1476
|
+
* Usage:
|
|
1477
|
+
*
|
|
1478
|
+
* ```javascript
|
|
1479
|
+
* import { formatDistanceToNow } from "mazey";
|
|
1480
|
+
*
|
|
1481
|
+
* const ret = formatDistanceToNow(new Date(Date.now() - 60 * 60 * 1000));
|
|
1482
|
+
* console.log(ret);
|
|
1483
|
+
* ```
|
|
1484
|
+
*
|
|
1485
|
+
* Output:
|
|
1486
|
+
*
|
|
1487
|
+
* ```text
|
|
1488
|
+
* about 1 hour
|
|
1489
|
+
* ```
|
|
1490
|
+
*
|
|
1491
|
+
* @param date A `Date`, millisecond timestamp, or string accepted by `isValidDate`.
|
|
1492
|
+
* @returns The absolute approximate distance phrase, or an empty string for invalid input.
|
|
1493
|
+
* @remarks Past and future dates use the same wording without `ago` or `in`. Months and years use fixed approximate durations of 30 and 365 days.
|
|
1494
|
+
* @category Util
|
|
1495
|
+
*/
|
|
1496
|
+
declare function formatDistanceToNow(date: MazeyDate): string;
|
|
1313
1497
|
/**
|
|
1314
1498
|
* Return the formatted date string in the given format.
|
|
1315
1499
|
*
|
|
@@ -1828,9 +2012,7 @@ declare function isValidHttpUrl(url: string, options?: {
|
|
|
1828
2012
|
strict: boolean;
|
|
1829
2013
|
}): boolean;
|
|
1830
2014
|
/**
|
|
1831
|
-
*
|
|
1832
|
-
*
|
|
1833
|
-
* ZH: 获取文件后缀名。
|
|
2015
|
+
* Get the file extension from a URL or path.
|
|
1834
2016
|
*
|
|
1835
2017
|
* Usage:
|
|
1836
2018
|
*
|
|
@@ -2082,9 +2264,7 @@ declare function setClass(ele: HTMLElement, cls: string): void;
|
|
|
2082
2264
|
*/
|
|
2083
2265
|
declare function removeClass(obj: MazeyElement, cls: string): void;
|
|
2084
2266
|
/**
|
|
2085
|
-
*
|
|
2086
|
-
*
|
|
2087
|
-
* ZH: 添加样式标签; style: 样式标签内的字符串; id: `<style>` 标签的 `id`; 返回: 添加成功/失败。
|
|
2267
|
+
* Add a `<style>` element to the document `<head>`.
|
|
2088
2268
|
*
|
|
2089
2269
|
* Usage:
|
|
2090
2270
|
*
|
|
@@ -2150,6 +2330,10 @@ declare function removeClass(obj: MazeyElement, cls: string): void;
|
|
|
2150
2330
|
* <style id="z-style">.footer>.x-wish>a:first-child,div.wish-flex>a[href^='https://github.com/chengchuu'],.m-hide{display: none;}.footer>.y-wish:before{content: 'Copyright (c) chengchuu';color: inherit;padding-inline-start: var(--y-wish-1_5);padding-inline-end: var(--y-wish-1_5);padding-top: var(--y-wish-1);padding-bottom: var(--y-wish-1);}</style>
|
|
2151
2331
|
* ```
|
|
2152
2332
|
*
|
|
2333
|
+
* @param style CSS text to add to the document.
|
|
2334
|
+
* @param options.id Optional `<style>` element ID. An existing element with
|
|
2335
|
+
* the same ID is updated instead of duplicated.
|
|
2336
|
+
* @returns Whether non-empty CSS text was added or updated.
|
|
2153
2337
|
* @category DOM
|
|
2154
2338
|
*/
|
|
2155
2339
|
declare function addStyle(style: string, options?: {
|
|
@@ -2271,6 +2455,62 @@ declare function genStyleString(selector: string, styleArray: Array<string>): st
|
|
|
2271
2455
|
* @category DOM
|
|
2272
2456
|
*/
|
|
2273
2457
|
declare function getPageMeta(name: string): string;
|
|
2458
|
+
/**
|
|
2459
|
+
* Check whether a value is a CSS selector supported by the supplied query
|
|
2460
|
+
* root without allowing selector syntax errors to escape.
|
|
2461
|
+
*
|
|
2462
|
+
* Usage:
|
|
2463
|
+
*
|
|
2464
|
+
* ```javascript
|
|
2465
|
+
* import { isValidCssSelector } from "mazey";
|
|
2466
|
+
*
|
|
2467
|
+
* isValidCssSelector(".message > img"); // true
|
|
2468
|
+
* isValidCssSelector("["); // false
|
|
2469
|
+
* isValidCssSelector("", { allowEmpty: true }); // true
|
|
2470
|
+
* ```
|
|
2471
|
+
*
|
|
2472
|
+
* @remarks Browser only unless a compatible `ParentNode` is supplied.
|
|
2473
|
+
* @param selector Value to validate as a CSS selector.
|
|
2474
|
+
* @param options.allowEmpty Whether an empty or whitespace-only string is accepted. Defaults to `false`.
|
|
2475
|
+
* @param options.root Query root used to validate browser support. Defaults to `document` when available.
|
|
2476
|
+
* @returns Whether the selector is accepted by the query root.
|
|
2477
|
+
* @category DOM
|
|
2478
|
+
*/
|
|
2479
|
+
declare function isValidCssSelector(selector: unknown, options?: {
|
|
2480
|
+
allowEmpty?: boolean;
|
|
2481
|
+
root?: ParentNode;
|
|
2482
|
+
}): boolean;
|
|
2483
|
+
/**
|
|
2484
|
+
* Extract text from a cloned element without modifying the original DOM.
|
|
2485
|
+
* Images can be replaced by their `alt` text, selected descendants can be
|
|
2486
|
+
* removed, and whitespace can be normalized before returning the text.
|
|
2487
|
+
*
|
|
2488
|
+
* Invalid exclusion selectors are ignored.
|
|
2489
|
+
*
|
|
2490
|
+
* Usage:
|
|
2491
|
+
*
|
|
2492
|
+
* ```javascript
|
|
2493
|
+
* import { extractElementText } from "mazey";
|
|
2494
|
+
*
|
|
2495
|
+
* const element = document.querySelector(".message");
|
|
2496
|
+
* const text = extractElementText(element, {
|
|
2497
|
+
* excludeSelector: ".message-actions",
|
|
2498
|
+
* });
|
|
2499
|
+
* ```
|
|
2500
|
+
*
|
|
2501
|
+
* @remarks Browser only.
|
|
2502
|
+
* @param element Element whose cloned contents are read.
|
|
2503
|
+
* @param options.excludeSelector Selector for descendants to remove from the clone.
|
|
2504
|
+
* @param options.replaceImagesWithAlt Whether images with an `alt` attribute are replaced by that text. Defaults to `true`.
|
|
2505
|
+
* @param options.normalizeWhitespace Whether whitespace is collapsed and trimmed. Defaults to `true`.
|
|
2506
|
+
* @returns Extracted text from the cloned element.
|
|
2507
|
+
* @category DOM
|
|
2508
|
+
*/
|
|
2509
|
+
declare function extractElementText(element: Element, options?: {
|
|
2510
|
+
excludeSelector?: string;
|
|
2511
|
+
replaceImagesWithAlt?: boolean;
|
|
2512
|
+
normalizeWhitespace?: boolean;
|
|
2513
|
+
}): string;
|
|
2274
2514
|
|
|
2275
2515
|
/**
|
|
2276
2516
|
* Prevent bubbling.
|
|
@@ -2373,9 +2613,7 @@ declare function invokeEvent(type: string, params?: MazeyObject): void;
|
|
|
2373
2613
|
declare function removeEvent(type: string, fn?: MazeyFn): void;
|
|
2374
2614
|
|
|
2375
2615
|
/**
|
|
2376
|
-
*
|
|
2377
|
-
*
|
|
2378
|
-
* ZH: 存储/获取数据到 sessionStorage/localStorage。
|
|
2616
|
+
* Serialize a value as JSON and store it in `sessionStorage`.
|
|
2379
2617
|
*
|
|
2380
2618
|
* Usage:
|
|
2381
2619
|
*
|
|
@@ -2388,7 +2626,7 @@ declare function removeEvent(type: string, fn?: MazeyFn): void;
|
|
|
2388
2626
|
* const ret2 = getLocalStorage("test");
|
|
2389
2627
|
* console.log(ret1, ret2);
|
|
2390
2628
|
*
|
|
2391
|
-
* //
|
|
2629
|
+
* // Wrap the helpers with a project-specific key prefix.
|
|
2392
2630
|
* const projectName = "mazey";
|
|
2393
2631
|
* function mSetLocalStorage (key, value) {
|
|
2394
2632
|
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
@@ -2405,15 +2643,14 @@ declare function removeEvent(type: string, fn?: MazeyFn): void;
|
|
|
2405
2643
|
* 123 123
|
|
2406
2644
|
* ```
|
|
2407
2645
|
*
|
|
2408
|
-
* @param {string} key
|
|
2409
|
-
* @
|
|
2646
|
+
* @param {string} key Storage key.
|
|
2647
|
+
* @param value Value to serialize and store.
|
|
2648
|
+
* @returns {void} This function does not return a value.
|
|
2410
2649
|
* @category Store
|
|
2411
2650
|
*/
|
|
2412
2651
|
declare function setSessionStorage<T>(key: string, value?: T | null): void;
|
|
2413
2652
|
/**
|
|
2414
|
-
*
|
|
2415
|
-
*
|
|
2416
|
-
* ZH: 存储/获取数据到 sessionStorage/localStorage。
|
|
2653
|
+
* Read a value from `sessionStorage`, parsing JSON when possible.
|
|
2417
2654
|
*
|
|
2418
2655
|
* Usage:
|
|
2419
2656
|
*
|
|
@@ -2426,7 +2663,7 @@ declare function setSessionStorage<T>(key: string, value?: T | null): void;
|
|
|
2426
2663
|
* const ret2 = getLocalStorage("test");
|
|
2427
2664
|
* console.log(ret1, ret2);
|
|
2428
2665
|
*
|
|
2429
|
-
* //
|
|
2666
|
+
* // Wrap the helpers with a project-specific key prefix.
|
|
2430
2667
|
* const projectName = "mazey";
|
|
2431
2668
|
* function mSetLocalStorage (key, value) {
|
|
2432
2669
|
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
@@ -2443,15 +2680,13 @@ declare function setSessionStorage<T>(key: string, value?: T | null): void;
|
|
|
2443
2680
|
* 123 123
|
|
2444
2681
|
* ```
|
|
2445
2682
|
*
|
|
2446
|
-
* @param {string} key
|
|
2447
|
-
* @returns
|
|
2683
|
+
* @param {string} key Storage key.
|
|
2684
|
+
* @returns The parsed value, raw stored value, or `null` when no value exists.
|
|
2448
2685
|
* @category Store
|
|
2449
2686
|
*/
|
|
2450
2687
|
declare function getSessionStorage<T>(key: string): T | null;
|
|
2451
2688
|
/**
|
|
2452
|
-
*
|
|
2453
|
-
*
|
|
2454
|
-
* ZH: 存储/获取数据到 sessionStorage/localStorage。
|
|
2689
|
+
* Serialize a value as JSON and store it in `localStorage`.
|
|
2455
2690
|
*
|
|
2456
2691
|
* Usage:
|
|
2457
2692
|
*
|
|
@@ -2464,7 +2699,7 @@ declare function getSessionStorage<T>(key: string): T | null;
|
|
|
2464
2699
|
* const ret2 = getLocalStorage("test");
|
|
2465
2700
|
* console.log(ret1, ret2);
|
|
2466
2701
|
*
|
|
2467
|
-
* //
|
|
2702
|
+
* // Wrap the helpers with a project-specific key prefix.
|
|
2468
2703
|
* const projectName = "mazey";
|
|
2469
2704
|
* function mSetLocalStorage (key, value) {
|
|
2470
2705
|
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
@@ -2481,15 +2716,14 @@ declare function getSessionStorage<T>(key: string): T | null;
|
|
|
2481
2716
|
* 123 123
|
|
2482
2717
|
* ```
|
|
2483
2718
|
*
|
|
2484
|
-
* @param {string} key
|
|
2485
|
-
* @
|
|
2719
|
+
* @param {string} key Storage key.
|
|
2720
|
+
* @param value Value to serialize and store.
|
|
2721
|
+
* @returns {void} This function does not return a value.
|
|
2486
2722
|
* @category Store
|
|
2487
2723
|
*/
|
|
2488
2724
|
declare function setLocalStorage<T>(key: string, value?: T | null): void;
|
|
2489
2725
|
/**
|
|
2490
|
-
*
|
|
2491
|
-
*
|
|
2492
|
-
* ZH: 存储/获取数据到 sessionStorage/localStorage。
|
|
2726
|
+
* Read a value from `localStorage`, parsing JSON when possible.
|
|
2493
2727
|
*
|
|
2494
2728
|
* Usage:
|
|
2495
2729
|
*
|
|
@@ -2502,7 +2736,7 @@ declare function setLocalStorage<T>(key: string, value?: T | null): void;
|
|
|
2502
2736
|
* const ret2 = getLocalStorage("test");
|
|
2503
2737
|
* console.log(ret1, ret2);
|
|
2504
2738
|
*
|
|
2505
|
-
* //
|
|
2739
|
+
* // Wrap the helpers with a project-specific key prefix.
|
|
2506
2740
|
* const projectName = "mazey";
|
|
2507
2741
|
* function mSetLocalStorage (key, value) {
|
|
2508
2742
|
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
@@ -2519,22 +2753,20 @@ declare function setLocalStorage<T>(key: string, value?: T | null): void;
|
|
|
2519
2753
|
* 123 123
|
|
2520
2754
|
* ```
|
|
2521
2755
|
*
|
|
2522
|
-
* @param {string} key
|
|
2523
|
-
* @returns
|
|
2756
|
+
* @param {string} key Storage key.
|
|
2757
|
+
* @returns The parsed value, raw stored value, or `null` when no value exists.
|
|
2524
2758
|
* @category Store
|
|
2525
2759
|
*/
|
|
2526
2760
|
declare function getLocalStorage<T>(key: string): T | null;
|
|
2527
2761
|
/**
|
|
2528
|
-
*
|
|
2529
|
-
*
|
|
2530
|
-
* ZH: 设置/获取 Cookie。
|
|
2762
|
+
* Get a cookie value by name.
|
|
2531
2763
|
*
|
|
2532
2764
|
* Usage:
|
|
2533
2765
|
*
|
|
2534
2766
|
* ```javascript
|
|
2535
2767
|
* import { setCookie, getCookie } from "mazey";
|
|
2536
2768
|
*
|
|
2537
|
-
* setCookie("test", "123", 30, "example.com"); //
|
|
2769
|
+
* setCookie("test", "123", 30, "example.com"); // name, value, days, domain
|
|
2538
2770
|
* const ret = getCookie("test");
|
|
2539
2771
|
* console.log(ret);
|
|
2540
2772
|
* ```
|
|
@@ -2545,20 +2777,20 @@ declare function getLocalStorage<T>(key: string): T | null;
|
|
|
2545
2777
|
* 123
|
|
2546
2778
|
* ```
|
|
2547
2779
|
*
|
|
2780
|
+
* @param name Cookie name.
|
|
2781
|
+
* @returns The cookie value, or an empty string when the cookie does not exist.
|
|
2548
2782
|
* @category Store
|
|
2549
2783
|
*/
|
|
2550
2784
|
declare function getCookie(name: string): string;
|
|
2551
2785
|
/**
|
|
2552
|
-
*
|
|
2553
|
-
*
|
|
2554
|
-
* ZH: 设置/获取 Cookie。
|
|
2786
|
+
* Set a cookie value.
|
|
2555
2787
|
*
|
|
2556
2788
|
* Usage:
|
|
2557
2789
|
*
|
|
2558
2790
|
* ```javascript
|
|
2559
2791
|
* import { setCookie, getCookie } from "mazey";
|
|
2560
2792
|
*
|
|
2561
|
-
* setCookie("test", "123", 30, "example.com"); //
|
|
2793
|
+
* setCookie("test", "123", 30, "example.com"); // name, value, days, domain
|
|
2562
2794
|
* const ret = getCookie("test");
|
|
2563
2795
|
* console.log(ret);
|
|
2564
2796
|
* ```
|
|
@@ -2569,6 +2801,11 @@ declare function getCookie(name: string): string;
|
|
|
2569
2801
|
* 123
|
|
2570
2802
|
* ```
|
|
2571
2803
|
*
|
|
2804
|
+
* @param name Cookie name.
|
|
2805
|
+
* @param value Cookie value.
|
|
2806
|
+
* @param days Number of days until expiration. Omit for a session cookie.
|
|
2807
|
+
* @param domain Optional cookie domain.
|
|
2808
|
+
* @returns {void} This function does not return a value.
|
|
2572
2809
|
* @category Store
|
|
2573
2810
|
*/
|
|
2574
2811
|
declare function setCookie(name: string, value: string, days?: number, domain?: string): void;
|
|
@@ -2603,9 +2840,7 @@ declare function removeCookie(name: string): boolean;
|
|
|
2603
2840
|
declare function delCookie(name: string): void;
|
|
2604
2841
|
|
|
2605
2842
|
/**
|
|
2606
|
-
*
|
|
2607
|
-
*
|
|
2608
|
-
* ZH: 动态加载 CSS 文件。
|
|
2843
|
+
* Load a CSS file dynamically.
|
|
2609
2844
|
*
|
|
2610
2845
|
* Usage:
|
|
2611
2846
|
*
|
|
@@ -2636,18 +2871,16 @@ declare function delCookie(name: string): void;
|
|
|
2636
2871
|
* Load CSS Success: loaded
|
|
2637
2872
|
* ```
|
|
2638
2873
|
*
|
|
2639
|
-
* @param {string} url
|
|
2640
|
-
* @param {string} options.id
|
|
2641
|
-
* @returns {Promise<string>}
|
|
2874
|
+
* @param {string} url URL of the CSS resource.
|
|
2875
|
+
* @param {string} options.id Optional ID for the `<link>` element.
|
|
2876
|
+
* @returns {Promise<string>} A promise that resolves to `"loaded"` after the stylesheet loads.
|
|
2642
2877
|
* @category Load
|
|
2643
2878
|
*/
|
|
2644
2879
|
declare function loadCSS(url: string, options?: {
|
|
2645
2880
|
id?: string;
|
|
2646
2881
|
}): Promise<string>;
|
|
2647
2882
|
/**
|
|
2648
|
-
*
|
|
2649
|
-
*
|
|
2650
|
-
* ZH: 动态加载 JavaScript 文件。
|
|
2883
|
+
* Load and execute a JavaScript file dynamically.
|
|
2651
2884
|
*
|
|
2652
2885
|
* Usage:
|
|
2653
2886
|
*
|
|
@@ -2679,16 +2912,16 @@ declare function loadCSS(url: string, options?: {
|
|
|
2679
2912
|
* Load JavaScript script: loaded
|
|
2680
2913
|
* ```
|
|
2681
2914
|
*
|
|
2682
|
-
* @param {string} url
|
|
2683
|
-
* @param {string} options.id
|
|
2684
|
-
* @param {function} options.callback
|
|
2685
|
-
* @param {number} options.timeout
|
|
2686
|
-
* @param {boolean} options.isDefer
|
|
2687
|
-
* @param {boolean} options.isAsync
|
|
2688
|
-
* @param {boolean} options.isCrossOrigin
|
|
2689
|
-
* @param {object} options.attributes
|
|
2690
|
-
* @param {string} options.cssUrl
|
|
2691
|
-
* @returns {Promise<string>}
|
|
2915
|
+
* @param {string} url URL of the JavaScript resource.
|
|
2916
|
+
* @param {string} options.id Optional ID for the `<script>` element.
|
|
2917
|
+
* @param {function} options.callback Callback invoked after the script loads.
|
|
2918
|
+
* @param {number} options.timeout Timeout in milliseconds.
|
|
2919
|
+
* @param {boolean} options.isDefer Whether to set the `defer` attribute.
|
|
2920
|
+
* @param {boolean} options.isAsync Whether to set the `async` attribute.
|
|
2921
|
+
* @param {boolean} options.isCrossOrigin Whether to request the script anonymously across origins.
|
|
2922
|
+
* @param {object} options.attributes Additional attributes for the `<script>` element.
|
|
2923
|
+
* @param {string} options.cssUrl Optional CSS resource to begin loading before the script is inserted.
|
|
2924
|
+
* @returns {Promise<string>} A promise that resolves to `"loaded"` after the script loads.
|
|
2692
2925
|
* @category Load
|
|
2693
2926
|
*/
|
|
2694
2927
|
declare function loadScript(url: string, options?: {
|
|
@@ -2702,9 +2935,8 @@ declare function loadScript(url: string, options?: {
|
|
|
2702
2935
|
cssUrl?: string;
|
|
2703
2936
|
}): LoadScriptReturns;
|
|
2704
2937
|
/**
|
|
2705
|
-
*
|
|
2706
|
-
*
|
|
2707
|
-
* ZH: 页面加载完成。
|
|
2938
|
+
* Wait for the page to finish loading, including when the `load` event has
|
|
2939
|
+
* already fired.
|
|
2708
2940
|
*
|
|
2709
2941
|
* Usage:
|
|
2710
2942
|
*
|
|
@@ -2726,8 +2958,8 @@ declare function loadScript(url: string, options?: {
|
|
|
2726
2958
|
* Load Success: load
|
|
2727
2959
|
* ```
|
|
2728
2960
|
*
|
|
2729
|
-
* @param {number} timeout
|
|
2730
|
-
* @returns {Promise<string>}
|
|
2961
|
+
* @param {number} timeout Timeout in milliseconds. Defaults to 30,000.
|
|
2962
|
+
* @returns {Promise<string>} A promise that resolves to `"complete"` or `"load"`, or rejects on timeout.
|
|
2731
2963
|
* @category Load
|
|
2732
2964
|
*/
|
|
2733
2965
|
declare function windowLoaded(timeout?: number): Promise<string>;
|
|
@@ -2948,9 +3180,7 @@ declare function getCLS(): Promise<number>;
|
|
|
2948
3180
|
*/
|
|
2949
3181
|
declare function getTTFB(): Promise<number>;
|
|
2950
3182
|
/**
|
|
2951
|
-
*
|
|
2952
|
-
*
|
|
2953
|
-
* ZH: 获取页面加载相关的各项数据。
|
|
3183
|
+
* Get page-load metrics from the Navigation Timing APIs.
|
|
2954
3184
|
*
|
|
2955
3185
|
* @remarks
|
|
2956
3186
|
* This function uses the [`PerformanceNavigationTiming`](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming) API to get page load time data.
|
|
@@ -2961,8 +3191,8 @@ declare function getTTFB(): Promise<number>;
|
|
|
2961
3191
|
* ```javascript
|
|
2962
3192
|
* import { getPerformance } from "mazey";
|
|
2963
3193
|
*
|
|
2964
|
-
* // `camelCase
|
|
2965
|
-
* // `camelCase
|
|
3194
|
+
* // `camelCase: false` (default) returns snake_case keys such as `a_b`.
|
|
3195
|
+
* // `camelCase: true` returns camelCase keys such as `aB`.
|
|
2966
3196
|
* getPerformance()
|
|
2967
3197
|
* .then(res => {
|
|
2968
3198
|
* console.log(JSON.stringify(res));
|
|
@@ -2992,8 +3222,8 @@ declare function getTTFB(): Promise<number>;
|
|
|
2992
3222
|
* | ssl_time | SSL | number | (Optional) connectEnd - secureConnectionStart |
|
|
2993
3223
|
* | download_time | Download | number | (Optional) responseEnd - responseStart |
|
|
2994
3224
|
*
|
|
2995
|
-
* @param {boolean} camelCase
|
|
2996
|
-
* @returns {Promise<object>}
|
|
3225
|
+
* @param {boolean} camelCase Whether to return camelCase keys. Defaults to `false`, which returns snake_case keys.
|
|
3226
|
+
* @returns {Promise<object>} The collected page-load metrics.
|
|
2997
3227
|
* @category Perf
|
|
2998
3228
|
*/
|
|
2999
3229
|
declare function getPerformance(camelCase?: boolean): Promise<WebPerformance>;
|
|
@@ -3060,9 +3290,7 @@ declare function isSafePWAEnv(options?: IsSafePWAEnvOptions): boolean;
|
|
|
3060
3290
|
*/
|
|
3061
3291
|
declare function isStandalonePWA(): boolean;
|
|
3062
3292
|
/**
|
|
3063
|
-
*
|
|
3064
|
-
*
|
|
3065
|
-
* ZH: 返回有关浏览器的详细信息。
|
|
3293
|
+
* Return detailed information about the current browser.
|
|
3066
3294
|
*
|
|
3067
3295
|
* Usage:
|
|
3068
3296
|
*
|
|
@@ -3107,9 +3335,7 @@ declare function isStandalonePWA(): boolean;
|
|
|
3107
3335
|
*/
|
|
3108
3336
|
declare function getBrowserInfo(): BrowserInfo;
|
|
3109
3337
|
/**
|
|
3110
|
-
*
|
|
3111
|
-
*
|
|
3112
|
-
* ZH: 生成浏览器属性。
|
|
3338
|
+
* Generate browser attributes from the detected browser information.
|
|
3113
3339
|
*
|
|
3114
3340
|
* Usage:
|
|
3115
3341
|
*
|
|
@@ -3158,9 +3384,119 @@ declare function genBrowserAttrs(prefix?: string, separator?: string): string[];
|
|
|
3158
3384
|
declare function isSupportWebp(): Promise<boolean>;
|
|
3159
3385
|
|
|
3160
3386
|
/**
|
|
3161
|
-
*
|
|
3387
|
+
* A user-selectable color-theme preference.
|
|
3162
3388
|
*
|
|
3163
|
-
*
|
|
3389
|
+
* @category Browser Information
|
|
3390
|
+
*/
|
|
3391
|
+
type ThemePreference = "system" | "light" | "dark";
|
|
3392
|
+
/**
|
|
3393
|
+
* A concrete color theme that can be applied to a page.
|
|
3394
|
+
*
|
|
3395
|
+
* @category Browser Information
|
|
3396
|
+
*/
|
|
3397
|
+
type ResolvedTheme = "light" | "dark";
|
|
3398
|
+
/**
|
|
3399
|
+
* Human-readable names for supported theme preferences.
|
|
3400
|
+
*
|
|
3401
|
+
* @category Browser Information
|
|
3402
|
+
*/
|
|
3403
|
+
type ThemePreferenceDisplayName = "System" | "Light" | "Dark";
|
|
3404
|
+
/**
|
|
3405
|
+
* The highest-priority source that selected a theme preference.
|
|
3406
|
+
*
|
|
3407
|
+
* @category Browser Information
|
|
3408
|
+
*/
|
|
3409
|
+
type ThemePreferenceSource = "query" | "storage" | "system" | "fallback";
|
|
3410
|
+
/**
|
|
3411
|
+
* Options for {@link resolveThemePreference}.
|
|
3412
|
+
*
|
|
3413
|
+
* @category Browser Information
|
|
3414
|
+
*/
|
|
3415
|
+
interface ResolveThemePreferenceOptions {
|
|
3416
|
+
/** Project-specific local-storage key, such as `MAZEY_THEME`. */
|
|
3417
|
+
storageKey: string;
|
|
3418
|
+
/** URL query parameter name. Defaults to `theme`. */
|
|
3419
|
+
queryParam?: string;
|
|
3420
|
+
/** URL inspected for a query override. Defaults to `window.location.href` when available. */
|
|
3421
|
+
url?: string | URL;
|
|
3422
|
+
/** Storage used to read a persisted preference. Defaults to `window.localStorage` when available. */
|
|
3423
|
+
storage?: Pick<Storage, "getItem"> | null;
|
|
3424
|
+
/** Media-query implementation. Defaults to `window.matchMedia` when available. */
|
|
3425
|
+
matchMedia?: (query: string) => Pick<MediaQueryList, "matches">;
|
|
3426
|
+
/** Theme used when the system preference is unavailable. Defaults to `light`. */
|
|
3427
|
+
fallback?: ResolvedTheme;
|
|
3428
|
+
}
|
|
3429
|
+
/**
|
|
3430
|
+
* Result returned by {@link resolveThemePreference}.
|
|
3431
|
+
*
|
|
3432
|
+
* @category Browser Information
|
|
3433
|
+
*/
|
|
3434
|
+
interface ThemePreferenceResult {
|
|
3435
|
+
/** Selected user-facing preference. */
|
|
3436
|
+
preference: ThemePreference;
|
|
3437
|
+
/** Effective light or dark theme. */
|
|
3438
|
+
resolvedTheme: ResolvedTheme;
|
|
3439
|
+
/** Human-readable name of the selected preference. */
|
|
3440
|
+
displayName: ThemePreferenceDisplayName;
|
|
3441
|
+
/** Highest-priority source that supplied the selected preference. */
|
|
3442
|
+
source: ThemePreferenceSource;
|
|
3443
|
+
}
|
|
3444
|
+
/**
|
|
3445
|
+
* Resolve a website theme preference without mutating the DOM or storage.
|
|
3446
|
+
*
|
|
3447
|
+
* Resolution uses four priority levels: a `light` or `dark` URL query
|
|
3448
|
+
* override, a stored `system`/`light`/`dark` preference, the current system
|
|
3449
|
+
* color scheme, and finally a configured `light` or `dark` fallback. The
|
|
3450
|
+
* default query parameter is `theme`, and the storage key must be supplied by
|
|
3451
|
+
* the consuming project. Query `system` and all unsupported values are ignored.
|
|
3452
|
+
*
|
|
3453
|
+
* The result separates the selected preference from the effective theme and
|
|
3454
|
+
* includes its display name (`System`, `Light`, or `Dark`) and source. A stored
|
|
3455
|
+
* `system` preference retains `source: "storage"` while its effective theme is
|
|
3456
|
+
* resolved from the current media query or fallback.
|
|
3457
|
+
*
|
|
3458
|
+
* Usage:
|
|
3459
|
+
*
|
|
3460
|
+
* ```javascript
|
|
3461
|
+
* import { resolveThemePreference } from "mazey";
|
|
3462
|
+
*
|
|
3463
|
+
* const theme = resolveThemePreference({
|
|
3464
|
+
* storageKey: "MY_WEBSITE_THEME",
|
|
3465
|
+
* });
|
|
3466
|
+
* console.log(theme.preference);
|
|
3467
|
+
* console.log(theme.resolvedTheme);
|
|
3468
|
+
* console.log(theme.displayName);
|
|
3469
|
+
* console.log(theme.source);
|
|
3470
|
+
* ```
|
|
3471
|
+
*
|
|
3472
|
+
* Possible output:
|
|
3473
|
+
*
|
|
3474
|
+
* ```text
|
|
3475
|
+
* system
|
|
3476
|
+
* dark
|
|
3477
|
+
* System
|
|
3478
|
+
* system
|
|
3479
|
+
* ```
|
|
3480
|
+
*
|
|
3481
|
+
* URL override:
|
|
3482
|
+
*
|
|
3483
|
+
* ```javascript
|
|
3484
|
+
* const theme = resolveThemePreference({
|
|
3485
|
+
* storageKey: "MY_WEBSITE_THEME",
|
|
3486
|
+
* url: "https://example.com/?theme=light",
|
|
3487
|
+
* });
|
|
3488
|
+
* ```
|
|
3489
|
+
*
|
|
3490
|
+
* @param options Project storage key and optional URL, storage, media-query, and fallback inputs.
|
|
3491
|
+
* @returns The selected preference, effective theme, display name, and preference source.
|
|
3492
|
+
* @throws {TypeError} If required options or explicitly supplied option values are invalid.
|
|
3493
|
+
* @remarks Safe during SSR and resilient to unavailable or throwing browser APIs. This function does not write storage, mutate the DOM, or add listeners. System resolution reads the current media-query result rather than cached browser information.
|
|
3494
|
+
* @category Browser Information
|
|
3495
|
+
*/
|
|
3496
|
+
declare function resolveThemePreference(options: ResolveThemePreferenceOptions): ThemePreferenceResult;
|
|
3497
|
+
|
|
3498
|
+
/**
|
|
3499
|
+
* Create a configurable wrapper around the global `console`.
|
|
3164
3500
|
*
|
|
3165
3501
|
* Usage:
|
|
3166
3502
|
*
|
|
@@ -3211,9 +3547,7 @@ declare function genCustomConsole(prefix?: string, options?: {
|
|
|
3211
3547
|
*/
|
|
3212
3548
|
declare const mazeyCon: Console;
|
|
3213
3549
|
/**
|
|
3214
|
-
*
|
|
3215
|
-
*
|
|
3216
|
-
* ZH: 打印带有时间的日志。
|
|
3550
|
+
* Print logs with a localized timestamp.
|
|
3217
3551
|
*
|
|
3218
3552
|
* Usage:
|
|
3219
3553
|
*
|
|
@@ -3224,12 +3558,12 @@ declare const mazeyCon: Console;
|
|
|
3224
3558
|
* Output:
|
|
3225
3559
|
*
|
|
3226
3560
|
* ```text
|
|
3227
|
-
*
|
|
3561
|
+
* [localized date and time] I am string.
|
|
3228
3562
|
* ```
|
|
3229
3563
|
*
|
|
3230
3564
|
* @hidden
|
|
3231
3565
|
*/
|
|
3232
3566
|
declare const timeCon: Console;
|
|
3233
3567
|
|
|
3234
|
-
export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHTML, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, convertHttpToHttps, convertKebabToCamel, convertObjectToQuery, convertToHtmlBreaks, convertUnderToCamel, cutCHSString, cutZHString, debounce, deepCopy, deepCopyObject, deepFreeze, delCookie, doFn, fireEvent, floatFixed, floatToPercent, formatDate, formatDurationFromMs, genBrowserAttrs, genCustomConsole, genHashCode, genRndNumString, genStyleString, genUniqueNumString, generateCalendarVersion, generateRndNum, generateUniqueNum, getAllQueryParams, getBrowserInfo, getCLS, getCookie, getCurrentVersion, getDateDifference, getDefineListeners, getDomain, getFCP, getFID, getFP, getFileSize, getFriendlyInterval, getHashQueryParam, getLCP, getLocalStorage, getPageMeta, getPerformance, getQueryParam, getScriptQueryParam, getSessionStorage, getTTFB, getUrlFileType, getUrlHost, getUrlParam, getUrlPath, hasClass, inRate, invokeEvent, invokeFn, isArray, isBool, isBoolean, isBrowser, isFunction, isHit, isJSONString, isJsonString, isNonEmptyArray, isNonEmptyObject, isNumber, isPureObject, isSafePWAEnv, isStandalonePWA, isString, isSupportWebp, isSupportedEntryType, isUdfOrNul, isValidData, isValidDate, isValidEmail, isValidHttpUrl, isValidPhoneNumber, isValidUrl, loadCSS, loadImage, loadScript, loadScriptIfUndefined, longestComSubsequence, longestComSubstring, mNow, mTrim, mazeyCon, newLine, onURLChange, parseGitHubRepository, removeClass, removeCookie, removeEvent, removeHTML, removeHtml, repeatUntilConditionMet, replaceHttp, sanitizeInput, setClass, setCookie, setImgSizeBySrc, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
|
|
3235
|
-
export type { GitHubRepositoryDetails, IsSafePWAEnvOptions };
|
|
3568
|
+
export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHTML, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, convertHttpToHttps, convertKebabToCamel, convertObjectToQuery, convertToHtmlBreaks, convertUnderToCamel, cutCHSString, cutZHString, debounce, deepCopy, deepCopyObject, deepFreeze, delCookie, doFn, extractElementText, fireEvent, floatFixed, floatToPercent, formatDate, formatDistanceToNow, formatDurationFromMs, genBrowserAttrs, genCustomConsole, genHashCode, genRndNumString, genStyleString, genUniqueNumString, generateCalendarVersion, generateRndNum, generateUniqueNum, getAllQueryParams, getBrowserInfo, getCLS, getCookie, getCurrentVersion, getDateDifference, getDefineListeners, getDomain, getFCP, getFID, getFP, getFileSize, getFriendlyInterval, getHashQueryParam, getLCP, getLocalStorage, getPageMeta, getPerformance, getQueryParam, getScriptQueryParam, getSessionStorage, getTTFB, getUrlFileType, getUrlHost, getUrlParam, getUrlPath, hasClass, inRate, invokeEvent, invokeFn, isArray, isBool, isBoolean, isBrowser, isFunction, isHit, isJSONString, isJsonString, isNonEmptyArray, isNonEmptyObject, isNumber, isPureObject, isSafePWAEnv, isStandalonePWA, isString, isSupportWebp, isSupportedEntryType, isThisHour, isThisMonth, isThisWeek, isThisYear, isToday, isUdfOrNul, isValidCssSelector, isValidData, isValidDate, isValidEmail, isValidHttpUrl, isValidPhoneNumber, isValidUrl, loadCSS, loadImage, loadScript, loadScriptIfUndefined, longestComSubsequence, longestComSubstring, mNow, mTrim, mazeyCon, newLine, onURLChange, parseGitHubRepository, parseJsonSafe, removeClass, removeCookie, removeEvent, removeHTML, removeHtml, repeatUntilConditionMet, replaceHttp, resolveThemePreference, sanitizeInput, setClass, setCookie, setImgSizeBySrc, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, sha256Hex, sleep, throttle, timeCon, toJavaScriptGlobalName, truncateZHString, unsanitize, unsanitizeInput, updateQueryParam, waitTime, windowLoaded, zAxiosIsValidRes };
|
|
3569
|
+
export type { GitHubRepositoryDetails, IsSafePWAEnvOptions, ResolveThemePreferenceOptions, ResolvedTheme, ThemePreference, ThemePreferenceDisplayName, ThemePreferenceResult, ThemePreferenceSource };
|