Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
beaugunderson committed Sep 19, 2020
1 parent 6443a2e commit 1cfa26a
Show file tree
Hide file tree
Showing 14 changed files with 807 additions and 764 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
@@ -1,2 +1,2 @@
dist/
node_modules/
node_modules/
23 changes: 2 additions & 21 deletions .eslintrc.js
Expand Up @@ -8,13 +8,7 @@ module.exports = {

parser: '@typescript-eslint/parser',

plugins: [
'filenames',
'import',
'prettier',
'sort-imports-es6-autofix',
'@typescript-eslint',
],
plugins: ['filenames', 'import', 'prettier', 'sort-imports-es6-autofix', '@typescript-eslint'],

extends: ['airbnb', 'prettier'],

Expand Down Expand Up @@ -75,19 +69,6 @@ module.exports = {
},
],

// JSX
'jsx-a11y/anchor-is-valid': 'off',
'jsx-a11y/click-events-have-key-events': 'off',
'jsx-a11y/href-no-hash': 'off',
'jsx-a11y/img-has-alt': 'off',
'jsx-a11y/label-has-associated-control': 'off',
'jsx-a11y/label-has-for': 'off',
'jsx-a11y/no-noninteractive-element-interactions': 'warn',
'jsx-a11y/no-static-element-interactions': 'off',
'jsx-a11y/tabindex-no-positive': 'off',

'rulesdir/import-match-filename': 'off',

'@typescript-eslint/no-unused-vars': 'error',

// preferable to built in 'sort-imports' because it handles default imports better and has better auto-fix
Expand All @@ -96,4 +77,4 @@ module.exports = {
{ ignoreCase: true, memberSyntaxSortOrder: ['none', 'all', 'single', 'multiple'] },
],
},
};
};
2 changes: 1 addition & 1 deletion ip-address.ts
Expand Up @@ -6,4 +6,4 @@ export { Address6 };

import * as helpers from './lib/v6/helpers';

export const v6 = { helpers }
export const v6 = { helpers };
13 changes: 8 additions & 5 deletions lib/common.ts
@@ -1,5 +1,5 @@
import { Address4 } from "./ipv4";
import { Address6 } from "./ipv6";
import { Address4 } from './ipv4';
import { Address6 } from './ipv6';

// A wrapper function that returns false if the address is not valid; used to
// avoid boilerplate checks for `if (!this.valid) { return false; }`
Expand All @@ -11,9 +11,12 @@ export function falseIfInvalid<F extends (...args: any[]) => any>(fn: F) {

return fn.apply(this, args);
};
};
}

