@jdeighan/coffee-utils 6.0.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "6.0.0",
4
+ "version": "6.0.1",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -46,6 +46,7 @@
46
46
  "dependencies": {
47
47
  "cross-env": "^7.0.3",
48
48
  "js-yaml": "^4.1.0",
49
+ "n-readlines": "^1.0.1",
49
50
  "readline-sync": "^1.4.10",
50
51
  "svelte": "^3.46.4"
51
52
  },
@@ -3,12 +3,13 @@
3
3
  import pathlib from 'path'
4
4
  import urllib from 'url'
5
5
  import fs from 'fs'
6
+ import NReadLines from 'n-readlines'
6
7
 
7
8
  import {
8
9
  assert, undef, pass, rtrim, error, nonEmpty,
9
10
  isString, isRegExp, isFunction, croak,
10
11
  } from '@jdeighan/coffee-utils'
11
- import {log} from '@jdeighan/coffee-utils/log'
12
+ import {log, LOG} from '@jdeighan/coffee-utils/log'
12
13
  import {debug} from '@jdeighan/coffee-utils/debug'
13
14
 
14
15
  # ---------------------------------------------------------------------------
@@ -98,14 +99,36 @@ export backup = (file, from, to, report=false) ->
98
99
  else
99
100
  fs.copyFileSync(src, dest)
100
101
 
102
+ # ---------------------------------------------------------------------------
103
+
104
+ export forEachLineInFile = (filepath, func) ->
105
+
106
+ reader = new NReadLines(filepath)
107
+ nLines = 0
108
+
109
+ while (buffer = reader.next())
110
+ nLines += 1
111
+ # --- text is split on \n chars, we also need to remove \r chars
112
+ line = buffer.toString().replace(/\r/g, '')
113
+ if func(line, nLines) == 'EOF'
114
+ reader.close() # allow premature termination
115
+ return
116
+
101
117
  # ---------------------------------------------------------------------------
102
118
  # slurp - read an entire file into a string
103
119
 
104
- export slurp = (filepath) ->
120
+ export slurp = (filepath, maxLines=undef) ->
105
121
 
106
122
  debug "enter slurp('#{filepath}')"
107
- filepath = filepath.replace(/\//g, "\\")
108
- contents = fs.readFileSync(filepath, 'utf8').toString()
123
+ if maxLines?
124
+ lLines = []
125
+ forEachLineInFile filepath, (line, nLines) ->
126
+ lLines.push line
127
+ return if nLines >= maxLines then 'EOF' else undef
128
+ contents = lLines.join("\n")
129
+ else
130
+ filepath = filepath.replace(/\//g, "\\")
131
+ contents = fs.readFileSync(filepath, 'utf8').toString()
109
132
  debug "return from slurp()", contents
110
133
  return contents
111
134
 
package/src/fs_utils.js CHANGED
@@ -6,6 +6,8 @@ import urllib from 'url';
6
6
 
7
7
  import fs from 'fs';
8
8
 
9
+ import NReadLines from 'n-readlines';
10
+
9
11
  import {
10
12
  assert,
11
13
  undef,
@@ -20,7 +22,8 @@ import {
20
22
  } from '@jdeighan/coffee-utils';
21
23
 
22
24
  import {
23
- log
25
+ log,
26
+ LOG
24
27
  } from '@jdeighan/coffee-utils/log';
25
28
 
26
29
  import {
@@ -117,13 +120,41 @@ export var backup = function(file, from, to, report = false) {
117
120
  }
118
121
  };
119
122
 
123
+ // ---------------------------------------------------------------------------
124
+ export var forEachLineInFile = function(filepath, func) {
125
+ var buffer, line, nLines, reader;
126
+ reader = new NReadLines(filepath);
127
+ nLines = 0;
128
+ while ((buffer = reader.next())) {
129
+ nLines += 1;
130
+ // --- text is split on \n chars, we also need to remove \r chars
131
+ line = buffer.toString().replace(/\r/g, '');
132
+ if (func(line, nLines) === 'EOF') {
133
+ reader.close(); // allow premature termination
134
+ }
135
+ }
136
+ };
137
+
120
138
  // ---------------------------------------------------------------------------
121
139
  // slurp - read an entire file into a string
122
- export var slurp = function(filepath) {
123
- var contents;
140
+ export var slurp = function(filepath, maxLines = undef) {
141
+ var contents, lLines;
124
142
  debug(`enter slurp('${filepath}')`);
125
- filepath = filepath.replace(/\//g, "\\");
126
- contents = fs.readFileSync(filepath, 'utf8').toString();
143
+ if (maxLines != null) {
144
+ lLines = [];
145
+ forEachLineInFile(filepath, function(line, nLines) {
146
+ lLines.push(line);
147
+ if (nLines >= maxLines) {
148
+ return 'EOF';
149
+ } else {
150
+ return undef;
151
+ }
152
+ });
153
+ contents = lLines.join("\n");
154
+ } else {
155
+ filepath = filepath.replace(/\//g, "\\");
156
+ contents = fs.readFileSync(filepath, 'utf8').toString();
157
+ }
127
158
  debug("return from slurp()", contents);
128
159
  return contents;
129
160
  };