UNPKG

731 BJavaScriptView Raw
1/**
2 * Helper functions
3 */
4'use strict';
5
6module.exports = {
7 parseVideoUrl( url ) {
8 function getIdFromUrl( videoUrl, re ) {
9 let matches = videoUrl.match( re );
10 return ( matches && matches[ 1 ] ) || null;
11 }
12
13 let id;
14
15 // https://www.youtube.com/watch?v=24X9FpeSASY
16 // http://stackoverflow.com/questions/5830387/how-to-find-all-youtube-video-ids-in-a-string-using-a-regex/5831191#5831191
17 if ( url.includes( 'youtube.com/' ) ) {
18 id = getIdFromUrl( url, /\/watch\?v=([A-Z0-9_-]+)/i );
19
20 return id ? [ 'youtube', id ] : null;
21 }
22
23 // https://vimeo.com/27986705
24 if ( url.includes( 'vimeo.com/' ) ) {
25 id = getIdFromUrl( url, /\/([0-9]+)/ );
26
27 return id ? [ 'vimeo', id ] : null;
28 }
29
30 return null;
31 }
32};