From af5a5b64dcbcf3492bc871e3c0643238fbaf7e48 Mon Sep 17 00:00:00 2001 From: aritrakarak <86865279+tr1ckydev@users.noreply.github.com> Date: Fri, 24 Sep 2021 10:53:57 +0530 Subject: [PATCH] Optimized validate function and added return types. --- play-dl/index.ts | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/play-dl/index.ts b/play-dl/index.ts index 5908a06..7e0c6b1 100644 --- a/play-dl/index.ts +++ b/play-dl/index.ts @@ -24,22 +24,17 @@ export async function stream_from_info( else return await yt_stream_info(info, cookie); } -export async function validate(url: string): Promise { - if (url.indexOf('spotify') !== -1) { - const check = sp_validate(url); - if (check) { - return 'sp_' + check; - } else return check; - } else if (url.indexOf('soundcloud') !== -1) { - const check = await so_validate(url); - if (check) { - return 'so_' + check; - } else return check; +export async function validate(url: string): Promise<"so_playlist" | "so_track" | "sp_track" | "sp_album" | "sp_playlist" | "yt_video" | "yt_playlist" | false> { + let check; + if (url.includes('spotify')) { + check = sp_validate(url); + return check !== false ? 'sp_' + check as "sp_track" | "sp_album" | "sp_playlist" : false; + } else if (url.includes('soundcloud')) { + check = await so_validate(url); + return check !== false ? 'so_' + check as "so_playlist" | "so_track" : false; } else { - const check = yt_validate(url); - if (check) { - return 'yt_' + check; - } else return check; + check = yt_validate(url); + return check !== false ? 'yt_' + check as "yt_video" | "yt_playlist" : false; } }