itiheima-cgbb
v1.0.0
Published
itiheima-cgbb
Readme
itiheima-cgbb
一个轻量级的工具函数库,提供日期格式化和 HTML 转义功能。
安装
npm install itiheima-cgbb使用方法
const { formatTime, htmlescape, htmlUnescape } = require('itiheima-cgbb');
// 日期格式化
const now = new Date();
const formattedTime = formatTime(now);
console.log(formattedTime); // 输出: 2024-01-01 12:00:00
// HTML 转义
const htmlStr = '<div>Hello "World"</div>';
const escapedStr = htmlescape(htmlStr);
console.log(escapedStr); // 输出: <div>Hello "World"</div>
// HTML 反转义
const unescapedStr = htmlUnescape(escapedStr);
console.log(unescapedStr); // 输出: <div>Hello "World"</div>API
formatTime(time)
格式化 Date 对象为字符串。
参数:
time{Date} - Date 对象
返回值:
- {string} - 格式化后的时间字符串,格式为
YYYY-MM-DD HH:mm:ss
htmlescape(str)
转义 HTML 特殊字符。
参数:
str{string} - 需要转义的字符串
返回值:
- {string} - 转义后的字符串
转义规则:
&→&<→<>→>"→"'→'
htmlUnescape(str)
反转义 HTML 特殊字符。
参数:
str{string} - 需要反转义的字符串
返回值:
- {string} - 反转义后的字符串
许可证
ISC
