chore: refactoring

This commit is contained in:
Cory Dransfeldt 2024-10-08 17:35:26 -07:00
parent df5fddefc0
commit e0593447eb
No known key found for this signature in database
40 changed files with 181 additions and 232 deletions

View file

@ -1,5 +1,5 @@
import sanitizeHtml from 'sanitize-html'
import { shuffleArray, sanitizeMediaString } from '../utilities/index.js'
import { shuffleArray } from '../utilities/index.js'
const BASE_URL = 'https://coryd.dev'
@ -12,7 +12,6 @@ export default {
},
formatNumber: (number) => number.toLocaleString('en-US'),
shuffleArray,
sanitizeMediaString,
sanitizeHtml: (html) => sanitizeHtml(html, {
textFilter: (text) => text.replace(/"/g, '')
}),

View file

@ -1,5 +1,5 @@
import { DateTime } from 'luxon'
import { shuffleArray, sanitizeMediaString } from '../utilities/index.js'
import { shuffleArray } from '../utilities/index.js'
export default {
featuredWatching: (watching, count) => {
@ -81,7 +81,6 @@ export default {
},
getLastWatched: (show) => show?.['episodes'][show['episodes']?.length - 1]?.['last_watched_at'],
sortByPlaysDescending: (data, key) => data.sort((a, b) => b[key] - a[key]),
genreStrings: (genres, key) => genres.map(genre => genre[key]),
mediaLinks: (data, type, count = 10) => {
if (!data || !type) return ''
@ -90,25 +89,17 @@ export default {
if (dataSlice.length === 0) return null
if (dataSlice.length === 1) {
const item = dataSlice[0]
if (type === 'genre') {
return `<a href="/music/genres/${sanitizeMediaString(item)}">${item}</a>`
} else if (type === 'artist') {
return `<a href="/music/artists/${sanitizeMediaString(item['name_string'])}-${sanitizeMediaString(item['country'].toLowerCase())}">${item['name_string']}</a>`
if (type === 'genre' || type === 'artist') {
return `<a href="${item['url']}">${item['name']}</a>`
} else if (type === 'book') {
return `<a href="/books/${item['isbn']}">${item['title']}</a>`
} else if (type === 'movie') {
return `<a href="${item['url']}">${item['title']}</a>`
}
}
const allButLast = dataSlice.slice(0, -1).map(item => {
if (type === 'genre') {
return `<a href="/music/genres/${sanitizeMediaString(item)}">${item}</a>`
} else if (type === 'artist') {
return `<a href="/music/artists/${sanitizeMediaString(item['name_string'])}-${sanitizeMediaString(item['country'].toLowerCase())}">${item['name_string']}</a>`
if (type === 'genre' || type === 'artist') {
return `<a href="${item['url']}">${item['name']}</a>`
} else if (type === 'book') {
return `<a href="/books/${item['isbn']}">${item['title']}</a>`
} else if (type === 'movie') {
return `<a href="${item['url']}">${item['title']}</a>`
}
}).join(', ')
@ -116,15 +107,12 @@ export default {
let last
const lastItem = dataSlice[dataSlice.length - 1]
if (type === 'genre') {
last = `<a href="/music/genres/${sanitizeMediaString(lastItem)}">${lastItem}</a>`
} else if (type === 'artist') {
last = `<a href="/music/artists/${sanitizeMediaString(lastItem['name_string'])}-${sanitizeMediaString(lastItem['country'].toLowerCase())}">${lastItem['name_string']}</a>`
if (type === 'genre' || type === 'artist') {
last = `<a href="${lastItem['url']}">${lastItem['name']}</a>`
} else if (type === 'book') {
last = `<a href="/books/${lastItem['isbn']}">${lastItem['title']}</a>`
} else if (type === 'movie') {
last = `<a href="${lastItem['url']}">${lastItem['title']}</a>`
}
return `${allButLast} and ${last}`
},
formatVenue: (venue) => venue.split(',')[0].trim(),

View file

@ -1,5 +1,3 @@
import slugify from 'slugify'
export const shuffleArray = array => {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1))
@ -10,17 +8,6 @@ export const shuffleArray = array => {
return array
}
export const sanitizeMediaString = (str) => {
if (!str) return null
const sanitizedString = str.normalize('NFD').replace(/[\u0300-\u036f\u2010—\.\?\(\)\[\]\{\}]/g, '').replace(/\.{3}/g, '')
return slugify(sanitizedString, {
replacement: '-',
remove: /[#,&,+()$~%.'":*?<>{}]/g,
lower: true,
})
}
export const regionNames = new Intl.DisplayNames(['en'], { type: 'region' })
export const getCountryName = (countryCode) => regionNames.of(countryCode.trim()) || countryCode.trim()