@jdeighan/coffee-utils 5.0.4 → 6.0.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "5.0.4",
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
  };
@@ -4,7 +4,7 @@ import yaml from 'js-yaml'
4
4
 
5
5
  import {
6
6
  assert, undef, isNumber, isInteger, isString, isHash, isFunction,
7
- escapeStr, sep_eq,
7
+ escapeStr, sep_eq, sep_dash
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
10
  import {tabify, untabify} from '@jdeighan/coffee-utils/indent'
@@ -17,9 +17,15 @@ export id = 42
17
17
  # ---------------------------------------------------------------------------
18
18
  # This is useful for debugging and easy to remove after debugging
19
19
 
20
- export LOG = (lArgs...) ->
20
+ export LOG = (label, item, ch='=') ->
21
21
 
22
- console.log lArgs...
22
+ if item
23
+ console.log ch.repeat(42)
24
+ console.log "[#{label}]:"
25
+ console.log untabify(orderedStringify(item))
26
+ console.log ch.repeat(42)
27
+ else
28
+ console.log label
23
29
  return
24
30
 
25
31
  # ---------------------------------------------------------------------------
package/src/log_utils.js CHANGED
@@ -13,7 +13,8 @@ import {
13
13
  isHash,
14
14
  isFunction,
15
15
  escapeStr,
16
- sep_eq
16
+ sep_eq,
17
+ sep_dash
17
18
  } from '@jdeighan/coffee-utils';
18
19
 
19
20
  import {
@@ -34,8 +35,15 @@ export var id = 42;
34
35
 
35
36
  // ---------------------------------------------------------------------------
36
37
  // This is useful for debugging and easy to remove after debugging
37
- export var LOG = function(...lArgs) {
38
- console.log(...lArgs);
38
+ export var LOG = function(label, item, ch = '=') {
39
+ if (item) {
40
+ console.log(ch.repeat(42));
41
+ console.log(`[${label}]:`);
42
+ console.log(untabify(orderedStringify(item)));
43
+ console.log(ch.repeat(42));
44
+ } else {
45
+ console.log(label);
46
+ }
39
47
  };
40
48
 
41
49
  // ---------------------------------------------------------------------------