Files
30-seconds-of-code/snippets/readFileLines.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

31 lines
676 B
Markdown

---
title: readFileLines
tags: node,array,string,beginner
---
Returns an array of lines from the specified file.
Use `readFileSync` function in `fs` node package to create a `Buffer` from a file.
convert buffer to string using `toString(encoding)` function.
creating an array from contents of file by `split`ing file content line by line (each `\n`).
```js
const fs = require('fs');
const readFileLines = filename =>
fs
.readFileSync(filename)
.toString('UTF8')
.split('\n');
```
```js
/*
contents of test.txt :
line1
line2
line3
___________________________
*/
let arr = readFileLines('test.txt');
console.log(arr); // ['line1', 'line2', 'line3']
```