chore: split out helpers

This commit is contained in:
Cory Dransfeldt 2023-07-21 15:51:07 -07:00
parent 341221c78c
commit 528a61ce77
No known key found for this signature in database
3 changed files with 58 additions and 51 deletions

23
src/utils/grammar.js Normal file
View file

@ -0,0 +1,23 @@
const titleCaseExceptions = require('./../_data/json/title-case-exceptions.json')
module.exports = {
/**
* Accepts a string that is then transformed to title case and returned.
*
* @name titleCase
* @param {string} string
* @returns {string}
*/
titleCase: (string) => {
if (!string) return ''
return string
.toLowerCase()
.split(' ')
.map((word, i) => {
return titleCaseExceptions.exceptions.includes(word) && i !== 0
? word
: word.charAt(0).toUpperCase().concat(word.substring(1))
})
.join(' ')
},
}

32
src/utils/media.js Normal file
View file

@ -0,0 +1,32 @@
const artistAliases = require('../_data/json/artist-aliases.json')
module.exports = {
/**
* Accepts a string representing an artist name, checks to see if said artist name
* exists in an artist alias group of shape string[]. If so, replaces the provided
* artist name with the canonical artist name.
*
* @name aliasArtist
* @param {string} artist
* @returns {string}
*/
aliasArtist: (artist) => {
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(artist))
if (aliased) artist = aliased.artist
return artist
},
/**
* Accepts a media name represented as a string (album or song name) and replaces
* matches in the `denyList` with an empty string before returning the result.
*
* @name sanitizeMedia
* @param {string} media
* @returns {string}
*/
sanitizeMedia: (media) => {
const denyList =
/-\s*(?:single|ep)\s*|(\[|\()(Deluxe Edition|Special Edition|Remastered|Full Dynamic Range Edition|Anniversary Edition)(\]|\))/gi
return media.replace(denyList, '').trim()
},
}