MCPcopy Create free account
hub / github.com/antfu/utils / partition

Function partition

src/array.ts:46–61  ·  view source on GitHub ↗
(array: readonly T[], ...filters: PartitionFilter<T>[])

Source from the content-addressed store, hash-verified

44export function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[]]
45export function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>, f6: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[], T[]]
46export function partition<T>(array: readonly T[], ...filters: PartitionFilter<T>[]): any {
47 const result: T[][] = Array.from({ length: filters.length + 1 }).fill(null).map(() => [])
48
49 array.forEach((e, idx, arr) => {
50 let i = 0
51 for (const filter of filters) {
52 if (filter(e, idx, arr)) {
53 result[i].push(e)
54 return
55 }
56 i += 1
57 }
58 result[i].push(e)
59 })
60 return result
61}
62
63/**
64 * Unique an Array

Callers 1

array.test.tsFile · 0.90

Calls 2

mapMethod · 0.80
forEachMethod · 0.80

Tested by

no test coverage detected