正在替代 lodash 的新JS工具, 旧的 lodash 语法太老, 新的 radash 集合了 ES6 语法, 效率更高
安装 :
npm i radash
常用API整理 :
数组 API :
1. 数组排序 alphabetical
按字母顺序, 正序倒序排列
import { alphabetical } from 'radash'const gods = [{name: 'Ra',power: 100},{name: 'Zeus',power: 98},{name: 'Loki',power: 72},{name: 'Vishnu',power: 100}
]alphabetical(gods, g => g.name) // => [Loki, Ra, Vishnu, Zeus]
alphabetical(gods, g => g.name, 'desc') // => [Zeus, Vishnu, Ra, Loki]
2. 取数组最大最小值 boil
import { boil } from 'radash'const gods = [{name: 'Ra',power: 100},{name: 'Zeus',power: 98},{name: 'Loki',power: 72}
]boil(gods, (a, b) => (a.power > b.power ? a : b))
// => { name: 'Ra', power: 100 }
3. 拆分指定长度数组, 组成二维数组 cluster
import { cluster } from 'radash'const gods = ['Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor', 'Apollo', 'Artemis', 'Athena']cluster(gods, 3)
// => [
// [ 'Ra', 'Zeus', 'Loki' ],
// [ 'Vishnu', 'Icarus', 'Osiris' ],
// ['Thor', 'Apollo', 'Artemis'],
// ['Athena']
// ]
4. 统计数组元素出现的次数 counting
import { counting } from 'radash'const gods = [{name: 'Ra',culture: 'egypt'},{name: 'Zeus',culture: 'greek'},{name: 'Loki',culture: 'greek'}
]counting(gods, g => g.culture) // => { egypt: 1, greek: 2 }
5. 获取数组的第一个元素, 如果不存在任何项目,则返回第一个项目或默认值 first
import { first } from 'radash'const gods = ['ra', 'loki', 'zeus']first(gods) // => 'ra'
first([], 'vishnu') // => 'vishnu'
6. 展平多维数组 flat
import { flat } from 'radash'const gods = [['ra', 'loki'], ['zeus']]flat(gods) // => [ra, loki, zeus]
7. 对数组对象以某个元素进行分组 group
import { group } from 'radash'const fish = [{name: 'Marlin',source: 'ocean'},{name: 'Bass',source: 'lake'},{name: 'Trout',source: 'lake'}
]const fishBySource = group(fish, f => f.source)// 输出 fishBySource
{"ocean": [{"name": "Marlin","source": "ocean"}],"lake": [{"name": "Bass","source": "lake"},{"name": "Trout","source": "lake"}]
}
8. 判断两个数组中元素是否有相同 intersects
import { intersects } from 'radash'const oceanFish = ['tuna', 'tarpon']
const lakeFish = ['bass', 'trout']intersects(oceanFish, lakeFish) // => falseconst brackishFish = ['tarpon', 'snook']intersects(oceanFish, brackishFish) // => true
9. 获取数组最后一个元素 ,如果不存在任何项目,则返回最后一个项目或默认值 last
import { last } from 'radash'const fish = ['marlin', 'bass', 'trout']const lastFish = last(fish) // => 'trout'
const lastItem = last([], 'bass') // => 'bass'
9. 返回数组中最大值 max
import { max } from 'radash'const fish = [{name: 'Marlin',weight: 105,source: 'ocean'},{name: 'Bass',weight: 8,source: 'lake'},{name: 'Trout',weight: 13,source: 'lake'}
]max(fish, f => f.weight) // => {name: "Marlin", weight: 105, source: "ocean"}
10. 合并数组对象, 并覆盖相同 key 值的对象 merge
import { merge } from 'radash'const gods = [{name: 'Zeus',power: 92},{name: 'Ra',power: 97}
]const newGods = [{name: 'Zeus',power: 100}
]merge(gods, newGods, f => f.name)
// => [{name: "Zeus" power: 100}, {name: "Ra", power: 97}]
11. 返回数组中最小值 min
import { min } from 'radash'const fish = [{name: 'Marlin',weight: 105,source: 'ocean'},{name: 'Bass',weight: 8,source: 'lake'},{name: 'Trout',weight: 13,source: 'lake'}
]min(fish, f => f.weight) // => {name: "Bass", weight: 8, source: "lake"}
12. 替换数组元素, 仅替换第一个匹配项 replace
import { replace } from 'radash'const fish = [{name: 'Marlin',weight: 105},{name: 'Bass',weight: 8},{name: 'Trout',weight: 13}
]const salmon = {name: 'Salmon',weight: 22
}// read: replace fish with salmon where the name is Bass
replace(fish, salmon, f => f.name === 'Bass')// 输出
[{"name": "Marlin","weight": 105},{"name": "Salmon","weight": 22},{"name": "Trout","weight": 13}
]
13. 数组元素整体向右移动 n 个位置 shift
import { shift } from 'radash'
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
shift(arr, 3) // => [7, 8, 9, 1, 2, 3, 4, 5, 6]
14. 过滤出数组中虚假的元素 sift
import { sift } from 'radash'const fish = ['salmon', null, false, NaN, 'sockeye', 'bass']sift(fish) // => ['salmon', 'sockeye', 'bass']
15. 数组排序, 按照数字大小正序倒序排列 sort
import { sort } from 'radash'const fish = [{name: 'Marlin',weight: 105},{name: 'Bass',weight: 8},{name: 'Trout',weight: 13}
]sort(fish, f => f.weight) // => [bass, trout, marlin]
sort(fish, f => f.weight, true) // => [marlin, trout, bass]
16. 数组元素求和 sum
import { sum } from 'radash'const fish = [{name: 'Marlin',weight: 100},{name: 'Bass',weight: 10},{name: 'Trout',weight: 15}
]sum(fish, f => f.weight) // => 125
17. 数组删除和添加元素, 已包含则删除, 未包含则添加 toggle
import { toggle } from 'radash'const gods = ['ra', 'zeus', 'loki']toggle(gods, 'ra') // => [zeus, loki]
toggle(gods, 'vishnu') // => [ra, zeus, loki, vishnu]
18. 数组去重 unique
import { unique } from 'radash'const fish = [{name: 'Marlin',weight: 105,source: 'ocean'},{name: 'Salmon',weight: 22,source: 'river'},{name: 'Salmon',weight: 22,source: 'river'}
]unique( fish, f => f.name )
// [
// { name: 'Marlin', weight: 105, source: 'ocean' },
// { name: 'Salmon', weight: 22, source: 'river' }
// ]
19. 两个数组合并成一个对象 zipToObject
import { zipToObject } from 'radash'const names = ['ra', 'zeus', 'loki']
const cultures = ['egypt', 'greek', 'norse']zipToObject(names, cultures)
// => { ra: egypt, zeus: greek, loki: norse }zipToObject(names, (k, i) => k + i)
// => { ra: ra0, zeus: zeus1, loki: loki2 }zipToObject(names, null)
// => { ra: null, zeus: null, loki: null }
20. 两个数组压缩成一个二维数组 zip
import { zip } from 'radash'const names = ['ra', 'zeus', 'loki']
const cultures = ['egypt', 'greek', 'norse']zip(names, cultures)
// => [
// [ra, egypt]
// [zeus, greek]
// [loki, norse]
// ]
21. 获取数组随机元素 draw
import { draw } from 'radash'const fish = ['marlin', 'bass', 'trout']draw(fish) // => 随机值
22. 重新随机排列数组元素顺序返回 shuffle
import { shuffle } from 'radash'const fish = [{name: 'Marlin',weight: 105,source: 'ocean'},{name: 'Salmon',weight: 22,source: 'river'},{name: 'Salmon',weight: 22,source: 'river'}
]shuffle(fish)
异步 API :
1. 将多个接口请求返回数据整合成一个数组 或 对象, 统一返回 all
// 将数组作为参数传递将以相同的顺序将解析的 promise 值作为数组返回
import { all } from 'radash'const [user] = await all([api.users.create(...),s3.buckets.create(...),slack.customerSuccessChannel.sendMessage(...)
])// 将对象作为参数传递将返回一个对象,该对象具有与解析的 promise 值相同的键和值
import { all } from 'radash'const { user } = await all({user: api.users.create(...),bucket: s3.buckets.create(...),message: slack.customerSuccessChannel.sendMessage(...)
})
2. 不断重复请求, 可设置间隔时间, 直到请求成功或达到指定的最大重试次数 retry
import { retry } from 'radash'; await retry({}, api.users.list)
await retry({ times: 10 }, api.users.list)await retry({ times: 2, delay: 1000 }, api.articles.list);// 在这个例子中,retry() 函数尝试执行 api.articles.list 异步操作,如果操作失败,它会等待 1000 毫秒(即 1 秒)后重试,最多重试 2 次。// 可以使用 retry() 函数来替代传统的异步重试库,因为它提供了更加简洁和灵活的接口。结合 Radash 的
// 其他功能,如 tryit、parallel 等,可以轻松构建出高效且健壮的异步处理逻辑,以应对后端服务的各种
// 不确定性。无论是处理网络请求、数据库操作还是其他异步任务,retry() 函数都能提供强大的容错能力,
// 确保应用程序的稳定性和可靠性
防抖 debounce :
import { debounce } from 'radash'// 请求接口
const getUser = async () => {const { data } = await infUser()
}// 点击按钮
const handleTest = debounce({ delay: 2000 }, () => {// delay: 2000 间隔时间 毫秒 才请求getUser()
})
节流 throttle :
import { throttle } from 'radash'// 请求接口
const getUser = async () => {const { data } = await infUser()
}const jieLiu = throttle({ interval: 2000 }, () => {// interval: 2000 间隔 2000 毫秒内只请求 1 次getUser ()
})
获取随机数字 random :
import { random } from 'radash'random(0, 100) // => 0 到 100 之间随机数字
对象 API :
1. 合并两个对象 assign
import { assign } from 'radash'const ra = {name: 'Ra',power: 100
}assign(ra, { name: 'Loki' })
// => { name: Loki, power: 100 }
2. 浅拷贝 clone
import { clone } from 'radash'const ra = {name: 'Ra',power: 100
}const gods = [{name: 'Ra',power: 100}
]clone(ra) // => copy of ra
clone(gods) // => copy of gods
3. 对象 key 和 value 颠倒 invert
import { invert } from 'radash'const powersByGod = {ra: 'sun',loki: 'tricks',zeus: 'lighning'
}invert(gods) // => { sun: ra, tricks: loki, lightning: zeus }
4. 获取对象所有 key 值 keys
import { keys } from 'radash'const ra = {name: 'ra',power: 100,friend: {name: 'loki',power: 80},enemies: [{name: 'hathor',power: 12}]
}keys(ra)
// => [
// 'name',
// 'power',
// 'friend.name',
// 'friend.power',
// 'enemies.0.name',
// 'enemies.0.power'
// ]
5. 返回提取对象中指定元素, 组成新对象 pick
import { pick } from 'radash'const fish = {name: 'Bass',weight: 8,source: 'lake',barckish: false
}pick(fish, ['name', 'source'])// 输出
{"name": "Bass","source": "lake"
}
6. 将对象所有 key 转为大写并返回 upperize
import { upperize } from 'radash'const ra = {Mode: 'god',Power: 'sun'
}upperize(ra)// 输出
{"MODE": "god","POWER": "sun"
}
7. 将对象所有 key 转为小写并返回 lowerize
import { lowerize } from 'radash'const ra = {Mode: 'god',Power: 'sun'
}// 输出
{"mode": "god","power": "sun"
}
字符串 API :
1. 将一串字符串以驼峰大小写格式返回 camel
import { camel } from 'radash'camel('green fish blue fish') // => greenFishBlueFish
判断类型 API :
1. 是否数组 isArray
import { isArray } from 'radash'isArray('hello') // => false
isArray(['hello']) // => true
2. 是否日期 isDate
import { isDate } from 'radash'isDate(new Date()) // => true
isDate(12) // => false
isDate('hello') // => false
3. 是否为空 isEmpty
import { isEmpty } from 'radash'isEmpty([]) // => true
isEmpty('') // => true
isEmpty(null) // => true
isEmpty({}) // => true
isEmpty(0) // => true
isEmpty(NaN) // => true
isEmpty(undefined) // => trueisEmpty('hello') // => false
isEmpty(['hello']) // => false
4. 是否相等 isEqual
import { isEqual } from 'radash'isEqual(null, null) // => true
isEqual([], []) // => trueisEqual('hello', 'world') // => false
isEqual(22, 'abc') // => false
5. 是否浮点数 isFloat
import { isFloat } from 'radash'isFloat(12.233) // => true
isFloat(12) // => false
isFloat('hello') // => false
6. 是否函数 isFunction
import { isFunction } from 'radash'isFunction('hello') // => false
isFunction(['hello']) // => false
isFunction(() => 'hello') // => true
7. 是否数字 isNumber
import { isNumber } from 'radash'isNumber('hello') // => false
isNumber(['hello']) // => false
isNumber(12) // => true
8. 是否对象 isObject
import { isObject } from 'radash'isObject('hello') // => false
isObject(['hello']) // => false
isObject(null) // => false
isObject({ say: 'hello' }) // => true
9. 是否字符串 isString
import { isString } from 'radash'isString('hello') // => true
isString(['hello']) // => false