1.0.7 • Published 2 years ago
code-tools-zjp v1.0.7
安装
npm install code-tools-zjp
_
导入
const tools = require('code-tools')
局部导入
cosnt { dtStr } = require('code-tools')
_
日期
格式化时间
// 对时间格式化 可自定义
tools.formatDateTime('2023-01-01 14:30:22', 'YYYY-MM-DD')
// console.log(2023-01-01)
tools.formatDateTime('2023-01-01 14:30:22', 'YYYY-MM-DD hh:mm:ss')
// console.log(2023-01-01)
tools.formatDateTime('2023-01-01 14:30:22', 'YYYY hh:mm:ss')
// console.log(2023 14:30:22)
计算开始和结束时间间隔 默认day
tools.getDateInterval('2023-01-01', '2024-01-01')
// console.log(365)
tools.getDateInterval('2023-01-01', '2024-04-01', 'month')
// console.log(15)
tools.getDateInterval('2023-01-01', '2024-04-01', 'year')
// console.log(1)
计算开始日期和结束日期xxxx年xx月xx日
tools.calculateDateDifference('2023-01-01', '2024-04-02')
// console.log(1年3个月1天)
tools.calculateDateDifference('2023-01-01', '2024-04-01')
// console.log(1年3个月)
/** 自定义写法 */
tools.calculateDateDifference(
'2023-01-01',
'2024-04-02',
(y, m, d) => { return `${y}很好年${m}很好月${d}很好天` }
)
// console.log(1很好年3很好月1很好天)
_
#转义html
转义 html
tools.htmlEscaper('<h1>123</h1>123123123')
// console.console.log(<h1>123</h1>123123123);
_
还原html
tools.htmlUnEscaper('<h1>123</h1>123123123')
// console.log(<h1>123</h1>123123123)
数字
数字转换为大写金额
tools.numToCny(123)
// console.log(壹佰贰拾叁元整)
数字转换成千分位
tools.numberWithCommas(122223.444)
// console.log(122,223.444)
_
对象
排除对象某个值
let obj = {
a: '123',
b: '456',
b: '456'
}
tools.excludeValue(obj, '456')
// console.log({a: '123'})
排除对象某个键值
let obj = {
a: '123',
b: '456',
b: '456'
}
tools.excludeKey(obj, 'a')
// console.log({ b: '456', b: '456'})
让对象按照字母顺序排序
let obj = {
aa: '123',
bb: '123',
oo: '33333',
uu: 'qewqwewqe',
cc: '123123'
}
console.log(tools.sortObjectKeys(obj))
// { aa: '123', bb: '123', cc: '123123', oo: '33333', uu: 'qewqwewqe' }
_
数组
数组分组
let array = [
{ type: 'name', value: '12312' },
{ type: 'age', value: 'valuedd' },
{ type: 'name', value: '12asd' },
]
console.log(tools.groupBy(array, 'type'))
// {
// name: [
// { type: 'name', value: '12312' },
// { type: 'name', value: '12asd' }
// ],
// age: [ { type: 'age', value: 'valuedd' } ]
// }