Skip to content

Commit

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

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

Enumerable.prototype.toDictionary = toDictionary;

declare module '../internal/Enumerable' {
interface Enumerable<TSource> {
toDictionary<TKey>(keySelector?: (element: TSource) => TKey): Map<TKey, TSource>;
toDictionary<TKey, TElement>(keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement>;
toDictionary<TKey>(keySelector: (element: TSource) => TKey): Map<TKey, TSource>;
toDictionary<TKey, TElement>(keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement>;
}
}
4 changes: 2 additions & 2 deletions src/toDictionary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function toDictionary<TSource, TKey>(this: Iterable<TSource>, keySelector?: (element: TSource) => TKey): Map<TKey, TSource>;
export default function toDictionary<TSource, TKey, TElement>(this: Iterable<TSource>, keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map<TKey, TElement> {
export default function toDictionary<TSource, TKey>(this: Iterable<TSource>, keySelector: (element: TSource) => TKey): Map<TKey, TSource>;
export default function toDictionary<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 dict = new Map<TKey, TElement>();
Expand Down
16 changes: 6 additions & 10 deletions test/toDictionaryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import Enumerable from '../src/';

describe('toDictionary()', () => {
it('should creates a Map<T> from Iterable<T>', () => {
let result = new Enumerable([]).toDictionary();
assert(result instanceof Map);
assert.deepEqual(Array.from(result), []);
const result1 = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x.length);
assert(result1 instanceof Map);
assert.deepEqual(Array.from(result1), [[1, 'i'], [2, 'gh'], [3, 'def']]);

result = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x.length);
assert(result instanceof Map);
assert.deepEqual(Array.from(result), [[1, 'i'], [2, 'gh'], [3, 'def']]);

result = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x, x => x.length);
assert(result instanceof Map);
assert.deepEqual(Array.from(result), [['a', 1], ['bc', 2], ['def', 3], ['gh', 2], ['i', 1]]);
const result2 = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x, x => x.length);
assert(result2 instanceof Map);
assert.deepEqual(Array.from(result2), [['a', 1], ['bc', 2], ['def', 3], ['gh', 2], ['i', 1]]);
});
});

0 comments on commit 7cb94aa

Please sign in to comment.