I have an output stream for a program which contains list of available printers and I want to turn it into array.
This is my stdout:
OneNote
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax
And I am seeking for a solution that will give me:
['OneNote', 'Microsoft XPS Document Writer', 'Microsoft Print to PDF', 'Fax']
I tried stdout.split('\n'), but it became
[
'OneNote \r\r',
'Microsoft XPS Document Writer \r\r',
'Microsoft Print to PDF \r\r',
'Fax \r\r',
'\r\r',
''
]
Then I tried stdout.split('\r\r\n') and it turns into
[
'OneNote ',
'Microsoft XPS Document Writer ',
'Microsoft Print to PDF ',
'Fax ',
'',
''
]
Now I can removes whitespace from ends of the strings stdout.split('\r\r\n').map(printer => printer.trim()) but I still need to filter out last two empty strings, so I was wondering is there any better/elegant way to solve the problem?
stdout.split('\r\r\n')
.map(printer => printer.trim())
.filter(printer => Boolean(printer.length))
trim()the string before splitting it