-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0468-validate-ip-address.js
More file actions
107 lines (86 loc) · 3.31 KB
/
0468-validate-ip-address.js
File metadata and controls
107 lines (86 loc) · 3.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Validate Ip Address
* Time Complexity: O(L)
* Space Complexity: O(L)
*/
var validIPAddress = function (queryIP) {
const checkIPv4 = (ipv4Candidate) => {
const partsDelimiter = '.';
const ipv4Parts = ipv4Candidate.split(partsDelimiter);
const segmentCount = ipv4Parts.length;
if (segmentCount !== 4) {
return false;
}
for (let idxOne = 0; idxOne < segmentCount; idxOne++) {
const currentPart = ipv4Parts[idxOne];
const partLength = currentPart.length;
if (partLength === 0 || partLength > 3) {
return false;
}
if (partLength > 1 && currentPart[0] === '0') {
return false;
}
let partNumericValue = 0;
const zeroCode = '0'.charCodeAt(0);
const nineCode = '9'.charCodeAt(0);
for (let charIndex = 0; charIndex < partLength; charIndex++) {
const segmentChar = currentPart[charIndex];
const charCode = segmentChar.charCodeAt(0);
if (charCode < zeroCode || charCode > nineCode) {
return false;
}
partNumericValue = partNumericValue * 10 + (charCode - zeroCode);
}
if (partNumericValue < 0 || partNumericValue > 255) {
return false;
}
}
return true;
};
const checkIPv6 = (ipv6Candidate) => {
const segmentDelimiter = ':';
const ipv6Segments = ipv6Candidate.split(segmentDelimiter);
const componentCount = ipv6Segments.length;
if (componentCount !== 8) {
return false;
}
const zeroAscii = '0'.charCodeAt(0);
const nineAscii = '9'.charCodeAt(0);
const aLowerAscii = 'a'.charCodeAt(0);
const fLowerAscii = 'f'.charCodeAt(0);
const aUpperAscii = 'A'.charCodeAt(0);
const fUpperAscii = 'F'.charCodeAt(0);
for (let idxTwo = 0; idxTwo < componentCount; idxTwo++) {
const currentSegment = ipv6Segments[idxTwo];
const segmentCharLength = currentSegment.length;
if (segmentCharLength === 0 || segmentCharLength > 4) {
return false;
}
for (let segCharIndex = 0; segCharIndex < segmentCharLength; segCharIndex++) {
const hexChar = currentSegment[segCharIndex];
const hexCharCode = hexChar.charCodeAt(0);
const isDigit = hexCharCode >= zeroAscii && hexCharCode <= nineAscii;
const isLowerHex = hexCharCode >= aLowerAscii && hexCharCode <= fLowerAscii;
const isUpperHex = hexCharCode >= aUpperAscii && hexCharCode <= fUpperAscii;
if (!isDigit && !isLowerHex && !isUpperHex) {
return false;
}
}
}
return true;
};
const dotPresence = queryIP.includes('.');
const colonPresence = queryIP.includes(':');
if (dotPresence && !colonPresence) {
const resultIPv4 = checkIPv4(queryIP);
if (resultIPv4) {
return "IPv4";
}
} else if (colonPresence && !dotPresence) {
const resultIPv6 = checkIPv6(queryIP);
if (resultIPv6) {
return "IPv6";
}
}
return "Neither";
};