Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Remove duplicate tags on paste #826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,12 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag
.on('input-paste', event => {
if (options.addOnPaste) {
let data = event.getTextData();
let tags = data.split(options.pasteSplitPattern);

let tags = data
.split(options.pasteSplitPattern)
.filter((element, index, array) =>
array.indexOf(element, index + 1) === -1
);

if (tags.length > 1) {
tags.forEach(tag => {
Expand Down
18 changes: 18 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,24 @@ describe('tags-input directive', () => {
expect(eventData.preventDefault).toHaveBeenCalled();
});

it('removes duplicate tags from split text (' + name + ')', () => {
// Arrange
compile('add-on-paste="true"');
setup('tag1, tag2, tag2, tag3');

// Act
let event = jQuery.Event('paste', eventData);
getInput().trigger(event);

// Assert
expect($scope.tags).toEqual([
{ text: 'tag1' },
{ text: 'tag2' },
{ text: 'tag3' }
]);
expect(eventData.preventDefault).toHaveBeenCalled();
});

it('doesn\'t split the pasted text into tags if there is just one tag (' + name + ')', () => {
// Arrange
compile('add-on-paste="true"');
Expand Down