mazey 5.9.1 → 5.10.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 CHANGED
@@ -78,6 +78,7 @@ There are some examples maintained by hand below. For more information, please c
78
78
  - [windowLoaded](#windowloaded)
79
79
  - [Util](#util)
80
80
  - [isNumber](#isnumber)
81
+ - [isNullish](#isnullish)
81
82
  - [isJSONString](#isjsonstring)
82
83
  - [parseJsonSafe](#parsejsonsafe)
83
84
  - [escapeHtmlAttribute](#escapehtmlattribute)
@@ -121,17 +122,19 @@ There are some examples maintained by hand below. For more information, please c
121
122
  - [Storage Helpers](#storage-helpers)
122
123
  - [DOM](#dom)
123
124
  - [Class Helpers](#class-helpers)
124
- - [hide and show](#hide-and-show)
125
+ - [hideElements and showElements](#hideelements-and-showelements)
125
126
  - [isValidCssSelector](#isvalidcssselector)
126
127
  - [resolveElementTarget](#resolveelementtarget)
127
128
  - [extractElementText](#extractelementtext)
128
- - [addStyle](#addstyle)
129
+ - [injectStyle](#injectstyle)
129
130
  - [genStyleString](#genstylestring)
130
131
  - [newLine](#newline)
132
+ - [Event](#event)
133
+ - [onEvent](#onevent)
131
134
  - [Calculate and Formula](#calculate-and-formula)
132
135
  - [calculateAspectRatio](#calculateaspectratio)
133
136
  - [calculateCAGR](#calculatecagr)
134
- - [inRate](#inrate)
137
+ - [randomBoolean](#randomboolean)
135
138
  - [longestComSubstring](#longestcomsubstring)
136
139
  - [longestComSubsequence](#longestcomsubsequence)
137
140
  - [Browser Information](#browser-information)
@@ -291,6 +294,9 @@ Load Success: load
291
294
 
292
295
  ### Util
293
296
 
297
+ Use native `Date.now()` to get the current epoch time in milliseconds. The
298
+ former `mNow()` helper remains available as a deprecated compatibility API.
299
+
294
300
  #### isNumber
295
301
 
296
302
  Check whether a value is an allowed numeric primitive. Optional constraints can
@@ -321,6 +327,21 @@ true false false true false true true false
321
327
  reversed bounds return `false`. Existing non-finite-number behavior is unchanged
322
328
  when `integer`, `min`, and `max` are omitted.
323
329
 
330
+ #### isNullish
331
+
332
+ Check whether a value is exactly `undefined` or `null`. Other falsy values are
333
+ not nullish.
334
+
335
+ ```javascript
336
+ isNullish(undefined); // true
337
+ isNullish(null); // true
338
+ isNullish(false); // false
339
+ isNullish(0); // false
340
+ isNullish(""); // false
341
+ ```
342
+
343
+ `isUdfOrNul` remains available as a deprecated alias of `isNullish`.
344
+
324
345
  #### isJSONString
325
346
 
326
347
  Check whether it is a valid JSON string.
@@ -1137,29 +1158,31 @@ addClass(dom, "test");
1137
1158
  removeClass(dom, "test");
1138
1159
  ```
1139
1160
 
1140
- #### hide and show
1161
+ #### hideElements and showElements
1141
1162
 
1142
1163
  Hide or show a CSS selector, one element, or an iterable or array-like element
1143
1164
  collection. Both helpers return the original target, so a caller can retain its
1144
1165
  own chaining convention. Duplicate elements are changed only once, and invalid
1145
1166
  selectors or unsupported values are ignored.
1146
1167
 
1147
- `hide()` preserves a visible element's inline `display` value. `show()` restores
1148
- that value, or recovers the element's normal display when a stylesheet would
1149
- otherwise keep it hidden.
1168
+ `hideElements()` preserves a visible element's inline `display` value.
1169
+ `showElements()` restores that value, or recovers the element's normal display
1170
+ when a stylesheet would otherwise keep it hidden.
1150
1171
 
1151
1172
  ```javascript
1152
- import { hide, show } from "mazey";
1173
+ import { hideElements, showElements } from "mazey";
1153
1174
 
1154
1175
  const notices = document.querySelectorAll(".notice");
1155
1176
 
1156
- hide(notices);
1157
- show(notices);
1177
+ hideElements(notices);
1178
+ showElements(notices);
1158
1179
 
1159
- hide("#temporary-message");
1160
- show(document.querySelector("#temporary-message"));
1180
+ hideElements("#temporary-message");
1181
+ showElements(document.querySelector("#temporary-message"));
1161
1182
  ```
1162
1183
 
1184
+ `hide` and `show` remain available as deprecated aliases.
1185
+
1163
1186
  #### isValidCssSelector
1164
1187
 
1165
1188
  Check whether a value is a CSS selector supported by the supplied query root.
@@ -1202,18 +1225,20 @@ const text = extractElementText(message, {
1202
1225
  });
1203
1226
  ```
1204
1227
 
1205
- #### addStyle
1228
+ #### injectStyle
1206
1229
 
1207
1230
  Add `<style>` in `<head>`.
1208
1231
 
1232
+ `addStyle` is a deprecated compatibility alias of `injectStyle`.
1233
+
1209
1234
  Usage:
1210
1235
 
1211
1236
  Example 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
1212
1237
 
1213
1238
  ```javascript
1214
- import { addStyle } from "mazey";
1239
+ import { injectStyle } from "mazey";
1215
1240
 
1216
- addStyle(
1241
+ injectStyle(
1217
1242
  "body { background-color: #333; }",
1218
1243
  { id: "test" }
1219
1244
  );
@@ -1228,9 +1253,9 @@ Output:
1228
1253
  Example 2: Add the `<style>` without `id`, and repeated invoking will add a new one.
1229
1254
 
1230
1255
  ```javascript
1231
- import { addStyle } from "mazey";
1256
+ import { injectStyle } from "mazey";
1232
1257
 
1233
- addStyle("body { background-color: #444; }");
1258
+ injectStyle("body { background-color: #444; }");
1234
1259
  ```
1235
1260
 
1236
1261
  Output:
@@ -1239,10 +1264,10 @@ Output:
1239
1264
  <style>body { background-color: #444; }</style>
1240
1265
  ```
1241
1266
 
1242
- Example 3: Combine `genStyleString` and `addStyle` to add multiple styles at once.
1267
+ Example 3: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1243
1268
 
1244
1269
  ```javascript
1245
- import { genStyleString, addStyle } from "mazey";
1270
+ import { genStyleString, injectStyle } from "mazey";
1246
1271
 
1247
1272
  const xStyle = genStyleString(
1248
1273
  ".footer>.x-wish>a:first-child" +
@@ -1261,7 +1286,7 @@ const yStyle = genStyleString(
1261
1286
  "padding-bottom: var(--y-wish-1)",
1262
1287
  ]
1263
1288
  );
1264
- addStyle(xStyle + yStyle, { id: "z-style" });
1289
+ injectStyle(xStyle + yStyle, { id: "z-style" });
1265
1290
  ```
1266
1291
 
1267
1292
  Output:
@@ -1290,10 +1315,10 @@ Output:
1290
1315
  #b{color:red;font-size:12px;}
1291
1316
  ```
1292
1317
 
1293
- Example: Combine `genStyleString` and `addStyle` to add multiple styles at once.
1318
+ Example: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1294
1319
 
1295
1320
  ```javascript
1296
- import { genStyleString, addStyle } from "mazey";
1321
+ import { genStyleString, injectStyle } from "mazey";
1297
1322
 
1298
1323
  const xStyle = genStyleString(
1299
1324
  ".footer>.x-wish>a:first-child" +
@@ -1312,7 +1337,7 @@ const yStyle = genStyleString(
1312
1337
  "padding-bottom: var(--y-wish-1)",
1313
1338
  ]
1314
1339
  );
1315
- addStyle(xStyle + yStyle, { id: "z-style" });
1340
+ injectStyle(xStyle + yStyle, { id: "z-style" });
1316
1341
  ```
1317
1342
 
1318
1343
  Output:
@@ -1341,6 +1366,23 @@ a<br />b<br />c
1341
1366
  a<br /><br />bc
1342
1367
  ```
1343
1368
 
1369
+ ### Event
1370
+
1371
+ #### onEvent
1372
+
1373
+ Register a named Mazey event callback. Duplicate callbacks are allowed.
1374
+ `addEvent` is a deprecated compatibility alias of `onEvent`.
1375
+
1376
+ ```javascript
1377
+ import { fireEvent, onEvent } from "mazey";
1378
+
1379
+ onEvent("test", event => {
1380
+ console.log("test event:", event);
1381
+ });
1382
+
1383
+ fireEvent("test", { type: "test" });
1384
+ ```
1385
+
1344
1386
  ### Calculate and Formula
1345
1387
 
1346
1388
  #### calculateAspectRatio
@@ -1414,14 +1456,14 @@ calculateCAGR(
1414
1456
 
1415
1457
  Date strings are validated using Mazey's strict date rules. Invalid dates, malformed or non-finite returns, and non-increasing date ranges throw errors. The parsed total return must be greater than `-1`, because `-1` represents a complete loss for which CAGR is undefined.
1416
1458
 
1417
- #### inRate
1459
+ #### randomBoolean
1418
1460
 
1419
- Hit probability (1% ~ 100%).
1461
+ Return whether a generated random value is less than the supplied probability.
1420
1462
 
1421
1463
  Usage:
1422
1464
 
1423
1465
  ```javascript
1424
- const ret = inRate(0.5); // 0.01 ~ 1 true/false
1466
+ const ret = randomBoolean(0.5); // A 50% chance of returning true.
1425
1467
  console.log(ret);
1426
1468
  ```
1427
1469
 
@@ -1438,7 +1480,7 @@ Example: Test the precision.
1438
1480
  let trueCount = 0;
1439
1481
  let falseCount = 0;
1440
1482
  new Array(1000000).fill(0).forEach(() => {
1441
- if (inRate(0.5)) {
1483
+ if (randomBoolean(0.5)) {
1442
1484
  trueCount++;
1443
1485
  } else {
1444
1486
  falseCount++;
@@ -1447,6 +1489,9 @@ new Array(1000000).fill(0).forEach(() => {
1447
1489
  console.log(trueCount, falseCount); // 499994 500006
1448
1490
  ```
1449
1491
 
1492
+ `randomBoolean` evaluates `Math.random() < rate` without clamping the supplied
1493
+ rate. `isHit` is a deprecated alias, and `inRate` remains a compatibility alias.
1494
+
1450
1495
  #### longestComSubstring
1451
1496
 
1452
1497
  Computes the longest common substring of two strings.
package/README.zh-CN.md CHANGED
@@ -78,6 +78,7 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
78
78
  - [windowLoaded](#windowloaded)
79
79
  - [通用工具](#通用工具)
80
80
  - [isNumber](#isnumber)
81
+ - [isNullish](#isnullish)
81
82
  - [isJSONString](#isjsonstring)
82
83
  - [isValidData](#isvaliddata)
83
84
  - [genRndNumString](#genrndnumstring)
@@ -107,13 +108,16 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
107
108
  - [Storage 工具](#storage-工具)
108
109
  - [DOM](#dom)
109
110
  - [Class 工具](#class-工具)
110
- - [addStyle](#addstyle)
111
+ - [hideElements 和 showElements](#hideelements-和-showelements)
112
+ - [injectStyle](#injectstyle)
111
113
  - [genStyleString](#genstylestring)
112
114
  - [newLine](#newline)
115
+ - [事件](#事件)
116
+ - [onEvent](#onevent)
113
117
  - [计算与公式](#计算与公式)
114
118
  - [calculateAspectRatio](#calculateaspectratio)
115
119
  - [calculateCAGR](#calculatecagr)
116
- - [inRate](#inrate)
120
+ - [randomBoolean](#randomboolean)
117
121
  - [longestComSubstring](#longestcomsubstring)
118
122
  - [longestComSubsequence](#longestcomsubsequence)
119
123
  - [浏览器信息](#浏览器信息)
@@ -264,6 +268,9 @@ windowLoaded()
264
268
 
265
269
  ### 通用工具
266
270
 
271
+ 使用原生 `Date.now()` 获取当前时间戳 (毫秒)。原有的 `mNow()` 工具仍作为弃用兼容 API
272
+ 保留。
273
+
267
274
  #### isNumber
268
275
 
269
276
  判断某个值是否为有效数字。
@@ -287,6 +294,20 @@ console.log(ret1, ret2, ret3, ret4, ret5, ret6);
287
294
  true false false true false true
288
295
  ```
289
296
 
297
+ #### isNullish
298
+
299
+ 判断值是否严格为 `undefined` 或 `null`。其他假值不属于空值。
300
+
301
+ ```javascript
302
+ isNullish(undefined); // true
303
+ isNullish(null); // true
304
+ isNullish(false); // false
305
+ isNullish(0); // false
306
+ isNullish(""); // false
307
+ ```
308
+
309
+ `isUdfOrNul` 仍作为 `isNullish` 的弃用别名保留。
310
+
290
311
  #### isJSONString
291
312
 
292
313
  判断字符串是否为有效的 JSON 字符串。
@@ -859,18 +880,41 @@ addClass(dom, "test");
859
880
  removeClass(dom, "test");
860
881
  ```
861
882
 
862
- #### addStyle
883
+ #### hideElements 和 showElements
884
+
885
+ 隐藏或显示 CSS 选择器、单个元素、可迭代元素集合或类数组元素集合。两个函数都会返回原始输入。
886
+ 重复元素只会被修改一次。函数会忽略无效选择器和不支持的值。
887
+
888
+ `hideElements()` 会保存可见元素的内联 `display` 值。`showElements()` 会恢复该值。如果样式表仍隐藏该元素,函数会恢复元素的默认显示方式。
889
+
890
+ ```javascript
891
+ import { hideElements, showElements } from "mazey";
892
+
893
+ const notices = document.querySelectorAll(".notice");
894
+
895
+ hideElements(notices);
896
+ showElements(notices);
897
+
898
+ hideElements("#temporary-message");
899
+ showElements(document.querySelector("#temporary-message"));
900
+ ```
901
+
902
+ `hide` 和 `show` 仍作为弃用别名保留。
903
+
904
+ #### injectStyle
863
905
 
864
906
  在 `<head>` 中添加 `<style>` 元素。
865
907
 
908
+ `addStyle` 是 `injectStyle` 的弃用兼容别名。
909
+
866
910
  用法:
867
911
 
868
912
  示例 1: 添加带有 `id` 的 `<style>`。重复调用会更新内容,不会添加新元素。
869
913
 
870
914
  ```javascript
871
- import { addStyle } from "mazey";
915
+ import { injectStyle } from "mazey";
872
916
 
873
- addStyle(
917
+ injectStyle(
874
918
  "body { background-color: #333; }",
875
919
  { id: "test" }
876
920
  );
@@ -885,9 +929,9 @@ addStyle(
885
929
  示例 2: 添加不带 `id` 的 `<style>`。重复调用会添加新元素。
886
930
 
887
931
  ```javascript
888
- import { addStyle } from "mazey";
932
+ import { injectStyle } from "mazey";
889
933
 
890
- addStyle("body { background-color: #444; }");
934
+ injectStyle("body { background-color: #444; }");
891
935
  ```
892
936
 
893
937
  输出:
@@ -896,10 +940,10 @@ addStyle("body { background-color: #444; }");
896
940
  <style>body { background-color: #444; }</style>
897
941
  ```
898
942
 
899
- 示例 3: 组合使用 `genStyleString` 和 `addStyle`,一次添加多条样式。
943
+ 示例 3: 组合使用 `genStyleString` 和 `injectStyle`,一次添加多条样式。
900
944
 
901
945
  ```javascript
902
- import { genStyleString, addStyle } from "mazey";
946
+ import { genStyleString, injectStyle } from "mazey";
903
947
 
904
948
  const xStyle = genStyleString(
905
949
  ".footer>.x-wish>a:first-child" +
@@ -918,7 +962,7 @@ const yStyle = genStyleString(
918
962
  "padding-bottom: var(--y-wish-1)",
919
963
  ]
920
964
  );
921
- addStyle(xStyle + yStyle, { id: "z-style" });
965
+ injectStyle(xStyle + yStyle, { id: "z-style" });
922
966
  ```
923
967
 
924
968
  输出:
@@ -947,10 +991,10 @@ console.log(ret2);
947
991
  #b{color:red;font-size:12px;}
948
992
  ```
949
993
 
950
- 下面的示例组合使用 `genStyleString` 和 `addStyle`,一次添加多条样式。
994
+ 下面的示例组合使用 `genStyleString` 和 `injectStyle`,一次添加多条样式。
951
995
 
952
996
  ```javascript
953
- import { genStyleString, addStyle } from "mazey";
997
+ import { genStyleString, injectStyle } from "mazey";
954
998
 
955
999
  const xStyle = genStyleString(
956
1000
  ".footer>.x-wish>a:first-child" +
@@ -969,7 +1013,7 @@ const yStyle = genStyleString(
969
1013
  "padding-bottom: var(--y-wish-1)",
970
1014
  ]
971
1015
  );
972
- addStyle(xStyle + yStyle, { id: "z-style" });
1016
+ injectStyle(xStyle + yStyle, { id: "z-style" });
973
1017
  ```
974
1018
 
975
1019
  输出:
@@ -998,6 +1042,23 @@ a<br />b<br />c
998
1042
  a<br /><br />bc
999
1043
  ```
1000
1044
 
1045
+ ### 事件
1046
+
1047
+ #### onEvent
1048
+
1049
+ 注册具名的 Mazey 事件回调。函数允许重复注册同一个回调。
1050
+ `addEvent` 是 `onEvent` 的弃用兼容别名。
1051
+
1052
+ ```javascript
1053
+ import { fireEvent, onEvent } from "mazey";
1054
+
1055
+ onEvent("test", event => {
1056
+ console.log("test event:", event);
1057
+ });
1058
+
1059
+ fireEvent("test", { type: "test" });
1060
+ ```
1061
+
1001
1062
  ### 计算与公式
1002
1063
 
1003
1064
  #### calculateAspectRatio
@@ -1071,14 +1132,14 @@ calculateCAGR(
1071
1132
 
1072
1133
  日期字符串遵循 Mazey 的严格日期校验规则。无效日期、格式错误或非有限的回报率,以及没有递增的日期范围都会抛出错误。解析后的总回报率必须大于 `-1`,因为 `-1` 表示本金完全损失,此时 CAGR 没有定义。
1073
1134
 
1074
- #### inRate
1135
+ #### randomBoolean
1075
1136
 
1076
- 按照指定概率返回命中结果。有效概率范围为 1%~100%。
1137
+ 判断生成的随机值是否小于指定概率。
1077
1138
 
1078
1139
  用法:
1079
1140
 
1080
1141
  ```javascript
1081
- const ret = inRate(0.5); // 0.01~1,返回 true 或 false
1142
+ const ret = randomBoolean(0.5); // 有 50% 的概率返回 true
1082
1143
  console.log(ret);
1083
1144
  ```
1084
1145
 
@@ -1095,7 +1156,7 @@ true
1095
1156
  let trueCount = 0;
1096
1157
  let falseCount = 0;
1097
1158
  new Array(1000000).fill(0).forEach(() => {
1098
- if (inRate(0.5)) {
1159
+ if (randomBoolean(0.5)) {
1099
1160
  trueCount++;
1100
1161
  } else {
1101
1162
  falseCount++;
@@ -1104,6 +1165,9 @@ new Array(1000000).fill(0).forEach(() => {
1104
1165
  console.log(trueCount, falseCount); // 499994 500006
1105
1166
  ```
1106
1167
 
1168
+ `randomBoolean` 直接计算 `Math.random() < rate`,不会限制传入的概率值。`isHit` 是弃用别名。
1169
+ `inRate` 仍作为兼容别名保留。
1170
+
1107
1171
  #### longestComSubstring
1108
1172
 
1109
1173
  计算两个字符串的最长公共子串长度。