I have a hostName string which may or may not be a fully qualified domain name.
hostName = host1.abc.com OR
hostName = host1
In case hostName is a fully qualified domain name, then I need to capture only the first portion in host
host = host1
I have written the following but it is giving empty string "" when the hostName is not a fully qualified domain name
const host = hostName.substring(0, hostName.indexOf('.'));
This way it will give the hostname correctly, in any of the 2 scenarios
const host = hostName.split('.')[0];
Or else
const idx = hostName.indexOf('.');
const host = idx > 0 ? hostName.substring(0, idx) : hostName;