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
|
9 | import {parseIp, stringifyIp, normalizeIp} from "ip-bigint";
|
10 |
|
11 | parseIp("2001:db8::");
|
12 | // => {number: 42540766411282592856903984951653826560n, version: 6}
|
13 |
|
14 | stringifyIp({number, version});
|
15 | // => "2001:db8::"
|
16 |
|
17 | normalizeIp("2001:db8::0:0:1")
|
18 | // => "2001:db8::1"
|
19 |
|
20 | ```
|
21 |
|
22 | ## API
|
23 |
|
24 | ### parseIp(ip)
|
25 |
|
26 | Parse a IP address string to a object (with null prototype).
|
27 |
|
28 | For IPv4 returns `{number, version}`.
|
29 | For IPv6 returns `{number, version, [ipv4mapped], [scopeid]}`.
|
30 |
|
31 | There 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 |
|
35 | Convert 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 |
|
39 | Round-trip an IP address through `parseIp` and `stringifyIp`, effectively normalizing its representation.
|
40 |
|
41 | ### Constants
|
42 |
|
43 | The 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
|