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 +43 -0
- package/index.js +12 -0
- package/package.json +13 -0
- package/src/dateFormat.js +24 -0
- package/src/htmlEscape.js +37 -0
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
|
+
// 结果 <h1> Hello & welcome to "yongying" </h1>
|
|
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
package/package.json
ADDED
|
@@ -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 '<'
|
|
7
|
+
case '>':
|
|
8
|
+
return '>'
|
|
9
|
+
case '"':
|
|
10
|
+
return '"'
|
|
11
|
+
case '&':
|
|
12
|
+
return '&'
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 定义还原 HTML 字符的函数
|
|
18
|
+
function htmlUnEscape(htmlStr) {
|
|
19
|
+
return htmlStr.replace(/<|>|"|&/g, (match) => {
|
|
20
|
+
switch (match) {
|
|
21
|
+
case '<':
|
|
22
|
+
return '<'
|
|
23
|
+
case '>':
|
|
24
|
+
return '>'
|
|
25
|
+
case '"':
|
|
26
|
+
return '"'
|
|
27
|
+
case '&':
|
|
28
|
+
return '&'
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
module.exports = {
|
|
35
|
+
htmlEscape,
|
|
36
|
+
htmlUnEscape
|
|
37
|
+
}
|