Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intToIp converter to network #360

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to

## [Unreleased][unreleased]

- add intToIp converter to network
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- add intToIp converter to network
- Add `intToIp` converter to `network.js`


## [2.2.0][] - 2020-07-10

### Added
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ $ npm install @metarhia/common
- [cryptoRandom](#cryptorandom)
- [methods](#methodsiface)
- [properties](#propertiesiface)
- [ipToInt](#iptointip)
- [ipToInt](#iptointip_string)
- [intToIp](#inttoipip_number)
- [localIPs](#localips)
- [parseHost](#parsehosthost)
- [override](#overrideobj-fn)
Expand Down Expand Up @@ -1296,13 +1297,23 @@ _Returns:_ [`<string[]>`][string] property names

List property names

### ipToInt(\[ip\])
### ipToInt(IP\_string)

- `ip`: [`<string>`][string] (optional), default: '127.0.0.1', IP address
- `IP_string`: [`<string>`][string] (optional), default: '127.0.0.1', IPv4
address in string form

_Returns:_ [`<number>`][number]

Convert IP string to number
Convert IPv4 address from string form to number form

### intToIp(IP\_number)

- `IP_number`: [`<number>`][number] (optional), default: 0, IPv4 address in
number form

_Returns:_ [`<string>`][string]

Convert IPv4 address from number form to string form

### localIPs()

Expand Down
1 change: 1 addition & 0 deletions common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const cryptoRandom = common.cryptoRandom;
export const methods = common.methods;
export const properties = common.properties;
export const ipToInt = common.ipToInt;
export const intToIp = common.intToIp;
export const localIPs = common.localIPs;
export const parseHost = common.parseHost;
export const override = common.override;
Expand Down
16 changes: 13 additions & 3 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

const os = require('os');

// Convert IP string to number
// Signature: [ip]
// ip - <string>, (optional), default: '127.0.0.1', IP address
// Convert IPv4 address from string form to number form
// Signature: IP_string
// IP_string - <string>, (optional), default: '127.0.0.1', IPv4 address in string form
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Convert IPv4 address from string form to number form
// Signature: IP_string
// IP_string - <string>, (optional), default: '127.0.0.1', IPv4 address in string form
// Convert IPv4 address from string form to number form
// Signature: [ip]
// ip - <string>, (optional), default: '127.0.0.1', IPv4 address in string form

// Returns: <number>
const ipToInt = (ip = '127.0.0.1') =>
ip.split('.').reduce((res, item) => (res << 8) + +item, 0);

// Convert IPv4 address from number form to string form
// Signature: IP_number
// IP_number - <number>, (optional), default: 0, IPv4 address in number form
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Convert IPv4 address from number form to string form
// Signature: IP_number
// IP_number - <number>, (optional), default: 0, IPv4 address in number form
// Convert IPv4 address from number form to string form
// Signature: [ip]
// ip - <number>, (optional), default: 0, IPv4 address in number form

// Returns: <string>
const intToIp = (ipInt = 0) =>
[ipInt >>> 24, (ipInt >> 16) & 255, (ipInt >> 8) & 255, ipInt & 255].join(
'.'
);
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to put 4 parts of address into 4 separate variables and concatenate them without creating array and call join


let LOCAL_IPS_CACHE;

// Get local network interfaces
Expand Down Expand Up @@ -44,6 +53,7 @@ const parseHost = host => {

module.exports = {
ipToInt,
intToIp,
localIPs,
parseHost,
};
11 changes: 11 additions & 0 deletions test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ metatests.case(
['8.8.8.8', 0x08080808],
[undefined, 0x7f000001],
],
'common.intToIp': [
[2130706433, '127.0.0.1'],
[167772161, '10.0.0.1'],
[-1062731510, '192.168.1.10'],
[-1511946858, '165.225.133.150'],
[0, '0.0.0.0'],
['', '0.0.0.0'],
[Number.NaN, '0.0.0.0'],
[0x08080808, '8.8.8.8'],
[0x7f000001, '127.0.0.1'],
],
'common.parseHost': [
['', 'no-host-name-in-http-headers'],
['domain.com', 'domain.com'],
Expand Down