itheima-tools-tcwgq1 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,37 @@
1
+ ## 安装方式
2
+
3
+ ```js
4
+ npm install itheima-tools
5
+ ```
6
+
7
+ ## 导入方式
8
+
9
+ ```js
10
+ const itheima = require('itheima-tools')
11
+ ```
12
+
13
+ ## 格式化时间
14
+
15
+ ```js
16
+ const time = itheima.dateFormat(new Date())
17
+ console.log(time) // 2026-02-01 11:58:26
18
+ ```
19
+
20
+ ## 转义 HTML 中的特殊字符
21
+
22
+ ```js
23
+ const html = '<h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>'
24
+ const htmlStr = itheima.htmlEscape(html)
25
+ console.log(htmlStr) // &lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;
26
+ ```
27
+
28
+ ## 还原 HTML 中的特殊字符
29
+
30
+ ```js
31
+ const htmlStr1 = itheima.htmlUnEscape(htmlStr)
32
+ console.log(htmlStr1) // <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>
33
+ ```
34
+
35
+ ## 开源协议
36
+
37
+ ISC
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ // 导入dateFormat模块
2
+ const format = require('./src/dateFormat')
3
+ // 导入htmlEscape模块
4
+ const escape = require('./src/htmlEscape')
5
+
6
+ // 利用展开运算符导出
7
+ module.exports = {
8
+ ...format,
9
+ ...escape
10
+ }
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "itheima-tools-tcwgq1",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "description": "提供了格式化时间,html标签转义的功能",
6
+ "keywords": [
7
+ "itheima",
8
+ "dateFormat",
9
+ "escape"
10
+ ],
11
+ "license": "ISC",
12
+ "author": "tcwgq",
13
+ "homepage": "https://www.tcwgq.com",
14
+ "type": "commonjs"
15
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 格式化时间
3
+ * @param {日期字符串或时间戳} str
4
+ * @returns 格式化后的时间
5
+ */
6
+ function dateFormat(str) {
7
+ const date = new Date(str)
8
+ const y = date.getFullYear()
9
+ const m = zeroPadding(date.getMonth() + 1)
10
+ const d = zeroPadding(date.getDate())
11
+ const hh = zeroPadding(date.getHours())
12
+ const mm = zeroPadding(date.getMinutes())
13
+ const ss = zeroPadding(date.getSeconds())
14
+ return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
15
+ }
16
+
17
+ // 补0函数
18
+ function zeroPadding(n) {
19
+ return n > 9 ? n : '0' + n
20
+ }
21
+
22
+ // 导出
23
+ module.exports = {
24
+ dateFormat
25
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 转义html片段
3
+ * @param {要转义的html片段} str
4
+ * @returns 转义后的内容
5
+ */
6
+ function htmlEscape(str) {
7
+ return str.replace(/<|>|"|&/g, match => {
8
+ switch (match) {
9
+ case '<':
10
+ return '&lt;'
11
+ case '>':
12
+ return '&gt;'
13
+ case '"':
14
+ return '&quot;'
15
+ case '&':
16
+ return '&amp;'
17
+ }
18
+ })
19
+ }
20
+
21
+ /**
22
+ * 还原html片段
23
+ * @param {要还原的html片段} str
24
+ * @returns 还原后的内容
25
+ */
26
+ function htmlUnEscape(str) {
27
+ return str.replace(/&lt;|&gt;|&quot;|&amp;/g, match => {
28
+ switch (match) {
29
+ case '&lt;':
30
+ return '<'
31
+ case '&gt;':
32
+ return '>'
33
+ case '&quot;':
34
+ return '"'
35
+ case '&amp;':
36
+ return '&'
37
+ }
38
+ })
39
+ }
40
+
41
+ // 导出
42
+ module.exports = {
43
+ htmlEscape,
44
+ htmlUnEscape
45
+ }