tips:点赞 + 收藏 = 学会!
Array
所有方法的思维导图;desc
字符则表示降序排序);import { alphabetical } from 'radash'
const ig = [
{
name: 'ning',
power: 100
},
{
name: 'rookie',
power: 98
},
{
name: 'jkl',
power: 95
},
{
name: 'theshy',
power: 100
}
]
// 这里输出的依然是对象数组,这里简单表示
alphabetical(ig, g => g.name) // => [jkl, ning, rookie, theshy]
alphabetical(ig, g => g.name, 'desc') // => [theshy, rookie, ning, jkl]
import { boil } from 'radash'
const rng = [
{
name: 'Uzi',
power: 100
},
{
name: 'Xiaohu',
power: 98
},
{
name: 'Ming',
power: 72
}
]
boil(gods, (a, b) => (a.power > b.power ? a : b)) // => { name: 'Uzi', power: 100 }
boil(gods, (a, b) => (a.power < b.power ? a : b)) // => { name: 'Ming', power: 72 }
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']
// ]
import { counting } from 'radash'
const skt = [
{
name: 'Ra',
culture: 'egypt'
},
{
name: 'Zeus',
culture: 'greek'
},
{
name: 'Loki',
culture: 'greek'
}
]
counting(gods, g => g.culture) // => { egypt: 1, greek: 2 }
import { diff } from 'radash'
const oldWorldGods = ['rng', 'uzi']
const newWorldGods = ['vishnu', 'uzi']
diff(oldWorldGods, newWorldGods) // => ['rng']
import { first } from 'radash'
const gods = ['lufee', 'loki', 'zeus']
first(gods) // => 'lufee'
first([], 'zuoluo') // => 'zuoluo'
使用说明
使用代码示例
import { flat } from 'radash'
const gods = [['shy', 'ning'], ['jkl']]
flat(gods) // => [shy, ning, jkl]
import { fork } from 'radash'
const gods = [
{
name: 'Uzi',
power: 100
},
{
name: 'Xiaohu',
power: 98
},
{
name: 'Ming',
power: 72
},
{
name: 'Mlxg',
power: 100
}
]
const [finalGods, lesserGods] = fork(gods, f => f.power > 90) // [[Uzi, Xiaohu, Mlxg], [Ming]]
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) // => { ocean: [marlin], lake: [bass, trout] }
import { intersects } from 'radash'
const oceanFish = ['tuna', 'tarpon']
const lakeFish = ['bass', 'trout']
intersects(oceanFish, lakeFish) // => false
const brackishFish = ['tarpon', 'snook']
intersects(oceanFish, brackishFish) // => true
import { iterate } from 'radash'
const value = iterate(
4,
(acc, idx) => {
return acc + idx
},
0
) // => 10
import { last } from 'radash'
const fish = ['marlin', 'bass', 'trout']
const lastFish = last(fish) // => 'trout'
const lastItem = last([], 'bass') // => 'bass'
import { list } from 'radash'
list(3) // [0, 1, 2, 3]
list(0, 3) // [0, 1, 2, 3]
list(0, 3, 'y') // [y, y, y, y]
list(0, 3, () => 'y') // [y, y, y, y]
list(0, 3, i => i) // [0, 1, 2, 3]
list(0, 3, i => `y${i}`) // [y0, y1, y2, y3]
list(0, 3, obj) // [obj, obj, obj, obj]
list(0, 6, i => i, 2) // [0, 2, 4, 6]
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"}
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}]
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"}
import { objectify } from 'radash'
const fish = [
{
name: 'Marlin',
weight: 105
},
{
name: 'Bass',
weight: 8
},
{
name: 'Trout',
weight: 13
}
]
objectify(fish, f => f.name) // => { Marlin: [marlin object], Bass: [bass object], ... }
objectify(
fish,
f => f.name,
f => f.weight
) // => { Marlin: 105, Bass: 8, Trout: 13 }
import { range } from 'radash'
range(3) // yields 0, 1, 2, 3
range(0, 3) // yields 0, 1, 2, 3
range(0, 3, 'y') // yields y, y, y, y
range(0, 3, () => 'y') // yields y, y, y, y
range(0, 3, i => i) // yields 0, 1, 2, 3
range(0, 3, i => `y${i}`) // yields y0, y1, y2, y3
range(0, 3, obj) // yields obj, obj, obj, obj
range(0, 6, i => i, 2) // yields 0, 2, 4, 6
for (const i of range(0, 200, 10)) {
console.log(i) // => 0, 10, 20, 30 ... 190, 200
}
for (const i of range(0, 5)) {
console.log(i) // => 0, 1, 2, 3, 4, 5
}
import { replaceOrAppend } from 'radash'
const fish = [
{
name: 'Marlin',
weight: 105
},
{
name: 'Salmon',
weight: 19
},
{
name: 'Trout',
weight: 13
}
]
const salmon = {
name: 'Salmon',
weight: 22
}
const sockeye = {
name: 'Sockeye',
weight: 8
}
replaceOrAppend(fish, salmon, f => f.name === 'Salmon') // => [marlin, salmon (weight:22), trout]
replaceOrAppend(fish, sockeye, f => f.name === 'Sockeye') // => [marlin, salmon, trout, sockeye]
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') // => [marlin, salmon, trout]
import { select } from 'radash'
const fish = [
{
name: 'Marlin',
weight: 105,
source: 'ocean'
},
{
name: 'Bass',
weight: 8,
source: 'lake'
},
{
name: 'Trout',
weight: 13,
source: 'lake'
}
]
select(
fish,
f => f.weight,
f => f.source === 'lake'
) // => [8, 13]
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]
import { sift } from 'radash'
const fish = ['salmon', null, false, NaN, 'sockeye', 'bass']
sift(fish) // => ['salmon', 'sockeye', 'bass']
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]
import { sum } from 'radash'
const fish = [
{
name: 'Marlin',
weight: 100
},
{
name: 'Bass',
weight: 10
},
{
name: 'Trout',
weight: 15
}
]
sum(fish, f => f.weight) // => 125
import { toggle } from 'radash'
// 基本用法
const gods = ['ra', 'zeus', 'loki']
toggle(gods, 'ra') // => [zeus, loki]
toggle(gods, 'vishnu') // => [ra, zeus, loki, vishnu]
// 切换(数组、条件项、指定标识符的条件函数)
import { toggle } from 'radash'
const ra = { name: 'Ra' }
const zeus = { name: 'Zeus' }
const loki = { name: 'Loki' }
const vishnu = { name: 'Vishnu' }
const gods = [ra, zeus, loki]
toggle(gods, ra, g => g.name) // => [zeus, loki]
toggle(gods, vishnu, g => g.name) // => [ra, zeus, loki, vishnu]
// 切换(数组、条件项、条件函数,覆盖项)
import { toggle } from 'radash'
const gods = ['ra', 'zeus', 'loki']
toggle(gods, 'vishnu', g => g, { strategy: 'prepend' }) // => [vishnu, ra, zeus, loki]
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' }
// ]
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 }
使用说明
使用代码示例
import { zip } from 'radash'
const names = ['ra', 'zeus', 'loki']
const cultures = ['egypt', 'greek', 'norse']
zip(names, cultures)
// => [
// [ra, egypt]
// [zeus, greek]
// [loki, norse]
// ]
Radash
库所有方法更新完毕,作者会整理一份完整方法目录上传,包括思维导图和使用目录。目前为止,radash库的所有方法我们已经分享完毕。如果你想尝试使用,又或者想了解下源码,阿瓜的文章都值得一读,相信你总能有所收获。后续我们回整理一份使用说明进行发布。
或许你最近在某个地方听过或者看过 `radash` 这个词,它是一个typescript编写的方法库,无论你是想简单使用还是深入了解他的源码,本系列文章都值得一读。