common-methods-krm
v1.2.0
Published
提供了一些常用的js方法
Downloads
4
Readme
安装
npm install common-methods-krm
导入
const krm = require('common-methods-krm')
开源协议
ISC
方法
table: { getDataById: [Function: getDataById] }
tree: {
getNodeById: [Function: getNodeById],
getParentNodeById: [Function: getParentNodeById]
}
time: {
getCurrentTime: [Function: getCurrentTime],
getUniqueId: [Function: getUniqueId]
}
common: {
debounce: [Function: debounce],
throttle: [Function: throttle],
getTypeOf: [Function: getTypeOf],
fmoney: [Function: fmoney],
numberToChinese: [Function: numberToChinese],
amountToChinese: [Function: amountToChinese]
}
escape: {
htmlEscape: [Function: htmlEscape],
htmlUnEscape: [Function: htmlUnEscape]
}
根据id 返回表格对应信息
var tableData = [
{ id: 1, name: 'Alice', age: 25, city: 'New York' },
{ id: 2, name: 'Bob', age: 30, city: 'San Francisco' },
{ id: 3, name: 'Charlie', age: 28, city: 'Los Angeles' },
// 更多数据项...
];
var res = getDataById(tableData, 1,)
console.log(res); // { id: 1, name: 'Alice', age: 25, city: 'New York' }
根据id 返回树形结构当前节点信息
var treeData = [{
id: '1',
name: "Root",
children: [
{
id: '1.1',
name: "Node 1.1",
children: [
{
id: '1.1.1',
name: "Node 1.1.1",
// children: []
},
{
id: '1.1.2',
name: "Node 1.1.2",
// children: []
}
]
},
{
id: '1.2',
name: "Node 1.2",
children: [
{
id: '1.2.1',
name: "Node 1.2.1",
children: []
},
{
id: '1.2.2',
name: "Node 1.2.2",
children: []
}
]
}
]
}];
var res = getNodeById(treeData, '1.2.1');
console.log(res); // { id: '1.2.1', name: 'Node 1.2.1', children: [] }
根据id 返回树形结构父节点信息
var treeData = [...同上]
var res = getParentNodeById(treeData, '1.2.2');
console.log(res);
// 打印结果
// {
// id: '1.2',
// name: 'Node 1.2',
// children: [
// { id: '1.2.2', name: 'Node 1.2.2', children: [] },
// { id: '1.2.1', name: 'Node 1.2.1', children: [] }
// ]
// }
转义 HTML 中的特殊字符
// 带转换的 HTML 字符串
const htmlStr = '<h1 title="abc">这是h1标签<span>123 </span></h1>'
// 调用 htmlEscape 方法进行转换
const str = itheima.htmlEscape(htmlStr)
// 转换的结果 <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>
console.log(str)
还原 HTML 中的特殊字符
// 待还原的 HTML 字符串
const str2 = itheima.htmlUnEscape(str)
// 输出的结果 <h1 title="abc">这是h1标签<span>123 </span></h1>
console.log(str2)