UNPKG

1.57 kBMarkdownView Raw
1# ip-bigint
2[![](https://img.shields.io/npm/v/ip-bigint.svg?style=flat)](https://www.npmjs.org/package/ip-bigint) [![](https://img.shields.io/npm/dm/ip-bigint.svg)](https://www.npmjs.org/package/ip-bigint) [![](https://img.shields.io/bundlephobia/minzip/ip-bigint.svg)](https://bundlephobia.com/package/ip-bigint)
3
4> Convert IPv4 and IPv6 addresses to native BigInt and vice-versa
5
6## Usage
7
8```js
9import {parseIp, stringifyIp, normalizeIp} from "ip-bigint";
10
11parseIp("2001:db8::");
12// => {number: 42540766411282592856903984951653826560n, version: 6}
13
14stringifyIp({number, version});
15// => "2001:db8::"
16
17normalizeIp("2001:db8::0:0:1")
18// => "2001:db8::1"
19
20```
21
22## API
23
24### parseIp(ip)
25
26Parse a IP address string to a object (with null prototype).
27
28For IPv4 returns `{number, version}`.
29For IPv6 returns `{number, version, [ipv4mapped], [scopeid]}`.
30
31There is only rudimentary validation that the passed string is actually an IP address. You are encouraged to validate yourself using modules like `ip-regex`.
32
33### stringifyIp({number, version, [ipv4mapped], [scopeid]})
34
35Convert a object to string. Returns `ip`. For IPv6, `ip` is normalized to the "best representation" all-lowercase shortest possible form.
36
37### normalizeIp(ip)
38
39Round-trip an IP address through `parseIp` and `stringifyIp`, effectively normalizing its representation.
40
41### Constants
42
43The module exports `max4` and `max6` properties which represent the biggest possible BigInt for IPv4 and IPv6 respectively.
44
45## License
46
47© [silverwind](https://github.com/silverwind), distributed under BSD licence