mazey 5.9.0 → 5.9.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
@@ -10,7 +10,9 @@ English | [简体中文](https://github.com/chengchuu/mazey/blob/main/README.zh-
10
10
  [l-image]: https://img.shields.io/npm/l/mazey
11
11
  [l-url]: https://github.com/chengchuu/mazey
12
12
 
13
- Mazey is a functional library for daily frontend work. There are already many excellent libraries for frontend development, but creating a file named `utils.js` or `common.js` is generally used to supply common functions in projects. It's boring to copy similar functions across multiple projects. That's why I've created this library and will keep updating it to serve as a reliable resource for frontend needs.
13
+ Mazey is a utility library for everyday frontend development. It provides
14
+ reusable functions that would otherwise be duplicated across project-specific
15
+ `utils.js` or `common.js` files.
14
16
 
15
17
  - [Project website](https://chengchuu.github.io/mazey/)
16
18
  - [Live playground](https://chengchuu.github.io/mazey/playground/)
@@ -34,41 +36,28 @@ You can also download and serve the
34
36
  [latest browser bundle](https://cdn.jsdelivr.net/npm/mazey@latest/lib/mazey.min.js)
35
37
  yourself.
36
38
 
37
- ## Browser support
38
-
39
- Mazey supports Chrome 109+, Edge 109+, Firefox 115+, Safari 16.4+, iOS Safari
40
- 16.4+, Android Chrome 109+, and Samsung Internet 21+. Package output can
41
- contain ES2022 syntax and does not include JavaScript polyfills. Internet
42
- Explorer, Opera Mini, KaiOS, the legacy Android Browser, and older browser
43
- versions are outside this support policy.
44
-
45
- Node.js 22 is used for development and continuous integration. Mazey does not
46
- declare a Node.js runtime compatibility range.
47
-
48
39
  ## Usage
49
40
 
50
- Example: Use a function to verify if a value is a number suitable for standard calculations and comparisons.
41
+ Example: Format a duration in milliseconds as readable text.
51
42
 
52
- Import from [npm](https://www.npmjs.com/package/mazey).
43
+ Import Mazey from [npm](https://www.npmjs.com/package/mazey).
53
44
 
54
45
  ```javascript
55
- import { isNumber } from "mazey";
46
+ import { formatDurationFromMs } from "mazey";
47
+
48
+ const duration = formatDurationFromMs(90000);
56
49
 
57
- const x = 123;
58
- const y = "abc";
59
- const z = Infinity;
60
- isNumber(x); // Output: true
61
- isNumber(y); // Output: false
62
- isNumber(z, { isInfinityAsNumber: true }); // Output: true
50
+ console.log(duration); // Output: "1.5 minutes"
63
51
  ```
64
52
 
65
- Import from CDN.
53
+ Use Mazey from CDN.
66
54
 
67
55
  ```html
68
56
  <script src="https://cdn.jsdelivr.net/npm/mazey@latest/lib/mazey.min.js"></script>
69
57
  <script>
70
- const x = 123;
71
- mazey.isNumber(x); // Output: true
58
+ const duration = mazey.formatDurationFromMs(90000);
59
+
60
+ console.log(duration); // Output: "1.5 minutes"
72
61
  </script>
73
62
  ```
74
63
 
@@ -136,9 +125,11 @@ There are some examples maintained by hand below. For more information, please c
136
125
  - [isValidCssSelector](#isvalidcssselector)
137
126
  - [resolveElementTarget](#resolveelementtarget)
138
127
  - [extractElementText](#extractelementtext)
139
- - [addStyle](#addstyle)
128
+ - [injectStyle](#injectstyle)
140
129
  - [genStyleString](#genstylestring)
141
130
  - [newLine](#newline)
131
+ - [Event](#event)
132
+ - [onEvent](#onevent)
142
133
  - [Calculate and Formula](#calculate-and-formula)
143
134
  - [calculateAspectRatio](#calculateaspectratio)
144
135
  - [calculateCAGR](#calculatecagr)
@@ -1213,18 +1204,20 @@ const text = extractElementText(message, {
1213
1204
  });
1214
1205
  ```
1215
1206
 
1216
- #### addStyle
1207
+ #### injectStyle
1217
1208
 
1218
1209
  Add `<style>` in `<head>`.
1219
1210
 
1211
+ `addStyle` is a deprecated compatibility alias of `injectStyle`.
1212
+
1220
1213
  Usage:
1221
1214
 
1222
1215
  Example 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
1223
1216
 
1224
1217
  ```javascript
1225
- import { addStyle } from "mazey";
1218
+ import { injectStyle } from "mazey";
1226
1219
 
1227
- addStyle(
1220
+ injectStyle(
1228
1221
  "body { background-color: #333; }",
1229
1222
  { id: "test" }
1230
1223
  );
@@ -1239,9 +1232,9 @@ Output:
1239
1232
  Example 2: Add the `<style>` without `id`, and repeated invoking will add a new one.
1240
1233
 
1241
1234
  ```javascript
1242
- import { addStyle } from "mazey";
1235
+ import { injectStyle } from "mazey";
1243
1236
 
1244
- addStyle("body { background-color: #444; }");
1237
+ injectStyle("body { background-color: #444; }");
1245
1238
  ```
1246
1239
 
1247
1240
  Output:
@@ -1250,10 +1243,10 @@ Output:
1250
1243
  <style>body { background-color: #444; }</style>
1251
1244
  ```
1252
1245
 
1253
- Example 3: Combine `genStyleString` and `addStyle` to add multiple styles at once.
1246
+ Example 3: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1254
1247
 
1255
1248
  ```javascript
1256
- import { genStyleString, addStyle } from "mazey";
1249
+ import { genStyleString, injectStyle } from "mazey";
1257
1250
 
1258
1251
  const xStyle = genStyleString(
1259
1252
  ".footer>.x-wish>a:first-child" +
@@ -1272,7 +1265,7 @@ const yStyle = genStyleString(
1272
1265
  "padding-bottom: var(--y-wish-1)",
1273
1266
  ]
1274
1267
  );
1275
- addStyle(xStyle + yStyle, { id: "z-style" });
1268
+ injectStyle(xStyle + yStyle, { id: "z-style" });
1276
1269
  ```
1277
1270
 
1278
1271
  Output:
@@ -1301,10 +1294,10 @@ Output:
1301
1294
  #b{color:red;font-size:12px;}
1302
1295
  ```
1303
1296
 
1304
- Example: Combine `genStyleString` and `addStyle` to add multiple styles at once.
1297
+ Example: Combine `genStyleString` and `injectStyle` to add multiple styles at once.
1305
1298
 
1306
1299
  ```javascript
1307
- import { genStyleString, addStyle } from "mazey";
1300
+ import { genStyleString, injectStyle } from "mazey";
1308
1301
 
1309
1302
  const xStyle = genStyleString(
1310
1303
  ".footer>.x-wish>a:first-child" +
@@ -1323,7 +1316,7 @@ const yStyle = genStyleString(
1323
1316
  "padding-bottom: var(--y-wish-1)",
1324
1317
  ]
1325
1318
  );
1326
- addStyle(xStyle + yStyle, { id: "z-style" });
1319
+ injectStyle(xStyle + yStyle, { id: "z-style" });
1327
1320
  ```
1328
1321
 
1329
1322
  Output:
@@ -1352,6 +1345,23 @@ a<br />b<br />c
1352
1345
  a<br /><br />bc
1353
1346
  ```
1354
1347
 
1348
+ ### Event
1349
+
1350
+ #### onEvent
1351
+
1352
+ Register a named Mazey event callback. Duplicate callbacks are allowed.
1353
+ `addEvent` is a deprecated compatibility alias of `onEvent`.
1354
+
1355
+ ```javascript
1356
+ import { fireEvent, onEvent } from "mazey";
1357
+
1358
+ onEvent("test", event => {
1359
+ console.log("test event:", event);
1360
+ });
1361
+
1362
+ fireEvent("test", { type: "test" });
1363
+ ```
1364
+
1355
1365
  ### Calculate and Formula
1356
1366
 
1357
1367
  #### calculateAspectRatio
@@ -2023,6 +2033,14 @@ MazeyLog: I am number. 123 456
2023
2033
  MazeyLog: I am object. {a: 123, b: 456}
2024
2034
  ```
2025
2035
 
2036
+ ## Browser support
2037
+
2038
+ Mazey supports Chrome 109+, Edge 109+, Firefox 115+, Safari 16.4+, iOS Safari
2039
+ 16.4+, Android Chrome 109+, and Samsung Internet 21+. Package output can
2040
+ contain ES2022 syntax and does not include JavaScript polyfills. Internet
2041
+ Explorer, Opera Mini, KaiOS, the legacy Android Browser, and older browser
2042
+ versions are outside this support policy.
2043
+
2026
2044
  ## Contributing
2027
2045
 
2028
2046
  ### Development Environment
@@ -2036,7 +2054,7 @@ MazeyLog: I am object. {a: 123, b: 456}
2036
2054
 
2037
2055
  | Command | Purpose |
2038
2056
  | --------------------- | --------------------------------------------------------------- |
2039
- | `npm install` | Install development dependencies. |
2057
+ | `pnpm install` | Install development dependencies. |
2040
2058
  | `npm run dev` | Start the website and playground development server. |
2041
2059
  | `npm run build` | Build the publishable package files. |
2042
2060
  | `npm test` | Run the Jest test suite. |
package/README.zh-CN.md CHANGED
@@ -107,9 +107,11 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
107
107
  - [Storage 工具](#storage-工具)
108
108
  - [DOM](#dom)
109
109
  - [Class 工具](#class-工具)
110
- - [addStyle](#addstyle)
110
+ - [injectStyle](#injectstyle)
111
111
  - [genStyleString](#genstylestring)
112
112
  - [newLine](#newline)
113
+ - [事件](#事件)
114
+ - [onEvent](#onevent)
113
115
  - [计算与公式](#计算与公式)
114
116
  - [calculateAspectRatio](#calculateaspectratio)
115
117
  - [calculateCAGR](#calculatecagr)
@@ -859,18 +861,20 @@ addClass(dom, "test");
859
861
  removeClass(dom, "test");
860
862
  ```
861
863
 
862
- #### addStyle
864
+ #### injectStyle
863
865
 
864
866
  在 `<head>` 中添加 `<style>` 元素。
865
867
 
868
+ `addStyle` 是 `injectStyle` 的弃用兼容别名。
869
+
866
870
  用法:
867
871
 
868
872
  示例 1: 添加带有 `id` 的 `<style>`。重复调用会更新内容,不会添加新元素。
869
873
 
870
874
  ```javascript
871
- import { addStyle } from "mazey";
875
+ import { injectStyle } from "mazey";
872
876
 
873
- addStyle(
877
+ injectStyle(
874
878
  "body { background-color: #333; }",
875
879
  { id: "test" }
876
880
  );
@@ -885,9 +889,9 @@ addStyle(
885
889
  示例 2: 添加不带 `id` 的 `<style>`。重复调用会添加新元素。
886
890
 
887
891
  ```javascript
888
- import { addStyle } from "mazey";
892
+ import { injectStyle } from "mazey";
889
893
 
890
- addStyle("body { background-color: #444; }");
894
+ injectStyle("body { background-color: #444; }");
891
895
  ```
892
896
 
893
897
  输出:
@@ -896,10 +900,10 @@ addStyle("body { background-color: #444; }");
896
900
  <style>body { background-color: #444; }</style>
897
901
  ```
898
902
 
899
- 示例 3: 组合使用 `genStyleString` 和 `addStyle`,一次添加多条样式。
903
+ 示例 3: 组合使用 `genStyleString` 和 `injectStyle`,一次添加多条样式。
900
904
 
901
905
  ```javascript
902
- import { genStyleString, addStyle } from "mazey";
906
+ import { genStyleString, injectStyle } from "mazey";
903
907
 
904
908
  const xStyle = genStyleString(
905
909
  ".footer>.x-wish>a:first-child" +
@@ -918,7 +922,7 @@ const yStyle = genStyleString(
918
922
  "padding-bottom: var(--y-wish-1)",
919
923
  ]
920
924
  );
921
- addStyle(xStyle + yStyle, { id: "z-style" });
925
+ injectStyle(xStyle + yStyle, { id: "z-style" });
922
926
  ```
923
927
 
924
928
  输出:
@@ -947,10 +951,10 @@ console.log(ret2);
947
951
  #b{color:red;font-size:12px;}
948
952
  ```
949
953
 
950
- 下面的示例组合使用 `genStyleString` 和 `addStyle`,一次添加多条样式。
954
+ 下面的示例组合使用 `genStyleString` 和 `injectStyle`,一次添加多条样式。
951
955
 
952
956
  ```javascript
953
- import { genStyleString, addStyle } from "mazey";
957
+ import { genStyleString, injectStyle } from "mazey";
954
958
 
955
959
  const xStyle = genStyleString(
956
960
  ".footer>.x-wish>a:first-child" +
@@ -969,7 +973,7 @@ const yStyle = genStyleString(
969
973
  "padding-bottom: var(--y-wish-1)",
970
974
  ]
971
975
  );
972
- addStyle(xStyle + yStyle, { id: "z-style" });
976
+ injectStyle(xStyle + yStyle, { id: "z-style" });
973
977
  ```
974
978
 
975
979
  输出:
@@ -998,6 +1002,23 @@ a<br />b<br />c
998
1002
  a<br /><br />bc
999
1003
  ```
1000
1004
 
1005
+ ### 事件
1006
+
1007
+ #### onEvent
1008
+
1009
+ 注册具名的 Mazey 事件回调。函数允许重复注册同一个回调。
1010
+ `addEvent` 是 `onEvent` 的弃用兼容别名。
1011
+
1012
+ ```javascript
1013
+ import { fireEvent, onEvent } from "mazey";
1014
+
1015
+ onEvent("test", event => {
1016
+ console.log("test event:", event);
1017
+ });
1018
+
1019
+ fireEvent("test", { type: "test" });
1020
+ ```
1021
+
1001
1022
  ### 计算与公式
1002
1023
 
1003
1024
  #### calculateAspectRatio