Simplify regular expressions

This commit is contained in:
absidue 2021-12-26 16:55:10 +01:00
parent 6c607f062f
commit 770e896cc0
4 changed files with 7 additions and 7 deletions

View File

@ -92,7 +92,7 @@ async function internalValidate(url: string): Promise<TypeData> {
return { type: false };
}
if ((path[1] === 'track' || path[1] === 'album' || path[1] === 'playlist') && path[2].match(/^[0-9]+$/)) {
if ((path[1] === 'track' || path[1] === 'album' || path[1] === 'playlist') && path[2].match(/^\d+$/)) {
return {
type: path[1],
id: path[2]
@ -105,7 +105,7 @@ async function internalValidate(url: string): Promise<TypeData> {
if (
path.length === 3 &&
(path[1] === 'track' || path[1] === 'album' || path[1] === 'playlist') &&
path[2].match(/^[0-9]+$/)
path[2].match(/^\d+$/)
) {
return {
type: path[1],

View File

@ -9,7 +9,7 @@ interface formatOptions {
s?: string;
}
// RegExp for various js functions
const var_js = '[a-zA-Z_\\$][a-zA-Z_0-9]*';
const var_js = '[a-zA-Z_\\$]\\w*';
const singlequote_js = `'[^'\\\\]*(:?\\\\[\\s\\S][^'\\\\]*)*'`;
const duoblequote_js = `"[^"\\\\]*(:?\\\\[\\s\\S][^"\\\\]*)*"`;
const quote_js = `(?:${singlequote_js}|${duoblequote_js})`;

View File

@ -468,12 +468,12 @@ function getNormalPlaylist(response: any, body: any): YouTubePlayList {
if (!data.title.runs || !data.title.runs.length) throw new Error('Failed to Parse Playlist info.');
const author = playlist_details[1]?.playlistSidebarSecondaryInfoRenderer.videoOwner;
const views = data.stats.length === 3 ? data.stats[1].simpleText.replace(/[^0-9]/g, '') : 0;
const views = data.stats.length === 3 ? data.stats[1].simpleText.replace(/\D/g, '') : 0;
const lastUpdate =
data.stats
.find((x: any) => 'runs' in x && x['runs'].find((y: any) => y.text.toLowerCase().includes('last update')))
?.runs.pop()?.text ?? null;
const videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, '') || 0;
const videosCount = data.stats[0].runs[0].text.replace(/\D/g, '') || 0;
const res = new YouTubePlayList({
continuation: {

View File

@ -146,7 +146,7 @@ export function parseVideo(data?: any): YouTubeVideo {
artist: Boolean(badge?.includes('artist'))
},
uploadedAt: data.videoRenderer.publishedTimeText?.simpleText ?? null,
views: data.videoRenderer.viewCountText?.simpleText?.replace(/[^0-9]/g, '') ?? 0,
views: data.videoRenderer.viewCountText?.simpleText?.replace(/\D/g, '') ?? 0,
live: durationText ? false : true
});
@ -179,7 +179,7 @@ export function parsePlaylist(data?: any): YouTubePlayList {
name: channel?.text,
url: `https://www.youtube.com${channel?.navigationEndpoint.commandMetadata.webCommandMetadata.url}`
},
videos: parseInt(data.playlistRenderer.videoCount.replace(/[^0-9]/g, ''))
videos: parseInt(data.playlistRenderer.videoCount.replace(/\D/g, ''))
},
true
);