Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217/** * What the app prints where it has a tag, and what two names agreeing means. * * Tags are written for a database, not for a bar. The record that made this file exists is tagged * `Partners For Life ft. Pharrell Williams & Jamie Foxx` on a bootleg called *The Neptunes: The * Other Side Of The Planet (Disc 12) [Presented by ESSNSE & Cory Townes]* — and the bar has about * fifty-two characters of column in which to say which song is being heard. Everything in here is * about the difference between the tag and the sentence: what can be taken off a name without * changing what it names, and what to do when even the remainder is too long for the space. * * Two rules hold the whole file together, and both come from the way `library/rows.ts` already * separates `nameKey` from the label a row carries: * * - **nothing here decides identity.** Every function in this file is allowed to *shorten* what the * listener reads and nothing else. The strings that find a track in the list, key a lyrics cache, * or ask LRCLIB a question stay byte-for-byte what the server sent — a tooltip, a `locate()` * label and a search query are not places where legibility is worth a wrong answer. * - **a strip only ever deletes decoration, and never wins.** When the tidied form would be empty * — an album tagged `[Explicit]` and nothing else — the original comes back. A label that has been * tidied into the blank is not legible, it is missing, and "missing" is the one thing a name in * the bar must not be able to become. */
/** How far a printed name may run before it has stopped being a name, in characters. */const NAME_CEILING = 52;
/** A name, as compared: no case, no punctuation, no runs of spaces. */export function fold(value: string | null | undefined): string { return (value ?? "") .toLocaleLowerCase() .replace(/[^\p{L}\p{N}]+/gu, " ") .trim();}
/** * A title taken apart into the song and the people credited on it. * * Because it is the library's tags and not the recording that are usually wrong: a rip off a * promotional compilation carries the collective as its artist and the box set's title as its album, * and the song's own name arrives with the guests appended to it — `Partners For Life ft. Pharrell * Williams & Jamie Foxx`, where the database has the same recording as *Partners for Life* by Diddy * and never heard of the Disc 12 it came off. Stripping the credit is the one edit that makes the * name match without making it vaguer, and it is the same edit whether the destination is the bar's * title line or LRCLIB's third question — which is why this is the one definition of a credit in the * app, imported by both rather than written twice. * * `with` is honoured only inside brackets. `With or Without You` is a title and not a credit, and a * stripper that ate it would ask about *You* — a wrong question is worse than the miss it came from. */export function creditedOut(title: string): { bare: string; credited: string[] } { const credited: string[] = []; let bare = title.trim();
// The bracketed spelling, anywhere in the title: "(feat. X)", "[Featuring X]", "(with X)". bare = bare.replace(/[([{]\s*(?:feat|ft|featuring|with|vs)\b[^)\]}]*[)\]}]/gi, (group) => { credited.push(...namesIn(group)); return ""; }); // The bare spelling, and only at the end: "Song ft. X". `vs` is not in this list at all — it is a // remix convention inside brackets (`(vs. someone's remix)`), and at the end of a bare title it is // ordinary prose: *Dogs vs. Cats* is a title, and the bare form of it is not *Dogs*. bare = bare.replace(/\s+\b(?:feat|ft|featuring)\b\.?\s+.*$/i, (group) => { credited.push(...namesIn(group)); return ""; });
return { bare: tidy(bare), credited };}
/** The names inside a credit: "feat. X & Y, Z" is three people and one word to throw away. */function namesIn(credit: string): string[] { return credit .replace(/^\s*[^a-zA-Z]*\b(?:feat|ft|featuring|with|vs)\b\.?\s*/i, "") .split(/\s*(?:,|&|;|\/|\band\b)\s*/i) .map((name) => name.replace(/[)[\]]/g, "").trim()) .filter((name) => name.length > 1 && !/^\d+$/.test(name));}
/** * What a bracket around a record name is *for*. * * A bracket in an album tag is either part of the record's name — *(Original Motion Picture * Soundtrack)*, *(Live at the Apollo)*, *(The Final Cut)* — or a label stapled to this particular * pressing, which says nothing a listener needs in order to recognise the record and everything * about which of its fifteen reissues the rip came from. Only the second kind comes off, and it is * decided by keyword rather than by "is in brackets", because the brackets are the one thing both * kinds have in common: eating every bracket would take *(Dark Side of the Moon)* off an album * called exactly that. * * The list is deliberately dull and deliberately short, and errs toward leaving a bracket alone: * `Remastered` and `Deluxe Edition` are the two debatable ones, and both stay, because a listener * comparing the bar to the sleeve in their hand is entitled to the same words on both. */const PRESSING = new RegExp( [ // Which disc, which side, which half of the double. "^\\s*(?:disc|disk|cd|vinyl|tape|lp|part|vol(?:ume)?|side)\\b\\s*\\.?\\s*\\d*", // Which enthusiastic nobody put this compilation together. "^\\s*(?:presented|hosted|compiled|selected|curated|assembled|mixed|arranged)\\b", // What this pressing has that the others don't, which is the opposite of a name. "^\\s*(?:bonus|extra|additional|bonus\\s+track|rare|unreleased|demo(?:s)?)\\b", // What the censors did or did not do to it. "^\\s*(?:explicit|clean|censored|uncensored)(?:\\s+(?:version|edit|album))?\\s*$", // "2 CD", "3 x LP": the count of the thing, stapled to its name. "^\\s*\\d+\\s*(?:x\\s*)?(?:cd|disc|lp|vinyl)\\s*$", ].join("|"), "i",);
/** * The artist's own name, repeated at the front of the record's. * * Box sets do this constantly — *The Neptunes: The Other Side Of The Planet* — and the bar prints * the artist on the line underneath, so the repetition is the same word twice in two lines. It comes * off only when it really is that word: an album titled *Blade Runner: The Final Cut* by Vangelis * keeps every character of its title, because a colon there is punctuation in a name and not a * label on a shelf. */function dropArtistPrefix(album: string, artist: string): string { const prefix = /^\s*([^:]{2,}?):\s+/.exec(album); if (!prefix) return album; const who = fold(artist); return who && fold(prefix[1]) === who ? album.slice(prefix[0].length) : album;}
/** Bracketed labels about this pressing, taken off; brackets that are part of the name stay. */function dropPressings(text: string): string { return text.replace(/[([{]([^)\]}]*)[)\]}]/g, (group, inside: string) => PRESSING.test(inside.trim()) ? "" : group, );}
/** The litter a strip leaves behind: the space where a bracket was, the colon that introduced it. */function tidy(text: string): string { return text .replace(/\s{2,}/g, " ") .replace(/\s+([,:;)\]])/g, "$1") .replace(/^[,;:\-–—\s]+|[,;:\-–—\s]+$/g, "") .trim();}
/** * The name as it is printed, if it still has to be cut to fit. * * The bar already has an ellipsis and this is not it. `.pb-meta-line` clips at whatever pixel the * window happens to be, mid-syllable if the window likes it, and — the part that is not cosmetic — * the three names are content-sized flex items, so a two-hundred-character bootleg title is claiming * width from the artist's name *behind* its own ellipsis. A ceiling on the content is what bounds * that argument, and it is drawn at 52 because that is roughly where the bar's column runs out at a * normal window anyway: a name longer than this has become a sentence, and a sentence in the bar is * a paragraph nobody reads. * * Cut at the last space that fits, so the words that go are whole words; a first word too long to * ever fit is cut where it stands, because a fragment of a very long word still beats a bar with * nothing printed on it. */export function capOut(text: string, ceiling = NAME_CEILING): string { if (text.length <= ceiling) return text; const room = text.slice(0, ceiling); const at = room.lastIndexOf(" "); // Half the ceiling is the floor on a word boundary: below that, what is left would be shorter than // the fragment the CSS would have shown, and an ellipsis after one syllable says nothing. if (at >= Math.ceil(ceiling / 2)) return `${room.slice(0, at)}…`; // No space to cut at: the first word alone is longer than the ceiling, and a fragment of it still // beats a bar with nothing printed where the title should be. return `${room.slice(0, ceiling - 1)}…`;}
/** The three names as the bar prints them. Empty string means "there is nothing of this to print". */export interface NameLines { title: string; album: string; artist: string;}
/** * What the bar says about a track, from what the tags say about it. * * Three strips, each narrower than the last, because the three tags are not equally reliable: * * - a **title** loses its credits, which are people rather than song, and nothing else — *Live at * the Apollo* is the title. * - a **record** loses the artist's name repeated in front of it and the pressing labels stapled to * the end, because both describe the release rather than the music, and a box set's release is * exactly the kind of thing that arrives tagged by whoever made the box set. * - an **artist** loses its credits too, since `A feat. B` in an ARTIST tag is the same two people * as in a TITLE tag, and keeps everything else: an artist called *(hed) p.e.* keeps its brackets. * * Every one of them falls back to the raw tag when the tidy form is empty, and every one of them is * capped. The originals stay available to everything that needs them — the tooltip, `locate()`, the * lyrics cache key and LRCLIB's questions all still see the tag the server sent. */export function nameLines( tags: | { title?: string | null; album?: string | null; artist?: string | null; } | null | undefined,): NameLines { const title = tags?.title?.trim() ?? ""; const album = tags?.album?.trim() ?? ""; const artist = tags?.artist?.trim() ?? "";
const strippedTitle = creditedOut(title).bare; const strippedArtist = creditedOut(artist).bare; const strippedAlbum = tidy(dropPressings(dropArtistPrefix(album, artist || strippedArtist)));
return { title: capOut(strippedTitle || title), album: capOut(strippedAlbum || album), artist: capOut(strippedArtist || artist), };}