adding read file to array function

This commit is contained in:
iamsoorena
2017-12-15 20:41:36 +03:30
parent 0c89d5ae69
commit 1fa3f7d3ac
2 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,19 @@
### Read a file to an Array
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 readFileToArray = filename => fs.readFileSync(filename).toString('UTF8').split('\n');
/*
contents of test.txt :
line1
line2
line3
___________________________
let arr = readFileToArray('test.txt')
console.log(arr) // -> ['line1', 'line2', 'line3']
*/
```

View File

@ -67,6 +67,7 @@ powerset:math
promisify:function
random-integer-in-range:utility
random-number-in-range:utility
read-file-to-array:node
redirect-to-URL:browser
reverse-a-string:string
RGB-to-hexadecimal:utility