@jdeighan/coffee-utils 5.0.5 → 6.0.2

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": "5.0.5",
4
+ "version": "6.0.2",
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
  },
@@ -28,6 +28,15 @@ export debugging = false
28
28
  shouldDebug = shouldLog = undef
29
29
 
30
30
  stack = new CallStack()
31
+ DEBUGDEBUG = false
32
+
33
+ # ---------------------------------------------------------------------------
34
+
35
+ export setDEBUGDEBUG = (flag=true) ->
36
+
37
+ DEBUGDEBUG = flag
38
+ console.log "DEBUGDEBUG = #{flag}"
39
+ return
31
40
 
32
41
  # ---------------------------------------------------------------------------
33
42
 
@@ -36,8 +45,8 @@ export resetDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
36
45
  debugging = false
37
46
  debugLevel = 0
38
47
  stack.reset()
39
- shouldDebug = (funcName, curDebugging) -> curDebugging
40
- shouldLog = (str) -> debugging || process.env.DEBUG
48
+ shouldDebug = (funcName) -> debugging
49
+ shouldLog = (str) -> debugging
41
50
  if funcDoDebug
42
51
  setDebugging funcDoDebug, funcDoLog
43
52
  return
@@ -47,15 +56,21 @@ export resetDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
47
56
  export setDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
48
57
 
49
58
  if isBoolean(funcDoDebug)
59
+ if DEBUGDEBUG
60
+ console.log "setDebugging #{funcDoDebug}"
50
61
  debugging = funcDoDebug
51
62
  else if isString(funcDoDebug)
52
63
  debugging = false
53
64
  lFuncNames = words(funcDoDebug)
54
65
  assert isArray(lFuncNames), "words('#{funcDoDebug}') returned non-array"
55
- shouldDebug = (funcName, curDebugging) ->
56
- curDebugging || funcMatch(funcName, lFuncNames)
66
+ shouldDebug = (funcName) ->
67
+ funcMatch(funcName, lFuncNames)
68
+ if DEBUGDEBUG
69
+ console.log "setDebugging FUNCS: #{lFuncNames.join(',')}"
57
70
  else if isFunction(funcDoDebug)
58
71
  shouldDebug = funcDoDebug
72
+ if DEBUGDEBUG
73
+ console.log "setDebugging to custom func"
59
74
  else
60
75
  croak "setDebugging(): bad parameter #{oneline(funcDoDebug)}"
61
76
 
@@ -114,17 +129,6 @@ getPrefix = (level) ->
114
129
 
115
130
  # ---------------------------------------------------------------------------
116
131
 
117
- envSaysDebug = (curFunc) ->
118
-
119
- if process.env.DEBUG_FUNC?
120
- return curFunc == process.env.DEBUG_FUNC
121
- else if process.env.DEBUG_FUNCS?
122
- return process.env.DEBUG_FUNCS.split(',').indexOf(curFunc) > -1
123
- else
124
- return false
125
-
126
- # ---------------------------------------------------------------------------
127
-
128
132
  export debug = (lArgs...) ->
129
133
  # --- either 1 or 2 args
130
134
 
@@ -157,7 +161,7 @@ export debug = (lArgs...) ->
157
161
  entering = true
158
162
  curFunc = lMatches[1]
159
163
  stack.call(curFunc, curEnv())
160
- debugging = envSaysDebug(curFunc) || shouldDebug(curFunc, debugging)
164
+ debugging = shouldDebug(curFunc)
161
165
  else if (lMatches = str.match(///^
162
166
  \s*
163
167
  return
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // debug_utils.coffee
3
- var arrow, arrowhead, corner, curEnv, debugLevel, envSaysDebug, getPrefix, hbar, indent, logger, reMethod, setEnv, shouldDebug, shouldLog, stack, undef, vbar;
3
+ var DEBUGDEBUG, arrow, arrowhead, corner, curEnv, debugLevel, getPrefix, hbar, indent, logger, reMethod, setEnv, shouldDebug, shouldLog, stack, undef, vbar;
4
4
 
5
5
  import {
6
6
  assert,
@@ -63,16 +63,24 @@ shouldDebug = shouldLog = undef;
63
63
 
64
64
  stack = new CallStack();
65
65
 
66
+ DEBUGDEBUG = false;
67
+
68
+ // ---------------------------------------------------------------------------
69
+ export var setDEBUGDEBUG = function(flag = true) {
70
+ DEBUGDEBUG = flag;
71
+ console.log(`DEBUGDEBUG = ${flag}`);
72
+ };
73
+
66
74
  // ---------------------------------------------------------------------------
67
75
  export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
68
76
  debugging = false;
69
77
  debugLevel = 0;
70
78
  stack.reset();
71
- shouldDebug = function(funcName, curDebugging) {
72
- return curDebugging;
79
+ shouldDebug = function(funcName) {
80
+ return debugging;
73
81
  };
74
82
  shouldLog = function(str) {
75
- return debugging || process.env.DEBUG;
83
+ return debugging;
76
84
  };
77
85
  if (funcDoDebug) {
78
86
  setDebugging(funcDoDebug, funcDoLog);
@@ -83,16 +91,25 @@ export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
83
91
  export var setDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
84
92
  var lFuncNames;
85
93
  if (isBoolean(funcDoDebug)) {
94
+ if (DEBUGDEBUG) {
95
+ console.log(`setDebugging ${funcDoDebug}`);
96
+ }
86
97
  debugging = funcDoDebug;
87
98
  } else if (isString(funcDoDebug)) {
88
99
  debugging = false;
89
100
  lFuncNames = words(funcDoDebug);
90
101
  assert(isArray(lFuncNames), `words('${funcDoDebug}') returned non-array`);
91
- shouldDebug = function(funcName, curDebugging) {
92
- return curDebugging || funcMatch(funcName, lFuncNames);
102
+ shouldDebug = function(funcName) {
103
+ return funcMatch(funcName, lFuncNames);
93
104
  };
105
+ if (DEBUGDEBUG) {
106
+ console.log(`setDebugging FUNCS: ${lFuncNames.join(',')}`);
107
+ }
94
108
  } else if (isFunction(funcDoDebug)) {
95
109
  shouldDebug = funcDoDebug;
110
+ if (DEBUGDEBUG) {
111
+ console.log("setDebugging to custom func");
112
+ }
96
113
  } else {
97
114
  croak(`setDebugging(): bad parameter ${oneline(funcDoDebug)}`);
98
115
  }
@@ -144,17 +161,6 @@ getPrefix = function(level) {
144
161
  return ' '.repeat(level);
145
162
  };
146
163
 
147
- // ---------------------------------------------------------------------------
148
- envSaysDebug = function(curFunc) {
149
- if (process.env.DEBUG_FUNC != null) {
150
- return curFunc === process.env.DEBUG_FUNC;
151
- } else if (process.env.DEBUG_FUNCS != null) {
152
- return process.env.DEBUG_FUNCS.split(',').indexOf(curFunc) > -1;
153
- } else {
154
- return false;
155
- }
156
- };
157
-
158
164
  // ---------------------------------------------------------------------------
159
165
  export var debug = function(...lArgs) {
160
166
  var curFunc, entering, hInfo, item, itemPrefix, lMatches, nArgs, prefix, returning, str;
@@ -179,7 +185,7 @@ export var debug = function(...lArgs) {
179
185
  entering = true;
180
186
  curFunc = lMatches[1];
181
187
  stack.call(curFunc, curEnv());
182
- debugging = envSaysDebug(curFunc) || shouldDebug(curFunc, debugging);
188
+ debugging = shouldDebug(curFunc);
183
189
  } else if ((lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
184
190
  returning = true;
185
191
  curFunc = lMatches[1];
@@ -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
  };
@@ -17,14 +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 = (item, label, ch='=') ->
20
+ export LOG = (label, item, ch='=') ->
21
21
 
22
- if label
22
+ if item
23
23
  console.log ch.repeat(42)
24
24
  console.log "[#{label}]:"
25
25
  console.log untabify(orderedStringify(item))
26
+ console.log ch.repeat(42)
26
27
  else
27
- console.log item
28
+ console.log label
28
29
  return
29
30
 
30
31
  # ---------------------------------------------------------------------------
package/src/log_utils.js CHANGED
@@ -35,13 +35,14 @@ export var id = 42;
35
35
 
36
36
  // ---------------------------------------------------------------------------
37
37
  // This is useful for debugging and easy to remove after debugging
38
- export var LOG = function(item, label, ch = '=') {
39
- if (label) {
38
+ export var LOG = function(label, item, ch = '=') {
39
+ if (item) {
40
40
  console.log(ch.repeat(42));
41
41
  console.log(`[${label}]:`);
42
42
  console.log(untabify(orderedStringify(item)));
43
+ console.log(ch.repeat(42));
43
44
  } else {
44
- console.log(item);
45
+ console.log(label);
45
46
  }
46
47
  };
47
48