Skip to content

Instantly share code, notes, and snippets.

@jamesliu96
Created June 8, 2021 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesliu96/ef3a32dff3e5ab95f680f33a8999df69 to your computer and use it in GitHub Desktop.
Save jamesliu96/ef3a32dff3e5ab95f680f33a8999df69 to your computer and use it in GitHub Desktop.
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
const data = [];
rl.on('line', (line) => {
data.push(line);
});
rl.on('close', () => {
console.log(handleData(data));
});
const regex = /^\[(\d{2}):(\d{2})(\.\d{2})?\](.*)$/;
/** @param {string[]} data */
function handleData(data) {
const d = [];
data.forEach((line = '') => {
const match = regex.exec(line);
if (match) {
const [_, mm, ss, xx = '00', txt = ''] = match;
d.push({ t: `00:${mm}:${ss},${xx.padStart(3, '0')}`, txt });
}
});
return d
.map(
({ t, txt }, idx) => `${idx}
${t} --> ${d[idx + 1]?.t ?? '01:00:00,000'}
${txt}`
)
.join('\n\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment