Count the number of items in an iterator
import { count } from 'goodish'
console.log(count([0, 1, 2, 3, 4])) // 5
A generator which yields all of the permutations of an array
import { permutationsOf } from 'goodish'
console.log(Array.from(permutationsOf([0, 1, 2, 3, 4]))
Returns a tuple containing a random element from a collection and the index of that element in the collection.
import { randomFrom, setSeed } from 'goodish'
const arr = [0, 1, 2, 3, 4]
console.log(randomFrom(arr)) // any single element of arr. will be different each time this code runs
setSeed(1)
console.log(randomFrom(arr)) // always the same element of arr
Returns an array of N randomly selected elements from the supplied array
import { randomNFrom, setSeed } from 'goodish'
const arr = 'abcdefghijklmnopqrstuvwxyz0123456789'
console.log(randomNFrom(arr, 10)) // a random alphanumeric array with 10 characters
setSeed(1)
console.log(randomNFrom(arr, 10).join('')) // always the same alphanumeric string
A generator which yields each value between the min and max values
import { range } from 'goodish'
for (const n of range(0, 5)) {
console.log(n) // 0, 1, 2, 3, 4
}
Returns an array of values between the min and max values.
import { rangeArr } from 'goodish'
const arr = rangeArr(0, 5) // [0, 1, 2, 3, 4, 5]
Shuffle an array in place.
import { shuffle, setSeed } from 'goodish'
const arr = [0, 1, 2, 3, 4]
console.log(shuffle(arr.slice())) // a different array order each time this code is run
setSeed(1)
console.log(shuffle(arr.slice()) // a different array order, but will be the same each time the code is run
Swap two indexes of an array
import { swap } from 'goodish'
const arr = [0, 1, 2]
swap(1, 2)
console.log(arr) // [0, 2, 1]
Transpose a 2D array.
import { transpose } from 'goodish'
console.log(transpose([[0], [1], [2]])) // [[0, 1, 2]]
Generated using TypeDoc
A generator which yields all of the combinations of N items from an array.
import { combinationsOf } from 'goodish' console.log(Array.from(combinationsOf([0, 1, 2], 2)) // [[0, 1], [0, 2], [1, 2]]
Copied verbatim from this site