Last active
November 30, 2020 13:38
-
-
Save i386net/81383dbabe8bcfa079b94709c9d636f3 to your computer and use it in GitHub Desktop.
Создание адресной книги для аппартов Fanvil #mango
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const phones = './path/to/file/phones1.txt' // исходный список абонентов | |
const phoneBookPath = './path/to/phonebook/pb.xml'; // файл адресной книги | |
//Пишем в синхронном режиме ---------------------------------------------------- | |
fs.writeFileSync(phoneBookPath, | |
'<?xml version="1.0" encoding="UTF-8"?>\r\n<PhoneBook>\r\n', | |
{encoding: 'utf-8'}, | |
(err) => console.log(err)); | |
fs.readFile(phones, 'utf8' , (err, data) => { | |
if(err) { | |
console.log(err); | |
return | |
} | |
// обработчик для Сафари!!! ---------------------------------------------------- | |
const lines = data.split(/\r?\n/); | |
const arr = []; | |
lines.forEach(line => { | |
let l = line.split(''); | |
l.splice(0,6) | |
l = (l.join('')).split(' – ') | |
const name = l[0]; | |
const num = l[1].split('').map(sym => '"'.includes(sym) ? '' : sym).join(''); | |
arr.push({name, num}); | |
}); | |
// запись в файл --------------------------------------------------------------- | |
arr.forEach(({name, num}) => { | |
fs.appendFileSync(phoneBookPath, | |
`<DirectoryEntry>\r\n<Name>${name}</Name>\r\n<Telephone>${num}</Telephone>\r\n<Ring>Default</Ring>\r\n<Group></Group>\r\n</DirectoryEntry>\r\n`, | |
'utf8', | |
(err) => console.log(err)) | |
}); | |
fs.appendFileSync(phoneBookPath, '</PhoneBook>\r\n', {encoding: 'utf-8'}, (err) => console.log(err)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment