it-tools/src/tools/ipv4-address-converter/ipv4-address-converter.service.ts
renovate[bot] 6ff9a01cc8
chore(deps): update dependency @antfu/eslint-config to ^0.40.0 (#552)
* chore(deps): update dependency @antfu/eslint-config to ^0.40.0

* chore(deps): updated eslint

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Corentin Thomasset <corentin.thomasset74@gmail.com>
2023-08-21 18:27:08 +00:00

38 lines
832 B
TypeScript

import _ from 'lodash';
export { ipv4ToInt, ipv4ToIpv6, isValidIpv4 };
function ipv4ToInt({ ip }: { ip: string }) {
if (!isValidIpv4({ ip })) {
return 0;
}
return ip
.trim()
.split('.')
.reduce((acc, part, index) => acc + Number(part) * 256 ** (3 - index), 0);
}
function ipv4ToIpv6({ ip, prefix = '0000:0000:0000:0000:0000:ffff:' }: { ip: string; prefix?: string }) {
if (!isValidIpv4({ ip })) {
return '';
}
return (
prefix
+ _.chain(ip)
.trim()
.split('.')
.map(part => Number.parseInt(part).toString(16).padStart(2, '0'))
.chunk(2)
.map(blocks => blocks.join(''))
.join(':')
.value()
);
}
function isValidIpv4({ ip }: { ip: string }) {
const cleanIp = ip.trim();
return /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/.test(cleanIp);
}