UNPKG

@mohtasimalam/clean-title

Version:
1 lines 2.8 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["const removeSpecialChars = (str: string) => {\r\n return str.replace(/[^\\p{L}\\p{N}\\s]/gu, \"\");\r\n};\r\n\r\n/**\r\n * Clean a title string by removing special characters, converting to lowercase, removing unnecessary tags,\r\n * extra spaces, and diacritics, ensuring a clean and normalized title.\r\n *\r\n * @param {string} str - The title string to clean.\r\n * @returns {string} The cleaned title string, truncated to a maximum of 255 characters.\r\n *\r\n * @example\r\n * // Basic usage:\r\n * cleanTitle(\"Official Music Video - AMAZING 🎶 song!\")\r\n * // returns \"amazing song\"\r\n *\r\n * @example\r\n * // Removes unnecessary words, extra spaces, and diacritics:\r\n * cleanTitle(\"L'animation Of My Life - Official Audio\")\r\n * // returns \"l animation of my life\"\r\n *\r\n * @description\r\n * This function performs the following operations in sequence:\r\n * 1. Removes special characters while preserving alphanumeric characters and spaces.\r\n * 2. Converts all characters to lowercase.\r\n * 3. Removes commonly used, unnecessary words in titles such as \"official,\" \"music video,\" and \"lyrics.\"\r\n * 4. Replaces multiple spaces with a single space.\r\n * 5. Removes any leading or trailing whitespace.\r\n * 6. Normalizes characters to remove diacritics (e.g., \"é\" becomes \"e\").\r\n * 7. Truncates the final string to 255 characters.\r\n */\r\n\r\nexport const cleanTitle = (str: string) => {\r\n let output = removeSpecialChars(str);\r\n\r\n output = output.toLowerCase();\r\n output = output.replace(\r\n /\\b(official|music|video|lyrics|audio|animated|amv|omv|m\\/v|a?m\\s*v)\\b|\\bofficial\\s*(audio|music|lyrics\\s*video|lyrics)?\\b/gi,\r\n \"\"\r\n );\r\n output = output.replace(/- Topic$/gi, \"\");\r\n output = output.replace(/s+/g, \" \");\r\n output = output.replace(/\\s{2,}/g, \" \");\r\n output = output.replace(/^\\s+|\\s+$/g, \"\");\r\n output = output.replace(/\\(([^)]*)\\)/gi, \"\");\r\n // biome-ignore lint/suspicious/noMisleadingCharacterClass: <explanation>\r\n output = output.normalize(\"NFD\").replace(/[\\u0300-\\u036f]/g, \"\");\r\n output = output.trim();\r\n\r\n output = output.slice(0, 255);\r\n\r\n return output;\r\n};\r\n"],"mappings":"AAAA,IAAMA,EAAsBC,GACnBA,EAAI,QAAQ,oBAAqB,EAAE,EA+B/BC,EAAcD,GAAgB,CACzC,IAAIE,EAASH,EAAmBC,CAAG,EAEnC,OAAAE,EAASA,EAAO,YAAY,EAC5BA,EAASA,EAAO,QACd,8HACA,EACF,EACAA,EAASA,EAAO,QAAQ,aAAc,EAAE,EACxCA,EAASA,EAAO,QAAQ,MAAO,GAAG,EAClCA,EAASA,EAAO,QAAQ,UAAW,GAAG,EACtCA,EAASA,EAAO,QAAQ,aAAc,EAAE,EACxCA,EAASA,EAAO,QAAQ,gBAAiB,EAAE,EAE3CA,EAASA,EAAO,UAAU,KAAK,EAAE,QAAQ,mBAAoB,EAAE,EAC/DA,EAASA,EAAO,KAAK,EAErBA,EAASA,EAAO,MAAM,EAAG,GAAG,EAErBA,CACT","names":["removeSpecialChars","str","cleanTitle","output"]}