Skip to content

Commit

Permalink
arrays.distinct do not filter out default string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
isidorn committed Mar 30, 2016
1 parent 787bd07 commit 6ab2686
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/vs/base/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function distinct<T>(array: T[], keyFn?: (t: T) => string): T[] {
});
}

const seen: { [key: string]: boolean; } = {};
const seen: { [key: string]: boolean; } = Object.create(null);
return array.filter((elem) => {
const key = keyFn(elem);
if (seen[key]) {
Expand Down
12 changes: 12 additions & 0 deletions src/vs/base/test/common/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,17 @@ suite('Arrays', () => {
assert.equal(arrays.binarySearch(array, 70, compare), ~10);

});

test('distinct', function() {
function compare(a: string): string {
return a;
}

assert.deepEqual(arrays.distinct(['32', '4', '5'], compare), ['32', '4', '5']);
assert.deepEqual(arrays.distinct(['32', '4', '5', '4'], compare), ['32', '4', '5']);
assert.deepEqual(arrays.distinct(['32', 'constructor', '5', '1'], compare), ['32', 'constructor', '5', '1']);
assert.deepEqual(arrays.distinct(['32', 'constructor', 'proto', 'proto', 'constructor'], compare), ['32', 'constructor', 'proto']);
assert.deepEqual(arrays.distinct(['32', '4', '5', '32', '4', '5', '32', '4', '5', '5'], compare), ['32', '4', '5']);
});
});

0 comments on commit 6ab2686

Please sign in to comment.