Created
November 27, 2019 15:43
-
-
Save DanielRamosAcosta/9ff0af493ed8f422b1769db2792bdce2 to your computer and use it in GitHub Desktop.
Functional IP Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const toInteger = digitString => parseInt(digitString) | |
| const isValidInteger = digit => !isNaN(digit) | |
| const isGreaterOrEqualThan = n1 => n2 => n1 >= n2 | |
| const isLessOrEqualThan = n1 => n2 => n1 <= n2 | |
| const isValidIp = ip => ip | |
| .split(".") | |
| .map(toInteger) | |
| .filter(isValidInteger) | |
| .filter(isGreaterOrEqualThan(255)) | |
| .filter(isLessOrEqualThan(0)) | |
| .length === 4 | |
| console.log(isValidIp("0.0.0.0")) | |
| console.log(isValidIp("192.168.1.1")) | |
| console.log(isValidIp("") ) | |
| console.log(isValidIp("1.1.1") ) | |
| console.log(isValidIp("a.a.a.a") ) | |
| console.log(isValidIp("192.168.1.999") ) | |
| console.log(isValidIp("192.168.1.-1") ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment