├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/is-ip 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | - 14 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if `string` is IPv6 or IPv4. 3 | 4 | @example 5 | ``` 6 | import {isIP} from 'is-ip'; 7 | 8 | isIP('1:2:3:4:5:6:7:8'); 9 | //=> true 10 | 11 | isIP('192.168.0.1'); 12 | //=> true 13 | ``` 14 | */ 15 | export function isIP(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention 16 | 17 | /** 18 | Check if `string` is IPv6. 19 | 20 | @example 21 | ``` 22 | import {isIPv6} from 'is-ip'; 23 | 24 | isIPv6('1:2:3:4:5:6:7:8'); 25 | //=> true 26 | ``` 27 | */ 28 | export function isIPv6(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention 29 | 30 | /** 31 | Check if `string` is IPv4. 32 | 33 | @example 34 | ``` 35 | import {isIPv4} from 'is-ip'; 36 | 37 | isIPv4('192.168.0.1'); 38 | //=> true 39 | ``` 40 | */ 41 | export function isIPv4(string: string): boolean; // eslint-disable-line @typescript-eslint/naming-convention 42 | 43 | /** 44 | @returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither. 45 | 46 | @example 47 | ``` 48 | import {ipVersion} from 'is-ip'; 49 | 50 | ipVersion('1:2:3:4:5:6:7:8'); 51 | //=> 6 52 | 53 | ipVersion('192.168.0.1'); 54 | //=> 4 55 | 56 | ipVersion('abc'); 57 | //=> undefined 58 | ``` 59 | */ 60 | export function ipVersion(string: string): 6 | 4 | undefined; 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import ipRegex from 'ip-regex'; 2 | import {isMatch} from 'super-regex'; 3 | 4 | const maxIPv4Length = 15; 5 | const maxIPv6Length = 45; 6 | 7 | const options = { 8 | timeout: 400, 9 | }; 10 | 11 | export function isIP(string) { 12 | if (string.length > maxIPv6Length) { 13 | return false; 14 | } 15 | 16 | return isMatch(ipRegex({exact: true}), string, options); 17 | } 18 | 19 | export function isIPv6(string) { 20 | if (string.length > maxIPv6Length) { 21 | return false; 22 | } 23 | 24 | return isMatch(ipRegex.v6({exact: true}), string, options); 25 | } 26 | 27 | export function isIPv4(string) { 28 | if (string.length > maxIPv4Length) { 29 | return false; 30 | } 31 | 32 | return isMatch(ipRegex.v4({exact: true}), string, options); 33 | } 34 | 35 | export function ipVersion(string) { 36 | if (isIPv6(string)) { 37 | return 6; 38 | } 39 | 40 | if (isIPv4(string)) { 41 | return 4; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {isIP, isIPv6, isIPv4, ipVersion} from './index.js'; 3 | 4 | expectType(isIP('127.0.0.1')); 5 | expectType(isIPv6('1:2:3:4:5:6:7:8')); 6 | expectType(isIPv4('127.0.0.1')); 7 | 8 | expectType<6 | 4 | undefined>(ipVersion('127.0.0.1')); 9 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-ip", 3 | "version": "5.0.1", 4 | "description": "Check if a string is an IP address", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-ip", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "engines": { 17 | "node": ">=14.16" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava && tsd" 21 | }, 22 | "files": [ 23 | "index.js", 24 | "index.d.ts" 25 | ], 26 | "keywords": [ 27 | "ip", 28 | "ipv6", 29 | "ipv4", 30 | "regex", 31 | "regexp", 32 | "match", 33 | "test", 34 | "find", 35 | "text", 36 | "pattern", 37 | "internet", 38 | "protocol", 39 | "address", 40 | "validate", 41 | "detect", 42 | "check", 43 | "is", 44 | "string" 45 | ], 46 | "dependencies": { 47 | "ip-regex": "^5.0.0", 48 | "super-regex": "^0.2.0" 49 | }, 50 | "devDependencies": { 51 | "ava": "^4.3.1", 52 | "tsd": "^0.22.0", 53 | "xo": "^0.54.0" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-ip 2 | 3 | > Check if a string is an IP address 4 | 5 | If you only need this for Node.js and don't care about browser support, you may want to use [`net.isIP`](https://nodejs.org/api/net.html#net_net_isip_input) instead. Note that it returns an integer instead of a boolean. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install is-ip 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import {isIP, isIPv4} from 'is-ip'; 17 | 18 | isIP('1:2:3:4:5:6:7:8'); 19 | //=> true 20 | 21 | isIP('192.168.0.1'); 22 | //=> true 23 | 24 | isIPv4('1:2:3:4:5:6:7:8'); 25 | //=> false 26 | ``` 27 | 28 | ## API 29 | 30 | ### isIP(string) 31 | 32 | Check if `string` is IPv6 or IPv4. 33 | 34 | ### isIPv6(string) 35 | 36 | Check if `string` is IPv6. 37 | 38 | ### isIPv4(string) 39 | 40 | Check if `string` is IPv4. 41 | 42 | ### ipVersion(string) 43 | 44 | Returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither. 45 | 46 | ```js 47 | import {ipVersion} from 'is-ip'; 48 | 49 | ipVersion('1:2:3:4:5:6:7:8'); 50 | //=> 6 51 | 52 | ipVersion('192.168.0.1'); 53 | //=> 4 54 | 55 | ipVersion('abc'); 56 | //=> undefined 57 | ``` 58 | 59 | ## Related 60 | 61 | - [ip-regex](https://github.com/sindresorhus/ip-regex) - Regular expression for matching IP addresses 62 | - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation 63 | - [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation 64 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {isIP, isIPv6, isIPv4, ipVersion} from './index.js'; 3 | 4 | test('isIP', t => { 5 | t.true(isIP('192.168.0.1')); 6 | t.true(isIP('1:2:3:4:5:6:7:8')); 7 | t.true(isIP('::1')); 8 | t.true(isIP('2001:0dc5:72a3:0000:0000:802e:3370:73E4')); 9 | }); 10 | 11 | test('isIPv6', t => { 12 | t.true(isIPv6('1:2:3:4:5:6:7:8')); 13 | t.true(isIPv6('::1')); 14 | t.true(isIPv6('0000:0000:0000:0000:0000:0000:0000:0000')); 15 | t.true(isIPv6('0000:0000:0000:0000:0000:ffff:192.168.100.228')); 16 | t.true(isIPv6('2001:0dc5:72a3:0000:0000:802e:3370:73E4')); 17 | t.false(isIPv6('2001:0dc5:72a3:0000:0000:802e:3370:73E4XXXXXXXX')); 18 | }); 19 | 20 | test('isIPv4', t => { 21 | t.true(isIPv4('192.168.0.1')); 22 | t.false(isIPv4('123.123.123.1234')); 23 | t.false(isIPv4('123.123.123.12345')); 24 | }); 25 | 26 | test('ipVersion', t => { 27 | t.is(ipVersion('192.168.0.1'), 4); 28 | t.is(ipVersion('1:2:3:4:5:6:7:8'), 6); 29 | t.is(ipVersion('abc'), undefined); 30 | t.is(ipVersion('123.123.123.12345'), undefined); 31 | }); 32 | --------------------------------------------------------------------------------