DEV Community

Cover image for Unraveling IPv6 with `is.ipv6` and `is.not_ipv6` in JavaScript: Navigating the Digital Highway
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Unraveling IPv6 with `is.ipv6` and `is.not_ipv6` in JavaScript: Navigating the Digital Highway

Welcome to the world of IP addresses, where every device on the internet has its unique identifier. Among these identifiers, IPv6 stands out as a futuristic address, and handling it in JavaScript is a breeze with the is.ipv6 method and its trusty sidekick, is.not_ipv6 from the 'thiis' package. In this article, we'll embark on a journey to explore these tools, demystify IPv6, and sprinkle in some practical examples.

Understanding the IPv6 Landscape

Image description

Before we dive into the magical world of is.ipv6, let's briefly talk about IPv6. Internet Protocol version 6 (IPv6) is the latest version of the Internet Protocol. It provides a vastly expanded address space compared to its predecessor, IPv4, allowing for an almost limitless number of unique IP addresses.

Meet is.ipv6 - Your IPv6 Navigator

Documentation link

Imagine you're on a quest to determine whether a given value is a valid IPv6 address. The is.ipv6 method is like your digital navigator, helping you sail smoothly through the IPv6 landscape. Let's witness its magic:

import { is } from 'thiis'; // Import the "is" object from the "thiis" package

const ipAddress = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
const result = is.ipv6(ipAddress);

console.log(result); // true
Enter fullscreen mode Exit fullscreen mode

In this example, we import the "is" object from the "thiis" package and use the is.ipv6 method to confirm that ipAddress is indeed a valid IPv6 address. As expected, it returns true because the value is indeed a valid IPv6 address.

The Journey of Examples

Now, let's embark on a journey through practical examples that showcase the versatility of is.ipv6 and its companion, is.not_ipv6. We'll explore six unique scenarios, including an exciting duo involving stream$ and an array.

1. Validating IPv6 Addresses

The primary use of is.ipv6 is to validate whether a given string is a valid IPv6 address. This is crucial for handling user inputs or parsing data from external sources:

import { is } from 'thiis';

const userEnteredIP = getUserInput();

