Skip to content

Commit

Permalink
adding read file to array function
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsoorena committed Dec 15, 2017
1 parent 7d874b0 commit bd3ed71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions snippets/read-file-to-array.md
Original file line number Diff line number Diff line change
@@ -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']
*/
```
1 change: 1 addition & 0 deletions tag_database
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bd3ed71

Please sign in to comment.