Created
December 21, 2022 10:33
-
-
Save mre/48dff9ab3c846b692c35fce600baae7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an algorithm, which creates an ID for a podcast episode. | |
// It is based on the the podcast's title, episode's title, the episode number, | |
// the date of the episode and the episode's mp3 file name. | |
// | |
// All fields can change over time, but the ID should remain the same. | |
// Think of it more like a user agent string than a unique ID: it is "unique | |
// enough" for identifying episodes in most cases. | |
// | |
// It's used to identify episodes in the database. | |
// The algorithm is as follows: | |
// 1. Get the podcast's title, the episode title, the episode number, the date of the episode, | |
// and the episode's mp3 file name. | |
// 2. Remove all non-alphanumeric characters | |
// 3. Remove all leading and trailing whitespace | |
// 4. Concatenate the fields. Separate the fields with a single `:` character. | |
// 5. Convert the concatenated string to lowercase. | |
// The resulting string is the ID of the episode. | |
// Example: | |
// Episode title: "Episode 1: The Beginning" | |
// Podcast title: "The Podcast" | |
// Episode number: "1" | |
// Date of the episode: "2018-01-01" | |
// Episode's mp3 file name: "episode-1.mp3" | |
// Resulting ID: "thepodcast:episode1thebeginning:1:2018-01-01:episode1.mp3" | |
// Cleanup function that removes all non-alphanumeric characters and leading and trailing whitespace. | |
const cleanup = (string) => { | |
// Remove all non-alphanumeric characters | |
const alphanumeric = string.replace(/[^a-zA-Z0-9]/g, ''); | |
// Remove all leading and trailing whitespace | |
const alphanumericTrimmed = alphanumeric.trim(); | |
// Convert the string to lowercase. | |
const lowercase = alphanumericTrimmed.toLowerCase(); | |
// Return the resulting string. | |
return lowercase; | |
} | |
// The function that creates the ID. | |
const id = (podcastTitle, episodeTitle, episodeNumber, episodeDate, episodeMp3FileName) => { | |
// Cleanup all fields | |
const cleanedPodcastTitle = cleanup(podcastTitle); | |
const cleanedEpisodeTitle = cleanup(episodeTitle); | |
const cleanedEpisodeNumber = cleanup(episodeNumber); | |
const cleanedEpisodeDate = cleanup(episodeDate); | |
const cleanedEpisodeMp3FileName = cleanup(episodeMp3FileName); | |
// Concatenate the fields. Separate the fields with a single `:` character. | |
const concatenated = cleanedPodcastTitle + ':' + cleanedEpisodeTitle + ':' + cleanedEpisodeNumber + ':' + cleanedEpisodeDate + ':' + cleanedEpisodeMp3FileName; | |
// Return the resulting string. | |
return concatenated; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment