-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractVideoId.js
40 lines (34 loc) · 1.14 KB
/
extractVideoId.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Extracts a YouTube video ID from a given string.
*
* This function takes a string as input, which can be either a full YouTube URL
* or a YouTube video ID. If the input is a full URL, the function extracts the
* video ID from it. If the input is already a valid YouTube video ID, the function
* returns it as is.
*
* @param {string} videoId - The string from which to extract the YouTube video ID.
* @returns {string} The extracted YouTube video ID.
*/
const extractVideoId = videoId => {
// Sanity check
if (typeof videoId !== 'string') {
throw new Error('Invalid input: videoId must be a string')
}
// Remove any leading or trailing whitespace
videoId = videoId.trim()
// Check if the videoId is already a valid YouTube video ID
if (/^[a-zA-Z0-9_-]{11}$/.test(videoId)) {
return videoId
}
// Remove the protocol and domain from the URL
videoId = videoId.replace(
/^https?:\/\/(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)/,
''
)
// If there are any query parameters, remove them
if (videoId.includes('&')) {
videoId = videoId.split('&')[0]
}
return videoId
}
export default extractVideoId