Skip to content

Commit 2d278d1

Browse files
committed
Improve error handling in case of illegal sequence input
1 parent e093114 commit 2d278d1

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## About Sequency
1616

17-
Sequency is a lightweight (**3 KB minified**), intensely tested (**175+ tests, 99% coverage**), type-safe functional programming library for processing iterable data such as arrays, sets and maps. It's written in TypeScript, compiles to ES5-compatible JavaScript and works in all current browsers and Node applications. The API is inspired by [Sequences](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/) from the programming language [Kotlin](https://kotlinlang.org/).
17+
Sequency is a lightweight (**3 KB minified**), intensely tested (**200+ tests, 99% coverage**), type-safe functional programming library for processing iterable data such as arrays, sets and maps. It's written in TypeScript, compiles to ES5-compatible JavaScript and works in all current browsers and Node applications. The API is inspired by [Sequences](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/) from the programming language [Kotlin](https://kotlinlang.org/).
1818

1919
> Not convinced? [Try Sequency](https://npm.runkit.com/sequency) right in your browser.
2020

src/Sequence.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ export function emptySequence<T>(): Sequence<T> {
165165
}
166166

167167
export function asSequence<T>(iterable: Iterable<T>): Sequence<T> {
168+
if (iterable === null) {
169+
throw new Error("Cannot create sequence for input: null");
170+
}
171+
if (iterable === undefined) {
172+
throw new Error("Cannot create sequence for input: undefined");
173+
}
174+
if (iterable[Symbol.iterator] == null) {
175+
throw new Error("Cannot create sequence for non-iterable input: " + iterable);
176+
}
168177
return new Sequence<T>(new IterableIterator(iterable));
169178
}
170179

test/asSequence.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@ describe("asSequence", () => {
5858
expect(array[1]).toEqual(["b", 2]);
5959
expect(array[2]).toEqual(["c", 3]);
6060
});
61+
62+
it("should throw understandable error message if input is undefined", () => {
63+
expect(
64+
() => asSequence(undefined as Array<number>).toArray()
65+
).toThrowError("Cannot create sequence for input: undefined");
66+
});
67+
68+
it("should throw understandable error message if input is null", () => {
69+
expect(
70+
() => asSequence(null as Array<number>).toArray()
71+
).toThrowError("Cannot create sequence for input: null");
72+
});
73+
74+
it("should throw understandable error message if input is not iterable", () => {
75+
expect(
76+
() => asSequence({} as Array<number>).toArray()
77+
).toThrowError("Cannot create sequence for non-iterable input: [object Object]");
78+
});
6179
});

0 commit comments

Comments
 (0)