if (is.ipv6(userEnteredIP)) {
  // It's a valid IPv6 address! Proceed with confidence.
} else {
  // Handle invalid inputs gracefully.
Enter fullscreen mode Exit fullscreen mode

2. Guarding Against Non-IPv6 Values

Conversely, is.not_ipv6 acts as your guardian against non-IPv6 values. Use it when you want to ensure that a value is not an IPv6 address before proceeding:

import { is } from 'thiis';

const potentialIPAddress = someFunctionThatMayReturnAnything();

if (is.not_ipv6(potentialIPAddress)) {
  // Guard against non-IPv6 values. Your digital guardian at work!
} else {
  // Continue exploring the IPv6 landscape.
Enter fullscreen mode Exit fullscreen mode

3. Handling IPv6 in Stream$

Let's integrate our IPv6 navigator into an RxJS stream. Using filter and is.ipv6, we can ensure that the stream processes only valid IPv6 addresses:

import { is } from 'thiis';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const ipv6Stream$ = from(['2001:0db8::1', 'notIPv6', 'fe80::1', 'anotherOne']);

  .subscribe(ipv6Address => {
    console.log(ipv6Address); // Only valid IPv6 addresses will be part of the stream's journey.
Enter fullscreen mode Exit fullscreen mode

Here, the filter(is.ipv6) ensures that only valid IPv6 addresses get processed by the stream.

4. Checking an Array of IP Addresses

Arrays are another playground for is.ipv6. Use every() to confirm that all elements are valid IPv6 addresses, and some() to check if at least one is:

import { is } from 'thiis';

const ipv6Addresses = ['2001:0db8::1', 'fe80::1', 'notIPv6', 'anotherOne'];

const allAreIPv6 = ipv6Addresses.every(is.ipv6); // false
const someAreIPv6 = ipv6Addresses.some(is.ipv6); // true

Enter fullscreen mode Exit fullscreen mode

In this example, allAreIPv6 checks if all elements in ipv6Addresses are valid IPv6 addresses, and someAreIPv6 checks if at least one is.

5. Additional examples

is.ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334') // true
is.ipv6('2001:db8:0:0:0:0:2:1') // true
is.ipv6('2001:db8::2:1') // true
is.ipv6('::') // true
is.ipv6('::1') // true
is.ipv6('::ffff:') // true
is.ipv6('2001:db8::567:1') // true

is.ipv6(''); // false
is.ipv6(''); // false
is.ipv6(''); // false
is.ipv6(''); // false
is.ipv6(`a`); // false
is.ipv6("r"); // false
is.ipv6('t'); // false
is.ipv6(`3`); // false
is.ipv6(`1`); // false
is.ipv6(`2`); // false
is.ipv6('hello'); // false
is.ipv6('hello123'); // false
is.ipv6('helloWorld'); // false
is.ipv6('a1B2c3d4e5f6g'); // false
is.ipv6('a1B2c3d4e5f6'); // false
is.ipv6('inCamelCase'); // false
is.ipv6('undefined'); // false
is.ipv6('null'); // false
is.ipv6(BigInt(1)) // false
is.ipv6(0) // false
is.ipv6(1) // false
is.ipv6(-1) // false
is.ipv6(Symbol()) // false
is.ipv6(null) // false
is.ipv6(true) // false
is.ipv6([]) // false
is.ipv6(false) // false
is.ipv6("") // false
is.ipv6('') // false
is.ipv6(``) // false
is.ipv6({}) // false
is.ipv6(undefined) // false
is.ipv6(Function) // false
is.ipv6(() => {}) // false
is.ipv6(BigInt) // false
is.ipv6(Symbol) // false
is.ipv6(NaN) // false
is.ipv6(Infinity) // false
is.ipv6(-Infinity) // false
is.ipv6(Number.POSITIVE_INFINITY) // false
is.ipv6(Number.NEGATIVE_INFINITY) // false
// And all other known types will return false
Enter fullscreen mode Exit fullscreen mode
is.not_ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334') // false
is.not_ipv6('2001:db8:0:0:0:0:2:1') // false
is.not_ipv6('2001:db8::2:1') // false
is.not_ipv6('::') // false
is.not_ipv6('::1') // false
is.not_ipv6('::ffff:') // false
is.not_ipv6('2001:db8::567:1') // false

is.not_ipv6(''); // true
is.not_ipv6(''); // true
is.not_ipv6(''); // true
is.not_ipv6(''); // true
is.not_ipv6(`a`); // true
is.not_ipv6("r"); // true
is.not_ipv6('t'); // true
is.not_ipv6(`3`); // true
is.not_ipv6(`1`); // true
is.not_ipv6(`2`); // true
is.not_ipv6('hello'); // true
is.not_ipv6('hello123'); // true
is.not_ipv6('helloWorld'); // true
is.not_ipv6('a1B2c3d4e5f6g'); // true
is.not_ipv6('a1B2c3d4e5f6'); // true
is.not_ipv6('inCamelCase'); // true
is.not_ipv6('undefined'); // true
is.not_ipv6('null'); // true
is.not_ipv6(BigInt(1)) // true
is.not_ipv6(0) // true
is.not_ipv6(1) // true
is.not_ipv6(-1) // true
is.not_ipv6(Symbol()) // true
is.not_ipv6(null) // true
is.not_ipv6(true) // true
is.not_ipv6([]) // true
is.not_ipv6(false) // true
is.not_ipv6("") // true
is.not_ipv6('') // true
is.not_ipv6(``) // true
is.not_ipv6({}) // true
is.not_ipv6(undefined) // true
is.not_ipv6(Function) // true
is.not_ipv6(() => {}) // true
is.not_ipv6(BigInt) // true
is.not_ipv6(Symbol) // true
is.not_ipv6(NaN) // true
is.not_ipv6(Infinity) // true
is.not_ipv6(-Infinity) // true
is.not_ipv6(Number.POSITIVE_INFINITY) // true
is.not_ipv6(Number.NEGATIVE_INFINITY) // true
// And all other known types will return true
Enter fullscreen mode Exit fullscreen mode

6. Combination example

is.ipv4_or_ipv6('2001:db8:0:0:0:0:2:1') // true
is.ipv6_or_ipv4('2001:db8:0:0:0:0:2:1') // true
Enter fullscreen mode Exit fullscreen mode

The Adventure Continues

The is.ipv6 and is.not_ipv6 methods from the 'thiis' package are your trusty guides on the digital highway of JavaScript. They make handling IPv6 addresses easy and ensure your code interacts with them exactly as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the IPv6 landscape with confidence and a sense of adventure.

So, keep exploring, and remember that the digital highway is full of exciting possibilities!

🎗 ChatGPT & DALL·E 3

Top comments (0)