import * as bigInt from "big-integer/BigInteger" ; import { IPNumType } from "./IPNumType" ; param num returns { string } export let decimalNumberToBinaryString = ( num ) : string => { return Number ( num ) . toString ( 2 ) ; } ; param num returns { string } export let bigIntegerNumberToBinaryString = ( num ) : string => { return num . toString ( 2 ) ; } ; param { number } num returns { string } export let decimalNumberToOctetString = ( num ) : string => { let binaryString = decimalNumberToBinaryString ( num ) ; let length = binaryString . length ; if ( length > 8 ) { throw new Error ( "Given decimal in binary contains digits greater than an octet" ) ; } return leftPadWithZeroBit ( binaryString , 8 ) ; } ; param num returns { number } export let parseBinaryStringToBigInteger = ( num ) : bigInt . BigInteger => { return bigInt ( num , 2 ) ; } ; param dottedDecimal returns { string } export let dottedDecimalNotationToBinaryString = ( dottedDecimal ) : string => { let stringOctets = dottedDecimal . split ( "." ) ; return stringOctets . reduce ( ( binaryAsString , octet ) => { return binaryAsString . concat ( decimalNumberToOctetString ( parseInt ( octet ) ) ) ; } , '' ) ; } ; param { string } binaryString param { number } finalStringLength returns { string } export let leftPadWithZeroBit = ( binaryString , finalStringLength ) : string => { if ( binaryString . length > finalStringLength ) { throw new Error ( `Given string is already longer than given final length after padding: ${ finalStringLength }` ) ; } return "0" . repeat ( finalStringLength - binaryString . length ) . concat ( binaryString ) ; } ; param { number } cidrPrefix param { IPNumType . IPv4 | IPNumType . IPv6 } ipType export let cidrPrefixToMaskBinaryString = ( cidrPrefix , ipType ) : string => { let cidrUpperValue ; if ( ipType == IPNumType . IPv4 ) { cidrUpperValue = 32 ; } else { cidrUpperValue = 128 ; } if ( cidrPrefix > cidrUpperValue ) throw Error ( `Value is greater than ${ cidrUpperValue }` ) ; let onBits = '1' . repeat ( cidrPrefix ) ; let offBits = '0' . repeat ( cidrUpperValue - cidrPrefix ) ; return `${ onBits }${ offBits }` ; } ; throws param givenNumber return export let intLog2 = ( givenNumber ) : number => { let result = 0 ; while ( givenNumber . isEven ( ) ) { if ( givenNumber . equals ( bigInt ( 2 ) ) ) { result ++ ; break ; } givenNumber = givenNumber . shiftRight ( bigInt ( 1 ) ) ; if ( givenNumber . isOdd ( ) ) { result = 0 ; break ; } result ++ ; } if ( result == 0 ) { throw new Error ( `The value of log2 for ${ givenNumber . toString ( ) } is not an integer` ) ; } return result ; } ; param firstBinaryString param secondBinaryString export let matchingBitCount = ( firstBinaryString , secondBinaryString ) : number => { let longerString ; let otherString ; if ( firstBinaryString . length >= secondBinaryString . length ) { longerString = firstBinaryString ; otherString = secondBinaryString ; } else { longerString = secondBinaryString ; otherString = firstBinaryString ; } let count = 0 ; for ( ; count < longerString . length ; count ++ ) { if ( longerString . charAt ( count ) === otherString . charAt ( count ) ) { continue ; } break ; } return count ; } ;