mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-05 05:47:10 -04:00
fix: refactor with unit test
This commit is contained in:
parent
00b85de1a0
commit
d14371c5c2
3 changed files with 37 additions and 9 deletions
|
@ -0,0 +1,15 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { convertMacCISCO, convertMacCanonical, convertMacCanonicalIEEE, convertMacCanonicalIETF } from './mac-address-converter.service';
|
||||||
|
|
||||||
|
describe('mac-address-converter', () => {
|
||||||
|
it('Convert MAC Address to given format', async () => {
|
||||||
|
expect(convertMacCanonical('')).to.equal('');
|
||||||
|
|
||||||
|
const macValue = 'AA:BB:CC:DD:EE:FF';
|
||||||
|
|
||||||
|
expect(convertMacCanonicalIETF(macValue)).to.equal('aa:bb:cc:dd:ee:ff');
|
||||||
|
expect(convertMacCanonical(macValue)).to.equal('AA.BB.CC.DD.EE.FF');
|
||||||
|
expect(convertMacCanonicalIEEE(macValue)).to.equal('AA-BB-CC-DD-EE-FF');
|
||||||
|
expect(convertMacCISCO(macValue)).to.equal('aabb.ccdd.eeff');
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,17 @@
|
||||||
|
function convertMac(mac: string, group: number = 2, char: string = ':'): string {
|
||||||
|
mac = mac.replace(/[\W_]+/g, '');
|
||||||
|
return mac.match(new RegExp(`.{1,${group}}`, 'g'))?.join(char) || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function convertMacCanonicalIETF(mac: string): string {
|
||||||
|
return convertMac(mac.toLocaleLowerCase());
|
||||||
|
};
|
||||||
|
export function convertMacCanonical(mac: string): string {
|
||||||
|
return convertMac(mac, 2, '.');
|
||||||
|
};
|
||||||
|
export function convertMacCanonicalIEEE(mac: string): string {
|
||||||
|
return convertMac(mac.toLocaleUpperCase(), 2, '-');
|
||||||
|
};
|
||||||
|
export function convertMacCISCO(mac: string): string {
|
||||||
|
return convertMac(mac.toLocaleLowerCase(), 4, '.');
|
||||||
|
};
|
|
@ -1,29 +1,25 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import InputCopyable from '../../components/InputCopyable.vue';
|
import InputCopyable from '../../components/InputCopyable.vue';
|
||||||
|
import { convertMacCISCO, convertMacCanonical, convertMacCanonicalIEEE, convertMacCanonicalIETF } from './mac-address-converter.service';
|
||||||
function convertMac(mac: string, group: number = 2, char: string = ':'): string {
|
|
||||||
mac = mac.replace(/[\W_]+/g, '');
|
|
||||||
return mac.match(new RegExp(`.{1,${group}}`, 'g'))!.join(char);
|
|
||||||
}
|
|
||||||
|
|
||||||
const input = ref('AA:BB:CC:DD:EE:FF');
|
const input = ref('AA:BB:CC:DD:EE:FF');
|
||||||
|
|
||||||
const formats = computed(() => [
|
const formats = computed(() => [
|
||||||
{
|
{
|
||||||
label: 'Canonical IETF Format:',
|
label: 'Canonical IETF Format:',
|
||||||
value: convertMac(input.value.toLocaleLowerCase()),
|
value: convertMacCanonicalIETF(input.value),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Canonical Format:',
|
label: 'Canonical Format:',
|
||||||
value: convertMac(input.value, 2, '.'),
|
value: convertMacCanonical(input.value),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Canonical IEEE Format:',
|
label: 'Canonical IEEE Format:',
|
||||||
value: convertMac(input.value.toLocaleUpperCase(), 2, '-'),
|
value: convertMacCanonicalIEEE(input.value),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Cisco:',
|
label: 'Cisco:',
|
||||||
value: convertMac(input.value.toLocaleLowerCase(), 4, '.'),
|
value: convertMacCISCO(input.value),
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue