Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ angular.module('material.components.select', [
* </div>
* </hljs>
*/
function SelectDirective($mdSelect, $mdUtil, $mdConstant, $mdTheming, $mdAria, $compile, $parse) {
function SelectDirective($mdSelect, $mdUtil, $mdConstant, $mdTheming, $mdAria, $parse) {
var keyCodes = $mdConstant.KEY_CODE;
var NAVIGATION_KEYS = [keyCodes.SPACE, keyCodes.ENTER, keyCodes.UP_ARROW, keyCodes.DOWN_ARROW];

Expand Down Expand Up @@ -408,7 +408,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdConstant, $mdTheming, $mdAria, $
}

scope.$watch(function() {
return selectMenuCtrl.selectedLabels();
return selectMenuCtrl.selectedLabels();
}, syncLabelText);

function syncLabelText() {
Expand Down Expand Up @@ -792,7 +792,9 @@ function SelectMenuDirective($parse, $mdUtil, $mdConstant, $mdTheming) {
} else if (mode == 'aria') {
mapFn = function(el) { return el.hasAttribute('aria-label') ? el.getAttribute('aria-label') : el.textContent; };
}
return selectedOptionEls.map(mapFn).join(', ');

// Ensure there are no duplicates; see https://github.com/angular/material/issues/9442
return $mdUtil.uniq(selectedOptionEls.map(mapFn)).join(', ');
} else {
return '';
}
Expand Down
19 changes: 19 additions & 0 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,25 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in

return start + change * (-2 * tc + 3 * ts);
}
},

/**
* Provides an easy mechanism for removing duplicates from an array.
*
* var myArray = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
*
* $mdUtil.uniq(myArray) => [1, 2, 3, 4]
*
* @param {array} array The array whose unique values should be returned.
*
* @returns {array} A copy of the array containing only unique values.
*/
uniq: function(array) {
if (!array) { return; }

return array.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/core/util/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,18 @@ describe('util', function() {
parent.remove();
});
});

describe('uniq', function() {
var $mdUtil;

beforeEach(inject(function(_$mdUtil_) {
$mdUtil = _$mdUtil_;
}));

it('returns a copy of the requested array with only unique values', function() {
var myArray = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];

expect($mdUtil.uniq(myArray)).toEqual([1, 2, 3, 4]);
});
});
});