Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to keep only not duplicated lines #132

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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);
Loading