Skip to content

Commit 48a51cb

Browse files
committed
Merge branch 'feat/luhn-checker' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents 165ea5a + 0d7e6ea commit 48a51cb

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"libphonenumber-js": "^1.10.28",
145145
"lodash": "^4.17.21",
146146
"luxon": "^3.5.0",
147+
"luhn-js": "^1.1.2",
147148
"markdown-it": "^14.0.0",
148149
"lodash.defaultsdeep": "^4.6.1",
149150
"lodash.flattendeep": "^4.4.0",

pnpm-lock.yaml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { tool as jsonToPhpArray } from './json-to-php-array';
2424
import { tool as phpArrayToJson } from './php-array-to-json';
2525
import { tool as jsonSizeAnalyzer } from './json-size-analyzer';
2626
import { tool as jsonToCsharp } from './json-to-csharp';
27+
import { tool as luhnValidator } from './luhn-validator';
2728

2829
import { tool as cssXpathConverter } from './css-xpath-converter';
2930
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -316,7 +317,11 @@ export const toolsByCategory: ToolCategory[] = [
316317
},
317318
{
318319
name: 'Data',
319-
components: [phoneParserAndFormatter, ibanValidatorAndParser],
320+
components: [
321+
phoneParserAndFormatter,
322+
ibanValidatorAndParser,
323+
luhnValidator,
324+
],
320325
},
321326
];
322327

src/tools/luhn-validator/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Check } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Luhn Validator',
6+
path: '/luhn-validator',
7+
description: 'Check and generate key for identifier validated by a Luhn checknum',
8+
keywords: ['luhn', 'credit-card', 'imei', 'identifier', 'validator'],
9+
component: () => import('./luhn-validator.vue'),
10+
icon: Check,
11+
createdAt: new Date('2024-08-15'),
12+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<script setup lang="ts">
2+
import Luhn from 'luhn-js';
3+
import type { CKeyValueListItems } from '@/ui/c-key-value-list/c-key-value-list.types';
4+
5+
const rawValue = ref('44540661970241257');
6+
const cleanedValue = computed(() => rawValue.value.replace(/[^\d]/g, ''));
7+
const isValid = computed(() => {
8+
try {
9+
return Luhn.isValid(cleanedValue.value);
10+
}
11+
catch (_) {
12+
return false;
13+
}
14+
});
15+
const luhnInfos = computed<CKeyValueListItems>(() => {
16+
return [
17+
{
18+
label: 'Is valid ?',
19+
value: isValid.value,
20+
},
21+
{
22+
label: 'Luhn Key',
23+
value: (isValid.value
24+
? cleanedValue.value.slice(-1)
25+
: Luhn.generate(cleanedValue.value).slice(-1)) || '',
26+
},
27+
{
28+
label: 'Value with Luhn Key',
29+
value: (isValid.value
30+
? cleanedValue.value
31+
: Luhn.generate(cleanedValue.value)) || '',
32+
},
33+
];
34+
});
35+
</script>
36+
37+
<template>
38+
<div>
39+
<c-input-text v-model:value="rawValue" placeholder="Enter a 'Luhn validated' value..." />
40+
<n-alert v-if="!isValid" type="error">
41+
Invalid Luhn Key.
42+
<input-copyable label="Probably correct" label-position="left" :value="Luhn.generate(cleanedValue)" disabled="true" />
43+
</n-alert>
44+
45+
<c-card v-if="luhnInfos.length > 0" mt-5 title="Infos">
46+
<c-key-value-list :items="luhnInfos" />
47+
</c-card>
48+
</div>
49+
</template>

0 commit comments

Comments
 (0)