Skip to content

Commit

Permalink
feat(list-sorter): add remove duplicates functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Eejit43 committed Oct 3, 2024
1 parent e81ff8c commit a4bf74b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/public/scripts/pages/tools/list-sorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const alphabetizeButton = document.querySelector('#alphabetize') as HTMLButtonEl
const numericalOrderButton = document.querySelector('#numerical-order') as HTMLButtonElement;
const randomizeButton = document.querySelector('#randomize') as HTMLButtonElement;
const reverseButton = document.querySelector('#reverse') as HTMLButtonElement;
const removeDuplicatesButton = document.querySelector('#remove-duplicates') as HTMLButtonElement;
const clearButton = document.querySelector('#clear') as HTMLButtonElement;
const result = document.querySelector('#result') as HTMLTextAreaElement;
const copyResultButton = document.querySelector('#copy-result') as HTMLButtonElement;
Expand All @@ -15,6 +16,7 @@ alphabetizeButton.addEventListener('click', alphabetize);
numericalOrderButton.addEventListener('click', numericalOrder);
randomizeButton.addEventListener('click', randomize);
reverseButton.addEventListener('click', reverse);
removeDuplicatesButton.addEventListener('click', removeDuplicates);
clearButton.addEventListener('click', () => {
input.value = '';
result.value = '';
Expand All @@ -37,7 +39,7 @@ copyResultButton.addEventListener('click', () => {
});

/**
* Alphabetizes the provided string and displays the result.
* Alphabetizes the provided list and displays the result.
*/
function alphabetize() {
if (input.value.length === 0) {
Expand All @@ -48,13 +50,14 @@ function alphabetize() {
.split(JSON.parse(`"${separatorInput.value}"`) as string)
.sort((a, b) => a.localeCompare(b))
.join(JSON.parse(`"${separatorInput.value}"`) as string);

showResult(alphabetizeButton, 'success');
copyResultButton.disabled = false;
}
}

/**
* Sorts the provided string in numerical order and displays the result.
* Sorts the provided list in numerical order and displays the result.
*/
function numericalOrder() {
if (input.value.length === 0) {
Expand All @@ -67,27 +70,29 @@ function numericalOrder() {
.filter((x) => x === 0 || Boolean(x))
.sort((a, b) => a - b)
.join(JSON.parse(`"${separatorInput.value}"`) as string);

showResult(numericalOrderButton, 'success');
copyResultButton.disabled = false;
}
}

/**
* Randomizes the provided string and displays the result.
* Randomizes the provided list and displays the result.
*/
function randomize() {
if (input.value.length === 0) {
showAlert('Empty input!', 'warning');
showResult(randomizeButton, 'warning');
} else {
result.value = shuffleArray(input.value.split(JSON.parse(`"${separatorInput.value}"`) as string)).join(JSON.parse(`"${separatorInput.value}"`) as string);

showResult(randomizeButton, 'success');
copyResultButton.disabled = false;
}
}

/**
* Reverses the provided string and displays the result.
* Reverses the provided list and displays the result.
*/
function reverse() {
if (input.value.length === 0) {
Expand All @@ -98,7 +103,24 @@ function reverse() {
.split(JSON.parse(`"${separatorInput.value}"`) as string)
.reverse()
.join(JSON.parse(`"${separatorInput.value}"`) as string);

showResult(reverseButton, 'success');
copyResultButton.disabled = false;
}
}

/**
* Removes duplicates from the provided list and displays the result.
*/
function removeDuplicates() {
if (input.value.length === 0) {
showAlert('Empty input!', 'warning');
showResult(reverseButton, 'warning');
} else {
const set = new Set(input.value.split(JSON.parse(`"${separatorInput.value}"`) as string));
result.value = [...set.values()].join(JSON.parse(`"${separatorInput.value}"`) as string);

showResult(removeDuplicatesButton, 'success');
copyResultButton.disabled = false;
}
}
1 change: 1 addition & 0 deletions src/views/pages/tools/list-sorter.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<button id="numerical-order">Sort in numerical order (filters non numbers)</button>
<button id="randomize">Randomize</button>
<button id="reverse">Reverse current order</button>
<button id="remove-duplicates">Remove duplicates</button>
<button id="clear" class="button-danger">Clear</button>
</section>
<hr />
Expand Down

0 comments on commit a4bf74b

Please sign in to comment.