Merge branch 'feat/ipv6-address-converter' into chore/all-my-stuffs

# Conflicts:
#	package.json
#	pnpm-lock.yaml
#	src/tools/index.ts
#	src/tools/integer-base-converter/integer-base-converter.model.test.ts
#	src/tools/integer-base-converter/integer-base-converter.model.ts
#	src/utils/ip.test.ts
This commit is contained in:
ShareVB committed 2024-11-03 22:57:26 +01:00
commit b3969d37c7
8 files changed
+229 -5

No files matched your search

+2
View File
@@ -208,6 +208,7 @@ import { tool as htmlToMarkdown } from './html-to-markdown';
import { tool as pdfUnlock } from './pdf-unlock';
import { tool as ipCidrToRange } from './ip-cidr-to-range';
import { tool as ipRangeToCidr } from './ip-range-to-cidr';
import { tool as ipv6AddressConverter } from './ipv6-address-converter';
export const toolsByCategory: ToolCategory[] = [
{
@@ -407,6 +408,7 @@ export const toolsByCategory: ToolCategory[] = [
ipv4AddressConverter,
// ipv6AddressConverter,
ipInRange,
ipv6AddressConverter,
ipv4RangeExpander,
macAddressLookup,
macAddressGenerator,
@@ -26,6 +26,9 @@ describe('integer-base-converter', () => {
expect(convertBase({ value: '0b10101010', fromBase: -1, toBase: 10 })).toEqual('170');
expect(convertBase({ value: '0b_1010_1010', fromBase: -1, toBase: 10 })).toEqual('170');
expect(convertBase({ value: '192,654', fromBase: 10, toBase: 8 })).toEqual('570216');
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 16 })).toEqual('20010db8000085a300000000ac1f8908');
expect(convertBase({ value: '20010db8000085a300000000ac1f8908', fromBase: 16, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
});
});
});
@@ -46,16 +46,16 @@ export function convertBase(
let decValue = cleanedValue
.split('')
.reverse()
.reduce((carry: number, digit: string, index: number) => {
.reduce((carry: bigint, digit: string, index: number) => {
if (!fromRange.includes(digit)) {
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
}
return (carry += fromRange.indexOf(digit) * finalFromBase ** index);
}, 0);
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index));
}, 0n);
let newValue = '';
while (decValue > 0) {
newValue = toRange[decValue % toBase] + newValue;
decValue = (decValue - (decValue % toBase)) / toBase;
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
}
return newValue || '0';
}
+43
View File
@@ -0,0 +1,43 @@
declare module 'cidr-tools' {
type IPv4Address = string;
type IPv4CIDR = string;
type IPv6Address = string;
type IPv6CIDR = string;
type Network = IPv4Address | IPv4CIDR | IPv6Address | IPv6CIDR;
type Networks = Network | Network[];
type Parsed = {
cidr: string;
version: number;
prefix: string;
start: bigint;
end: bigint;
single: boolean;
ip: string;
};
type NormalizeOpts = {
compress?: boolean;
hexify?: boolean;
};
export function merge(networks: Networks): Network[];
export function exclude(baseNetworks: Networks, excludeNetworks: Networks): Network[];
export function expand(networks: Networks): Network[];
export function overlap(networksA: Networks, networksB: Networks): boolean;
export function normalize(cidr: Networks, opts?: NormalizeOpts): Networks;
export function contains(networksA: Networks, networksB: Networks): boolean;
export function parse(network: Network): Parsed;
declare const _default: {
merge: typeof merge;
exclude: typeof exclude;
expand: typeof expand;
overlap: typeof overlap;
normalize: typeof normalize;
contains: typeof contains;
parse: typeof parse;
};
export default _default;
}
+12
View File
@@ -0,0 +1,12 @@
import { Binary } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Ipv6 address converter',
path: '/ipv6-address-converter',
description: 'Convert an ip address into decimal, binary, hexadecimal and get infos',
keywords: ['ipv6', 'address', 'converter', 'decimal', 'hexadecimal', 'binary', 'ipv4'],
component: () => import('./ipv6-address-converter.vue'),
icon: Binary,
createdAt: new Date('2024-01-10'),
});
+17
View File
@@ -0,0 +1,17 @@
declare module 'ip-bigint' {
type IPInfo = {
number: bigint;
version: number;
ipv4mapped?: boolean;
scopeid?:string;
};
type StringifyOptions = {
compress?:boolean;
hexify?:boolean;
};
export function normalizeIp(ip: string, options: StringifyOptions = {compress = true, hexify = false} = {})
export function stringifyIp(ip: IPInfo, options: StringifyOptions = {compress = true, hexify = false}): string;
export function ipVersion(ip: string): number;
export function parseIp(ip): IPInfo;
}
@@ -0,0 +1,84 @@
<script setup lang="ts">
import { isIPv6 } from 'is-ip';
import { parse } from 'cidr-tools';
import { stringifyIp } from 'ip-bigint';
import { convertBase } from '../integer-base-converter/integer-base-converter.model';
import { getIPNetworkType, toARPA, toMicrosoftTranscription } from '@/utils/ip';
import { useValidation } from '@/composable/validation';
const rawIpAddress = useStorage('ipv6-converter:ip', '2001:db8:0:85a3::ac1f:8001'); // NOSONAR
const convertedSections = computed(() => {
try {
const parsedIPv6 = parse(rawIpAddress.value);
const ipInDecimal = parsedIPv6.start;
return [
{
label: 'Decimal: ',
value: String(ipInDecimal),
},
{
label: 'Hexadecimal: ',
value: convertBase({ fromBase: 10, toBase: 16, value: String(ipInDecimal) }).toUpperCase(),
},
{
label: 'Binary: ',
value: convertBase({ fromBase: 10, toBase: 2, value: String(ipInDecimal) }),
},
{
label: 'CIDR: ',
value: parsedIPv6.cidr,
},
{
label: 'Ipv6 (short): ',
value: stringifyIp({ number: ipInDecimal, version: 6 }, { compress: true }),
},
{
label: 'Ipv6 (long): ',
value: stringifyIp({ number: ipInDecimal, version: 6 }, { compress: false }),
},
{
label: 'ARPA: ',
value: toARPA(parsedIPv6.ip),
},
{
label: 'Microsoft Transcription: ',
value: toMicrosoftTranscription(parsedIPv6.ip),
},
{
label: 'Type: ',
value: getIPNetworkType(parsedIPv6.ip),
},
];
}
catch (e) {
return [];
}
});
const { attrs: validationAttrs } = useValidation({
source: rawIpAddress,
rules: [{ message: 'Invalid ipv6 address', validator: ip => isIPv6(ip) }],
});
</script>
<template>
<div>
<c-input-text v-model:value="rawIpAddress" label="The ipv6 address:" placeholder="The ipv6 address..." />
<n-divider />
<input-copyable
v-for="{ label, value } of convertedSections"
:key="label"
:label="label"
label-position="left"
label-width="100px"
label-align="right"
mb-2
:value="validationAttrs.validationStatus === 'error' ? '' : value"
placeholder="Set a correct ipv6 address"
/>
</div>
</template>
+63
View File
@@ -0,0 +1,63 @@
declare module 'is-ip' {
/**
Check if `string` is IPv6 or IPv4.
@example
```
import {isIP} from 'is-ip';
isIP('1:2:3:4:5:6:7:8');
//=> true
isIP('192.168.0.1');
//=> true
```
*/
export function isIP(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention
/**
Check if `string` is IPv6.
@example
```
import {isIPv6} from 'is-ip';
isIPv6('1:2:3:4:5:6:7:8');
//=> true
```
*/
export function isIPv6(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention
/**
Check if `string` is IPv4.
@example
```
import {isIPv4} from 'is-ip';
isIPv4('192.168.0.1');
//=> true
```
*/
export function isIPv4(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention
/**
@returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither.
@example
```
import {ipVersion} from 'is-ip';
ipVersion('1:2:3:4:5:6:7:8');
//=> 6
ipVersion('192.168.0.1');
//=> 4
ipVersion('abc');
//=> undefined
```
*/
export function ipVersion(string: string): 6 | 4 | undefined;
}