-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
897d457
commit 5243a23
Showing
9 changed files
with
128 additions
and
125 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,60 @@ | ||
declare const isIp: { | ||
/** | ||
Check if `string` is IPv4 or IPv6. | ||
@example | ||
``` | ||
import isIp = require('is-ip'); | ||
isIp('192.168.0.1'); | ||
//=> true | ||
isIp('1:2:3:4:5:6:7:8'); | ||
//=> true | ||
``` | ||
*/ | ||
(string: string): boolean; | ||
|
||
/** | ||
Check if `string` is IPv4. | ||
@example | ||
``` | ||
import isIp = require('is-ip'); | ||
isIp.v4('192.168.0.1'); | ||
//=> true | ||
``` | ||
*/ | ||
v4(string: string): boolean; | ||
|
||
/** | ||
Check if `string` is IPv6. | ||
@example | ||
``` | ||
import isIp = require('is-ip'); | ||
isIp.v6('1:2:3:4:5:6:7:8'); | ||
//=> true | ||
``` | ||
*/ | ||
v6(string: string): boolean; | ||
|
||
/** | ||
@returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither. | ||
@example | ||
``` | ||
import isIp = require('is-ip'); | ||
isIp.version('192.168.0.1'); | ||
//=> 4 | ||
isIp.version('1:2:3:4:5:6:7:8'); | ||
//=> 6 | ||
isIp.version('abc'); | ||
//=> undefined | ||
``` | ||
*/ | ||
version(string: string): 4 | 6 | undefined; | ||
}; | ||
|
||
export = isIp; | ||
/** | ||
Check if `string` is IPv6 or IPv4. | ||
@example | ||
``` | ||
import {isIP} from 'is-ip'; | ||
isIP('1:2:3:4:5:6:7:8'); | ||
//=> true | ||
isIP('192.168.0.1'); | ||
//=> true | ||
``` | ||
*/ | ||
export function isIP(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention | ||
|
||
/** | ||
Check if `string` is IPv6. | ||
@example | ||
``` | ||
import {isIPv6} from 'is-ip'; | ||
isIPv6('1:2:3:4:5:6:7:8'); | ||
//=> true | ||
``` | ||
*/ | ||
export function isIPv6(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention | ||
|
||
/** | ||
Check if `string` is IPv4. | ||
@example | ||
``` | ||
import {isIPv4} from 'is-ip'; | ||
isIPv4('192.168.0.1'); | ||
//=> true | ||
``` | ||
*/ | ||
export function isIPv4(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention | ||
|
||
/** | ||
@returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither. | ||
@example | ||
``` | ||
import {ipVersion} from 'is-ip'; | ||
ipVersion('1:2:3:4:5:6:7:8'); | ||
//=> 6 | ||
ipVersion('192.168.0.1'); | ||
//=> 4 | ||
ipVersion('abc'); | ||
//=> undefined | ||
``` | ||
*/ | ||
export function ipVersion(string: string): 6 | 4 | undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
'use strict'; | ||
const ipRegex = require('ip-regex'); | ||
import ipRegex from 'ip-regex'; | ||
|
||
const isIp = string => ipRegex({exact: true}).test(string); | ||
isIp.v4 = string => ipRegex.v4({exact: true}).test(string); | ||
isIp.v6 = string => ipRegex.v6({exact: true}).test(string); | ||
isIp.version = string => isIp(string) ? (isIp.v4(string) ? 4 : 6) : undefined; | ||
export function isIP(string) { | ||
return ipRegex({exact: true}).test(string); | ||
} | ||
|
||
module.exports = isIp; | ||
export function isIPv6(string) { | ||
return ipRegex.v6({exact: true}).test(string); | ||
} | ||
|
||
export function isIPv4(string) { | ||
return ipRegex.v4({exact: true}).test(string); | ||
} | ||
|
||
export function ipVersion(string) { | ||
return isIP(string) ? (isIPv6(string) ? 6 : 4) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {expectType} from 'tsd'; | ||
import isIp = require('.'); | ||
import {isIP, isIPv6, isIPv4, ipVersion} from './index.js'; | ||
|
||
expectType<boolean>(isIp('127.0.0.1')); | ||
expectType<boolean>(isIp.v4('127.0.0.1')); | ||
expectType<boolean>(isIp.v6('1:2:3:4:5:6:7:8')); | ||
expectType<boolean>(isIP('127.0.0.1')); | ||
expectType<boolean>(isIPv6('1:2:3:4:5:6:7:8')); | ||
expectType<boolean>(isIPv4('127.0.0.1')); | ||
|
||
expectType<4 | 6 | undefined>(isIp.version('127.0.0.1')); | ||
expectType<6 | 4 | undefined>(ipVersion('127.0.0.1')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import test from 'ava'; | ||
import isIp from '.'; | ||
import {isIP, isIPv6, isIPv4, ipVersion} from './index.js'; | ||
|
||
test('main', t => { | ||
t.true(isIp('192.168.0.1')); | ||
t.true(isIp('1:2:3:4:5:6:7:8')); | ||
t.true(isIp('::1')); | ||
t.true(isIp.v4('192.168.0.1')); | ||
t.true(isIp.v6('1:2:3:4:5:6:7:8')); | ||
t.true(isIp.v6('::1')); | ||
test('isIP', t => { | ||
t.true(isIP('192.168.0.1')); | ||
t.true(isIP('1:2:3:4:5:6:7:8')); | ||
t.true(isIP('::1')); | ||
}); | ||
|
||
test('.version()', t => { | ||
t.is(isIp.version('192.168.0.1'), 4); | ||
t.is(isIp.version('1:2:3:4:5:6:7:8'), 6); | ||
t.is(isIp.version('abc'), undefined); | ||
test('isIPv6', t => { | ||
t.true(isIPv6('1:2:3:4:5:6:7:8')); | ||
t.true(isIPv6('::1')); | ||
}); | ||
|
||
test('isIPv4', t => { | ||
t.true(isIPv4('192.168.0.1')); | ||
}); | ||
|
||
test('ipVersion', t => { | ||
t.is(ipVersion('192.168.0.1'), 4); | ||
t.is(ipVersion('1:2:3:4:5:6:7:8'), 6); | ||
t.is(ipVersion('abc'), undefined); | ||
}); |