Node.js Array to text with newline

Multi tool use
Multi tool use


Node.js Array to text with newline



I wanted to convert an array to a textfile with a newline separating each entry.



I found out about an npm package called array-to-txt-file.
Here is the webpage:
array-to-txt-file



This package claims that will concatenate every element of the array with a newline, so that every element of the array appears on its own line on the text file.



So i gave it a try and while it works great it doesn't concatenate the elements with a newline. Where one element ends, another one begins.



So i took a look at the source code of the package and this is the code that creates this effect.


try {
array.forEach(v => {
if(_.isPlainObject(v)) {
ws.write(`${JSON.stringify(v)}n`)
return
}
ws.write(`${v}n`)
})



Especially the ws.write(${v}n) part.


ws.write(


)



I then imported my output text file into a hex editor. In the hex editor there was dot between each element. Now, this dot was different to a regular dot.



While a regular dot has the hex value of 2E, the dot that appears between the elements has 0A.
Please also note that i am using Windows 7, and when viewed with notepad, nothing appears between the elements - where one ends, another one begins straight up.


2E


0A



So is there a way to modify that line in the code i posted above, so it really creates a newline in that part?





Please add the output file here as code with your question or upload it somewhere on websites like pastebin.com and link it here.
– Saif Ur Rahman
Jul 1 at 23:49






Which text editor are you using to view the contents of the file?
– Saif Ur Rahman
Jul 1 at 23:54





Notepad.exe on Windows 7 doesn't handle new lines that only use n.
– reduckted
Jul 2 at 2:01



n




2 Answers
2



Is there some reason a simple join doesn't work for you?


join


let arr = ["a", "b", "c", "d", "e", "f"];

console.log(arr); // [ 'a', 'b', 'c', 'd', 'e', 'f' ]

console.log(arr.join('n'));
// a
// b
// c
// d
// e
// f

let text = arr.join('n');

fs.writeFileSync('modified.txt', text, "utf8");



If you need to you can replace n with rn in the join as @Saif suggests, but I don't seem to need to, perhaps vscode is doing this automatically for me though. I'm on a Windows machine.


n


rn


r


n



On Linux, n represents new line.


n



Two characters combined rn represent a new line on Windows.


rn



Try replacing n with rn in both the write statements inside index.js file of the module.


n


rn


write


index.js



Those dots represent non-printable characters. While the regular dot . FULL STOP (U+002E) is represented by 2E.


.


2E



More information: https://en.wikipedia.org/wiki/Newline






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

I290gh5ukvReejFfpc6 mXb8NWiqiOi3Jl J,Ydy9seOGpWLR,Qm zNuA3
VS6 GT2410kuGYraanYCx2t6X,Df4n2 gNwl wrg6xwosxWac0o6uEjq5Vmrh8tbiwRjla 68U

Popular posts from this blog

Rothschild family

Cinema of Italy