Added title casing

This commit is contained in:
Lukas Wölfer
2025-06-05 23:01:25 +02:00
parent 76bc73ba97
commit 1fd15470f8
5 changed files with 85 additions and 18 deletions

View File

@@ -1,10 +1,71 @@
import { VideoDescription } from "./main.ts";
export const SMALL_WORDS = new Set([
"a",
"an",
"and",
"as",
"at",
"because",
"but",
"by",
"en",
"for",
"if",
"in",
"neither",
"nor",
"of",
"on",
"only",
"or",
"over",
"per",
"so",
"some",
"than",
"that",
"the",
"to",
"up",
"upon",
"v",
"versus",
"via",
"vs",
"when",
"with",
"without",
"yet",
]);
function capitalize(word: string): string {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
function camelToTitleCase(camelCaseStr: string): string {
// Insert a space before each uppercase letter
const titleCaseStr = camelCaseStr.replace(/([A-Z])/g, ' $1');
// Split the string into words and process each word using map
const words = titleCaseStr.split(' ')
.map(v => v.toLowerCase())
.map(word => {
if (SMALL_WORDS.has(word)) {
return word;
}
return capitalize(word)
});
return capitalize(words.join(' '));
}
export function singleVideoDescription(video: VideoDescription): string {
const teachersList = video.teachers.map(v => "[[" + v + "]]").join(" & ");
const nagElement = video.nags.length > 0 ? `<span title="${video.nags.join("&#010;")}">🔴</span>` : "";
return `=== ${video.title} ===
return `=== ${camelToTitleCase(video.title)} ===
Date: {{#time: Y-m-d (D) | ${video.date}}} ${nagElement}<br>
Teachers: ${teachersList}<br>
Level: ${video.level}
@@ -38,21 +99,23 @@ export function writeSections(events: VideoDescription[][]): string {
}).join("\n\n\n")
}
export function bucketEvents(events: VideoDescription[]): VideoDescription[][] {
const buckets: Record<string, VideoDescription[]> = {}
for (const e of events) {
const tag = e.event + e.location + new Date(e.date).getFullYear().toString()
if (tag in buckets) {
buckets[tag].push(e)
} else {
buckets[tag] = [e]
}
}
/**
*
* @param videos
* @returns Bucket of videos for of each event, grouped by `name`, `location` and `year`
*/
export function bucketEvents(videos: VideoDescription[]): VideoDescription[][] {
const buckets = Object.groupBy(videos, (video) => {
return `${video.event}${video.location}${new Date(video.date).getFullYear()}`;
Object.values(buckets)
.forEach(b =>
})
const sortedBuckets = Object.values(buckets)
.filter(v => v !== undefined)
.map(b =>
b.sort((a, b) =>
new Date(a.date).getTime() - new Date(b.date).getTime()))
return Object.values(buckets).sort((a, b) => new Date(a[0].date).getTime() - new Date(b[0].date).getTime())
return sortedBuckets
.sort((a, b) => new Date(a[0].date).getTime() - new Date(b[0].date).getTime())
}