Skip to content

Commit

Permalink
Update README.md badges
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Oct 15, 2024
1 parent a4555e4 commit 11a2379
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ipv46 [![tests](https://github.com/badrap/ipv46/workflows/tests/badge.svg)](https://github.com/badrap/ipv46/actions?query=workflow%3Atests) [![npm](https://img.shields.io/npm/v/@badrap/ipv46.svg)](https://www.npmjs.com/package/@badrap/ipv46)
# ipv46 [![CI](https://github.com/badrap/ipv46/actions/workflows/ci.yml/badge.svg)](https://github.com/badrap/ipv46/actions/workflows/ci.yml) [![npm](https://img.shields.io/npm/v/@badrap/ipv46.svg)](https://www.npmjs.com/package/@badrap/ipv46) [![JSR](https://jsr.io/badges/@badrap/ipv46)](https://jsr.io/@badrap/ipv46)

**ipv46** is a small JavaScript library for parsing, formatting and sorting IPv4/6 addresses. It works on both Node.js and browser environments.

Expand All @@ -23,50 +23,51 @@ Returns the given string parsed into an IPv4 or IPv6 address object.
If the string is not a valid address then the result is null.

```js
IP.parse("192.0.2.1"); // IPv4 { ... }
IP.parse("2001:db8::1"); // IPv6 { ... }
IP.parse("non-address"); // null
IP.parse("192.0.2.1"); // IPv4 { ... }
IP.parse("2001:db8::1"); // IPv6 { ... }
IP.parse("non-address"); // null
```

**IP.parse** supports IPv6 addresses with embedded IPv4 addresses.

```js
IP.parse("2001:db8::192.0.2.1"); // IPv6 { ... }
IP.parse("2001:db8::192.0.2.1"); // IPv6 { ... }
```

## IP#version

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

```js
IP.parse("192.0.2.1").version; // 4
IP.parse("2001:db8::1").version; // 6
IP.parse("192.0.2.1").version; // 4
IP.parse("2001:db8::1").version; // 6
```

## IP#toString()

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.

```js
IP.parse("192.0.2.1").toString(); // '192.0.2.1'
IP.parse("2001:db8::1").toString(); // '2001:db8::1'
IP.parse("2001:db8::192.0.2.1").toString(); // '2001:db8::c000:201'
IP.parse("192.0.2.1").toString(); // '192.0.2.1'
IP.parse("2001:db8::1").toString(); // '2001:db8::1'
IP.parse("2001:db8::192.0.2.1").toString(); // '2001:db8::c000:201'
```

## IP.cmp(other)

Compare and sort addresses. **IP.cmp(a, b)** returns:
* **-1** if **a** is sorted before **b**
* **0** if **a** equals **b**
* **1** otherwise

- **-1** if **a** is sorted before **b**
- **0** if **a** equals **b**
- **1** otherwise

```js
const a = IP.parse("192.0.2.1");
const b = IP.parse("203.0.113.0");

IP.cmp(a, a); // 0
IP.cmp(a, b); // -1
IP.cmp(b, a); // 1
IP.cmp(a, a); // 0
IP.cmp(a, b); // -1
IP.cmp(b, a); // 1
```

IPv4 addresses are always sorted before IPv6 addresses.
Expand All @@ -75,25 +76,25 @@ IPv4 addresses are always sorted before IPv6 addresses.
const ipv4 = IP.parse("192.0.2.1");
const ipv6 = IP.parse("2001:db8::1");

IP.cmp(ipv4, ipv6); // -1
IP.cmp(ipv4, ipv6); // -1
```

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

```js
const a = IP.parse("2001:0db8::1");
const b = IP.parse("2001:0db8:0000::0001")
const b = IP.parse("2001:0db8:0000::0001");

IP.cmp(a, b); // 0
IP.cmp(a, b); // 0
```

**IP.cmp** is directly compatible with **Array#sort**.

```js
const a = IP.parse("2001:0db8::2");
const b = IP.parse("2001:0db8::1")
const b = IP.parse("2001:0db8::1");
const c = IP.parse("2001:0db8::")

[a, b, c].sort(IP.cmp); // [c, b, a]
[(a, b, c)].sort(IP.cmp); // [c, b, a]
```

0 comments on commit 11a2379

Please sign in to comment.