Skip to content

Instantly share code, notes, and snippets.

@romanoffs
Created June 12, 2019 12:00
Show Gist options
  • Save romanoffs/55a0607e7aa20b66d48a792dbfa4290c to your computer and use it in GitHub Desktop.
Save romanoffs/55a0607e7aa20b66d48a792dbfa4290c to your computer and use it in GitHub Desktop.
YouTubeParce Video
static youtubePattern(isString?: boolean): string | RegExp {
const p: string = '^((?:https?:)?\\/\\/)?((?:www|m)\\.)?((?:youtube\\.com|youtu.be))(\\/(?:[\\w\\-]+\\?v=|embed\\/|v\\/)?)([\\w\\-]+)(\\S+)?$';
return isString ? p : new RegExp(p);
}
static parseYouTubeVideo(url: string): string {
if (!Utils.isString(url)) throw new Error('Url is not a string');
return url.replace(Utils.youtubePattern(), '$5');
}
static calculateRatioHeight(width: number): number {
return Math.round((width / 16) * 9);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment