studyalways-itheima-tool-hwb-2026 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/READEMY.md +38 -0
- package/index.js +7 -0
- package/package.json +8 -0
- package/src/dataFormat.js +18 -0
- package/src/htmlEscape.js +31 -0
- package/test.js +9 -0
package/READEMY.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
## 安装
|
|
2
|
+
```
|
|
3
|
+
npm install studyalways-itheima-tool-hwb-2026
|
|
4
|
+
```
|
|
5
|
+
|
|
6
|
+
## 导入
|
|
7
|
+
```js
|
|
8
|
+
const itheima = require('itheima-tools-hwb')
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 格式化时间
|
|
12
|
+
```js
|
|
13
|
+
// 调用 dateFormat 对时间进行格式化
|
|
14
|
+
const dtStr = itheima.dateFormat(new Date())
|
|
15
|
+
// 结果 2020-04-03 17:20:58
|
|
16
|
+
console.log(dtStr)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 转义 HTML 中的特殊字符
|
|
20
|
+
```js
|
|
21
|
+
// 带转换的 HTML 字符串
|
|
22
|
+
const htmlStr = '<h1 title="abc">这是h1标签<span>123 </span></h1>'
|
|
23
|
+
// 调用 htmlEscape 方法进行转换
|
|
24
|
+
const str = itheima.htmlEscape(htmlStr)
|
|
25
|
+
// 转换的结果 <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>
|
|
26
|
+
console.log(str)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 还原 HTML 中的特殊字符
|
|
30
|
+
```js
|
|
31
|
+
// 待还原的 HTML 字符串
|
|
32
|
+
const str2 = itheima.htmlUnEscape(str)
|
|
33
|
+
// 输出的结果 <h1 title="abc">这是h1标签<span>123 </span></h1>
|
|
34
|
+
console.log(str2)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 开源协议
|
|
38
|
+
ISC
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function dataformat(datastr){
|
|
2
|
+
const dt=new Date(datastr);
|
|
3
|
+
|
|
4
|
+
const y=padzeros(dt.getFullYear())
|
|
5
|
+
const m=padzeros(dt.getMonth()+1)
|
|
6
|
+
const d=padzeros(dt.getDate())
|
|
7
|
+
|
|
8
|
+
const hh=padzeros(dt.getHours())
|
|
9
|
+
const mm=padzeros(dt.getMinutes())
|
|
10
|
+
const ss=padzeros(dt.getSeconds())
|
|
11
|
+
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function padzeros(n){
|
|
15
|
+
return n>9 ? n:'0'+n
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports={dataformat}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function htmlEscape(htmlstr){
|
|
2
|
+
return htmlstr.replace(/<|>|"|&/g,(match)=>{
|
|
3
|
+
switch(match){
|
|
4
|
+
case '<':
|
|
5
|
+
return '<'
|
|
6
|
+
case '>':
|
|
7
|
+
return '>'
|
|
8
|
+
case '"':
|
|
9
|
+
return '"'
|
|
10
|
+
case '&':
|
|
11
|
+
return '&'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function htmlUnEscape(htmlstr){
|
|
17
|
+
return htmlstr.replace(/<|>|"|&/g,(match)=>{
|
|
18
|
+
switch(match){
|
|
19
|
+
case '<':
|
|
20
|
+
return '<'
|
|
21
|
+
case '>':
|
|
22
|
+
return '>'
|
|
23
|
+
case '"':
|
|
24
|
+
return '"'
|
|
25
|
+
case '&':
|
|
26
|
+
return '&'
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {htmlEscape,htmlUnEscape}
|