Functional utils for your flat mapping needs.
npm i flatmap-fns
import insert from 'flatmap-fns/insert'
const res = ['foo', 'bar', 'baz'].flatMap(insert('foo', 1))
console.log(res) // ['foo', 'foo', 'bar', 'baz']
import keep from 'flatmap-fns/keep'
const resA = ['foo', 'bar', 'baz'].flatMap(keep(1))
console.log(resB) // ['bar']
const resB = ['foo', 'bar', 'baz'].flatMap(keep(1, 2))
console.log(resB) // ['bar', 'baz']
import key from 'flatmap-fns/key'
const res = [{ id: 'foo' }, { id: 'bar' }].flatMap(key('id'))
console.log(res) // ['foo', 'bar']
import length from 'flatmap-fns/length'
const res = ['foo', ['bar', 'baz']].flatMap(length())
console.log(res) // [3, 2]
import remove from 'flatmap-fns/remove'
const resA = ['foo', 'bar', 'baz'].flatMap(remove(1))
console.log(resA) // ['foo', 'baz']
const resB = ['foo', 'bar', 'baz'].flatMap(remove(0, 1))
console.log(resB) // ['baz']
import replace from 'flatmap-fns/replace'
const res = ['foo', 'bar', 'baz'].flatMap(replace(1, 'foo'))
console.log(res) // ['foo', 'foo', 'baz']
import resize from 'flatmap-fns/resize'
const resA = [1].flatMap(resize(3))
console.log(resA) // [1, undefined, undefined]
const resB = [1, 2, 3, 4, 5].flatMap(resize(3))
console.log(resB) // [1, 2, 3]
import rotate from 'flatmap-fns/rotate'
const resA = [1, 2, 3].flatMap(rotate())
console.log(resA) // [3, 1, 2] - default is to rotate right by one step.
const resB = [1, 2, 3].flatMap(rotate({ direction: 'left', steps: 2 }))
console.log(resB) // [3, 1, 2]
import shuffle from 'flatmap-fns/shuffle'
const res = [1, 2, 3, 4, 5].flatMap(shuffle())
console.log(res) // will be shuffled, eg, [5, 2, 1, 4, 3]
import sum from 'flatmap-fns/sum'
const res = [1, 2, 3].flatMap(sum())
console.log(res) // [6]
import transpose from 'flatmap-fns/transpose'
const res = ['foo', 'bar', 'baz'].flatMap(transpose(1, 2))
console.log(res) // ['foo', 'baz', 'bar']
import truthy from 'flatmap-fns/truthy'
const res = ['foo', undefined, 'baz', null].flatMap(truthy())
console.log(res) // ['foo', 'baz']
import uniq from 'flatmap-fns/uniq'
const res = ['foo', 'foo', 'bar', 1, 1].flatMap(uniq())
console.log(res) // ['foo', 'bar', 1]