mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-10 16:15:01 -04:00
![renovate[bot]](/assets/img/avatar_default.png)
* 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>
38 lines
832 B
TypeScript
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);
|
|
}
|