Skip to content

Commit

Permalink
Fix toLookup() type signature
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Nov 24, 2016
1 parent 7cb94aa commit f79139a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/extensions/toLookup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import toLookupFn from '../toLookup';
import { Enumerable } from '../internal/Enumerable';

function toLookup<TSource, TKey>(this: Enumerable<TSource>, keySelector?: (element: TSource) => TKey): Map<TKey, TSource[]>;
function toLookup<TSource, TKey, TElement>(this: Enumerable<TSource>, keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]> {
function toLookup<TSource, TKey>(this: Enumerable<TSource>, keySelector: (element: TSource) => TKey): Map<TKey, TSource[]>;
function toLookup<TSource, TKey, TElement>(this: Enumerable<TSource>, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]> {
return toLookupFn.call(this.source, keySelector, elementSelector);
}

Enumerable.prototype.toLookup = toLookup;

declare module '../internal/Enumerable' {
interface Enumerable<TSource> {
toLookup<TKey>(keySelector?: (element: TSource) => TKey): Map<TKey, TSource[]>;
toLookup<TKey, TElement>(keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]>;
toLookup<TKey>(keySelector: (element: TSource) => TKey): Map<TKey, TSource[]>;
toLookup<TKey, TElement>(keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]>;
}
}
4 changes: 2 additions & 2 deletions src/toLookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function toLookup<TSource, TKey>(this: Iterable<TSource>, keySelector?: (element: TSource) => TKey): Map<TKey, TSource[]>;
export default function toLookup<TSource, TKey, TElement>(this: Iterable<TSource>, keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]> {
export default function toLookup<TSource, TKey>(this: Iterable<TSource>, keySelector: (element: TSource) => TKey): Map<TKey, TSource[]>;
export default function toLookup<TSource, TKey, TElement>(this: Iterable<TSource>, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement[]> {
if (elementSelector == null) elementSelector = x => x as any;

const lookup = new Map<TKey, TElement[]>();
Expand Down
1 change: 0 additions & 1 deletion test/toLookupTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Enumerable from '../src/';

describe('toLookup()', () => {
it('should creates a generic Map<TKey, TElement> from an Iterable<T>', () => {
assert.deepEqual(Array.from(new Enumerable([]).toLookup()), []);
assert.deepEqual(Array.from(new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toLookup(x => x.length)), [[1, ['a', 'i']], [2, ['bc', 'gh']], [3, ['def']]]);
assert.deepEqual(Array.from(new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toLookup(x => x.length, x => x + x)), [[1, ['aa', 'ii']], [2, ['bcbc', 'ghgh']], [3, ['defdef']]]);
});
Expand Down

0 comments on commit f79139a

Please sign in to comment.