Skip to content

Commit

Permalink
Add Line sort/de-duper utility
Browse files Browse the repository at this point in the history
  • Loading branch information
JStruk committed Sep 12, 2023
1 parent 571821f commit 2a12d6b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
18 changes: 17 additions & 1 deletion cypress/e2e/utils/lineSort.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('LineSort', () => {
})

const text: string = 'c\nb\na'
const duplicateText: string = 'c\nc\nc\nb\na'

it('should allow the user to enter text to sort', () => {
cy.get('textarea[aria-label="input-to-sort"]').type(text)
Expand All @@ -14,7 +15,22 @@ describe('LineSort', () => {
it('should display the sorted text', () => {
cy.get('textarea[aria-label="input-to-sort"]').type(text)
cy.get('button[aria-label="sort-button"]').click()
const sortedText: string = lineSort(text)
const sortedText: string = lineSort(text, false)
cy.get('textarea[aria-label="sorted-text"]').should('have.value', sortedText)
})

it('should remove duplicate lines and sort', () => {
cy.get('textarea[aria-label="input-to-sort"]').type(duplicateText)
cy.get('button[aria-label="sort-button"]').click()
const sortedText: string = lineSort(duplicateText)
cy.get('textarea[aria-label="sorted-text"]').should('have.value', sortedText)
})

it('should not remove duplicate lines if not selected', () => {
cy.get('textarea[aria-label="input-to-sort"]').type(duplicateText)
cy.get('input[aria-label="remove-duplicates-checkbox"]').click()
cy.get('button[aria-label="sort-button"]').click()
const sortedText: string = lineSort(duplicateText, false)
cy.get('textarea[aria-label="sorted-text"]').should('have.value', sortedText)
})
})
10 changes: 7 additions & 3 deletions src/utilities/LineSort.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export function lineSort(input: string) {
return input
.split('\n')
export function lineSort(input: string, removeDuplicates: boolean = true) {
const cleanInput = input.replace(/\n$/g, '')
const lines: string[] = removeDuplicates
? [...new Set(cleanInput.split('\n'))]
: cleanInput.split('\n')

return lines
.sort()
.join('\n')
}
12 changes: 11 additions & 1 deletion src/views/LineSort.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<button class="bg-indigo-600 text-white text-sm leading-6 font-medium mx-4 rounded-lg w-28" type="button" aria-label="sort-button"
@click="convert">Convert
</button>
<label>Remove Duplicates</label>
<input class="bg-indigo-600 text-white text-sm leading-6 font-medium mx-4 w-12 rounded-lg" type="checkbox" aria-label="remove-duplicates-checkbox"
@click="removeDuplicates"
:checked="RemoveDuplicates"
/>
</div>
<textarea :value="SortedInput" readonly rows="30"
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
Expand All @@ -22,8 +27,13 @@ import { lineSort } from '@/utilities/LineSort'
const Input = ref<string>('')
const SortedInput = ref<string>('')
const RemoveDuplicates = ref<boolean>(true)
function convert() {
SortedInput.value = lineSort(Input.value)
SortedInput.value = lineSort(Input.value, RemoveDuplicates.value)
}
function removeDuplicates() {
RemoveDuplicates.value = !RemoveDuplicates.value
}
</script>
3 changes: 2 additions & 1 deletion src/views/ToolsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const links = [
{ route: 'PHPToJSON', label: 'PHP To JSON' },
{ route: 'JSONToPHP', label: 'JSON To PHP' },
{ route: 'Base64Encode', label: 'Base64Encode' },
{ route: 'Base64Decode', label: 'Base64Decode' }
{ route: 'Base64Decode', label: 'Base64Decode' },
{ route: 'LineSort', label: 'LineSort' }
]
</script>
33 changes: 33 additions & 0 deletions tests/utilities/LineSort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,37 @@ describe('LineSort', () => {

expect(lineSort(input)).toEqual(sorted)
})

it('should remove duplicates', () => {
const input:string = 'c\n' +
'b\n' +
'b\n' +
'b\n' +
'a\n' +
'a'

const sorted: string = 'a\nb\nc'

expect(lineSort(input, true)).toEqual(sorted)
})

it('should not remove duplicates if false provided', () => {
const input:string = 'c\n' +
'b\n' +
'b\n' +
'b\n' +
'a\n' +
'a'

const sorted: string = 'a\na\nb\nb\nb\nc'

expect(lineSort(input, false)).toEqual(sorted)
})

it('handles extra newline at end of input', () => {
const input:string = 'c\nb\na\n'
const sorted: string = 'a\nb\nc'

expect(lineSort(input)).toEqual(sorted)
})
})

0 comments on commit 2a12d6b

Please sign in to comment.