From d434b19c9b46b01c877cb06cdfe1e1487cfe12be Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 26 Dec 2020 13:29:47 -0600 Subject: [PATCH] Use whole word regex matches for descriptions --- helpers/build-video-list.js | 6 +++- helpers/get-list.js | 2 +- helpers/related.js | 57 ++++++++++++++++++++++++++++++++++ pages/app/_slug/benchmarks.vue | 17 ++++------ pages/tv/_slug.vue | 33 +++----------------- 5 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 helpers/related.js diff --git a/helpers/build-video-list.js b/helpers/build-video-list.js index e7c22a7..9cf4094 100644 --- a/helpers/build-video-list.js +++ b/helpers/build-video-list.js @@ -6,6 +6,10 @@ import { byTimeThenNull } from './sort-list.js' import { getVideoEndpoint } from './app-derived.js' import parseGithubDate from './parse-github-date' +export function matchesWholeWord (needle, haystack) { + return new RegExp('\\b' + needle + '\\b').test(haystack) +} + const videoFeaturesApp = function (app, video) { const appFuzzyName = app.name.toLowerCase() if (video.title.toLowerCase().includes(appFuzzyName)) return true @@ -14,7 +18,7 @@ const videoFeaturesApp = function (app, video) { if (appIsInTimestamps) return true - if (video.description.toLowerCase().includes(appFuzzyName)) return true + if (matchesWholeWord(appFuzzyName, video.description.toLowerCase())) return true return false } diff --git a/helpers/get-list.js b/helpers/get-list.js index 856a6d6..7bb6366 100644 --- a/helpers/get-list.js +++ b/helpers/get-list.js @@ -4,7 +4,7 @@ import homebrewList from '~/static/homebrew-list.json' import { byTimeThenNull } from '~/helpers/sort-list.js' -export const allVideoList = [ +export const allVideoAppsList = [ ...appList.sort(byTimeThenNull), ...gameList, ] diff --git a/helpers/related.js b/helpers/related.js new file mode 100644 index 0000000..324541d --- /dev/null +++ b/helpers/related.js @@ -0,0 +1,57 @@ +import { allVideoAppsList } from '~/helpers/get-list.js' +import videoList from '~/static/video-list.json' + +export function matchesWholeWord (needle, haystack) { + return new RegExp('\\b' + needle + '\\b').test(haystack) +} + +export function appsRelatedToVideo ( video ) { + const relatedApps = [] + + // Find the apps listed in this video + for (const app of allVideoAppsList) { + // Skip this app if it's not listed in the videos apps + if (!video.apps.includes(app.slug)) continue + + // Add this app to our featured app list + relatedApps.push(app) + } + + return relatedApps +} + +export function videosRelatedToVideo ( video ) { + const relatedVideos = [] + + const featuredApps = appsRelatedToVideo( video ) + + // Find other videos that also feature this video's app + for (const otherVideo of videoList) { + for (const app of featuredApps) { + // Skip if this app is not in the other video's apps + if (!otherVideo.apps.includes(app.slug)) continue + + // Skip if the other video is, in fact, this video + if (otherVideo.slug === video.slug) continue + + // Add this video to our related videos list + relatedVideos.push(otherVideo) + } + } + + return relatedVideos +} + + +export function videosRelatedToApp ( app ) { + const relatedVideos = [] + + // Find other videos that also feature this video's app + for (const video of videoList) { + if (!video.apps.includes(app.slug)) continue + + relatedVideos.push(video) + } + + return relatedVideos +} diff --git a/pages/app/_slug/benchmarks.vue b/pages/app/_slug/benchmarks.vue index d3fe7a6..fd168c5 100644 --- a/pages/app/_slug/benchmarks.vue +++ b/pages/app/_slug/benchmarks.vue @@ -70,21 +70,16 @@ export default { }, async asyncData ({ params: { slug } }) { - const { allVideoList } = await import('~/helpers/get-list.js') - const { default: videoList } = await import('~/static/video-list.json') + const { allVideoAppsList } = await import('~/helpers/get-list.js') + // const { default: videoList } = await import('~/static/video-list.json') - const app = allVideoList.find(app => (app.slug === slug)) + const { videosRelatedToApp } = await import('~/helpers/related.js') + + const app = allVideoAppsList.find(app => (app.slug === slug)) // const featuredApps = [] - const relatedVideos = [] - - // Find other videos that also feature this video's app - for (const video of videoList) { - if (!video.apps.includes(app.slug)) continue - - relatedVideos.push(video) - } + const relatedVideos = videosRelatedToApp( app ) return { app, diff --git a/pages/tv/_slug.vue b/pages/tv/_slug.vue index d381ef2..68f05f5 100644 --- a/pages/tv/_slug.vue +++ b/pages/tv/_slug.vue @@ -71,40 +71,17 @@ export default { }, async asyncData ({ params: { slug } }) { - const { allVideoList: allVideoAppsList } = await import('~/helpers/get-list.js') + const { appsRelatedToVideo, videosRelatedToVideo } = await import('~/helpers/related.js') const { default: videoList } = await import('~/static/video-list.json') - const featuredApps = [] - // Find the video for our current page const video = videoList.find(video => (video.slug === slug)) - console.log('video', video) + // Get featured apps + const featuredApps = appsRelatedToVideo(video) - // Find the apps listed in this video - for (const app of allVideoAppsList) { - // Skip this app if it's not listed in the videos apps - if (!video.apps.includes(app.slug)) continue - - // Add this app to our featured app list - featuredApps.push(app) - } - - const relatedVideos = [] - - // Find other videos that also feature this video's app - for (const otherVideo of videoList) { - for (const app of featuredApps) { - // Skip if this app is not in the other video's apps - if (!otherVideo.apps.includes(app.slug)) continue - - // Skip if the other video is, in fact, this video - if (otherVideo.slug === video.slug) continue - - // Add this video to our related videos list - relatedVideos.push(otherVideo) - } - } + // Get related videos + const relatedVideos = videosRelatedToVideo(video) return { video,