diff --git a/fixtures/line_length_expected/keepOnlyNotDuplicateLines b/fixtures/line_length_expected/keepOnlyNotDuplicateLines new file mode 100644 index 0000000..fa8020d --- /dev/null +++ b/fixtures/line_length_expected/keepOnlyNotDuplicateLines @@ -0,0 +1,15 @@ +back +band +blah +buck +burp +deck +drill +duck +grand +hello +help +puck +quill +trill +zebra \ No newline at end of file diff --git a/fixtures/shuffled_lowercase_expected/keepOnlyNotDuplicateLines b/fixtures/shuffled_lowercase_expected/keepOnlyNotDuplicateLines new file mode 100644 index 0000000..0d6968c --- /dev/null +++ b/fixtures/shuffled_lowercase_expected/keepOnlyNotDuplicateLines @@ -0,0 +1,7 @@ +cc +d +aa +c +b +bb +a \ No newline at end of file diff --git a/fixtures/unicode_expected/keepOnlyNotDuplicateLines b/fixtures/unicode_expected/keepOnlyNotDuplicateLines new file mode 100644 index 0000000..686eaba --- /dev/null +++ b/fixtures/unicode_expected/keepOnlyNotDuplicateLines @@ -0,0 +1,3 @@ +012345 +0123456789 +𝟘𝟙𝟚𝟛 \ No newline at end of file diff --git a/fixtures/variables_expected/keepOnlyNotDuplicateLines b/fixtures/variables_expected/keepOnlyNotDuplicateLines new file mode 100644 index 0000000..9ee2944 --- /dev/null +++ b/fixtures/variables_expected/keepOnlyNotDuplicateLines @@ -0,0 +1,2 @@ +var test10 = 1; +var test100 = 100; \ No newline at end of file diff --git a/package.json b/package.json index 7dbe046..de820b2 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,10 @@ "command": "sortLines.keepOnlyDuplicateLines", "title": "Sort lines (keep only duplicated lines)" }, + { + "command": "sortLines.keepOnlyNotDuplicateLines", + "title": "Sort lines (keep only not duplicated lines)" + }, { "command": "sortLines.sortLinesShuffle", "title": "Sort lines (shuffle)" @@ -178,9 +182,13 @@ "command": "sortLines.keepOnlyDuplicateLines", "group": "2_sortLines@12" }, + { + "command": "sortLines.keepOnlyNotDuplicateLines", + "group": "2_sortLines@13" + }, { "command": "sortLines.sortLinesShuffle", - "group": "2_sortLines@13" + "group": "2_sortLines@14" } ] }, diff --git a/src/extension.ts b/src/extension.ts index f501a61..1666a63 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,7 +15,8 @@ export function activate(context: vscode.ExtensionContext): void { vscode.commands.registerCommand('sortLines.sortLinesUnique', sortLines.sortUnique), vscode.commands.registerCommand('sortLines.sortLinesShuffle', sortLines.sortShuffle), vscode.commands.registerCommand('sortLines.removeDuplicateLines', sortLines.removeDuplicateLines), - vscode.commands.registerCommand('sortLines.keepOnlyDuplicateLines', sortLines.keepOnlyDuplicateLines) + vscode.commands.registerCommand('sortLines.keepOnlyDuplicateLines', sortLines.keepOnlyDuplicateLines), + vscode.commands.registerCommand('sortLines.keepOnlyNotDuplicateLines', sortLines.keepOnlyNotDuplicateLines) ]; commands.forEach(command => context.subscriptions.push(command)); diff --git a/src/sort-lines.ts b/src/sort-lines.ts index 132c7d8..64da0c4 100644 --- a/src/sort-lines.ts +++ b/src/sort-lines.ts @@ -60,6 +60,10 @@ function keepOnlyDuplicates(lines: string[]): string[] { return Array.from(new Set(lines.filter((element, index, array) => array.indexOf(element) !== index))); } +function keepOnlyNotDuplicates(lines: string[]): string[] { + return Array.from(new Set(lines.filter((element, index, array) => (array.lastIndexOf(element) === array.indexOf(element))))); +} + function removeBlanks(lines: string[]): void { for (let i = 0; i < lines.length; ++i) { if (lines[i].trim() === '') { @@ -143,7 +147,8 @@ const transformerSequences = { sortNatural: [makeSorter(naturalCompare)], sortShuffle: [shuffleSorter], removeDuplicateLines: [removeDuplicates], - keepOnlyDuplicateLines: [keepOnlyDuplicates] + keepOnlyDuplicateLines: [keepOnlyDuplicates], + keepOnlyNotDuplicateLines: [keepOnlyNotDuplicates] }; export const sortNormal = () => sortActiveSelection(transformerSequences.sortNormal); @@ -159,3 +164,4 @@ export const sortNatural = () => sortActiveSelection(transformerSequences.sortNa export const sortShuffle = () => sortActiveSelection(transformerSequences.sortShuffle); export const removeDuplicateLines = () => sortActiveSelection(transformerSequences.removeDuplicateLines); export const keepOnlyDuplicateLines = () => sortActiveSelection(transformerSequences.keepOnlyDuplicateLines); +export const keepOnlyNotDuplicateLines = () => sortActiveSelection(transformerSequences.keepOnlyNotDuplicateLines);