itheima-tool-wenh-version 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 +36 -0
- package/index.js +6 -0
- package/package.json +12 -0
- package/src/dateFormat.js +16 -0
- package/src/htmlEscape.js +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
## 安装
|
|
2
|
+
```
|
|
3
|
+
npm install itheima-tool
|
|
4
|
+
```
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## 导入
|
|
8
|
+
```js
|
|
9
|
+
const itheima = require('itheima-tool')
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 转义html中的特殊字符
|
|
13
|
+
```js
|
|
14
|
+
//待转换的html字符串
|
|
15
|
+
const htmlStr = '<h1>这是h1标签<span>123 </span></h1>'
|
|
16
|
+
//调用htmlEscape方法进行转换
|
|
17
|
+
const str = custom.htmlEscape(htmlStr)
|
|
18
|
+
//转换的结果
|
|
19
|
+
console.log(str);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## 还原html中的特殊字符
|
|
24
|
+
```js
|
|
25
|
+
//待还原的html字符串
|
|
26
|
+
const htmlStr1 = '<h1>这是h1标签<span>123&nbsp;</span></h1>'
|
|
27
|
+
//调用htmlUnescape方法进行转换
|
|
28
|
+
const str1 = custom.htmlUnescape(htmlStr1)
|
|
29
|
+
//转换的结果
|
|
30
|
+
console.log(str1);
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## 开源协议
|
|
36
|
+
ISC
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function dateFormat(dataStr) {
|
|
2
|
+
const dt = new Date(dataStr)
|
|
3
|
+
const year = dt.getFullYear()
|
|
4
|
+
const month = padZero(dt.getMonth() + 1)
|
|
5
|
+
const date = padZero(dt.getDate())
|
|
6
|
+
const hour = padZero(dt.getHours())
|
|
7
|
+
const min = padZero(dt.getMinutes())
|
|
8
|
+
const sec = padZero(dt.getSeconds())
|
|
9
|
+
return `${year}-${month}-${date} ${hour}:${min}:${sec}`
|
|
10
|
+
}
|
|
11
|
+
function padZero(n) {
|
|
12
|
+
return n > 9 ? n : `0${n}`
|
|
13
|
+
}
|
|
14
|
+
module.exports = {
|
|
15
|
+
dateFormat
|
|
16
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//htmlEscape
|
|
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
|
+
//htmlUnescape
|
|
17
|
+
function htmlUnescape(htmlStr) {
|
|
18
|
+
return htmlStr.replace(/<|>|"|&/g, (match) => {
|
|
19
|
+
switch (match) {
|
|
20
|
+
case '<':
|
|
21
|
+
return '<'
|
|
22
|
+
case '>':
|
|
23
|
+
return '>'
|
|
24
|
+
case '"':
|
|
25
|
+
return '"'
|
|
26
|
+
case '&':
|
|
27
|
+
return '&'
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
module.exports = {
|
|
32
|
+
htmlEscape,
|
|
33
|
+
htmlUnescape
|
|
34
|
+
}
|