Added BigInt return to strToIpv6()

This shouldn't change the outcome of any existing calls to strToIpv6 as the additional parameter is defaulted to not have any effect.
This commit is contained in:
Michael Rowley 2021-12-29 23:23:19 +00:00
parent ae1b12c120
commit 8ae5fd084c

View file

@ -336,16 +336,27 @@ export function ipv4ToStr(ipInt) {
/** /**
* Converts an IPv6 address from string format to numerical array format. * Converts an IPv6 address from string format to numerical format.
* *
* @param {string} ipStr * @param {string} ipStr
* @returns {number[]} * @param {boolean} retArr If true, an array is returned. If false, a BigInt is returned.
* @returns {(number[]|BigInt)}
* *
* @example * @example
* // returns [65280, 0, 0, 0, 0, 0, 4369, 8738] * // returns [65280, 0, 0, 0, 0, 0, 4369, 8738]
* strToIpv6("ff00::1111:2222"); * strToIpv6("ff00::1111:2222");
*
* // returns 126946n
* strToIpv6("1:f000", false);
*/ */
export function strToIpv6(ipStr) { export function strToIpv6(ipStr, retArr=true) {
if (retArr === false) {
if (ipStr.length % 2 === 1) {
ipStr = "0" + ipStr;
}
return BigInt("0x" + ipStr.replace(":", ""));
return
}
let j = 0; let j = 0;
const blocks = ipStr.split(":"), const blocks = ipStr.split(":"),
numBlocks = parseBlocks(blocks), numBlocks = parseBlocks(blocks),