Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 1, 2022
1 parent 897d457 commit 5243a23
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 125 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
124 changes: 60 additions & 64 deletions index.d.ts
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;
22 changes: 15 additions & 7 deletions index.js
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;
}
10 changes: 5 additions & 5 deletions index.test-d.ts
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'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Check if a string is an IP address",
"license": "MIT",
"repository": "sindresorhus/is-ip",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -40,11 +43,11 @@
"string"
],
"dependencies": {
"ip-regex": "^4.0.0"
"ip-regex": "^5.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.19.1",
"xo": "^0.47.0"
}
}
44 changes: 19 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,58 @@ If you only need this for Node.js and don't care about browser support, you may

## Install

```sh
npm install is-ip
```
$ npm install is-ip
```


## Usage

```js
const isIp = require('is-ip');
import {isIP, isIPv4} from 'is-ip';

isIp('192.168.0.1');
isIP('1:2:3:4:5:6:7:8');
//=> true

isIp('1:2:3:4:5:6:7:8');
isIP('192.168.0.1');
//=> true

isIp.v4('1:2:3:4:5:6:7:8');
isIPv4('1:2:3:4:5:6:7:8');
//=> false
```


## API

### isIp(string)
### isIP(string)

Check if `string` is IPv4 or IPv6.
Check if `string` is IPv6 or IPv4.

### isIp.v4(string)
### isIPv6(string)

Check if `string` is IPv4.
Check if `string` is IPv6.

### isIp.v6(string)
### isIPv4(string)

Check if `string` is IPv6.
Check if `string` is IPv4.

### isIp.version(string)
### ipVersion(string)

Returns `6` if `string` is IPv6, `4` if `string` is IPv4, or `undefined` if `string` is neither.

```js
isIp.version('192.168.0.1');
//=> 4
import {ipVersion} from 'is-ip';

isIp.version('1:2:3:4:5:6:7:8');
ipVersion('1:2:3:4:5:6:7:8');
//=> 6

isIp.version('abc');
ipVersion('192.168.0.1');
//=> 4

ipVersion('abc');
//=> undefined
```


## Related

- [ip-regex](https://github.com/sindresorhus/ip-regex) - Regular expression for matching IP addresses
- [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
- [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
30 changes: 18 additions & 12 deletions test.js
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);
});

0 comments on commit 5243a23

Please sign in to comment.