/** * The letters `remove_accents()` in WordPress core converts to basic Latin, * copied from its `$chars` table. The locale-specific rules (German, Danish, * Catalan, Serbian and Bosnian) are left out, since they depend on the site * locale. */ const CHARS: Record< string, string > = { // Decompositions for Latin-1 Supplement. '\u00aa': 'a', '\u00ba': 'o', '\u00c0': 'A', '\u00c1': 'A', '\u00c2': 'A', '\u00c3': 'A', '\u00c4': 'A', '\u00c5': 'A', '\u00c6': 'AE', '\u00c7': 'C', '\u00c8': 'E', '\u00c9': 'E', '\u00ca': 'E', '\u00cb': 'E', '\u00cc': 'I', '\u00cd': 'I', '\u00ce': 'I', '\u00cf': 'I', '\u00d0': 'D', '\u00d1': 'N', '\u00d2': 'O', '\u00d3': 'O', '\u00d4': 'O', '\u00d5': 'O', '\u00d6': 'O', '\u00d9': 'U', '\u00da': 'U', '\u00db': 'U', '\u00dc': 'U', '\u00dd': 'Y', '\u00de': 'TH', '\u00df': 's', '\u00e0': 'a', '\u00e1': 'a', '\u00e2': 'a', '\u00e3': 'a', '\u00e4': 'a', '\u00e5': 'a', '\u00e6': 'ae', '\u00e7': 'c', '\u00e8': 'e', '\u00e9': 'e', '\u00ea': 'e', '\u00eb': 'e', '\u00ec': 'i', '\u00ed': 'i', '\u00ee': 'i', '\u00ef': 'i', '\u00f0': 'd', '\u00f1': 'n', '\u00f2': 'o', '\u00f3': 'o', '\u00f4': 'o', '\u00f5': 'o', '\u00f6': 'o', '\u00f8': 'o', '\u00f9': 'u', '\u00fa': 'u', '\u00fb': 'u', '\u00fc': 'u', '\u00fd': 'y', '\u00fe': 'th', '\u00ff': 'y', '\u00d8': 'O', // Decompositions for Latin Extended-A. '\u0100': 'A', '\u0101': 'a', '\u0102': 'A', '\u0103': 'a', '\u0104': 'A', '\u0105': 'a', '\u0106': 'C', '\u0107': 'c', '\u0108': 'C', '\u0109': 'c', '\u010a': 'C', '\u010b': 'c', '\u010c': 'C', '\u010d': 'c', '\u010e': 'D', '\u010f': 'd', '\u0110': 'D', '\u0111': 'd', '\u0112': 'E', '\u0113': 'e', '\u0114': 'E', '\u0115': 'e', '\u0116': 'E', '\u0117': 'e', '\u0118': 'E', '\u0119': 'e', '\u011a': 'E', '\u011b': 'e', '\u011c': 'G', '\u011d': 'g', '\u011e': 'G', '\u011f': 'g', '\u0120': 'G', '\u0121': 'g', '\u0122': 'G', '\u0123': 'g', '\u0124': 'H', '\u0125': 'h', '\u0126': 'H', '\u0127': 'h', '\u0128': 'I', '\u0129': 'i', '\u012a': 'I', '\u012b': 'i', '\u012c': 'I', '\u012d': 'i', '\u012e': 'I', '\u012f': 'i', '\u0130': 'I', '\u0131': 'i', '\u0132': 'IJ', '\u0133': 'ij', '\u0134': 'J', '\u0135': 'j', '\u0136': 'K', '\u0137': 'k', '\u0138': 'k', '\u0139': 'L', '\u013a': 'l', '\u013b': 'L', '\u013c': 'l', '\u013d': 'L', '\u013e': 'l', '\u013f': 'L', '\u0140': 'l', '\u0141': 'L', '\u0142': 'l', '\u0143': 'N', '\u0144': 'n', '\u0145': 'N', '\u0146': 'n', '\u0147': 'N', '\u0148': 'n', '\u0149': 'n', '\u014a': 'N', '\u014b': 'n', '\u014c': 'O', '\u014d': 'o', '\u014e': 'O', '\u014f': 'o', '\u0150': 'O', '\u0151': 'o', '\u0152': 'OE', '\u0153': 'oe', '\u0154': 'R', '\u0155': 'r', '\u0156': 'R', '\u0157': 'r', '\u0158': 'R', '\u0159': 'r', '\u015a': 'S', '\u015b': 's', '\u015c': 'S', '\u015d': 's', '\u015e': 'S', '\u015f': 's', '\u0160': 'S', '\u0161': 's', '\u0162': 'T', '\u0163': 't', '\u0164': 'T', '\u0165': 't', '\u0166': 'T', '\u0167': 't', '\u0168': 'U', '\u0169': 'u', '\u016a': 'U', '\u016b': 'u', '\u016c': 'U', '\u016d': 'u', '\u016e': 'U', '\u016f': 'u', '\u0170': 'U', '\u0171': 'u', '\u0172': 'U', '\u0173': 'u', '\u0174': 'W', '\u0175': 'w', '\u0176': 'Y', '\u0177': 'y', '\u0178': 'Y', '\u0179': 'Z', '\u017a': 'z', '\u017b': 'Z', '\u017c': 'z', '\u017d': 'Z', '\u017e': 'z', '\u017f': 's', // Decompositions for Latin Extended-B. '\u018f': 'E', '\u01dd': 'e', '\u0218': 'S', '\u0219': 's', '\u021a': 'T', '\u021b': 't', // Euro sign. '\u20ac': 'E', // GBP (Pound) sign. '\u00a3': '', // Vowels with diacritic (Vietnamese). Unmarked. '\u01a0': 'O', '\u01a1': 'o', '\u01af': 'U', '\u01b0': 'u', // Grave accent. '\u1ea6': 'A', '\u1ea7': 'a', '\u1eb0': 'A', '\u1eb1': 'a', '\u1ec0': 'E', '\u1ec1': 'e', '\u1ed2': 'O', '\u1ed3': 'o', '\u1edc': 'O', '\u1edd': 'o', '\u1eea': 'U', '\u1eeb': 'u', '\u1ef2': 'Y', '\u1ef3': 'y', // Hook. '\u1ea2': 'A', '\u1ea3': 'a', '\u1ea8': 'A', '\u1ea9': 'a', '\u1eb2': 'A', '\u1eb3': 'a', '\u1eba': 'E', '\u1ebb': 'e', '\u1ec2': 'E', '\u1ec3': 'e', '\u1ec8': 'I', '\u1ec9': 'i', '\u1ece': 'O', '\u1ecf': 'o', '\u1ed4': 'O', '\u1ed5': 'o', '\u1ede': 'O', '\u1edf': 'o', '\u1ee6': 'U', '\u1ee7': 'u', '\u1eec': 'U', '\u1eed': 'u', '\u1ef6': 'Y', '\u1ef7': 'y', // Tilde. '\u1eaa': 'A', '\u1eab': 'a', '\u1eb4': 'A', '\u1eb5': 'a', '\u1ebc': 'E', '\u1ebd': 'e', '\u1ec4': 'E', '\u1ec5': 'e', '\u1ed6': 'O', '\u1ed7': 'o', '\u1ee0': 'O', '\u1ee1': 'o', '\u1eee': 'U', '\u1eef': 'u', '\u1ef8': 'Y', '\u1ef9': 'y', // Acute accent. '\u1ea4': 'A', '\u1ea5': 'a', '\u1eae': 'A', '\u1eaf': 'a', '\u1ebe': 'E', '\u1ebf': 'e', '\u1ed0': 'O', '\u1ed1': 'o', '\u1eda': 'O', '\u1edb': 'o', '\u1ee8': 'U', '\u1ee9': 'u', // Dot below. '\u1ea0': 'A', '\u1ea1': 'a', '\u1eac': 'A', '\u1ead': 'a', '\u1eb6': 'A', '\u1eb7': 'a', '\u1eb8': 'E', '\u1eb9': 'e', '\u1ec6': 'E', '\u1ec7': 'e', '\u1eca': 'I', '\u1ecb': 'i', '\u1ecc': 'O', '\u1ecd': 'o', '\u1ed8': 'O', '\u1ed9': 'o', '\u1ee2': 'O', '\u1ee3': 'o', '\u1ee4': 'U', '\u1ee5': 'u', '\u1ef0': 'U', '\u1ef1': 'u', '\u1ef4': 'Y', '\u1ef5': 'y', // Vowels with diacritic (Chinese, Hanyu Pinyin). '\u0251': 'a', // Macron. '\u01d5': 'U', '\u01d6': 'u', // Acute accent. '\u01d7': 'U', '\u01d8': 'u', // Caron. '\u01cd': 'A', '\u01ce': 'a', '\u01cf': 'I', '\u01d0': 'i', '\u01d1': 'O', '\u01d2': 'o', '\u01d3': 'U', '\u01d4': 'u', '\u01d9': 'U', '\u01da': 'u', // Grave accent. '\u01db': 'U', '\u01dc': 'u', }; const CHARS_REGEXP = new RegExp( `[${ Object.keys( CHARS ).join( '' ) }]`, 'g' ); /** * Converts accented letters to basic Latin letters, like `remove_accents()` * in WordPress core without a locale. * * @param string The string to process. * * @return The processed string. */ export function removeAccents( string: string ): string { return string .normalize( 'NFC' ) .replace( CHARS_REGEXP, ( char ) => CHARS[ char ] ); }