tamda
Version:
Practical functional programming library for TypeScript
17 lines • 614 B
JavaScript
import { infer } from '../function/infer';
export function flatMap() {
return inferred.apply(undefined, arguments);
}
const inferred = infer((array, mapFn) => {
const mapOrEmpty = (item, index) => {
const mapped = mapFn(item, index, array);
return mapped instanceof Array ? mapped : [];
};
return array.reduce((flatten, item, index) => {
// Faster than spreading, safe here
// i.e. [ ...flatten, ...mapOrEmpty(item, index) ]
flatten.push(...mapOrEmpty(item, index));
return flatten;
}, []);
});
//# sourceMappingURL=flatMap.js.map