import { describe, expect, it } from "vitest"; import { fold } from "./normalize"; describe("fold", () => { it("removes apostrophes so the word is one term again", () => { expect(fold("won't do")).toBe("wont do"); expect(fold("Don't Stop Believin'")).toBe("dont stop believin"); // the needle side is folded too, so both spellings mean the same search expect(fold("won’t do")).toBe(fold("won't do")); // typographic apostrophe }); it("folds accents off letters that can shed them and maps the ones that cannot", () => { expect(fold("Beyoncé")).toBe("beyonce"); // decomposable: NFD + strip expect(fold("Björk")).toBe("bjork"); // ø has no canonical decomposition expect(fold("Ærvinder/Straße")).toBe("aervinderstrasse"); expect(fold("Łódź")).toBe("lodz"); }); it("deletes punctuation rather than spacing it out", () => { // "AC/DC" must answer to "acdc" — which only works if the parts join expect(fold("AC/DC")).toBe("acdc"); expect(fold("Pt. II")).toBe("pt ii"); // the space was already there expect(fold("R&B")).toBe("rb"); expect(fold("★")).toBe(""); }); it("leaves scripts it cannot transliterate alone", () => { // the tempting shortcut — drop what you do not understand — would empty a // CJK library's haystacks, and an empty haystack matches nothing expect(fold("宇多田ヒカル")).toBe("宇多田ヒカル"); expect(fold("Земфира")).toBe("земфира"); // case only }); it("collapses whitespace and trims", () => { expect(fold(" A Night\tat\nthe Opera ")).toBe("a night at the opera"); expect(fold("")).toBe(""); expect(fold("!!!")).toBe(""); }); it("is stable, so folding twice cannot drift from folding once", () => { for (const s of ["won't do", "Björk — Ø", "AC/DC", "Track 21 (Live '99)"]) { expect(fold(fold(s))).toBe(fold(s)); } }); it("keeps digits, which are the whole query for a year or a track number", () => { expect(fold("1999")).toBe("1999"); expect(fold("Track 21")).toBe("track 21"); }); });