Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isEmpty and isNotEmpty utility methods #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions src/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {GroupBy} from "./groupBy";
import {IndexOf} from "./indexOf";
import {IndexOfFirst} from "./indexOfFirst";
import {IndexOfLast} from "./indexOfLast";
import {IsEmpty} from "./isEmpty";
import {IsNotEmpty} from "./isNotEmpty";
import {JoinToString} from "./joinToString";
import {Last} from "./last";
import {LastOrNull} from "./lastOrNull";
Expand Down Expand Up @@ -88,7 +90,7 @@ export default Sequence;
*/
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,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, IsEmpty, IsNotEmpty, 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 @@ -100,7 +102,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,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, IsEmpty, IsNotEmpty, 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
12 changes: 12 additions & 0 deletions src/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Sequence from "./Sequence";

export class IsEmpty {
/**
* Returns `true` the sequence is empty
*
* @returns {boolean}
*/
isEmpty<T>(this: Sequence<T>) {
return !this.isNotEmpty();
}
}
12 changes: 12 additions & 0 deletions src/isNotEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Sequence from "./Sequence";

export class IsNotEmpty {
/**
* Returns `true` the sequence is not empty
*
* @returns {boolean}
*/
isNotEmpty<T>(this: Sequence<T>) {
return this.any(() => true);
}
}
19 changes: 19 additions & 0 deletions test/isEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { range, sequenceOf } from "../src/Sequence";

describe("isEmpty", () => {
it("returns false for infinite sequences", () => {
expect(range(0, Infinity).isEmpty()).toBeFalsy();
});

it("returns true for empty sequences", () => {
expect(sequenceOf().isEmpty()).toBeTruthy();
});

it("returns true for sequences that would be empty", () => {
expect(sequenceOf(1, 3).filter(x => x % 2 === 0).isEmpty()).toBeTruthy();
});

it("returns false for sequences that would not be empty", () => {
expect(sequenceOf(1, 3).filter(x => x % 2 !== 0).isEmpty()).toBeFalsy();
});
});
19 changes: 19 additions & 0 deletions test/isNotEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { range, sequenceOf } from "../src/Sequence";

describe("isNotEmpty", () => {
it("returns true for infinite sequences", () => {
expect(range(0, Infinity).isNotEmpty()).toBeTruthy();
});

it("returns false for empty sequences", () => {
expect(sequenceOf().isNotEmpty()).toBeFalsy();
});

it("returns false for sequences that would be empty", () => {
expect(sequenceOf(1, 3).filter(x => x % 2 === 0).isNotEmpty()).toBeFalsy();
});

it("returns true for sequences that would not be empty", () => {
expect(sequenceOf(1, 3).filter(x => x % 2 !== 0).isNotEmpty()).toBeTruthy();
});
});