-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3-improve.js
More file actions
71 lines (59 loc) · 1.61 KB
/
3-improve.js
File metadata and controls
71 lines (59 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const assert = require('node:assert').strict;
// Convert IP string to number
// ip <string> - IP address
// Returns: <number>
const ipToInt = (ip) => {
if (typeof ip !== 'string') throw Error('String expected');
if (ip === '') throw Error('Empty is not allowed');
const parts = ip.split('.');
if (parts.length !== 4) throw Error('Wrong IPv4 format');
const nums = parts.map((n) => parseInt(n, 10));
if (nums.includes(NaN)) throw Error('Wrong IPv4 format');
return nums.reduce((res, item) => (res << 8) + item);
};
// Tests
const testLocalhost = () => {
const n = ipToInt('127.0.0.1');
assert.strictEqual(n, 2130706433, 'Localhost IP address');
};
const testPrivateNetork = () => {
const n = ipToInt('10.0.0.1');
assert.strictEqual(n, 167772161, 'Private class A network');
};
const testNegative = () => {
const n = ipToInt('192.168.1.10');
assert.strictEqual(n, -1062731510, 'Negative number');
};
const testFourZeros = () => {
const n = ipToInt('0.0.0.0');
assert.strictEqual(n, 0, 'Four zeros');
};
const testWrongString = () => {
const n = ipToInt('wrong-string');
assert.strictEqual(n, Number.NaN, 'Wrong string');
};
const testEmptyString = () => {
const n = ipToInt('');
assert.strictEqual(n, 0, 'Empty string');
};
const testFourEights = () => {
const n = ipToInt('8.8.8.8');
assert.strictEqual(n, 0x08080808, 'Four eights');
};
const tests = [
testLocalhost,
testPrivateNetork,
testNegative,
testFourZeros,
testWrongString,
testEmptyString,
testFourEights,
];
for (const test of tests) {
try {
test();
} catch (err) {
console.log(err);
}
}