From eeb54c4e39aae14ad7c4029573cc623520484cc1 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 31 Dec 2018 16:59:46 +0000 Subject: [PATCH] [ip] Improve types (#31641) --- types/ip/index.d.ts | 181 ++++++++++++++++++++--------------------- types/ip/ip-tests.ts | 24 +++--- types/ip/tsconfig.json | 4 +- types/ip/tslint.json | 78 +----------------- 4 files changed, 104 insertions(+), 183 deletions(-) diff --git a/types/ip/index.d.ts b/types/ip/index.d.ts index ed9ba584bc..edcf3ba669 100644 --- a/types/ip/index.d.ts +++ b/types/ip/index.d.ts @@ -1,11 +1,12 @@ -// Type definitions for node-ip +// Type definitions for ip 1.1 // Project: https://github.com/indutny/node-ip // Definitions by: Peter Harris +// BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -interface SubnetInfo { +export interface SubnetInfo { networkAddress: string; firstAddress: string; lastAddress: string; @@ -17,110 +18,108 @@ interface SubnetInfo { contains(ip: string): boolean; } -declare module "ip" { - /** - * Check two IP address are the same. - **/ - export function isEqual(ip1: string, ip2: string): boolean; +/** + * Check two IP address are the same. + */ +export function isEqual(ip1: string, ip2: string): boolean; - /** - * Convert an IP string into a buffer. - **/ - export function toBuffer(ip: string, buffer?: Buffer, offset?: number): Buffer; +/** + * Convert an IP string into a buffer. + */ +export function toBuffer(ip: string, buffer?: Buffer, offset?: number): Buffer; - /** - * Convert an IP buffer into a string. - **/ - export function toString(ip: Buffer, offset?: number, length?: number): string; +/** + * Convert an IP buffer into a string. + */ +export function toString(ip: Buffer, offset?: number, length?: number): string; - /** - * Get the subnet mask from a CIDR prefix length. - * - * @param family The IP family is infered from the prefixLength, but can be explicity specified as either "ipv4" or "ipv6". - **/ - export function fromPrefixLen(prefixLength: number, family?:string): string; +/** + * Get the subnet mask from a CIDR prefix length. + * + * @param family The IP family is infered from the prefixLength, but can be explicity specified as either "ipv4" or "ipv6". + */ +export function fromPrefixLen(prefixLength: number, family?: 'ipv4' | 'ipv6'): string; - /** - * Get the network ID IP address from an IP address and its subnet mask. - **/ - export function mask(ip: string, mask: string): string; +/** + * Get the network ID IP address from an IP address and its subnet mask. + */ +export function mask(ip: string, mask: string): string; - /** - * Get the network ID IP address from an IP address in CIDR notation. - **/ - export function cidr(cidr: string): string; +/** + * Get the network ID IP address from an IP address in CIDR notation. + */ +export function cidr(cidr: string): string; - /** - * Get the bitwise inverse (NOT every octet) of an IP address or subnet mask. - **/ - export function not(ip: string): string; +/** + * Get the bitwise inverse (NOT every octet) of an IP address or subnet mask. + */ +export function not(ip: string): string; - /** - * Get the bitwise OR of two IP addresses (usually an IP address and a subnet mask). - **/ - export function or(ip: string, mask:string): string; +/** + * Get the bitwise OR of two IP addresses (usually an IP address and a subnet mask). + */ +export function or(ip: string, mask: string): string; - /** - * Check whether an IP is within a private IP address range. - **/ - export function isPrivate(ip: string): boolean; +/** + * Check whether an IP is within a private IP address range. + */ +export function isPrivate(ip: string): boolean; - /** - * Check whether an IP is within a public IP address range. - **/ - export function isPublic(ip: string): boolean; +/** + * Check whether an IP is within a public IP address range. + */ +export function isPublic(ip: string): boolean; - /** - * Check whether an IP is a loopback address. - **/ - export function isLoopback(ip: string): boolean; +/** + * Check whether an IP is a loopback address. + */ +export function isLoopback(ip: string): boolean; - /** - * Check whether an IP is a IPv4 address. - **/ - export function isV4Format(ip: string): boolean; +/** + * Check whether an IP is a IPv4 address. + */ +export function isV4Format(ip: string): boolean; - /** - * Check whether an IP is a IPv6 address. - **/ - export function isV6Format(ip: string): boolean; +/** + * Check whether an IP is a IPv6 address. + */ +export function isV6Format(ip: string): boolean; - /** - * Get the loopback address for an IP family. - * - * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". - **/ - export function loopback(family?: string): string; +/** + * Get the loopback address for an IP family. + * + * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". + */ +export function loopback(family?: 'ipv4' | 'ipv6'): string; - /** - * Get the address for the network interface on the current system with the specified 'name'. - * If no interface name is specified, the first IPv4 address or loopback address is returned. - * - * @param name The name can be any named interface, or 'public' or 'private'. - * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". - **/ - export function address(name?: string, family?: string):string; +/** + * Get the address for the network interface on the current system with the specified 'name'. + * If no interface name is specified, the first IPv4 address or loopback address is returned. + * + * @param name The name can be any named interface, or 'public' or 'private'. + * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4". + */ +export function address(name?: 'public' | 'private' | string, family?: 'ipv4' | 'ipv6'): string; - /** - * Convert a string IPv4 IP address to the equivalent long numeric value. - **/ - export function toLong(ip: string): number; +/** + * Convert a string IPv4 IP address to the equivalent long numeric value. + */ +export function toLong(ip: string): number; - /** - * Convert an IPv4 IP address from its the long numeric value to a string. - **/ - export function fromLong(ip: number): string; +/** + * Convert an IPv4 IP address from its the long numeric value to a string. + */ +export function fromLong(ip: number): string; - /** - * Get the subnet information. - * @param ip IP address. - * @param subnet Subnet address. - */ - export function subnet(ip: string, subnet: string): SubnetInfo; +/** + * Get the subnet information. + * @param ip IP address. + * @param subnet Subnet address. + */ +export function subnet(ip: string, subnet: string): SubnetInfo; - /** - * Get the subnet information. - * @param cidr CIDR address. - */ - export function cidrSubnet(cidr: string): SubnetInfo; -} +/** + * Get the subnet information. + * @param cidr CIDR address. + */ +export function cidrSubnet(cidr: string): SubnetInfo; diff --git a/types/ip/ip-tests.ts b/types/ip/ip-tests.ts index f1fbf4726d..efbf1a574c 100644 --- a/types/ip/ip-tests.ts +++ b/types/ip/ip-tests.ts @@ -1,26 +1,24 @@ - - import ip = require('ip'); -var myIP = ip.address(); +const myIP = ip.address(); -ip.cidr("192.168.0.1/24"); +ip.cidr('192.168.0.1/24'); ip.fromLong(ip.toLong(myIP)); ip.fromPrefixLen(24); -ip.isEqual("192.168.0.1", myIP); -ip.isLoopback("127.0.0.1"); +ip.isEqual('192.168.0.1', myIP); +ip.isLoopback('127.0.0.1'); ip.isPrivate(myIP); ip.isPublic(myIP); -ip.loopback("ipv6"); -ip.mask("192.168.0.1", "255.255.255.0"); -ip.not("255.255.255.0"); -ip.or("192.168.0.1", "255.255.255.0"); -var buff:any = ip.toBuffer(myIP); +ip.loopback('ipv6'); +ip.mask('192.168.0.1', '255.255.255.0'); +ip.not('255.255.255.0'); +ip.or('192.168.0.1', '255.255.255.0'); +const buff: any = ip.toBuffer(myIP); ip.toString(buff); ip.subnet('192.168.1.134', '255.255.255.192'); ip.cidrSubnet('192.168.1.134/26'); ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.134'); -var buf = new Buffer(128); -var offset = 64; +const buf = new Buffer(128); +const offset = 64; ip.toBuffer('127.0.0.1', buf, offset); ip.toString(buf, offset, 4); diff --git a/types/ip/tsconfig.json b/types/ip/tsconfig.json index a445ebefe8..ee6d3f766e 100644 --- a/types/ip/tsconfig.json +++ b/types/ip/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "ip-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/ip/tslint.json b/types/ip/tslint.json index a41bf5d19a..f93cf8562a 100644 --- a/types/ip/tslint.json +++ b/types/ip/tslint.json @@ -1,79 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "extends": "dtslint/dt.json" }