Created
February 5, 2017 10:33
-
-
Save jwheare/a1b5cae7f6adeb402bfcd1ec764b585a to your computer and use it in GitHub Desktop.
IRC URL parsing in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function ircUrlParts (ircUrl) { | |
// http://tools.ietf.org/html/draft-butcher-irc-url-04 | |
if (!ircUrl) { | |
return; | |
} | |
var urlParts = ircUrl.split('://'); | |
if (!urlParts[1]) { | |
return; | |
} | |
var protocol = urlParts[0]; | |
var bufferParts = urlParts[1].split('#'); | |
var rootPart, bufferString; | |
if (bufferParts[0].indexOf('/') === -1) { | |
// No slash used as channel delimiter, just hash | |
rootPart = bufferParts.shift(); | |
if (bufferParts.length) { | |
bufferString = '#' + bufferParts.join('#'); | |
} | |
} else { | |
bufferParts = urlParts[1].split('/'); | |
rootPart = bufferParts.shift(); | |
if (bufferParts.length) { | |
bufferString = bufferParts.join('/'); | |
} | |
} | |
// Convert +6697 to ircs port | |
var rootPartSplit = rootPart.split(':'); | |
if (rootPartSplit[1]) { | |
var rootPortSSL = rootPartSplit[1].match(/^\+(\d+)$/); | |
if (rootPortSSL) { | |
protocol = 'ircs'; | |
rootPart = rootPartSplit[0] + ':' + rootPortSSL[1]; | |
} | |
} | |
if (protocol === 'ircs') { | |
rootPart = protocol + '://' + rootPart; | |
} | |
var url = rootPart; | |
var label; | |
if (bufferString) { | |
var bufferStringParts = bufferString.split(','); | |
label = bufferStringParts[0]; | |
if (bufferStringParts[1] != 'isuser' && $.inArray(label[0], ['#', '#', '&', '+', '!']) == -1) { | |
label = '#' + label; | |
} | |
url += '/' + label; | |
} | |
return { | |
root: rootPart, | |
label: label, | |
url: url | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment