In this Article we will go through how to prepend a line number to each line of a text document only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function
.
Let's define this short function:
const prependNumbers = str => str.split(/\r?\n/).map((line, i) => `${(i + 1).toString().padStart(2, ' ')} ${line}`).join('\n');
prependNumbers(`one
two
three
four`);
/* Output */
/*
1 one
2 two
3 three
4 four
*/