UNPKG

@js-data-tools/js-helpers

Version:

A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

35 lines (34 loc) 757 B
export function ipv4AsString(ip) { if (typeof ip === "string") { return ip; } const tokens = [0, 0, 0, 0]; let current = ip; for (let i = 3; i >= 0; --i) { tokens[i] = current & 0xff; current >>= 8; } return tokens.join("."); } export function ipv4AsNumber(ip) { if (typeof ip === "number") { return ip; } let result = 0; ip.split(".").forEach((token, i) => { result += parseInt(token, 10) << (8 * (3 - i)); }); return result >>> 0; } export function areIpsEqual(a, b) { if (!a) { return !b; } if (!b) { return false; } if (typeof a === typeof b) { return a === b; } return ipv4AsString(a) === ipv4AsString(b); }