taxes_az/convert-int-to-float.js

130 lines
5.9 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
// Счетчики
let totalFilesProcessed = 0;
let totalChanges = 0;
let filesModified = 0;
function processJsonFile(filePath) {
try {
// Читаем файл как текст
let content = fs.readFileSync(filePath, 'utf8');
const originalContent = content;
const fileName = path.basename(path.dirname(filePath));
console.log(`📁 Обработка: ${fileName}/`);
let changesCount = 0;
// Список полей-исключений (точно как они записаны в JSON файлах)
const exclusionPatterns = [
/"fieldname":\s*"il"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"v\\u00f6en"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"mobn\\u00f6mr\\u02591"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"mobn\\u00f6mr\\u02592"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"\\u015f\\u0259h\\u0259rn\\u00f6mr\\u0259si1"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"\\u015f\\u0259h\\u0259rn\\u00f6mr\\u0259si2"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"\\u0259sasf\\u0259aliyy\\u0259tn\\u00f6v\\u00fcn\\u00fcnkodu"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"t\\u0259qdimolunmu\\u015f\\u0259lav\\u0259l\\u0259rinsay\\u0131"[^}]*"fieldtype":\s*"Int"/g,
/"fieldname":\s*"d\\u0259qiql\\u0259\\u015fdirilmi\\u015fb\\u0259yannam\\u0259nint\\u0259qdimedilm\\u0259sibildiri\\u015finn\\u00f6mr\\u0259si"[^}]*"fieldtype":\s*"Int"/g
];
// Сначала помечаем все исключения временным маркером
exclusionPatterns.forEach(pattern => {
content = content.replace(pattern, (match) => {
// Находим имя поля для логирования
const fieldMatch = match.match(/"fieldname":\s*"([^"]+)"/);
if (fieldMatch) {
console.log(` ⊗ Пропущено: ${fieldMatch[1]}`);
}
return match.replace('"Int"', '"Int_KEEP"');
});
});
// Теперь заменяем все оставшиеся "fieldtype": "Int" на "fieldtype": "Float"
const remainingIntPattern = /"fieldtype":\s*"Int"/g;
const matches = content.match(remainingIntPattern);
if (matches) {
changesCount = matches.length;
content = content.replace(remainingIntPattern, '"fieldtype": "Float"');
console.log(` ✓ Изменено полей: ${changesCount}`);
}
// Возвращаем обратно временные маркеры
content = content.replace(/"Int_KEEP"/g, '"Int"');
// Сохраняем если были изменения
if (changesCount > 0 && content !== originalContent) {
fs.writeFileSync(filePath, content, 'utf8');
console.log(` ✅ Обновлено. Изменений: ${changesCount}\n`);
filesModified++;
totalChanges += changesCount;
} else {
console.log(` Изменений не требуется\n`);
}
totalFilesProcessed++;
} catch (error) {
console.error(`❌ Ошибка в файле ${filePath}:`, error.message);
}
}
function processAllDoctypes(rootPath) {
try {
const entries = fs.readdirSync(rootPath, { withFileTypes: true });
entries.forEach(entry => {
if (entry.isDirectory()) {
const doctypePath = path.join(rootPath, entry.name);
const files = fs.readdirSync(doctypePath);
const jsonFiles = files.filter(f => f.endsWith('.json'));
jsonFiles.forEach(jsonFile => {
processJsonFile(path.join(doctypePath, jsonFile));
});
}
});
} catch (error) {
console.error(`❌ Ошибка при чтении папки:`, error.message);
}
}
// ГЛАВНАЯ ФУНКЦИЯ
const doctypePath = 'D:\\taxes_az\\taxes_az\\taxes_az\\doctype';
console.log('========================================');
console.log(' Замена Int → Float');
console.log('========================================');
console.log('Исключения (остаются Int):');
console.log(' • il');
console.log(' • v\\u00f6en (VÖEN)');
console.log(' • mobn\\u00f6mr\\u02591, mobn\\u00f6mr\\u02592');
console.log(' • \\u015f\\u0259h\\u0259rn\\u00f6mr\\u0259si1, \\u015f\\u0259h\\u0259rn\\u00f6mr\\u0259si2');
console.log(' • \\u0259sasf\\u0259aliyy\\u0259tn\\u00f6v\\u00fcn\\u00fcnkodu');
console.log(' • t\\u0259qdimolunmu\\u015f\\u0259lav\\u0259l\\u0259rinsay\\u0131');
console.log(' • d\\u0259qiql\\u0259\\u015fdirilmi\\u015fb\\u0259yannam\\u0259nin...n\\u00f6mr\\u0259si');
console.log('========================================\n');
if (!fs.existsSync(doctypePath)) {
console.error('❌ Папка не найдена:', doctypePath);
process.exit(1);
}
processAllDoctypes(doctypePath);
console.log('========================================');
console.log('📊 РЕЗУЛЬТАТ:');
console.log(` Файлов проверено: ${totalFilesProcessed}`);
console.log(` Файлов изменено: ${filesModified}`);
console.log(` Полей изменено: ${totalChanges}`);
console.log('========================================');
if (filesModified > 0) {
console.log('\n✅ Готово! Все Unicode последовательности сохранены.');
console.log(' Поля-исключения остались с типом Int.');
}