From dffb29fde3326134c265ff3e5ef8b8bb2ad0e634 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 8 Mar 2023 03:46:32 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#64428=20Update=20t?= =?UTF-8?q?ypes=20for=20'netmask'=20package=20by=20@JanST123?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add types for netmask package * satisfy the tests * applied some advises from the bot standardized tsling.json, restore previous definition owner (sorry) --- types/netmask/index.d.ts | 95 ++++++++++++++++++++++------------ types/netmask/netmask-tests.ts | 17 ++---- types/netmask/tslint.json | 10 +--- 3 files changed, 68 insertions(+), 54 deletions(-) diff --git a/types/netmask/index.d.ts b/types/netmask/index.d.ts index 8bbcee715c2e78..483a241646bb94 100644 --- a/types/netmask/index.d.ts +++ b/types/netmask/index.d.ts @@ -1,46 +1,75 @@ -// Type definitions for Netmask 1.0.5 +// Type definitions for netmask 2.0 // Project: https://github.com/rs/node-netmask // Definitions by: Matt Frantz +// JanST123 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// netmask.d.ts +/** + * converts long to an ip address + */ +export function long2ip(long: number): string; +/** + * converts ip address to long + */ +export function ip2long(ip: string): number; - - -export declare function long2ip(long: number): string; -export declare function ip2long(ip: string): number; - -export declare class Netmask { - maskLong: number; - bitmask: number; - netLong: number; - // The number of IP address in the block (eg.: 254) - size: number; - // The address of the network block as a string (eg.: 216.240.32.0) +export class Netmask { + /** + * The base address of the network block as a string (eg: 216.240.32.0). Base does not give an indication of the size of the network block. + */ base: string; - // The netmask as a string (eg.: 255.255.255.0) + /** + * The netmask as a string (eg: 255.255.255.0). + */ mask: string; - // The host mask, the opposite of the netmask (eg.: 0.0.0.255) + /** + * The netmask as a number of bits in the network portion of the address for this block (eg: 24). + */ + bitmask: number; + /** + * The host mask which is the opposite of the netmask (eg: 0.0.0.255). + */ hostmask: string; - // The first usable address of the block + /** + * The blocks broadcast address (eg: 192.168.1.0/24 => 192.168.1.255) + */ + broadcast: string; + /** + * The number of IP addresses in a block (eg: 256). + */ + size: number; + /** + * First useable address + */ first: string; - // The last usable address of the block + /** + * Last useable address + */ last: string; - // The block's broadcast address: the last address of the block (eg.: 192.168.1.255) - broadcast: string; - - constructor(netmask: string); - constructor(net: string, mask: string); - - // Returns true if the given ip or netmask is contained in the block - contains(ip: string | Netmask | number): boolean; - - // Returns the Netmask object for the block which follow this one - next(count?: number): Netmask; - // Evaluate a function on each IP address - forEach(fn: (ip: string, long: number, index: number) => void): void; + /** + * Returns a true if the IP number ip is part of the network. That is, a true value is returned if ip is between base and broadcast. + * If a Netmask object or a block is given, it returns true only of the given block fits inside the network. + */ + contains: (address: string | Netmask | number) => boolean; + /** + * Similar to the Array prototype method. It loops through all the useable addresses, ie between first and last. + */ + forEach: (cb: (ip: string, long: number, index: number) => void) => void; + /** + * Without a count, return the next block of the same size after the current one. With a count, return the Nth block after the current one. + * A count of -1 returns the previous block. Undef will be returned if out of legal address space. + */ + next: (count?: number) => Netmask; + /** + * The netmask in base/bitmask format (e.g., '216.240.32.0/24') + */ + toString: () => string; - // Returns the complete netmask formatted as `base/bitmask` - toString(): string; + /** + * + * @param net A network - e.g 216.240.32.0/24 + * @param mask - optional netmask if not provided in `net` + */ + constructor(net: string, mask?: string); } diff --git a/types/netmask/netmask-tests.ts b/types/netmask/netmask-tests.ts index 4532ecd3b55aea..d4397331bf4316 100644 --- a/types/netmask/netmask-tests.ts +++ b/types/netmask/netmask-tests.ts @@ -1,15 +1,8 @@ - - import netmask = require('netmask'); -var address: string = '127.0.0.1'; - -var nm = new netmask.Netmask(address, '255.255.255.0'); - -var nm2 = new netmask.Netmask('127.0.0.1/255.255.255.0'); - -if (nm.contains('127.0.0.123')) {} - -nm.forEach((ip: string): void => console.log(ip)); +const block = new netmask.Netmask('10.0.0.0/12'); +console.log('base', block.base); -var adjacent: netmask.Netmask = nm.next(); +if (block.contains('10.0.8.10')) { + console.log('block contains 10.0.8.10'); +} diff --git a/types/netmask/tslint.json b/types/netmask/tslint.json index bc023cb705b944..2efa283002e7cf 100644 --- a/types/netmask/tslint.json +++ b/types/netmask/tslint.json @@ -1,11 +1,3 @@ { - "extends": "@definitelytyped/dtslint/dt.json", - "rules": { - "no-consecutive-blank-lines": false, - "no-inferrable-types": false, - "no-var-keyword": false, - "prefer-const": false, - "strict-export-declare-modifiers": false, - "unified-signatures": false - } + "extends": "@definitelytyped/dtslint/dt.json" }