Merge branch 'master' into fr_lua_ipv6

This commit is contained in:
lion 2025-10-19 23:08:31 +08:00
commit ec7cb6d3ed
2 changed files with 17 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{
"name": "ip2region.js",
"version": "3.1.3",
"version": "3.1.4",
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
"type": "module",
"main": "index.js",

View File

@ -155,20 +155,30 @@ function _ipv4_to_string(v4Bytes) {
// ipv6 bytes to string
function _ipv6_to_string(v6Bytes, compress) {
let ps = [];
let ps = [], needCompress = false;
let last_hex = -1, hex = 0;
for (var i = 0; i < v6Bytes.length; i += 2) {
ps.push(v6Bytes.readUint16BE(i).toString(16));
hex = v6Bytes.readUint16BE(i).toString(16);
ps.push(hex);
// check the necessity for compress
if (last_hex > -1
&& hex == 0 && last_hex == 0) {
needCompress = true;
}
// reset the last hex
last_hex = hex;
}
if (compress === false) {
if (needCompress == false || compress === false) {
return ps.join(':');
}
// auto compression of consecutive zero
let j = 0, mi = ps.length - 1;
let _ = [];
let _ = [], mi = ps.length - 1;
for (i = 0; i < ps.length; i++) {
if (i >= mi || j > 0) {
if (i >= mi) {
_.push(ps[i]);
continue;
}