Skip to content

Commit

Permalink
bugfix: LinkedList.of() and Stream.of() (without parameters) would re…
Browse files Browse the repository at this point in the history
…turn a list with a single item, undefined. That call was impossible in typescript (wouldn't compile) but it was possible in javascript. Now make it that it returns an empty list as you would expect, while retaining the intended feature, that if there are parameters, the compiler knows we're dealing with a non-empty list (thanks to typescript overloads).
  • Loading branch information
emmanueltouzery committed May 15, 2018
1 parent 91ce10a commit dead06a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export class LinkedListStatic {
/**
* Create a LinkedList with the elements you give.
*/
of<T>(elt:T,...elts:T[]): ConsLinkedList<T> {
return new ConsLinkedList(elt, LinkedList.ofIterable(elts));
of<T>(elt: T, ...elts:T[]): ConsLinkedList<T>;
of<T>(...elts:T[]): LinkedList<T>;
of<T>(...elts:T[]): LinkedList<T> {
return LinkedList.ofIterable(elts);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export class StreamStatic {
/**
* Create a Stream with the elements you give.
*/
of<T>(elt: T, ...elts:T[]): ConsStream<T> {
return new ConsStream(elt, Lazy.of(()=>Stream.ofIterable(elts)));
of<T>(elt:T, ...elts:T[]): ConsStream<T>;
of<T>(...elts:T[]): Stream<T>;
of<T>(...elts:T[]): Stream<T> {
return Stream.ofIterable(elts);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function runTests(seqName: string,
const nonEmptySeqName = nonEmptySeqName_ || seqName;
CollectionTest.runTests(seqName, of, empty);
describe(seqName + " creation", () => {
it("handles of() without parameters ok", () => assert.deepEqual(
[], of().toArray()));
it("creates from a JS array", () => assert.deepEqual(
["a","b", "c"],
ofIterable<string>(["a","b","c"]).toArray()));
Expand Down

0 comments on commit dead06a

Please sign in to comment.