Skip to content

Commit

Permalink
Merge pull request #63 from sunnydanu/feat(new-tool)--list-comparaison
Browse files Browse the repository at this point in the history
feat(new-tool):-list-comparaison
  • Loading branch information
sunnydanu authored Nov 4, 2024
2 parents a39d39b + be586f7 commit 79c0be5
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ declare module '@vue/runtime-core' {
JsUnobfuscator: typeof import('./src/tools/js-unobfuscator/js-unobfuscator.vue')['default']
JwtParser: typeof import('./src/tools/jwt-parser/jwt-parser.vue')['default']
KeycodeInfo: typeof import('./src/tools/keycode-info/keycode-info.vue')['default']
ListComparer: typeof import('./src/tools/list-comparer/list-comparer.vue')['default']
ListConverter: typeof import('./src/tools/list-converter/list-converter.vue')['default']
LiveCode: typeof import('./src/tools/live-code/live-code.vue')['default']
LocaleSelector: typeof import('./src/modules/i18n/components/locale-selector.vue')['default']
Expand Down Expand Up @@ -195,6 +196,8 @@ declare module '@vue/runtime-core' {
NMenu: typeof import('naive-ui')['NMenu']
NP: typeof import('naive-ui')['NP']
NProgress: typeof import('naive-ui')['NProgress']
NRadio: typeof import('naive-ui')['NRadio']
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@tiptap/pm": "2.1.6",
"@tiptap/starter-kit": "2.1.6",
"@tiptap/vue-3": "2.0.3",
"@types/arr-diff": "^4.0.3",
"@types/figlet": "^1.5.8",
"@types/markdown-it": "^13.0.7",
"@types/memorystream": "^0.3.4",
Expand All @@ -54,6 +55,7 @@
"@vueuse/core": "^10.3.0",
"@vueuse/head": "^1.0.0",
"@vueuse/router": "^10.0.0",
"arr-diff": "^4.0.0",
"bcryptjs": "^2.4.3",
"change-case": "^4.1.2",
"cidr-tools": "^7.0.4",
Expand All @@ -74,6 +76,7 @@
"exif-be-gone": "^1.5.1",
"exifreader": "^4.20.0",
"fflate": "^0.8.2",
"fast_array_intersect": "^1.1.0",
"figlet": "^1.7.0",
"figue": "^1.2.0",
"fuse.js": "^6.6.2",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { tool as tomlToJson } from './toml-to-json';
import { tool as jsonToCsv } from './json-to-csv';
import { tool as cameraRecorder } from './camera-recorder';
import { tool as listConverter } from './list-converter';
import { tool as listComparer } from './list-comparer';
import { tool as phoneParserAndFormatter } from './phone-parser-and-formatter';
import { tool as jsonDiff } from './json-diff';
import { tool as ipv4RangeExpander } from './ipv4-range-expander';
Expand Down Expand Up @@ -150,6 +151,7 @@ export const toolsByCategory: ToolCategory[] = [
jsonToYaml,
jsonToToml,
listConverter,
listComparer,
tomlToJson,
tomlToYaml,
xmlToJson,
Expand Down
12 changes: 12 additions & 0 deletions src/tools/list-comparer/index.ts
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'),
});
97 changes: 97 additions & 0 deletions src/tools/list-comparer/list-comparer.service.test.ts
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',
],
});
});
});
});
36 changes: 36 additions & 0 deletions src/tools/list-comparer/list-comparer.service.ts
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))],
};
}
82 changes: 82 additions & 0 deletions src/tools/list-comparer/list-comparer.vue
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>

0 comments on commit 79c0be5

Please sign in to comment.