Skip to content

Commit

Permalink
fix(utils): fixed Support for youtube /live/ in regex
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Dec 14, 2023
1 parent ce44d21 commit e5aa935
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/components/AddContentModal/utils/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ describe('youtubeRegex', () => {
expect(getInputType('https://www.youtube.com/watch?v=83eQ9flwVS0&ab_channel=EthanChlebowski')).toBe(LINK)
})

it('should assert we can check for youtube live clip regex', async () => {
expect(getInputType('https://youtube.com/live/tkdMgjEFNWs')).toBe(LINK)
})

it('should assert we can check for twitter spaces regex', async () => {
expect(getInputType('https://twitter.com/i/spaces/1zqKVqwrVzlxB?s=20')).toBe(LINK)
})
Expand All @@ -29,6 +33,9 @@ describe('youtubeRegex', () => {
it('should assert we can check for twitter handle regex', async () => {
expect(getInputType('https://twitter.com/@KevKevPal')).toBe(TWITTER_HANDLE)
})
it('should assert we can check for youtube live clip regex', async () => {

Check failure on line 36 in src/components/AddContentModal/utils/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Expected blank line before this statement
expect(getInputType('https://www.youtube.com/@MrBeast')).toBe(YOUTUBE_CHANNEL)
})
})

describe('extractNameFromLink', () => {
Expand Down
19 changes: 11 additions & 8 deletions src/components/AddContentModal/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ import { DOCUMENT, LINK, TWITTER_HANDLE, TWITTER_SOURCE, WEB_PAGE, YOUTUBE_CHANN
export const twitterHandlePattern = /\btwitter\.com\/(?:@)?([\w_]+)(?:$|\?[^/]*$)/

const youtubeRegex = /(https?:\/\/)?(www\.)?youtube\.com\/watch\?v=([A-Za-z0-9_-]+)/
const youtubeLiveRegex = /(https?:\/\/)?(www\.)?youtube\.com\/live\/([A-Za-z0-9_-]+)/
const twitterSpaceRegex = /https:\/\/twitter\.com\/i\/spaces\/([A-Za-z0-9_-]+)/
const tweetUrlRegex = /https:\/\/twitter\.com\/[^/]+\/status\/(\d+)/
const mp3Regex = /(https?:\/\/)?([A-Za-z0-9_-]+)\.mp3/
const youtubeChannelPattern = /https?:\/\/(www\.)?youtube\.com\/(@)?([\w-]+)/i
const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/

export function getInputType(source: string) {
let inputType = DOCUMENT
let inputType = DOCUMENT;

if (youtubeRegex.test(source) || twitterSpaceRegex.test(source) || mp3Regex.test(source)) {
inputType = LINK
if (youtubeLiveRegex.test(source)) {
inputType = LINK;
} else if (youtubeRegex.test(source) || twitterSpaceRegex.test(source) || mp3Regex.test(source)) {
inputType = LINK;
} else if (youtubeChannelPattern.test(source)) {
inputType = YOUTUBE_CHANNEL
inputType = YOUTUBE_CHANNEL;
} else if (twitterHandlePattern.test(source)) {
inputType = TWITTER_HANDLE
inputType = TWITTER_HANDLE;
} else if (tweetUrlRegex.test(source)) {
inputType = TWITTER_SOURCE
inputType = TWITTER_SOURCE;
} else if (genericUrlRegex.test(source)) {
inputType = WEB_PAGE
inputType = WEB_PAGE;
}

return inputType
return inputType;
}

export const extractNameFromLink = (inputString: string, type = ''): string | null => {
Expand Down

0 comments on commit e5aa935

Please sign in to comment.