Skip to content

Commit 0b98d32

Browse files
committed
Update README.md
1 parent 1a1b254 commit 0b98d32

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

README.md

+38-36
Original file line numberDiff line numberDiff line change
@@ -10,85 +10,87 @@ $ npm install @hownetworks/ipv46
1010

1111
# Usage
1212

13-
On environments that support the **import** syntax:
14-
15-
```js
16-
import parseIP from "@hownetworks/ipv46";
17-
```
18-
19-
On environments that don't support the above syntax (e.g. default Node.js):
20-
2113
```js
22-
const parseIP = require("@hownetworks/ipv46").default;
14+
const { IP } = require("@hownetworks/ipv46");
2315
```
2416

25-
## parseIP(string)
17+
## IP.parse(string)
2618

2719
Returns the given string parsed into an IPv4 or IPv6 address object.
2820
If the string is not a valid address then the result is null.
2921

3022
```js
31-
parseIP("192.0.2.1"); // IPv4 { ... }
32-
parseIP("2001:db8::1"); // IPv6 { ... }
33-
parseIP("non-address"); // null
23+
IP.parse("192.0.2.1"); // IPv4 { ... }
24+
IP.parse("2001:db8::1"); // IPv6 { ... }
25+
IP.parse("non-address"); // null
3426
```
3527

36-
**parseIP** supports IPv6 addresses with embedded IPv4 addresses.
28+
**IP.parse** supports IPv6 addresses with embedded IPv4 addresses.
3729

3830
```js
39-
parseIP("2001:db8::192.0.2.1"); // IPv6 { ... }
31+
IP.parse("2001:db8::192.0.2.1"); // IPv6 { ... }
4032
```
4133

42-
## ip.version
34+
## IP#version
4335

4436
Valid IPv4/6 address objects have their version as an attribute.
4537

4638
```js
47-
parseIP("192.0.2.1").version; // 4
48-
parseIP("2001:db8::1").version; // 6
39+
IP.parse("192.0.2.1").version; // 4
40+
IP.parse("2001:db8::1").version; // 6
4941
```
5042

51-
## ip.toString()
43+
## IP#toString()
5244

5345
Address objects implement the **toString** method for turning the addresses back into strings. The strings are printed lower-cased sans any extra leading zeroes. IPv6 formatting follows the [RFC 5952](https://tools.ietf.org/html/rfc5952) recommendations, except that formatting doesn't output IPv6 addresses with embedded IPv4 addresses.
5446

5547
```js
56-
parseIP("192.0.2.1").toString(); // '192.0.2.1'
57-
parseIP("2001:db8::1").toString(); // '2001:db8::1'
58-
parseIP("2001:db8::192.0.2.1").toString(); // '2001:db8::c000:201'
48+
IP.parse("192.0.2.1").toString(); // '192.0.2.1'
49+
IP.parse("2001:db8::1").toString(); // '2001:db8::1'
50+
IP.parse("2001:db8::192.0.2.1").toString(); // '2001:db8::c000:201'
5951
```
6052

61-
## ip.cmp(other)
53+
## IP.cmp(other)
6254

63-
Compare and sort addresses. **a.cmp(b)** returns:
55+
Compare and sort addresses. **IP.cmp(a, b)** returns:
6456
* **-1** if **a** is sorted before **b**
6557
* **0** if **a** equals **b**
6658
* **1** otherwise
67-
59+
6860
```js
69-
const a = parseIP("192.0.2.1");
70-
const b = parseIP("203.0.113.0");
61+
const a = IP.parse("192.0.2.1");
62+
const b = IP.parse("203.0.113.0");
7163

72-
a.cmp(a); // 0
73-
a.cmp(b); // -1
74-
b.cmp(a); // 1
64+
IP.cmp(a, a); // 0
65+
IP.cmp(a, b); // -1
66+
IP.cmp(b, a); // 1
7567
```
7668

7769
IPv4 addresses are always sorted before IPv6 addresses.
7870

7971
```js
80-
const ipv4 = parseIP("192.0.2.1");
81-
const ipv6 = parseIP("2001:db8::1");
72+
const ipv4 = IP.parse("192.0.2.1");
73+
const ipv6 = IP.parse("2001:db8::1");
8274

83-
ipv4.cmp(ipv6); // -1
75+
IP.cmp(ipv4, ipv6); // -1
8476
```
8577

8678
Parsed addresses get normalized. For example extra leading zeroes don't
8779
matter in comparisons.
8880

8981
```js
90-
const a = parseIP("2001:0db8::1");
91-
const b = parseIP("2001:0db8:0000::0001")
82+
const a = IP.parse("2001:0db8::1");
83+
const b = IP.parse("2001:0db8:0000::0001")
84+
85+
IP.cmp(a, b); // 0
86+
```
87+
88+
**IP.cmp** is directly compatible with **Array#sort**.
89+
90+
```js
91+
const a = IP.parse("2001:0db8::2");
92+
const b = IP.parse("2001:0db8::1")
93+
const c = IP.parse("2001:0db8::")
9294

93-
a.cmp(b); // 0
95+
[a, b, c].sort(IP.cmp); // [c, b, a]
9496
```

0 commit comments

Comments
 (0)