This commit is contained in:
sharevb 2024-04-07 21:10:47 +02:00 committed by GitHub
commit 90189e1b99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 130 additions and 1 deletions

1
components.d.ts vendored
View file

@ -106,6 +106,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']

View file

@ -64,6 +64,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",

9
pnpm-lock.yaml generated
View file

@ -92,6 +92,9 @@ dependencies:
ibantools:
specifier: ^4.3.3
version: 4.3.3
isbn3:
specifier: ^1.1.44
version: 1.1.45
json5:
specifier: ^2.2.3
version: 2.2.3
@ -6434,6 +6437,12 @@ packages:
resolution: {integrity: sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==}
dev: true
/isbn3@1.1.45:
resolution: {integrity: sha512-9oD3yItVFRlYhm/K6RI2K6f+Dti0unzG74X9I7D+xVKMK3TbRl2PMvnWgK6scKnvk4t0BZFyNIznX4H45RrwzQ==}
engines: {node: '>= 6.4.0'}
hasBin: true
dev: false
/isexe@2.0.0:
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
dev: true

View file

@ -80,6 +80,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[] = [
@ -176,7 +177,11 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Data',
components: [phoneParserAndFormatter, ibanValidatorAndParser],
components: [
phoneParserAndFormatter,
ibanValidatorAndParser,
isbnValidatorAndParser,
],
},
];

View file

@ -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'),
});

View file

@ -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>