export const isInSubnet = falseIfInvalid(function (this: Address4 | Address6, address: Address4 | Address6) {
export const isInSubnet = falseIfInvalid(function (
this: Address4 | Address6,
address: Address4 | Address6
) {
if (this.subnetMask < address.subnetMask) {
return false;
}
Expand All @@ -26,7 +29,7 @@ export const isInSubnet = falseIfInvalid(function (this: Address4 | Address6, ad
});

export const isCorrect = function (defaultBits: number) {
return falseIfInvalid(function (this: Address4 | Address6, ) {
return falseIfInvalid(function (this: Address4 | Address6) {
if (this.addressMinusSuffix !== this.correctForm()) {
return false;
}
Expand Down
127 changes: 60 additions & 67 deletions lib/ipv4.ts
@@ -1,11 +1,11 @@
import { BigInteger } from 'jsbn';
import { sprintf } from 'sprintf-js';

import padStart from 'lodash.padstart';
import repeat from 'lodash.repeat';
/* eslint-disable no-param-reassign */

import * as common from './common';
import * as constants from './v4/constants';
import padStart from 'lodash.padstart';
import repeat from 'lodash.repeat';
import { BigInteger } from 'jsbn';
import { sprintf } from 'sprintf-js';

/**
* Represents an IPv4 address
Expand All @@ -27,12 +27,12 @@ export class Address4 {
constructor(address: string) {
this.address = address;

var subnet = constants.RE_SUBNET_STRING.exec(address);
const subnet = constants.RE_SUBNET_STRING.exec(address);

if (subnet) {
this.parsedSubnet = subnet[0].replace('/', '');
this.subnetMask = parseInt(this.parsedSubnet, 10);
this.subnet = '/' + this.subnetMask;
this.subnet = `/${this.subnetMask}`;

if (this.subnetMask < 0 || this.subnetMask > constants.BITS) {
this.valid = false;
Expand All @@ -53,7 +53,7 @@ export class Address4 {
* Parses a v4 address
*/
parse(address: string) {
var groups = address.split('.');
const groups = address.split('.');

if (address.match(constants.RE_ADDRESS)) {
this.valid = true;
Expand All @@ -62,7 +62,7 @@ export class Address4 {
}

return groups;
};
}

/**
* Return true if the address is valid
Expand All @@ -72,19 +72,17 @@ export class Address4 {
*/
isValid(): boolean {
return this.valid;
};
}

/**
* Returns the correct form of an address
* @memberof Address4
* @instance
* @returns {String}
*/
correctForm() {
return this.parsedAddress.map(function (part) {
return parseInt(part, 10);
}).join('.');
};
correctForm(): string {
return this.parsedAddress.map((part) => parseInt(part, 10)).join('.');
}

/**
* Returns true if the address is correct, false otherwise
Expand All @@ -101,19 +99,19 @@ export class Address4 {
* @param {string} hex - a hex string to convert
* @returns {Address4}
*/
static fromHex(hex: string) {
var padded = padStart(hex.replace(/:/g, ''), 8, '0');
var groups = [];
var i;
static fromHex(hex: string): Address4 {
const padded = padStart(hex.replace(/:/g, ''), 8, '0');
const groups = [];
let i;

for (i = 0; i < 8; i += 2) {
var h = padded.slice(i, i + 2);
const h = padded.slice(i, i + 2);

groups.push(parseInt(h, 16));
}

return new Address4(groups.join('.'));
};
}

/**
* Converts an integer into a IPv4 address object
Expand All @@ -122,9 +120,9 @@ export class Address4 {
* @param {integer} integer - a number to convert
* @returns {Address4}
*/
static fromInteger(integer: number) {
static fromInteger(integer: number): Address4 {
return Address4.fromHex(integer.toString(16));
};
}

/**
* Converts an IPv4 address object to a hex string
Expand All @@ -133,10 +131,8 @@ export class Address4 {
* @returns {String}
*/
toHex(): string {
return this.parsedAddress.map(function (part) {
return sprintf('%02x', parseInt(part, 10));
}).join(':');
};
return this.parsedAddress.map((part) => sprintf('%02x', parseInt(part, 10))).join(':');
}

/**
* Converts an IPv4 address object to an array of bytes
Expand All @@ -145,10 +141,8 @@ export class Address4 {
* @returns {Array}
*/
toArray(): number[] {
return this.parsedAddress.map(function (part) {
return parseInt(part, 10);
});
};
return this.parsedAddress.map((part) => parseInt(part, 10));
}

/**
* Converts an IPv4 address object to an IPv6 address group
Expand All @@ -157,19 +151,21 @@ export class Address4 {
* @returns {String}
*/
toGroup6(): string {
var output = [];
var i;
const output = [];
let i;

for (i = 0; i < constants.GROUPS; i += 2) {
var hex = sprintf('%02x%02x',
const hex = sprintf(
'%02x%02x',
parseInt(this.parsedAddress[i], 10),
parseInt(this.parsedAddress[i + 1], 10));
parseInt(this.parsedAddress[i + 1], 10)
);

output.push(sprintf('%x', parseInt(hex, 16)));
}

return output.join(':');
};
}

/**
* Returns the address as a BigInteger
Expand All @@ -182,10 +178,11 @@ export class Address4 {
return null;
}

return new BigInteger(this.parsedAddress.map(function (n) {
return sprintf('%02x', parseInt(n, 10));
}).join(''), 16);
};
return new BigInteger(
this.parsedAddress.map((n) => sprintf('%02x', parseInt(n, 10))).join(''),
16
);
}

/**
* Helper function getting start address.
Expand All @@ -194,10 +191,8 @@ export class Address4 {
* @returns {BigInteger}
*/
_startAddress(): BigInteger {
return new BigInteger(
this.mask() + repeat('0', constants.BITS - this.subnetMask), 2
);
};
return new BigInteger(this.mask() + repeat('0', constants.BITS - this.subnetMask), 2);
}

/**
* The first address in the range given by this address' subnet.
Expand All @@ -206,9 +201,9 @@ export class Address4 {
* @instance
* @returns {Address4}
*/
startAddress() {
startAddress(): Address4 {
return Address4.fromBigInteger(this._startAddress());
};
}

/**
* The first host address in the range given by this address's subnet ie
Expand All @@ -217,22 +212,20 @@ export class Address4 {
* @instance
* @returns {Address4}
*/
startAddressExclusive() {
var adjust = new BigInteger('1');
startAddressExclusive(): Address4 {
const adjust = new BigInteger('1');
return Address4.fromBigInteger(this._startAddress().add(adjust));
};
}

/**
* Helper function getting end address.
* @memberof Address4
* @instance
* @returns {BigInteger}
*/
_endAddress() {
return new BigInteger(
this.mask() + repeat('1', constants.BITS - this.subnetMask), 2
);
};
_endAddress(): BigInteger {
return new BigInteger(this.mask() + repeat('1', constants.BITS - this.subnetMask), 2);
}

/**
* The last address in the range given by this address' subnet
Expand All @@ -241,9 +234,9 @@ export class Address4 {
* @instance
* @returns {Address4}
*/
endAddress() {
endAddress(): Address4 {
return Address4.fromBigInteger(this._endAddress());
};
}

/**
* The last host address in the range given by this address's subnet ie
Expand All @@ -252,10 +245,10 @@ export class Address4 {
* @instance
* @returns {Address4}
*/
endAddressExclusive() {
var adjust = new BigInteger('1');
endAddressExclusive(): Address4 {
const adjust = new BigInteger('1');
return Address4.fromBigInteger(this._endAddress().subtract(adjust));
};
}

/**
* Converts a BigInteger to a v4 address object
Expand All @@ -264,9 +257,9 @@ export class Address4 {
* @param {BigInteger} bigInteger - a BigInteger to convert
* @returns {Address4}
*/
static fromBigInteger(bigInteger: BigInteger) {
static fromBigInteger(bigInteger: BigInteger): Address4 {
return Address4.fromInteger(parseInt(bigInteger.toString(), 10));
};
}

/**
* Returns the first n bits of the address, defaulting to the
Expand All @@ -281,7 +274,7 @@ export class Address4 {
}

return this.getBitsBase2(0, mask);
};
}

/**
* Returns the bits in the given range as a base-2 string
Expand All @@ -291,7 +284,7 @@ export class Address4 {
*/
getBitsBase2(start: number, end: number): string {
return this.binaryZeroPad().slice(start, end);
};
}

/**
* Returns true if the given address is in the subnet of the current address
Expand All @@ -307,17 +300,17 @@ export class Address4 {
* @instance
* @returns {boolean}
*/
isMulticast() {
isMulticast(): boolean {
return this.isInSubnet(new Address4('224.0.0.0/4'));
};
}

/**
* Returns a zero-padded base-2 string representation of the address
* @memberof Address4
* @instance
* @returns {string}
*/
binaryZeroPad() {
binaryZeroPad(): string {
return padStart(this.bigInteger().toString(2), constants.BITS, '0');
};
}
}

0 comments on commit 1cfa26a

Please sign in to comment.