yongying-tools 1.0.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 ADDED
@@ -0,0 +1,43 @@
1
+ ## 安装
2
+
3
+ ```
4
+ npm install yongying-tools
5
+ ```
6
+
7
+ ## 导入
8
+
9
+ ```js
10
+ const yongying = require("./yongying-tools");
11
+ ```
12
+
13
+ ## 格式化时间
14
+
15
+ ```js
16
+ // 格式化时间的功能
17
+ const dtSTr = yongying.dateFormat(new Date());
18
+ // 结果 2026-06-08 10:20:31
19
+ console.log(dtSTr);
20
+ ```
21
+
22
+ ## 转译 HTML 中特殊字符
23
+
24
+ ```js
25
+ // 转义 HTML 字符的功能
26
+ const htmlStr = '<h1> Hello & welcome to "yongying" </h1>';
27
+ const str = yongying.htmlEscape(htmlStr);
28
+ // 结果 &lt;h1&gt; Hello &amp; welcome to &quot;yongying&quot; &lt;/h1&gt;
29
+ console.log(str);
30
+ ```
31
+
32
+ ## 还原 HTML 中的特殊字符
33
+
34
+ ```js
35
+ // 还原 HTML 字符的功能
36
+ const str2 = yongying.htmlUnEscape(str);
37
+ // 结果 <h1> Hello & welcome to "yongying" </h1>
38
+ console.log(str2);
39
+ ```
40
+
41
+ ## 开源协议
42
+
43
+ ISC
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // 这是包的入口文件
2
+
3
+ // 导入需要暴露的成员
4
+ const date = require('./src/dateFormat')
5
+ const escape = require('./src/htmlEscape')
6
+
7
+
8
+ // 向外暴露需要的成员
9
+ module.exports = {
10
+ ...date,
11
+ ...escape
12
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "yongying-tools",
3
+ "version": "1.0.0",
4
+ "description": "提供格式化时间, HTMLEscape特殊字符转换等功能",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "yongying",
8
+ "dateFormat",
9
+ "escapeHtml"
10
+ ],
11
+ "license": "ISC",
12
+ "author": "yongying"
13
+ }
@@ -0,0 +1,24 @@
1
+ // 格式化时间的函数
2
+ function dateFormat(dateStr) {
3
+ const dt = new Date(dateStr)
4
+
5
+ const y = dt.getFullYear()
6
+ const m = padZero(dt.getMonth() + 1)
7
+ const d = padZero(dt.getDate())
8
+
9
+ const hh = padZero(dt.getHours())
10
+ const mm = padZero(dt.getMinutes())
11
+ const ss = padZero(dt.getSeconds())
12
+
13
+ return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
14
+ }
15
+
16
+
17
+ // 补零函数
18
+ function padZero(n) {
19
+ return n > 9 ? n : '0' + n
20
+ }
21
+
22
+ module.exports = {
23
+ dateFormat
24
+ }
@@ -0,0 +1,37 @@
1
+ // 定义转译 HTML 字符的函数
2
+ function htmlEscape(htmlStr) {
3
+ return htmlStr.replace(/<|>|"|&/g, (match) => {
4
+ switch (match) {
5
+ case '<':
6
+ return '&lt;'
7
+ case '>':
8
+ return '&gt;'
9
+ case '"':
10
+ return '&quot;'
11
+ case '&':
12
+ return '&amp;'
13
+ }
14
+ })
15
+ }
16
+
17
+ // 定义还原 HTML 字符的函数
18
+ function htmlUnEscape(htmlStr) {
19
+ return htmlStr.replace(/&lt;|&gt;|&quot;|&amp;/g, (match) => {
20
+ switch (match) {
21
+ case '&lt;':
22
+ return '<'
23
+ case '&gt;':
24
+ return '>'
25
+ case '&quot;':
26
+ return '"'
27
+ case '&amp;':
28
+ return '&'
29
+ }
30
+ })
31
+ }
32
+
33
+
34
+ module.exports = {
35
+ htmlEscape,
36
+ htmlUnEscape
37
+ }