readlineq 2.0.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
File without changes
package/index.js CHANGED
@@ -1,92 +1,49 @@
1
- /**
2
- * read lines by file path
3
- */
4
- const debug = require("debug")("readlineq");
5
- const fs = require("fs");
6
-
7
- /**
8
- * read file into lines
9
- * @param {[type]} file_path [description]
10
- * @return {[type]} [description]
11
- */
12
- function _read(file_path) {
13
- var lines = [];
14
- return new Promise((resolve, reject) => {
15
- var lineReader = require("readline").createInterface({
16
- input: require("fs").createReadStream(file_path),
17
- });
18
-
19
- lineReader.on("line", function (line) {
20
- debug("Line from file: %s", line);
21
- lines.push(line);
22
- });
23
-
24
- lineReader.on("close", function (err) {
25
- debug("all lines are read, line size %d", lines.length);
26
- resolve(lines);
27
- });
28
-
29
- lineReader.on("error", function (err) {
30
- debug("error happens", err);
31
- reject(err);
32
- });
33
- });
34
- }
35
-
36
- // const JSONStream = require('JSONStream');
37
- /**
38
- * Dump huge array to file
39
- * @param {*} to dump obj to file path
40
- * @param {*} obj JSONObject or JSONArray
41
- */
42
- // function _write(to, obj) {
43
- // return new Promise((resolve, reject) => {
44
- // var records = obj
45
- // var transformStream = JSONStream.stringify();
46
- // var outputStream = fs.createWriteStream(to);
47
- // transformStream.pipe(outputStream);
48
- // records.forEach(transformStream.write);
49
- // transformStream.end();
50
-
51
- // outputStream.on(
52
- // "finish",
53
- // function handleFinish() {
54
- // resolve();
55
- // }
56
- // );
57
-
58
- // outputStream.on(
59
- // "error",
60
- // function handleFinish(err) {
61
- // reject(err);
62
- // }
63
- // );
64
- // });
65
- // }
66
- //
67
- /**
68
- * Write lines to file
69
- * @param {[type]} from_ [description]
70
- * @param {[type]} to_ [description]
71
- * @return {[type]} [description]
72
- */
73
- function _write(lines, to_) {
74
- if (typeof lines === "string") {
75
- fs.writeFileSync(to_, lines);
76
- } else if (lines.length > 0) {
77
- fs.writeFileSync(to_, lines.join(""));
78
- }
79
- }
80
-
81
- /**
82
- * write or read data
83
- * @param {[type]} file_path [description]
84
- * @param {[type]} obj [description]
85
- * @return {[type]} [description]
86
- */
87
- exports = module.exports = function (file_path, obj) {
88
- if (obj) {
89
- return _write(obj, file_path);
90
- }
91
- return _read(file_path);
92
- };
1
+ /**
2
+ * read lines by file path
3
+ */
4
+ import Debug from "debug";
5
+ import fs from "fs";
6
+
7
+ const debug = Debug("readlineq");
8
+
9
+ /**
10
+ * read file into lines
11
+ * @param {[type]} file_path [description]
12
+ * @return {[type]} [description]
13
+ */
14
+ function _read(file_path) {
15
+ let lines = [];
16
+ const allFileContents = fs.readFileSync(file_path, 'utf-8');
17
+ allFileContents.split(/\r?\n/).forEach(line => {
18
+ lines.push(line + "\n");
19
+ });
20
+
21
+ return lines;
22
+ }
23
+
24
+ /**
25
+ * Write lines to file
26
+ * @param {[type]} from_ [description]
27
+ * @param {[type]} to_ [description]
28
+ * @return {[type]} [description]
29
+ */
30
+ function _write(lines, to_) {
31
+ if (typeof lines === "string") {
32
+ fs.writeFileSync(to_, lines);
33
+ } else if (lines.length > 0) {
34
+ fs.writeFileSync(to_, lines.join(""));
35
+ }
36
+ }
37
+
38
+ /**
39
+ * write or read data
40
+ * @param {[type]} file_path [description]
41
+ * @param {[type]} obj [description]
42
+ * @return {[type]} [description]
43
+ */
44
+ export default function (file_path, obj) {
45
+ if (obj) {
46
+ return _write(obj, file_path);
47
+ }
48
+ return _read(file_path);
49
+ };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "readlineq",
3
- "version": "2.0.5",
3
+ "version": "3.0.0",
4
4
  "description": "Read or write file by lines with promise.",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "directories": {
7
8
  "test": "test"
8
9
  },
@@ -25,9 +26,12 @@
25
26
  },
26
27
  "homepage": "https://github.com/chatopera/node-readlineq#readme",
27
28
  "dependencies": {
28
- "debug": "^2.6.8"
29
+ "debug": "^4.4.3"
29
30
  },
30
31
  "devDependencies": {
31
- "ava": "^0.21.0"
32
+ "ava": "^6.4.1"
33
+ },
34
+ "engines": {
35
+ "node": ">=18.0.0"
32
36
  }
33
- }
37
+ }
package/test/index.js CHANGED
@@ -1,15 +1,14 @@
1
- var test = require('ava')
1
+ import test from "ava";
2
+ import readlineq from "../index.js";
2
3
 
3
- test('Readlines # read', async (t)=>{
4
- var readlineq = require('../index.js');
5
- var lines = await readlineq('./tmp/stopwords.txt');
6
- // console.log(lines);
4
+ test('Readlines # read', async (t) => {
5
+ var lines = readlineq('./tmp/stopwords.txt');
6
+ console.log(lines);
7
7
  t.pass()
8
8
  })
9
9
 
10
- test('Readlines # write', async (t)=>{
11
- var readlineq = require('../index.js');
12
- var lines = await readlineq('./tmp/test.txt', ["foo\n", "bar\n"]);
13
- // console.log(lines);
10
+ test('Readlines # write', async (t) => {
11
+ var lines = readlineq('./tmp/test.txt', ["foo\n", "bar\n"]);
12
+ console.log(lines);
14
13
  t.pass()
15
14
  })