JSFiddle - React, Tailwind, and code Playground

by MegaScience

JavaScript

const testCases = ['http://www.youtube.com/watch?v=j_QLzthSkfM&feature=feedrec_grec_index',
'http://www.youtube.com/user/UberHaxorNova#p/a/u/1/59WpvWkGBbE',
'http://www.youtube.com/v/j_QLzthSkfM?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/watch?v=j_QLzthSkfM#t=0m10s',
'http://www.youtube.com/embed/j_QLzthSkfM?rel=0',
'http://www.youtube.com/watch?v=j_QLzthSkfM',
'http://youtu.be/j_QLzthSkfM']

const gYTID = {
	re: {},
	init: function () {
		this.re.proto = new RegExp(`^[a-zA-Z]+:\/\/`)
		this.re.videoStr = '([a-zA-Z0-9\-_]+)'
		this.re.videoReg = new RegExp(`^${this.re.videoStr}$`)
		this.re.pathEnd = new RegExp(`\/${this.re.videoStr}$`)
		this.re.pathEndF = (u, p) => {
			const o = { 'success' : true}
			const videoID = this.re.pathEnd.exec(u[p])
			if(videoID && videoID[1]) o.videoID = videoID[1]
			else {
				o.success = false
				o.reason =`Domain "${u.hostname}" has incorrect ${p}: ${u[p]}`
			}
			return o
		}
		return this
	},
	getYouTubeID: function(ad) {
		let o = { 'success' : true, 'videoID' : false }
		const URLObj = this.getURLObject(ad)
		try {
			if(!URLObj) throw `URL was not valid.`
			const LCHN = URLObj.hostname.toLowerCase()
			if(LCHN.toLowerCase().endsWith('youtu.be')) {
				o = this.re.pathEndF(URLObj, 'pathname')
				if(!o.success) throw o.reason
			}
			else if(LCHN.endsWith('youtube.com')) {
				const LCPN = URLObj.pathname.toLowerCase()
				if(LCPN.startsWith('/watch')) {
					const videoID = URLObj.searchParams.get('v') || URLObj.searchParams.get('vi') || null
					if(videoID && this.re.videoReg.test(videoID)) o.videoID = videoID
					else throw `Valid Video ID not found in Watch Page Query: ${URLObj.search}`
				}
				else if(LCPN.startsWith('/v') || LCPN.startsWith('/vi') || LCPN.startsWith('/embed')) {
					o = this.re.pathEndF(URLObj, 'pathname')
					if(!o.success) throw o.reason
				}
				else if(LCPN.startsWith('/user') && URLObj.hash.length) {
					o = this.re.pathEndF(URLObj, 'hash')
					if(!o.success) throw...