Skip to content

Commit

Permalink
Merge pull request #132 from almeidaxan/master
Browse files Browse the repository at this point in the history
Add option to keep only not duplicated lines
  • Loading branch information
Tyriar authored Nov 8, 2024
2 parents 1b182fe + 22a8f39 commit f7e771c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
15 changes: 15 additions & 0 deletions fixtures/line_length_expected/keepOnlyNotDuplicateLines
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
back
band
blah
buck
burp
deck
drill
duck
grand
hello
help
puck
quill
trill
zebra
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cc
d
aa
c
b
bb
a
3 changes: 3 additions & 0 deletions fixtures/unicode_expected/keepOnlyNotDuplicateLines
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
012345
0123456789
𝟘𝟙𝟚𝟛
2 changes: 2 additions & 0 deletions fixtures/variables_expected/keepOnlyNotDuplicateLines
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var test10 = 1;
var test100 = 100;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -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"
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 7 additions & 1 deletion src/sort-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() === '') {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

0 comments on commit f7e771c

Please sign in to comment.