Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {FilterNotNull} from "./filterNotNull";
import {First} from "./first";
import {FirstOrNull} from "./firstOrNull";
import {FlatMap} from "./flatMap";
import {FlatMapM} from "./flatMapM";
import {Flatten} from "./flatten";
import {Fold} from "./fold";
import {FoldIndexed} from "./foldIndexed";
Expand Down Expand Up @@ -87,7 +88,7 @@ export default Sequence;
* @hidden
*/
export interface SequenceOperators<T> extends All, Any, AsIterable, Associate, AssociateBy<T>, Average, Chunk, Contains, Count, Distinct, DistinctBy, Drop,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, Flatten, Fold, FoldIndexed,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, FlatMapM, Flatten, Fold, FoldIndexed,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedBy, SortedByDescending, SortedDescending, SortedWith,
Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip {
Expand All @@ -99,7 +100,7 @@ class SequenceImpl<T> {
}

applyMixins(SequenceImpl, [All, Any, AsIterable, Associate, AssociateBy, Average, Chunk, Contains, Count, Distinct, DistinctBy, Drop,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, Flatten, Fold, FoldIndexed,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, FlatMapM, Flatten, Fold, FoldIndexed,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedBy, SortedByDescending, SortedDescending, SortedWith,
Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip]);
Expand Down
23 changes: 23 additions & 0 deletions src/flatMapM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Sequence, { asSequence } from "./Sequence";

export class FlatMapM {
/**
* Transforms each element into an iterable of items and returns a flat single sequence of all those items. The M stands for Monadic
*
* @param {(value: S) => Iterable<T>} transform
* @returns {Sequence<T>}
* @example
* asSequence(document.querySelectorAll("form"))
* .flatMapM(form => form.querySelectorAll("input"))
* // same as
* asSequence(document.querySelectorAll("form"))
* .map(form => form.querySelectorAll("input"))
* .flatMap(asSequence)
*
*
*/
flatMapM<S, T>(this: Sequence<S>, transform: (value: S) => Iterable<T>): Sequence<T> {
return this.flatMap(value => asSequence(transform(value)));
}

}
32 changes: 32 additions & 0 deletions test/flatMapM.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { asSequence, sequenceOf } from "../src/Sequence";

describe("flatMapM", () => {
it("should flatten element arrays", () => {
const array = sequenceOf([1, 2], [3, 4], [5, 6])
.flatMapM(it => it)
.toArray();

expect(array.length).toBe(6);
expect(array[0]).toBe(1);
expect(array[1]).toBe(2);
expect(array[2]).toBe(3);
expect(array[3]).toBe(4);
expect(array[4]).toBe(5);
expect(array[5]).toBe(6);
});

it("should flatten iterable iterators", () => {
const array = asSequence([
new Map([["a", 1], ["c", 3]]),
new Map([["b", 2], ["d", 4]]),
])
.flatMapM(map => map.values())
.toArray();

expect(array.length).toBe(4);
expect(array[0]).toBe(1);
expect(array[1]).toBe(3);
expect(array[2]).toBe(2);
expect(array[3]).toBe(4);
});
});