Tuesday, June 18, 2019

Iterables and Iterators in JavaScript

This post is the second in a series of posts on Symbols, Iterables, Iterators and Generators. The previous post in the series was about Symbols in JavaScript. This post on the other hand, will be focusing on explaining what Iterables and Iterators are in JavaScript.

It contains the following sections:

Saturday, June 15, 2019

Symbols In JavaScript

This post is part of the series on Symbols, Iterables, Iterators and Generators. It focuses on Symbols in JavaScript.

My first encounter with Symbol in JavaScript was when I wanted to turn some classes in the ip-num library to iterables in other for them to be used in the "for of" syntax JavaScript provides.

The code involving Symbol that I ended up writing looked somewhat like this:

 [Symbol.iterator](): IterableIterator<IPv4> {
      return this;
  }

You can see one of such places where this exist in the ip-num codebase here

Back then, this piece of code totally threw me off, as I have never encountered something like this previously in JavaScript, and I had to take some time out to dig into Symbol in order to understand what was going on.

This post is a jot down of some of the key things I got to learn about Symbol. It also marks the beginning of a series of posts on Iterables and Iterators in JavaScript.

This post on Symbol will contain the following:

Friday, June 14, 2019

Symbols, Iterables, Iterators and Generators

This post introduces a series of posts that would be about Symbols, Iterables, Iterators and Generators in JavaScript and TypeScript.

It would capture some of the things I learnt about these topics while working on ip-num: a TypeScript library for working with ASN, IPv4, and IPv6 numbers.

The posts in this series include:

  • Symbols In JavaScript
  • Iterables and Iterators in JavaScript [not published yet]
  • Typing Iterables and Iterators with TypeScript [not published yet]
  • Generators and Iterators in JavaScript [not published yet]


Sunday, June 02, 2019

How to Quickly Calculate Exponents With Base 2

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

This post is about how to quickly calculate exponents with base 2.

Unfortunately I am yet to find a magical tip to use for this task that does not involve having to memorise the result of some calculations, so the core of being able to quickly perform exponents of base 2, involves some memorisation.

Below are the calculations I have come to memorise:

21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
216 = 65536

The only tip to know is that with each increment of the exponent, the value of the calculation doubles. For example 26 is 64, increasing the exponent by 1, ie 27 leads to 128 which is a doubling of 64.

With this, I can then easily deduce the value of 217 as the double of 65536 which is 131072.



Previous post: How To Tell If Two IP Ranges Are Adjacent

Saturday, June 01, 2019

How To Tell If Two IP Ranges Are Adjacent

This post is about how to tell if two IP ranges are adjacent. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How to tell if two IP ranges are adjacent


Task:
You have two CIDR notations representing two IP ranges, and you want to tell if they are adjacent, or not.

Steps to answer:
  • The prefix value must be the same. CIDR notations with different prefix can never be adjacent
  • Used the steps outlined in How to quickly tell the first and last IP in a CIDR range, to figure out the first and last ip numbers in the two ranges
  • Check if adding 1 to the last IP address of one will lead to the starting IP address of the other. If so, then the two ranges are adjacent

Example:
Determine if 10.0.4.0/24 and 10.0.5.0/24 are adjacent




Previous post: How To Calculate How Many Smaller IP Ranges Can Be Gotten From a Bigger IP Range
Next post: How to quickly calculate exponents of base 2

How To Calculate How Many Smaller IP Ranges Can Be Gotten From a Bigger IP Range

This post is about how to calculate how many smaller IP range are there in a bigger IP range. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How many smaller IP ranges can be gotten from a bigger IP range


Task:
You have a big IP range, and you want to break it into smaller IP range, and you want to know how many of those smaller IP range you will end up with. For example, having a /22, how many /25 are in a /22

Steps to answer:
  • Subtract the ranges.
  • Calculate 2 raised to the power of the value above.
  • The number in the previous step will be the amount of the smaller range in the big range

Example:
How much /25 are in /22?
  • 25 - 22 = 3
  • 2 raised to power 3 = 8
  • There are 8 /25 in /22


Previous post: How To Quickly Tell The First And Last IP In a CIDR Range
Next post: How to tell if two IP ranges are adjacent

How To Quickly Tell The First And Last IP In a CIDR Range

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

In this post I would show the steps I have come to use to be able to determine the start IP address depicted by a CIDR notation.

How to quickly tell the first and last ip in a CIDR range

Task: How to tell the first and last IP number in a CIDR range. Ideally the format of a CIDR range dictates that the portion of the notation that depicts the IP number should be the first number in the range, but often times, you cannot be sure that this is being adhered to when you encounter CIDR notations in the wild, so it is useful to be able to look at a CIDR notation, and tell which are the first and last IP addresses and whether the IP portion of the notation is indeed the first IP in the range.

Step to answer: Specialised to IPv4
  • Have /8, /16 and /24 as prefix bit boundaries 
  • If the prefix is any of the prefix boundary, then the starting octet will be zero and will always be in the 2nd, 3rd and 4th octet for /8, /16, /24 respectively. If the prefix under consideration is not the bit boundary, then continue with the following steps
  • Pick the bit boundary prefix which is least smaller than the prefix of the CIDR notation you are reading. So: 
    • if you have /26, pick /24
    • If you have /23, pick /16
    • If you have /19, pick /16
    • If you have /12, pick /8
    • If you have a prefix less than 8, pick /8 then follow the steps for prefix less than /8 below
  • Subtract the prefix in your CIDR notation, from the bit boundary prefix you picked above. So If you have a 10.3.4.5/19 do 19 - 16 = 3
  • Subtract the number above from 8. In this case that would be 8 - 3 = 5 
  • Find 2 raised to the power of the subtraction. For above that will be 25 = 32
  • This means the starting octet of the first IP number must be a multiple of the value calculated in previous step. For example, in previous step, the calculated value is 32, hence the starting octet must be a multiple of 32 - 32, 64, 96 etc
  • Pick which of the four octets would contain the starting octet based on the bit boundary prefix picked
  • If you pick /24, the 3rd octet will be fixed, starting will be on the 4th octet: fixed.fixed.fixed.start
  • If you pick /16, the 2nd octet will be fixed, starting will be on the 3rd octet:
    fixed.fixed.start.x
  • If you pick /8 the 1st octet will be fixed, starting will be on the 2nd octet:
    fixed.start.x.x