-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 860 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const fse = require('fs-extra')
const csvParse = require('csv-parse/lib/sync')
module.exports = async function searchAndReplace (replacementsFilePath, textFilePath) {
const [textFileContent, replacementsContent] = await Promise
.all([
fse.readFile(textFilePath, 'utf8'),
fse.readFile(replacementsFilePath, 'utf-8'),
])
let fixedText = textFileContent
const records = csvParse(
replacementsContent,
{
delimiter: '\t',
comment: '#',
}
)
records.forEach(pair => {
const [match, replacement] = pair
const pattern = new RegExp(`(\\W|^)${match}(\\W|$)`, 'igm')
const normReplacement = (/^\\u.*/g).test(replacement)
? String.fromCharCode(`0x ${replacement.substr(2)}`)
: replacement
fixedText = fixedText.replace(pattern, `$1${normReplacement}$2`)
})
console.info(fixedText)
}