open-nomad/ui/app/utils/ip-parts.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

26 lines
668 B
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2017-09-19 14:47:10 +00:00
// Splits any IP address into an address and a port
export default function ipParts(ip) {
const parts = ip ? ip.split(':') : [];
if (parts.length === 0) {
// ipv4, no port
return { address: ip, port: undefined };
} else if (parts.length === 2) {
// ipv4, with port
return { address: parts[0], port: parts[1] };
} else if (ip.startsWith('[')) {
// ipv6, with port
return {
address: parts.slice(0, parts.length - 1).join(':'),
port: parts[parts.length - 1],
};
} else {
// ipv6, no port
return { address: ip, port: undefined };
}
}