From 8ae5fd084c8aad0300c3882c2a5c4024193fb451 Mon Sep 17 00:00:00 2001 From: Michael Rowley Date: Wed, 29 Dec 2021 23:23:19 +0000 Subject: [PATCH] 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. --- src/core/lib/IP.mjs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/core/lib/IP.mjs b/src/core/lib/IP.mjs index c97f87ab..08c712e0 100644 --- a/src/core/lib/IP.mjs +++ b/src/core/lib/IP.mjs @@ -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 - * @returns {number[]} + * @param {boolean} retArr If true, an array is returned. If false, a BigInt is returned. + * @returns {(number[]|BigInt)} * * @example * // returns [65280, 0, 0, 0, 0, 0, 4369, 8738] * 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; const blocks = ipStr.split(":"), numBlocks = parseBlocks(blocks),