mazey 5.10.0 → 5.10.2

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 CHANGED
@@ -81,6 +81,9 @@ There are some examples maintained by hand below. For more information, please c
81
81
  - [isNullish](#isnullish)
82
82
  - [isJSONString](#isjsonstring)
83
83
  - [parseJsonSafe](#parsejsonsafe)
84
+ - [isCNMobileNumber](#iscnmobilenumber)
85
+ - [escapeHTML and unescapeHTML](#escapehtml-and-unescapehtml)
86
+ - [truncateByWeightedLength](#truncatebyweightedlength)
84
87
  - [escapeHtmlAttribute](#escapehtmlattribute)
85
88
  - [sha256Hex](#sha256hex)
86
89
  - [isValidData](#isvaliddata)
@@ -105,10 +108,13 @@ There are some examples maintained by hand below. For more information, please c
105
108
  - [debounce](#debounce)
106
109
  - [throttle](#throttle)
107
110
  - [convertCamelToKebab](#convertcameltokebab)
108
- - [convertCamelToUnder](#convertcameltounder)
111
+ - [convertCamelToSnake](#convertcameltosnake)
112
+ - [convertSnakeToCamel](#convertsnaketocamel)
113
+ - [formatPercentage](#formatpercentage)
109
114
  - [toJavaScriptGlobalName](#tojavascriptglobalname)
110
115
  - [derivePackageMetadata](#derivepackagemetadata)
111
116
  - [URL](#url)
117
+ - [getURLPathExtension](#geturlpathextension)
112
118
  - [getQueryParam](#getqueryparam)
113
119
  - [getUrlParam](#geturlparam)
114
120
  - [getHashQueryParam](#gethashqueryparam)
@@ -127,7 +133,7 @@ There are some examples maintained by hand below. For more information, please c
127
133
  - [resolveElementTarget](#resolveelementtarget)
128
134
  - [extractElementText](#extractelementtext)
129
135
  - [injectStyle](#injectstyle)
130
- - [genStyleString](#genstylestring)
136
+ - [createCSSRule](#createcssrule)
131
137
  - [newLine](#newline)
132
138
  - [Event](#event)
133
139
  - [onEvent](#onevent)
@@ -153,6 +159,7 @@ There are some examples maintained by hand below. For more information, please c
153
159
  - [isWindows](#iswindows)
154
160
  - [isLinux](#islinux)
155
161
  - [getBrowserInfo](#getbrowserinfo)
162
+ - [getBrowserClassNames](#getbrowserclassnames)
156
163
  - [isSafePWAEnv](#issafepwaenv)
157
164
  - [isStandalonePWA](#isstandalonepwa)
158
165
  - [listenMediaQueryChanges](#listenmediaquerychanges)
@@ -373,6 +380,55 @@ const fallback = parseJsonSafe("invalid", {});
373
380
  console.log(data, fallback);
374
381
  ```
375
382
 
383
+ #### isCNMobileNumber
384
+
385
+ Check the Chinese mobile-number format: `1` followed by ten digits. This checks
386
+ format only, without verifying assigned prefixes, ownership, or reachability.
387
+
388
+ ```javascript
389
+ import { isCNMobileNumber } from "mazey";
390
+
391
+ isCNMobileNumber("13800138000"); // true
392
+ isCNMobileNumber("+8613800138000"); // false
393
+ ```
394
+
395
+ `isValidPhoneNumber` and `isMobile` remain deprecated aliases.
396
+
397
+ #### escapeHTML and unescapeHTML
398
+
399
+ Escape six HTML-sensitive characters or decode that fixed entity set in one
400
+ pass. These helpers do not sanitize arbitrary HTML or validate URLs.
401
+ `unescapeHTML` preserves unrecognized entities.
402
+
403
+ ```javascript
404
+ import { escapeHTML, unescapeHTML } from "mazey";
405
+
406
+ escapeHTML('<b title="x">A&B</b>');
407
+ // '&lt;b title=&quot;x&quot;&gt;A&amp;B&lt;&#x2F;b&gt;'
408
+ unescapeHTML("&lt;b&gt;A&amp;B&lt;&#x2F;b&gt;");
409
+ // "<b>A&B</b>"
410
+ ```
411
+
412
+ `sanitizeInput` and `unsanitizeInput` remain deprecated aliases. The legacy
413
+ `unsanitize` alias also remains available.
414
+
415
+ #### truncateByWeightedLength
416
+
417
+ Truncate by weighted UTF-16 length: code units from `U+0000` through `U+00FF`
418
+ count as one; all other code units count as two. Optional truncation text is
419
+ appended after the limit. This does not measure bytes or rendered width and can
420
+ split surrogate pairs.
421
+
422
+ ```javascript
423
+ import { truncateByWeightedLength } from "mazey";
424
+
425
+ truncateByWeightedLength("Hello世界", 7); // "Hello世"
426
+ truncateByWeightedLength("Hello世界", 7, { hasDot: true }); // "Hello世..."
427
+ ```
428
+
429
+ `cutZHString` remains a deprecated alias. The legacy `truncateZHString` and
430
+ `cutCHSString` signatures retain their boolean `hasDot` argument.
431
+
376
432
  #### escapeHtmlAttribute
377
433
 
378
434
  Escape text for use inside a quoted HTML attribute without escaping `/`.
@@ -836,15 +892,18 @@ a-b-c
836
892
  a-b-c
837
893
  ```
838
894
 
839
- #### convertCamelToUnder
895
+ #### convertCamelToSnake
840
896
 
841
- Transfer CamelCase to Underscore.
897
+ Insert an underscore before each uppercase ASCII letter, lowercase the result,
898
+ and remove one leading underscore. `XMLParser` becomes `x_m_l_parser`.
899
+ Other existing underscores are preserved. `convertCamelToUnder` remains a
900
+ deprecated direct alias; `camelCase2Underscore` remains available.
842
901
 
843
902
  Usage:
844
903
 
845
904
  ```javascript
846
- const ret1 = convertCamelToUnder("ABC");
847
- const ret2 = convertCamelToUnder("aBC");
905
+ const ret1 = convertCamelToSnake("ABC");
906
+ const ret2 = convertCamelToSnake("aBC");
848
907
  console.log(ret1);
849
908
  console.log(ret2);
850
909
  ```
@@ -856,6 +915,32 @@ a_b_c
856
915
  a_b_c
857
916
  ```
858
917
 
918
+ #### convertSnakeToCamel
919
+
920
+ Replace `_` followed by a lowercase ASCII letter with its uppercase letter.
921
+ Other underscores and characters remain unchanged. `convertUnderToCamel`
922
+ remains a deprecated direct alias.
923
+
924
+ ```javascript
925
+ convertSnakeToCamel("a_b_c"); // "aBC"
926
+ convertSnakeToCamel("a__b_"); // "a_B_"
927
+ ```
928
+
929
+ #### formatPercentage
930
+
931
+ Multiply a numeric ratio by 100 and append `%`. Precision defaults to `0`.
932
+ Falsy precision uses `Math.floor`, including for negative values; truthy
933
+ precision uses native `toFixed`, preserving trailing zeros, floating-point
934
+ behavior, and exceptions for unsupported precision. `floatToPercent` remains
935
+ a deprecated direct alias.
936
+
937
+ ```javascript
938
+ formatPercentage(0.129); // "12%"
939
+ formatPercentage(-0.129); // "-13%"
940
+ formatPercentage(0.125, 1); // "12.5%"
941
+ formatPercentage(0.12, 2); // "12.00%"
942
+ ```
943
+
859
944
  #### toJavaScriptGlobalName
860
945
 
861
946
  Convert text to a deterministic uppercase ASCII identifier suitable for an
@@ -912,6 +997,23 @@ manifest.
912
997
 
913
998
  ### URL
914
999
 
1000
+ #### getURLPathExtension
1001
+
1002
+ Extract a file extension from a URL or path, excluding query and fragment text.
1003
+ This helper does not detect MIME types or inspect file contents.
1004
+ It preserves the existing string-based behavior: `archive.tar.gz` returns
1005
+ `tar.gz`, and an origin-only input such as `https://example.com` returns `com`.
1006
+
1007
+ ```javascript
1008
+ import { getURLPathExtension } from "mazey";
1009
+
1010
+ getURLPathExtension("https://example.com/image.png?width=200#preview"); // "png"
1011
+ getURLPathExtension("/images/photo.jpg"); // "jpg"
1012
+ getURLPathExtension("/images/photo"); // ""
1013
+ ```
1014
+
1015
+ `getUrlFileType` remains a deprecated alias.
1016
+
915
1017
  #### getQueryParam
916
1018
 
917
1019
  Get the query param's value of the current Web URL(`location.search`).
@@ -1264,18 +1366,18 @@ Output:
1264
1366
  <style>body { background-color: #444; }</style>
1265
1367
  ```
1266
1368
 
1267
- Example 3: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1369
+ Example 3: Combine `createCSSRule` and `injectStyle` to add multiple styles at once.
1268
1370
 
1269
1371
  ```javascript
1270
- import { genStyleString, injectStyle } from "mazey";
1372
+ import { createCSSRule, injectStyle } from "mazey";
1271
1373
 
1272
- const xStyle = genStyleString(
1374
+ const xStyle = createCSSRule(
1273
1375
  ".footer>.x-wish>a:first-child" +
1274
1376
  ",div.wish-flex>a[href^='https://github.com/chengchuu']" +
1275
1377
  ",.m-hide",
1276
1378
  [ "display: none" ]
1277
1379
  );
1278
- const yStyle = genStyleString(
1380
+ const yStyle = createCSSRule(
1279
1381
  ".footer>.y-wish:before",
1280
1382
  [
1281
1383
  `content: 'Copyright (c) chengchuu'`,
@@ -1295,15 +1397,19 @@ Output:
1295
1397
  <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>
1296
1398
  ```
1297
1399
 
1298
- #### genStyleString
1400
+ #### createCSSRule
1299
1401
 
1300
- Generate the inline style string from the given parameters. The first parameter is the query selector, and the second parameter is the style array.
1402
+ Create CSS rule text from a selector and an array of declarations. The helper
1403
+ joins declarations with semicolons without validating or escaping CSS.
1404
+ `genStyleString` remains a deprecated alias.
1301
1405
 
1302
1406
  Usage:
1303
1407
 
1304
1408
  ```javascript
1305
- const ret1 = genStyleString(".a", [ "color:red" ]);
1306
- const ret2 = genStyleString("#b", [ "color:red", "font-size:12px" ]);
1409
+ import { createCSSRule } from "mazey";
1410
+
1411
+ const ret1 = createCSSRule(".a", [ "color:red" ]);
1412
+ const ret2 = createCSSRule("#b", [ "color:red", "font-size:12px" ]);
1307
1413
  console.log(ret1);
1308
1414
  console.log(ret2);
1309
1415
  ```
@@ -1315,18 +1421,18 @@ Output:
1315
1421
  #b{color:red;font-size:12px;}
1316
1422
  ```
1317
1423
 
1318
- Example: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1424
+ Example: Combine `createCSSRule` and `injectStyle` to add multiple styles at once.
1319
1425
 
1320
1426
  ```javascript
1321
- import { genStyleString, injectStyle } from "mazey";
1427
+ import { createCSSRule, injectStyle } from "mazey";
1322
1428
 
1323
- const xStyle = genStyleString(
1429
+ const xStyle = createCSSRule(
1324
1430
  ".footer>.x-wish>a:first-child" +
1325
1431
  ",div.wish-flex>a[href^='https://github.com/chengchuu']" +
1326
1432
  ",.m-hide",
1327
1433
  [ "display: none" ]
1328
1434
  );
1329
- const yStyle = genStyleString(
1435
+ const yStyle = createCSSRule(
1330
1436
  ".footer>.y-wish:before",
1331
1437
  [
1332
1438
  `content: 'Copyright (c) chengchuu'`,
@@ -1421,7 +1527,7 @@ The dates may be supported structured date strings, millisecond timestamps, or `
1421
1527
  Number input is a decimal ratio, so `0.202` represents `20.2%`. String input is a percentage value, so `"20.2%"` and `"20.2"` both represent `20.2%`; strict scientific notation such as `"2.02e1%"` is also accepted. The returned CAGR is an unrounded decimal ratio.
1422
1528
 
1423
1529
  ```javascript
1424
- import { calculateCAGR, floatToPercent } from "mazey";
1530
+ import { calculateCAGR, formatPercentage } from "mazey";
1425
1531
 
1426
1532
  const cagr = calculateCAGR(
1427
1533
  "2022-04-01",
@@ -1431,7 +1537,7 @@ const cagr = calculateCAGR(
1431
1537
 
1432
1538
  console.log({
1433
1539
  cagr,
1434
- percentage: floatToPercent(cagr, 2),
1540
+ percentage: formatPercentage(cagr, 2),
1435
1541
  });
1436
1542
  ```
1437
1543
 
@@ -1815,7 +1921,7 @@ reports iOS and Android as `"mobile"`; the new helpers provide a more specific
1815
1921
  phone, tablet, or desktop classification.
1816
1922
 
1817
1923
  `isPhone` checks device form factor. The separate `isMobile` API is a direct
1818
- alias of `isValidPhoneNumber`; it validates an 11-digit Chinese mobile-shaped
1924
+ alias of `isCNMobileNumber`; it validates an 11-digit Chinese mobile-shaped
1819
1925
  number and does not inspect the browser or device.
1820
1926
 
1821
1927
  #### isIOS
@@ -1930,6 +2036,20 @@ const { system, shell } = getBrowserInfo();
1930
2036
  const isMobileQQ = ["android", "ios"].includes(system) && ["qq_browser", "qq_app"].includes(shell);
1931
2037
  ```
1932
2038
 
2039
+ #### getBrowserClassNames
2040
+
2041
+ Return class-name tokens from the cached browser classification. An optional
2042
+ prefix and separator apply to each token; the helper does not modify the DOM.
2043
+
2044
+ ```javascript
2045
+ import { getBrowserClassNames } from "mazey";
2046
+
2047
+ console.log(getBrowserClassNames("browser"));
2048
+ // Possible output: ["browser-windows", "browser-desktop", "browser-webkit", "browser-chrome"]
2049
+ ```
2050
+
2051
+ `genBrowserAttrs` remains a deprecated alias.
2052
+
1933
2053
  #### isSafePWAEnv
1934
2054
 
1935
2055
  Detect whether the current browser document provides the minimum prerequisites
package/README.zh-CN.md CHANGED
@@ -92,7 +92,9 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
92
92
  - [debounce](#debounce)
93
93
  - [throttle](#throttle)
94
94
  - [convertCamelToKebab](#convertcameltokebab)
95
- - [convertCamelToUnder](#convertcameltounder)
95
+ - [convertCamelToSnake](#convertcameltosnake)
96
+ - [convertSnakeToCamel](#convertsnaketocamel)
97
+ - [formatPercentage](#formatpercentage)
96
98
  - [toJavaScriptGlobalName](#tojavascriptglobalname)
97
99
  - [URL](#url)
98
100
  - [getQueryParam](#getqueryparam)
@@ -599,15 +601,17 @@ a-b-c
599
601
  a-b-c
600
602
  ```
601
603
 
602
- #### convertCamelToUnder
604
+ #### convertCamelToSnake
603
605
 
604
- 将驼峰命名转换为下划线命名。
606
+ 在每个 ASCII 大写字母前插入下划线,将结果转换为小写,并删除第一个前导下划线。
607
+ `XMLParser` 转换为 `x_m_l_parser`,其余已有下划线保留。
608
+ `convertCamelToUnder` 保留为弃用的直接别名,`camelCase2Underscore` 仍可使用。
605
609
 
606
610
  用法:
607
611
 
608
612
  ```javascript
609
- const ret1 = convertCamelToUnder("ABC");
610
- const ret2 = convertCamelToUnder("aBC");
613
+ const ret1 = convertCamelToSnake("ABC");
614
+ const ret2 = convertCamelToSnake("aBC");
611
615
  console.log(ret1);
612
616
  console.log(ret2);
613
617
  ```
@@ -619,6 +623,30 @@ a_b_c
619
623
  a_b_c
620
624
  ```
621
625
 
626
+ #### convertSnakeToCamel
627
+
628
+ 将下划线及其后的 ASCII 小写字母替换为大写字母,其他字符保持不变。
629
+ `convertUnderToCamel` 保留为弃用的直接别名。
630
+
631
+ ```javascript
632
+ convertSnakeToCamel("a_b_c"); // "aBC"
633
+ convertSnakeToCamel("a__b_"); // "a_B_"
634
+ ```
635
+
636
+ #### formatPercentage
637
+
638
+ 将数值比率乘以 100 并添加 `%`。小数位数默认为 `0`。
639
+ 小数位数为假值时使用 `Math.floor`,负数也向下取整;为真值时使用原生 `toFixed`,
640
+ 保留末尾零、浮点数行为,以及不支持的小数位数导致的异常。
641
+ `floatToPercent` 保留为弃用的直接别名。
642
+
643
+ ```javascript
644
+ formatPercentage(0.129); // "12%"
645
+ formatPercentage(-0.129); // "-13%"
646
+ formatPercentage(0.125, 1); // "12.5%"
647
+ formatPercentage(0.12, 2); // "12.00%"
648
+ ```
649
+
622
650
  #### toJavaScriptGlobalName
623
651
 
624
652
  将文本转换为确定的、全大写的 ASCII JavaScript 标识符。该标识符适合作为 IIFE 全局名称。函数会将无效字符替换为下划线。结果以数字开头时,函数会添加下划线前缀。
@@ -1097,7 +1125,7 @@ CAGR = (1 + totalReturnRate)^(365 / durationInDays) - 1
1097
1125
  数值输入使用十进制比率,因此 `0.202` 表示 `20.2%`。字符串输入使用百分比数值,因此 `"20.2%"` 和 `"20.2"` 都表示 `20.2%`;也支持 `"2.02e1%"` 这类严格的科学记数法。返回的 CAGR 是未经舍入的十进制比率。
1098
1126
 
1099
1127
  ```javascript
1100
- import { calculateCAGR, floatToPercent } from "mazey";
1128
+ import { calculateCAGR, formatPercentage } from "mazey";
1101
1129
 
1102
1130
  const cagr = calculateCAGR(
1103
1131
  "2022-04-01",
@@ -1107,7 +1135,7 @@ const cagr = calculateCAGR(
1107
1135
 
1108
1136
  console.log({
1109
1137
  cagr,
1110
- percentage: floatToPercent(cagr, 2),
1138
+ percentage: formatPercentage(cagr, 2),
1111
1139
  });
1112
1140
  ```
1113
1141