-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (44 loc) · 1.05 KB
/
index.js
File metadata and controls
49 lines (44 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* read lines by file path
*/
import Debug from "debug";
import fs from "fs";
const debug = Debug("readlineq");
/**
* read file into lines
* @param {[type]} file_path [description]
* @return {[type]} [description]
*/
function _read(file_path) {
let lines = [];
const allFileContents = fs.readFileSync(file_path, 'utf-8');
allFileContents.split(/\r?\n/).forEach(line => {
lines.push(line + "\n");
});
return lines;
}
/**
* Write lines to file
* @param {[type]} from_ [description]
* @param {[type]} to_ [description]
* @return {[type]} [description]
*/
function _write(lines, to_) {
if (typeof lines === "string") {
fs.writeFileSync(to_, lines);
} else if (lines.length > 0) {
fs.writeFileSync(to_, lines.join(""));
}
}
/**
* write or read data
* @param {[type]} file_path [description]
* @param {[type]} obj [description]
* @return {[type]} [description]
*/
export default function (file_path, obj) {
if (obj) {
return _write(obj, file_path);
}
return _read(file_path);
};