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

feat(new-tool):-isbn-parser-and-formatter #223

Open
wants to merge 2 commits into
base: developing/2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ declare module '@vue/runtime-core' {
Ipv4RangeExpander: typeof import('./src/tools/ipv4-range-expander/ipv4-range-expander.vue')['default']
Ipv4SubnetCalculator: typeof import('./src/tools/ipv4-subnet-calculator/ipv4-subnet-calculator.vue')['default']
Ipv6UlaGenerator: typeof import('./src/tools/ipv6-ula-generator/ipv6-ula-generator.vue')['default']
IsbnValidatorAndParser: typeof import('./src/tools/isbn-validator-and-parser/isbn-validator-and-parser.vue')['default']
JsonDiff: typeof import('./src/tools/json-diff/json-diff.vue')['default']
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
"isbn3": "^1.1.44",
"json5": "^2.2.3",
"jwt-decode": "^3.1.2",
"libphonenumber-js": "^1.10.28",
Expand Down
15 changes: 12 additions & 3 deletions pnpm-lock.yaml

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

7 changes: 6 additions & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { tool as urlParser } from './url-parser';
import { tool as uuidGenerator } from './uuid-generator';
import { tool as macAddressLookup } from './mac-address-lookup';
import { tool as xmlFormatter } from './xml-formatter';
import { tool as isbnValidatorAndParser } from './isbn-validator-and-parser';
import { tool as yamlViewer } from './yaml-viewer';

export const toolsByCategory: ToolCategory[] = [
Expand Down Expand Up @@ -163,7 +164,11 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Data',
components: [phoneParserAndFormatter, ibanValidatorAndParser],
components: [
phoneParserAndFormatter,
ibanValidatorAndParser,
isbnValidatorAndParser,
],
},
];

Expand Down
12 changes: 12 additions & 0 deletions src/tools/isbn-validator-and-parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Books } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'ISBN Validator and Parser',
path: '/isbn-validator-and-parser',
description: 'Parse, validate, format and get infos for an ISBN',
keywords: ['isbn', 'validator', 'parser', 'formatter'],
component: () => import('./isbn-validator-and-parser.vue'),
icon: Books,
createdAt: new Date('2024-01-10'),
});
101 changes: 101 additions & 0 deletions src/tools/isbn-validator-and-parser/isbn-validator-and-parser.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<script setup lang="ts">
import ISBN3 from 'isbn3';
import type { CKeyValueListItems } from '@/ui/c-key-value-list/c-key-value-list.types';

const rawIsbn = ref('9782021304534');

const isbnInfos = computed<CKeyValueListItems>(() => {
const isbn = ISBN3.parse(rawIsbn.value);

if (isbn == null) {
return [];
}

return [
{
label: 'Is ISBN valid ?',
value: isbn.isValid,
},
{
label: 'Country',
value: isbn.groupname,
},
{
label: 'ISBN 13',
value: isbn.isbn13,
},
{
label: 'ISBN 13 Formatted',
value: isbn.isbn13h,
},
{
label: 'ISBN 10',
value: isbn.isbn10,
},
{
label: 'ISBN 10 Formatted',
value: isbn.isbn10h,
},
];
});

function isbnChecksum(isbn: string) {
let check = 0;

if (isbn.length === 9) {
for (let n = 0; n < 9; n += 1) {
check += (10 - n) * Number(isbn.charAt(n));
}
check = (11 - check % 11) % 11;
return check === 10 ? 'X' : String(check);
}
else if (isbn.length === 12) {
for (let n = 0; n < 12; n += 2) {
check += Number(isbn.charAt(n)) + 3 * Number(isbn.charAt(n + 1));
}
return String((10 - check % 10) % 10);
}

return null;
}

const normalizedISBN = computed(() => {
let normalized = rawIsbn.value.replace(/[^\dX]/g, '');
if (normalized.length >= 13) {
normalized = normalized.substring(0, 12);
}
else if (normalized.length >= 10) {
normalized = normalized.substring(0, 9);
}
const checksum = isbnChecksum(normalized);
return ISBN3.parse(`${normalized}${checksum}`)?.isbn13h;
});

const isbnAuditInfos = computed<{ isValid: boolean; clues: CKeyValueListItems }>(() => {
const isbn = ISBN3.audit(rawIsbn.value);

const isValid = (isbn?.validIsbn ?? false);
return {
isValid,
clues: Array.from((isbn?.clues ?? []),
clue => ({ label: `${clue.message} (${clue.groupname})`, value: clue.candidate })),
};
});
</script>

<template>
<div>
<c-input-text v-model:value="rawIsbn" placeholder="Enter an ISBN to check for validity..." test-id="isbn-input" />
<n-alert v-if="!isbnAuditInfos.isValid" type="error">
Invalid ISBN.
<input-copyable v-if="normalizedISBN" label="Probably correct" label-position="left" :value="normalizedISBN" disabled="true" />
</n-alert>

<c-card v-if="isbnInfos.length > 0" mt-5 title="ISBN Infos">
<c-key-value-list :items="isbnInfos" data-test-id="isbn-info" />
</c-card>
<c-card v-if="isbnAuditInfos.clues.length > 0" mt-5 title="ISBN Audit Infos">
<c-key-value-list :items="isbnAuditInfos" data-test-id="isbn-info" />
</c-card>
</div>
</template>