-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from sunnydanu/feat(new-tool)--list-comparaison
feat(new-tool):-list-comparaison
- Loading branch information
Showing
8 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { List } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'Lists Comparer', | ||
path: '/list-comparer', | ||
description: 'Compare two list items', | ||
keywords: ['list', 'comparer'], | ||
component: () => import('./list-comparer.vue'), | ||
icon: List, | ||
createdAt: new Date('2024-08-15'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { compareLists } from './list-comparer.service'; | ||
|
||
describe('list-comparer', () => { | ||
describe('compareLists', () => { | ||
it('return correct comparaison', () => { | ||
expect(compareLists({ | ||
list1: '1\n 2\n3\n4\n5\n4\n7\nA', | ||
list2: '1\n2\n3\n4\n6\n4\n7\na', | ||
trimItems: true, | ||
ignoreCase: true, | ||
})).to.deep.eq({ | ||
list1Not2: [ | ||
'5', | ||
], | ||
list2Not1: [ | ||
'6', | ||
], | ||
same: [ | ||
'1', | ||
'2', | ||
'3', | ||
'4', | ||
'7', | ||
'a', | ||
], | ||
}); | ||
|
||
expect(compareLists({ | ||
list1: '1\n 2\n3\n4\n5\n4\n7\nA', | ||
list2: '1\n2\n3\n4\n6\n4\n7\na', | ||
trimItems: false, | ||
ignoreCase: false, | ||
})).to.deep.eq({ | ||
list1Not2: [ | ||
' 2', | ||
'5', | ||
'A', | ||
], | ||
list2Not1: [ | ||
'2', | ||
'6', | ||
'a', | ||
], | ||
same: [ | ||
'1', | ||
'3', | ||
'4', | ||
'7', | ||
], | ||
}); | ||
|
||
expect(compareLists({ | ||
list1: '1, 2,3,4,5\n4,7,A,A', | ||
list2: '1\n2\n3\n4\n6\n4\n7\na', | ||
trimItems: false, | ||
ignoreCase: false, | ||
separator: ',', | ||
})).to.deep.eq({ | ||
list1Not2: [ | ||
' 2', | ||
'5', | ||
'A', | ||
], | ||
list2Not1: [ | ||
'2', | ||
'6', | ||
'a', | ||
], | ||
same: [ | ||
'1', | ||
'3', | ||
'4', | ||
'7', | ||
], | ||
}); | ||
|
||
expect(compareLists({ | ||
list1: '10\n20\n20\n30', | ||
list2: '30\n20\n40', | ||
trimItems: false, | ||
ignoreCase: false, | ||
})).to.deep.eq({ | ||
list1Not2: [ | ||
'10', | ||
], | ||
list2Not1: [ | ||
'40', | ||
], | ||
same: [ | ||
'30', | ||
'20', | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import _ from 'lodash'; | ||
import intersect from 'fast_array_intersect'; | ||
import diff from 'arr-diff'; | ||
|
||
export function compareLists({ | ||
list1, | ||
list2, | ||
ignoreCase = false, | ||
trimItems = true, | ||
separator = '', | ||
}: { | ||
list1: string | ||
list2: string | ||
separator?: string | ||
ignoreCase?: boolean | ||
trimItems?: boolean | ||
}) { | ||
const splitSep = separator ? `${separator}|` : ''; | ||
const splitRegExp = new RegExp(`(?:${splitSep}\\n)`, 'g'); | ||
|
||
const prepareList = (list: string) => | ||
_.chain(list ?? '') | ||
.thru(text => ignoreCase ? text.toLowerCase() : text) | ||
.split(splitRegExp) | ||
.map(text => trimItems ? text.trim() : text) | ||
.value(); | ||
|
||
const list1Arr = prepareList(list1); | ||
const list2Arr = prepareList(list2); | ||
|
||
return { | ||
same: [...new Set(intersect([list1Arr, list2Arr]))], | ||
list2Not1: [...new Set(diff(list2Arr, list1Arr))], | ||
list1Not2: [...new Set(diff(list1Arr, list2Arr))], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<script setup lang="ts"> | ||
import { compareLists } from './list-comparer.service'; | ||
const compareConfig = useStorage<{ ignoreCase: boolean; trimItems: boolean; noDuplicate: boolean; separator: string }>('list-cmp:conf', { | ||
ignoreCase: false, | ||
trimItems: true, | ||
noDuplicate: false, | ||
separator: '', | ||
}); | ||
const list1 = ref(''); | ||
const list2 = ref(''); | ||
const compareResult = computed(() => { | ||
return compareLists({ | ||
list1: list1.value, | ||
list2: list2.value, | ||
ignoreCase: compareConfig.value.ignoreCase, | ||
trimItems: compareConfig.value.trimItems, | ||
separator: compareConfig.value.separator, | ||
}); | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<n-space justify="center" gap-1 align="baseline"> | ||
<n-form-item | ||
label="Trim items" | ||
label-placement="left" | ||
> | ||
<n-switch v-model:value="compareConfig.trimItems" /> | ||
</n-form-item> | ||
|
||
<n-form-item | ||
label="Ignore case" | ||
label-placement="left" | ||
mb-2 | ||
> | ||
<n-switch v-model:value="compareConfig.ignoreCase" /> | ||
</n-form-item> | ||
|
||
<n-form-item | ||
label="Separator" | ||
label-placement="left" | ||
> | ||
<n-input | ||
v-model:value="compareConfig.separator" | ||
placeholder="Additional separator" | ||
/> | ||
</n-form-item> | ||
</n-space> | ||
|
||
<div flex gap-1> | ||
<c-input-text | ||
v-model:value="list1" | ||
multiline | ||
rows="10" | ||
label="List 1" | ||
/> | ||
<c-input-text | ||
v-model:value="list2" | ||
multiline | ||
rows="10" | ||
label="List 2" | ||
/> | ||
</div> | ||
|
||
<div v-if="list1 || list2"> | ||
<n-divider /> | ||
|
||
<c-card title="Items in both lists" mb-2> | ||
<textarea-copyable :value="compareResult.same.join('\n')" /> | ||
</c-card> | ||
<c-card title="Items in List 1 but not in List 2" mb-2> | ||
<textarea-copyable :value="compareResult.list1Not2.join('\n')" /> | ||
</c-card> | ||
<c-card title="Items in List 2 but not in List 1" mb-2> | ||
<textarea-copyable :value="compareResult.list2Not1.join('\n')" /> | ||
</c-card> | ||
</div> | ||
</div> | ||
</template> |