Ce tutoriel vous montre comment remplacer les accents par leurs caractères Unicode en JavaScript.
Ce petit script permet de remplacer tous les caractères accentués dans un texte par leur code Unicode correspondant. Il fonctionne avec Node.js et peut être utilisé pour préparer des fichiers destinés à un bot Discord ou autres scripts nécessitant l'UTF-8.
// Library
const fs = require('fs');
const path = require('path');
// Case to be processed
const directoryPath = './NOM DU DOSSIER/';
// Accent replacement function
function replaceAccentsWithUnicode(str) {
const accents = {
'á': '\\u00e1', 'à': '\\u00e0', 'â': '\\u00e2', 'ä': '\\u00e4', 'ã': '\\u00e3', 'å': '\\u00e5',
'é': '\\u00e9', 'è': '\\u00e8', 'ê': '\\u00ea', 'ë': '\\u00eb', 'ē': '\\u0113', 'ė': '\\u0117', 'ę': '\\u0119',
'í': '\\u00ed', 'ì': '\\u00ec', 'î': '\\u00ee', 'ï': '\\u00ef', 'ī': '\\u012b', 'į': '\\u012f',
'ó': '\\u00f3', 'ò': '\\u00f2', 'ô': '\\u00f4', 'ö': '\\u00f6', 'õ': '\\u00f5', 'ø': '\\u00f8', 'ō': '\\u014d',
'ú': '\\u00fa', 'ù': '\\u00f9', 'û': '\\u00fb', 'ü': '\\u00fc', 'ū': '\\u016b',
'ç': '\\u00e7', 'ć': '\\u0107', 'č': '\\u010d',
'ñ': '\\u00f1', 'ń': '\\u0144',
'ß': '\\u00df',
'ÿ': '\\u00ff',
'ž': '\\u017e', 'ź': '\\u017a', 'ż': '\\u017c',
'Á': '\\u00c1', 'À': '\\u00c0', 'Â': '\\u00c2', 'Ä': '\\u00c4', 'Ã': '\\u00c3', 'Å': '\\u00c5',
'É': '\\u00c9', 'È': '\\u00c8', 'Ê': '\\u00ca', 'Ë': '\\u00cb', 'Ē': '\\u0112', 'Ė': '\\u0116', 'Ę': '\\u0118',
'Í': '\\u00cd', 'Ì': '\\u00cc', 'Î': '\\u00ce', 'Ï': '\\u00cf', 'Ī': '\\u012a', 'Į': '\\u012e',
'Ó': '\\u00d3', 'Ò': '\\u00d2', 'Ô': '\\u00d4', 'Ö': '\\u00d6', 'Õ': '\\u00d5', 'Ø': '\\u00d8', 'Ō': '\\u014c',
'Ú': '\\u00da', 'Ù': '\\u00d9', 'Û': '\\u00db', 'Ü': '\\u00dc', 'Ū': '\\u016a',
'Ç': '\\u00c7', 'Ć': '\\u0106', 'Č': '\\u010c',
'Ñ': '\\u00d1', 'Ń': '\\u0143',
'Ÿ': '\\u0178',
'Ž': '\\u017d', 'Ź': '\\u0179', 'Ż': '\\u017b'
};
return str.split('').map(char => accents[char] || char).join('');
}
// Function to process a file
function processFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading the file ${filePath}:`, err);
return;
}
const newData = replaceAccentsWithUnicode(data);
fs.writeFile(filePath, newData, 'utf8', (err) => {
if (err) {
console.error(`Error writing the file ${filePath}:`, err);
} else {
console.log(`File ${filePath} processed successfully.`);
}
});
});
}
// Function to process all files in a directory
function processDirectory(directoryPath) {
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error(`Error reading the ${directoryPath} directory:`, err);
return;
}
files.forEach(file => {
const filePath = path.join(directoryPath, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(`Error checking ${filePath} file :`, err);
return;
}
if (stats.isFile()) {
processFile(filePath);
}
});
});
});
}
// Utilisation
processDirectory(directoryPath);
On peut donc modifier son répertoire d’analyse à cette ligne (ligne 5 du script complet) :
// Case to be processed
const directoryPath = './NOM DU DOSSIER/';
J’espère que ce petit script vous permettra un gain de temps dans l’édition de vos fichiers.