cluster
Divides an array into many arrays of the given size
114 bytes
since v12.1.0
Usage
Takes an array and divides it into many arrays of the given size. The last array may be smaller than the given size, if not enough items are left.
import * as import _
_ from 'radashi'
const const gods: string[]
gods = [ 'Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor', 'Apollo', 'Artemis', 'Athena',]
const const clustered: [string, string, string][]
clustered = import _
_.function cluster<string, 3>(array: readonly string[], size: 3): [string, string, string][]
Splits a single list into many lists of the desired size.
cluster(const gods: string[]
gods, 3)// => [// ['Ra', 'Zeus', 'Loki'],// ['Vishnu', 'Icarus', 'Osiris'],// ['Thor', 'Apollo', 'Artemis'],// ['Athena']// ]Type Inference
The cluster function provides precise type inference for common cluster sizes (1-8):
// With explicit size parameterconst result1 = _.cluster(['a', 'b', 'c'], 1)// ^? [string][]
const result2 = _.cluster(['a', 'b', 'c', 'd'], 2)// ^? [string, string][]
const result3 = _.cluster(['a', 'b', 'c', 'd', 'e', 'f'], 3)// ^? [string, string, string][]
// Using default size parameter (2)const defaultSize = _.cluster(['a', 'b', 'c', 'd'])// ^? [string, string][]
// For sizes > 8, falls back to string[][]const largeSize = _.cluster(['a', 'b', 'c', 'd'], 10)// ^? string[][]
const veryLargeSize = _.cluster(['a', 'b', 'c', 'd'], 10000)// ^? string[][]