本文将介绍lodash-super
开源库。
这是一个?lodash
?加强版的工具库,因为lodash
?只提供了最基础的一些工具方法,当我们在业务上使用时,往往需要对其进行再次封装,比较麻烦。
所以,该库提供了一个在 业务场景中?更加?通用的工具库。
希望对你有所帮助!
希望大家点个star
,或者一同共建。
npm install lodash-super
?或?yarn add lodash-super
对数值 进行进位 转换
const field = _S.formatUnit(1048576)
// => {size:1.00,unit'M'}
const field2 = _S.formatUnit(1048576,'KB')
// => {size:1.00,unit'G'}
const field3 = _S.formatUnit(1000000,'KB',2,1000)
// => 按1000进位 {size:1.00,unit'G'}
对数值进行显示简化
const field = _S.formatUnit(1048576)
// => {size:1,unit:'M'}
const field2 = _S.formatUnit(1048576,'KB')
// => {size:1,unit:'G'}
const field3 = _S.formatUnit(1000000,'KB',2,1000)
// => 按1000进位 {size:1,unit'G'}
对数值 进行进位 转换
const field = _S.formatUnitChange(2000)
// => 1.953
const field2 = _S.formatUnitChange(2,'KB','B')
// => 2048
对时间进行显示简化
const field = _S.formatUnitT(1000)
// => {size:1,unit:'s'}
const field2 = _S.formatUnitT(60,'s')
// => {size:1,unit:'m'}
对时间 进行进位 转换
const field = _S.formatUnitChangeT(2000)
// => 2
const field2 = _S.formatUnitChangeT(2,'m','s')
// => 120
随机生成指定位数和范围的字符串
const field = _S.randomStr()
// => efcww
判断 对象数组内是否有相同key 的value 值
const flag = repeatKey([{ name: 1,} { name: 1 }])
// => true
const flag = repeatKey([{ name1: { name: 1 } }, { name1: { name: 1 } }, { name1: '1' }], ['name1', 'name'])
// => true
过滤出 对象数组内,满足?comparator
?条件并为?true
?的对象,同时也对对象内的?children
?有效。
如果?
children
满足条件,则对应的父级也会被保留?comparator
?也接收一个字符串,对象拥有这个属性会被保留
const field1 = filterField([{ name: 1,children:[{name:1}]} { name: 2 }],(obj) => obj.name ===1)
// => [{name:1,children:[{name:1}}]
const field2 = filterField([{ name: 2 }, { name:3,children:[{ name: 1 }] }, (obj) => obj.name ===1])
// => [{name:3,children:[{name:1}}]
const field1 = filterField([{ name: 1} { name1: 2 }],'name')
// => [{name:1}]