JSFiddle - React, Tailwind, and code Playground
by Rogier
HTML
<div id=output></div>
CSS
#output > div {
border: 1px solid gold;
margin: 0 0 1em;
}
code:empty::before {
content: '<empty>';
font-style: italic;
}
JavaScript
// helper to visualize output
function log(title, desc, outcome) {
var output = document.querySelector('#output'),
wrapper = output.appendChild(document.createElement('div')),
tags = ['strong', 'p', 'code'],
i;
for (i = 0; i < tags.length; ++i) {
wrapper.appendChild(document.createElement(tags[i]))
.appendChild(document.createTextNode(arguments[i]));
}
}
function subdomain(host) {
var part = host.split('.').reverse(),
index = 0;
while (part[index].length === 2 || !index) {
++index;
}
++index;
return part.length > index && part[index] !== 'www' ? part[index] : '';
}
var tests = {
'example.com':
'return value is blank since no sub domain',
'www.example.com':
'return value is blank since no sub domain',
'test.example.com':
'return value should be test since this is the sub domain',
'example.co.in':
'return value is blank since no sub domain',
'www.example.co.in':
'return value is blank since no sub domain',
'test.example.co.in':
'return value should be test since this is the sub domain',
'183.87.46.82':
'return value is blank since IP passed'
},
key;
for (key in tests) {
log(key, tests[key], subdomain(key));
}