Last active
November 5, 2021 23:06
-
-
Save SanSan-/c292c8ddea8bbbc969b441cbd32478d7 to your computer and use it in GitHub Desktop.
Fast mass replace in text by JavaScript when sed processor don't understand Unicode
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
var fs = require('fs'); | |
const notesToTabs = [ | |
{ key: '𝄢[до]', val: 'Z' }, | |
{ key: '𝄢[ре]', val: 'X' }, | |
{ key: '𝄢[ми]', val: 'C' }, | |
{ key: '𝄢[фа]', val: 'V' }, | |
{ key: '𝄢[соль]', val: 'B' }, | |
{ key: '𝄢[ля]', val: 'N' }, | |
{ key: '𝄢[си]', val: 'M' }, | |
{ key: '𝄡[до]', val: 'A' }, | |
{ key: '𝄡[ре]', val: 'S' }, | |
{ key: '𝄡[ми]', val: 'D' }, | |
{ key: '𝄡[фа]', val: 'F' }, | |
{ key: '𝄡[соль]', val: 'G' }, | |
{ key: '𝄡[ля]', val: 'H' }, | |
{ key: '𝄡[си]', val: 'J' }, | |
{ key: '𝄞[до]', val: 'Q' }, | |
{ key: '𝄞[ре]', val: 'W' }, | |
{ key: '𝄞[ми]', val: 'E' }, | |
{ key: '𝄞[фа]', val: 'R' }, | |
{ key: '𝄞[соль]', val: 'T' }, | |
{ key: '𝄞[ля]', val: 'Y' }, | |
{ key: '𝄞[си]', val: 'U' } | |
]; | |
const notesToPs4Old = [ | |
{ key: '[до]', val: '🡄' }, | |
{ key: '[ре]', val: '🡅' }, | |
{ key: '[ми]', val: '🡆' }, | |
{ key: '[фа]', val: '🡇' }, | |
{ key: '[соль]', val: '□' }, | |
{ key: '[ля]', val: '△' }, | |
{ key: '[си]', val: '○' } | |
]; | |
const notesToPs4 = [ | |
{ key: '[до]', val: '←' }, | |
{ key: '[ре]', val: '↑' }, | |
{ key: '[ми]', val: '→' }, | |
{ key: '[фа]', val: '↓' }, | |
{ key: '[соль]', val: '□' }, | |
{ key: '[ля]', val: '△' }, | |
{ key: '[си]', val: '○' } | |
]; | |
function replaceAll (input, from, to) { | |
const fromLen = from.length; | |
let output = ''; | |
let pos = 0; | |
for (; ;) { | |
let matchPos = input.indexOf(from, pos); | |
if (matchPos === -1) { | |
output += input.slice(pos); | |
break; | |
} | |
output += input.slice(pos, matchPos); | |
output += to; | |
pos = matchPos + fromLen; | |
} | |
return output; | |
} | |
function formatLine (line, tabulation) { | |
for (var i = 0; i < tabulation.length; i++) { | |
line = replaceAll(line, tabulation[i].key, tabulation[i].val); | |
} | |
return line; | |
} | |
function reverseFormatLine (line, tabulation) { | |
for (var i = 0; i < tabulation.length; i++) { | |
line = replaceAll(line, tabulation[i].val, tabulation[i].key); | |
} | |
return line; | |
} | |
function processFile (inputFileName, mode, formatter) { | |
var readline = require('readline'), | |
instream = fs.createReadStream(`${inputFileName}.${isReverse ? mode : 'notes'}.txt`), | |
outstream = new (require('stream'))(), | |
rl = readline.createInterface(instream, outstream); | |
formatted = ''; | |
rl.on('line', function (line) { | |
switch (mode) { | |
case 'ps4': { | |
formatted += formatter(line, notesToPs4); | |
break; | |
} | |
case 'ps4-old': { | |
formatted += formatter(line, notesToPs4Old); | |
break; | |
} | |
default: { | |
formatted += formatter(line, notesToTabs); | |
break; | |
} | |
} | |
formatted += '\n'; | |
}); | |
rl.on('close', function (_line) { | |
fs.writeFile( | |
`${inputFileName}.${isReverse ? 'notes' : (mode === 'ps4-old' ? 'ps4' : mode)}.txt`, formatted, 'utf8', | |
function (err) { | |
if (err) { | |
return console.log(err); | |
} | |
} | |
); | |
}); | |
} | |
if (process.argv.length < 3) { | |
console.log('Usage: node ' + process.argv[1] + ' FILENAME'); | |
process.exit(1); | |
} | |
var filename = process.argv[2], mode = process.argv[3] || 'notes', | |
isReverse = process.argv[4] && process.argv[4] === 'reverse'; | |
fs.readFile( | |
`${filename}.${isReverse ? (mode === 'ps4-old' ? 'ps4' : mode) : 'notes'}.txt`, 'utf8', function (err, _data) { | |
if (err) { | |
throw err; | |
} | |
console.log('OK: ' + filename); | |
processFile(filename, mode, isReverse ? reverseFormatLine : formatLine); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment