@jdeighan/coffee-utils 6.0.9 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,38 +2,31 @@
2
2
 
3
3
  import {
4
4
  assert, undef, error, croak, warn, isString, isFunction, isBoolean,
5
- oneline, escapeStr, isNumber, isArray, words,
5
+ OL, escapeStr, isNumber, isArray, words, pass,
6
6
  } from '@jdeighan/coffee-utils'
7
7
  import {blockToArray} from '@jdeighan/coffee-utils/block'
8
8
  import {untabify} from '@jdeighan/coffee-utils/indent'
9
9
  import {slurp} from '@jdeighan/coffee-utils/fs'
10
10
  import {CallStack} from '@jdeighan/coffee-utils/stack'
11
11
  import {
12
- log, LOG, setStringifier, orderedStringify,
12
+ log, logItem, LOG, setStringifier, orderedStringify,
13
13
  } from '@jdeighan/coffee-utils/log'
14
- import {getPrefix, arrow, removeArrow} from '@jdeighan/coffee-utils/arrow'
15
-
16
- debugLevel = 0 # controls amount of indentation - we ensure it's never < 0
17
14
 
18
15
  # --- These are saved/restored on the call stack
19
16
  export debugging = false
20
-
21
- # --- By default, when entering a function, keep the debugging flag
22
- # as it was
23
- shouldDebugFunc = (func) -> debugging
24
-
25
- # --- By default, log everything when debugging flag is on
17
+ shouldLogFunc = (func) -> debugging
26
18
  shouldLogString = (str) -> debugging
27
19
 
28
20
  stack = new CallStack()
29
- DEBUGDEBUG = false
21
+ doDebugDebug = false
30
22
 
31
23
  # ---------------------------------------------------------------------------
32
24
 
33
- export setDEBUGDEBUG = (flag=true) ->
25
+ export debugDebug = (flag=true) ->
34
26
 
35
- DEBUGDEBUG = flag
36
- console.log "DEBUGDEBUG = #{flag}"
27
+ doDebugDebug = flag
28
+ if doDebugDebug
29
+ LOG "doDebugDebug = #{flag}"
37
30
  return
38
31
 
39
32
  # ---------------------------------------------------------------------------
@@ -41,9 +34,10 @@ export setDEBUGDEBUG = (flag=true) ->
41
34
  resetDebugging = () ->
42
35
 
43
36
  debugging = false
44
- debugLevel = 0
37
+ if doDebugDebug
38
+ LOG "resetDebugging() - debugging = false"
45
39
  stack.reset()
46
- shouldDebugFunc = (func) -> debugging
40
+ shouldLogFunc = (func) -> debugging
47
41
  shouldLogString = (str) -> debugging
48
42
  return
49
43
 
@@ -53,23 +47,23 @@ export setDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
53
47
 
54
48
  resetDebugging()
55
49
  if isBoolean(funcDoDebug)
56
- if DEBUGDEBUG
57
- console.log "setDebugging #{funcDoDebug}"
58
50
  debugging = funcDoDebug
51
+ if doDebugDebug
52
+ LOG "setDebugging(): debugging = #{funcDoDebug}"
59
53
  else if isString(funcDoDebug)
60
54
  debugging = false
61
55
  lFuncNames = words(funcDoDebug)
62
56
  assert isArray(lFuncNames), "words('#{funcDoDebug}') returned non-array"
63
- shouldDebugFunc = (funcName) ->
57
+ shouldLogFunc = (funcName) ->
64
58
  funcMatch(funcName, lFuncNames)
65
- if DEBUGDEBUG
66
- console.log "setDebugging FUNCS: #{lFuncNames.join(',')}"
59
+ if doDebugDebug
60
+ LOG "setDebugging FUNCS: #{lFuncNames.join(',')}, debugging = false"
67
61
  else if isFunction(funcDoDebug)
68
- shouldDebugFunc = funcDoDebug
69
- if DEBUGDEBUG
70
- console.log "setDebugging to custom func"
62
+ shouldLogFunc = funcDoDebug
63
+ if doDebugDebug
64
+ LOG "setDebugging to custom func"
71
65
  else
72
- croak "setDebugging(): bad parameter #{oneline(funcDoDebug)}"
66
+ croak "setDebugging(): bad parameter #{OL(funcDoDebug)}"
73
67
 
74
68
  if funcDoLog
75
69
  assert isFunction(funcDoLog), "setDebugging: arg 2 not a function"
@@ -93,52 +87,37 @@ export funcMatch = (curFunc, lFuncNames) ->
93
87
  return false
94
88
 
95
89
  # ---------------------------------------------------------------------------
90
+ # 1. adjust call stack on 'enter' or 'return from'
91
+ # 2. adjust debugging flag
92
+ # 3. return [mainPrefix, auxPrefix, hEnv] - hEnv can be undef
93
+ # 4. disable logging by setting mainPrefix to undef
96
94
 
97
- curEnv = () ->
98
-
99
- return {debugging, shouldDebugFunc, shouldLogString}
100
-
101
- # ---------------------------------------------------------------------------
102
-
103
- setEnv = (hEnv) ->
104
-
105
- {debugging, shouldDebugFunc, shouldLogString} = hEnv
106
- return
107
-
108
- # ---------------------------------------------------------------------------
109
-
110
- export debug = (lArgs...) ->
111
-
112
- # --- We want to allow item to be undef. Therefore, we need to
113
- # distinguish between 1 arg sent vs. 2+ args sent
114
- nArgs = lArgs.length
115
- if DEBUGDEBUG
116
- LOG "debug() called with #{nArgs} args"
117
- [label, item] = lArgs
118
-
119
- # --- We always need to manipulate the stack when we encounter
120
- # either "enter X" or "return from X", so we can't short-circuit
121
- # when debugging is off
122
-
123
- assert isString(label),
124
- "debug(): 1st arg #{oneline(label)} should be a string"
95
+ adjustStack = (str) ->
125
96
 
126
- # --- determine if we're entering or returning from a function
127
- entering = returning = false
128
- curFunc = undef
129
- if (lMatches = label.match(///^
97
+ if (lMatches = str.match(///^
130
98
  \s*
131
99
  enter
132
100
  \s+
133
101
  ([A-Za-z_][A-Za-z0-9_\.]*)
134
102
  ///))
135
- entering = true
136
103
  curFunc = lMatches[1]
137
- stack.call(curFunc, curEnv())
138
- debugging = shouldDebugFunc(curFunc)
139
- if DEBUGDEBUG
140
- LOG "ENTER #{curFunc}, debugging = #{debugging}"
141
- else if (lMatches = label.match(///^
104
+ hEnv = {
105
+ debugging
106
+ shouldLogFunc
107
+ shouldLogString
108
+ }
109
+ debugging = shouldLogFunc(curFunc)
110
+ if doDebugDebug
111
+ trans = "#{hEnv.debugging} => #{debugging}"
112
+ LOG " ENTER #{curFunc}, debugging: #{trans}"
113
+ [mainPre, auxPre, _] = stack.call(curFunc, hEnv, debugging)
114
+ return [
115
+ mainPre
116
+ auxPre
117
+ undef
118
+ if shouldLogFunc(curFunc) then 'enter' else undef
119
+ ]
120
+ else if (lMatches = str.match(///^
142
121
  \s*
143
122
  return
144
123
  .+
@@ -146,43 +125,73 @@ export debug = (lArgs...) ->
146
125
  \s+
147
126
  ([A-Za-z_][A-Za-z0-9_\.]*)
148
127
  ///))
149
- returning = true
150
128
  curFunc = lMatches[1]
151
- hInfo = stack.returnFrom(curFunc)
152
- if DEBUGDEBUG && hInfo
153
- LOG "RETURN FROM #{curFunc}, debugging = #{hInfo.debugging}"
154
-
155
- if shouldLogString(label)
156
- # --- set the prefix, i.e. indentation to use
157
- if returning
158
- if (debugLevel==0)
159
- prefix = arrow
160
- else
161
- prefix = getPrefix(debugLevel, true) # with arrow
162
- else
163
- prefix = getPrefix(debugLevel, false) # no arrow
129
+ [mainPre, auxPre, hEnv] = stack.returnFrom(curFunc)
130
+ if doDebugDebug
131
+ LOG " RETURN FROM #{curFunc}"
132
+ return [
133
+ mainPre
134
+ auxPre
135
+ hEnv
136
+ if shouldLogFunc(curFunc) then 'return' else undef
137
+ ]
138
+ else
139
+ [mainPre, auxPre, _] = stack.logStr()
140
+ return [
141
+ mainPre
142
+ auxPre
143
+ undef
144
+ if shouldLogString(str) then 'string' else undef
145
+ ]
146
+ return
147
+
148
+ # ---------------------------------------------------------------------------
149
+
150
+ export debug = (lArgs...) ->
151
+
152
+ # --- We want to allow item to be undef. Therefore, we need to
153
+ # distinguish between 1 arg sent vs. 2+ args sent
154
+ nArgs = lArgs.length
155
+ assert (nArgs==1) || (nArgs==2), "debug(): #{nArgs} args"
156
+ [label, item] = lArgs
157
+ assert isString(label),
158
+ "debug(): 1st arg #{OL(label)} should be a string"
164
159
 
165
- if (nArgs==1)
166
- log label, {prefix}
160
+ if doDebugDebug
161
+ if nArgs==1
162
+ LOG "debug('#{escapeStr(label)}')"
167
163
  else
168
- itemPrefix = removeArrow(prefix, false)
169
- log item, {
170
- label
171
- prefix
172
- itemPrefix
173
- }
174
- else if DEBUGDEBUG
175
- LOG "shouldLogString('#{label}') returned FALSE"
176
-
177
- # --- Adjust debug level & contents of hInfo
178
- if returning
179
- if debugLevel > 0
180
- debugLevel -= 1
181
- if hInfo
182
- setEnv(hInfo)
183
- else if entering
184
- if debugging
185
- debugLevel += 1
164
+ LOG "debug('#{escapeStr(label)}', #{typeof item})"
165
+
166
+ # --- We always need to manipulate the stack when we encounter
167
+ # either "enter X" or "return from X", so we can't short-circuit
168
+ # when debugging is off
169
+
170
+ [mainPre, auxPre, hEnv, type] = adjustStack(label)
171
+ hOptions = {prefix: mainPre, itemPrefix: auxPre}
172
+ switch type
173
+ when 'enter'
174
+ log label, hOptions
175
+ if item
176
+ # --- don't repeat the label
177
+ logItem undef, item, hOptions
178
+ when 'return'
179
+ log label, hOptions
180
+ if item
181
+ # --- don't repeat the label
182
+ logItem undef, item, hOptions
183
+ when 'string'
184
+ if item
185
+ logItem label, item, hOptions
186
+ else
187
+ log label, hOptions
188
+
189
+ if hEnv
190
+ orgDebugging = debugging
191
+ {debugging, shouldLogFunc, shouldLogString} = hEnv
192
+ if doDebugDebug
193
+ trans = "#{orgDebugging} => #{debugging}"
194
+ LOG " Restore hEnv: debugging: #{trans}"
186
195
  return
187
196
 
188
197
  # ---------------------------------------------------------------------------
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // debug_utils.coffee
3
- var DEBUGDEBUG, curEnv, debugLevel, reMethod, resetDebugging, setEnv, shouldDebugFunc, shouldLogString, stack;
3
+ var adjustStack, doDebugDebug, reMethod, resetDebugging, shouldLogFunc, shouldLogString, stack;
4
4
 
5
5
  import {
6
6
  assert,
@@ -11,11 +11,12 @@ import {
11
11
  isString,
12
12
  isFunction,
13
13
  isBoolean,
14
- oneline,
14
+ OL,
15
15
  escapeStr,
16
16
  isNumber,
17
17
  isArray,
18
- words
18
+ words,
19
+ pass
19
20
  } from '@jdeighan/coffee-utils';
20
21
 
21
22
  import {
@@ -36,50 +37,43 @@ import {
36
37
 
37
38
  import {
38
39
  log,
40
+ logItem,
39
41
  LOG,
40
42
  setStringifier,
41
43
  orderedStringify
42
44
  } from '@jdeighan/coffee-utils/log';
43
45
 
44
- import {
45
- getPrefix,
46
- arrow,
47
- removeArrow
48
- } from '@jdeighan/coffee-utils/arrow';
49
-
50
- debugLevel = 0; // controls amount of indentation - we ensure it's never < 0
51
-
52
-
53
46
  // --- These are saved/restored on the call stack
54
47
  export var debugging = false;
55
48
 
56
- // --- By default, when entering a function, keep the debugging flag
57
- // as it was
58
- shouldDebugFunc = function(func) {
49
+ shouldLogFunc = function(func) {
59
50
  return debugging;
60
51
  };
61
52
 
62
- // --- By default, log everything when debugging flag is on
63
53
  shouldLogString = function(str) {
64
54
  return debugging;
65
55
  };
66
56
 
67
57
  stack = new CallStack();
68
58
 
69
- DEBUGDEBUG = false;
59
+ doDebugDebug = false;
70
60
 
71
61
  // ---------------------------------------------------------------------------
72
- export var setDEBUGDEBUG = function(flag = true) {
73
- DEBUGDEBUG = flag;
74
- console.log(`DEBUGDEBUG = ${flag}`);
62
+ export var debugDebug = function(flag = true) {
63
+ doDebugDebug = flag;
64
+ if (doDebugDebug) {
65
+ LOG(`doDebugDebug = ${flag}`);
66
+ }
75
67
  };
76
68
 
77
69
  // ---------------------------------------------------------------------------
78
70
  resetDebugging = function() {
79
71
  debugging = false;
80
- debugLevel = 0;
72
+ if (doDebugDebug) {
73
+ LOG("resetDebugging() - debugging = false");
74
+ }
81
75
  stack.reset();
82
- shouldDebugFunc = function(func) {
76
+ shouldLogFunc = function(func) {
83
77
  return debugging;
84
78
  };
85
79
  shouldLogString = function(str) {
@@ -92,27 +86,27 @@ export var setDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
92
86
  var lFuncNames;
93
87
  resetDebugging();
94
88
  if (isBoolean(funcDoDebug)) {
95
- if (DEBUGDEBUG) {
96
- console.log(`setDebugging ${funcDoDebug}`);
97
- }
98
89
  debugging = funcDoDebug;
90
+ if (doDebugDebug) {
91
+ LOG(`setDebugging(): debugging = ${funcDoDebug}`);
92
+ }
99
93
  } else if (isString(funcDoDebug)) {
100
94
  debugging = false;
101
95
  lFuncNames = words(funcDoDebug);
102
96
  assert(isArray(lFuncNames), `words('${funcDoDebug}') returned non-array`);
103
- shouldDebugFunc = function(funcName) {
97
+ shouldLogFunc = function(funcName) {
104
98
  return funcMatch(funcName, lFuncNames);
105
99
  };
106
- if (DEBUGDEBUG) {
107
- console.log(`setDebugging FUNCS: ${lFuncNames.join(',')}`);
100
+ if (doDebugDebug) {
101
+ LOG(`setDebugging FUNCS: ${lFuncNames.join(',')}, debugging = false`);
108
102
  }
109
103
  } else if (isFunction(funcDoDebug)) {
110
- shouldDebugFunc = funcDoDebug;
111
- if (DEBUGDEBUG) {
112
- console.log("setDebugging to custom func");
104
+ shouldLogFunc = funcDoDebug;
105
+ if (doDebugDebug) {
106
+ LOG("setDebugging to custom func");
113
107
  }
114
108
  } else {
115
- croak(`setDebugging(): bad parameter ${oneline(funcDoDebug)}`);
109
+ croak(`setDebugging(): bad parameter ${OL(funcDoDebug)}`);
116
110
  }
117
111
  if (funcDoLog) {
118
112
  assert(isFunction(funcDoLog), "setDebugging: arg 2 not a function");
@@ -136,79 +130,87 @@ export var funcMatch = function(curFunc, lFuncNames) {
136
130
  };
137
131
 
138
132
  // ---------------------------------------------------------------------------
139
- curEnv = function() {
140
- return {debugging, shouldDebugFunc, shouldLogString};
141
- };
142
-
143
- // ---------------------------------------------------------------------------
144
- setEnv = function(hEnv) {
145
- ({debugging, shouldDebugFunc, shouldLogString} = hEnv);
133
+ // 1. adjust call stack on 'enter' or 'return from'
134
+ // 2. adjust debugging flag
135
+ // 3. return [mainPrefix, auxPrefix, hEnv] - hEnv can be undef
136
+ // 4. disable logging by setting mainPrefix to undef
137
+ adjustStack = function(str) {
138
+ var _, auxPre, curFunc, hEnv, lMatches, mainPre, trans;
139
+ if ((lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
140
+ curFunc = lMatches[1];
141
+ hEnv = {debugging, shouldLogFunc, shouldLogString};
142
+ debugging = shouldLogFunc(curFunc);
143
+ if (doDebugDebug) {
144
+ trans = `${hEnv.debugging} => ${debugging}`;
145
+ LOG(` ENTER ${curFunc}, debugging: ${trans}`);
146
+ }
147
+ [mainPre, auxPre, _] = stack.call(curFunc, hEnv, debugging);
148
+ return [mainPre, auxPre, undef, shouldLogFunc(curFunc) ? 'enter' : undef];
149
+ } else if ((lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
150
+ curFunc = lMatches[1];
151
+ [mainPre, auxPre, hEnv] = stack.returnFrom(curFunc);
152
+ if (doDebugDebug) {
153
+ LOG(` RETURN FROM ${curFunc}`);
154
+ }
155
+ return [mainPre, auxPre, hEnv, shouldLogFunc(curFunc) ? 'return' : undef];
156
+ } else {
157
+ [mainPre, auxPre, _] = stack.logStr();
158
+ return [mainPre, auxPre, undef, shouldLogString(str) ? 'string' : undef];
159
+ }
146
160
  };
147
161
 
148
162
  // ---------------------------------------------------------------------------
149
163
  export var debug = function(...lArgs) {
150
- var curFunc, entering, hInfo, item, itemPrefix, lMatches, label, nArgs, prefix, returning;
164
+ var auxPre, hEnv, hOptions, item, label, mainPre, nArgs, orgDebugging, trans, type;
151
165
  // --- We want to allow item to be undef. Therefore, we need to
152
166
  // distinguish between 1 arg sent vs. 2+ args sent
153
167
  nArgs = lArgs.length;
154
- if (DEBUGDEBUG) {
155
- LOG(`debug() called with ${nArgs} args`);
156
- }
168
+ assert((nArgs === 1) || (nArgs === 2), `debug(): ${nArgs} args`);
157
169
  [label, item] = lArgs;
170
+ assert(isString(label), `debug(): 1st arg ${OL(label)} should be a string`);
171
+ if (doDebugDebug) {
172
+ if (nArgs === 1) {
173
+ LOG(`debug('${escapeStr(label)}')`);
174
+ } else {
175
+ LOG(`debug('${escapeStr(label)}', ${typeof item})`);
176
+ }
177
+ }
158
178
  // --- We always need to manipulate the stack when we encounter
159
179
  // either "enter X" or "return from X", so we can't short-circuit
160
180
  // when debugging is off
161
- assert(isString(label), `debug(): 1st arg ${oneline(label)} should be a string`);
162
- // --- determine if we're entering or returning from a function
163
- entering = returning = false;
164
- curFunc = undef;
165
- if ((lMatches = label.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
166
- entering = true;
167
- curFunc = lMatches[1];
168
- stack.call(curFunc, curEnv());
169
- debugging = shouldDebugFunc(curFunc);
170
- if (DEBUGDEBUG) {
171
- LOG(`ENTER ${curFunc}, debugging = ${debugging}`);
172
- }
173
- } else if ((lMatches = label.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
174
- returning = true;
175
- curFunc = lMatches[1];
176
- hInfo = stack.returnFrom(curFunc);
177
- if (DEBUGDEBUG && hInfo) {
178
- LOG(`RETURN FROM ${curFunc}, debugging = ${hInfo.debugging}`);
179
- }
180
- }
181
- if (shouldLogString(label)) {
182
- // --- set the prefix, i.e. indentation to use
183
- if (returning) {
184
- if (debugLevel === 0) {
185
- prefix = arrow;
181
+ [mainPre, auxPre, hEnv, type] = adjustStack(label);
182
+ hOptions = {
183
+ prefix: mainPre,
184
+ itemPrefix: auxPre
185
+ };
186
+ switch (type) {
187
+ case 'enter':
188
+ log(label, hOptions);
189
+ if (item) {
190
+ // --- don't repeat the label
191
+ logItem(undef, item, hOptions);
192
+ }
193
+ break;
194
+ case 'return':
195
+ log(label, hOptions);
196
+ if (item) {
197
+ // --- don't repeat the label
198
+ logItem(undef, item, hOptions);
199
+ }
200
+ break;
201
+ case 'string':
202
+ if (item) {
203
+ logItem(label, item, hOptions);
186
204
  } else {
187
- prefix = getPrefix(debugLevel, true); // with arrow
205
+ log(label, hOptions);
188
206
  }
189
- } else {
190
- prefix = getPrefix(debugLevel, false); // no arrow
191
- }
192
- if (nArgs === 1) {
193
- log(label, {prefix});
194
- } else {
195
- itemPrefix = removeArrow(prefix, false);
196
- log(item, {label, prefix, itemPrefix});
197
- }
198
- } else if (DEBUGDEBUG) {
199
- LOG(`shouldLogString('${label}') returned FALSE`);
200
207
  }
201
- // --- Adjust debug level & contents of hInfo
202
- if (returning) {
203
- if (debugLevel > 0) {
204
- debugLevel -= 1;
205
- }
206
- if (hInfo) {
207
- setEnv(hInfo);
208
- }
209
- } else if (entering) {
210
- if (debugging) {
211
- debugLevel += 1;
208
+ if (hEnv) {
209
+ orgDebugging = debugging;
210
+ ({debugging, shouldLogFunc, shouldLogString} = hEnv);
211
+ if (doDebugDebug) {
212
+ trans = `${orgDebugging} => ${debugging}`;
213
+ LOG(` Restore hEnv: debugging: ${trans}`);
212
214
  }
213
215
  }
214
216
  };
@@ -84,7 +84,7 @@ export getFullPath = (filepath) ->
84
84
 
85
85
  # --- If report is true, missing source files are not an error
86
86
  # but both missing source files and successful copies
87
- # are reported via console.log
87
+ # are reported via LOG
88
88
 
89
89
  export backup = (file, from, to, report=false) ->
90
90
  src = mkpath(from, file)
@@ -92,10 +92,10 @@ export backup = (file, from, to, report=false) ->
92
92
 
93
93
  if report
94
94
  if fs.existsSync(src)
95
- console.log "OK #{file}"
95
+ LOG "OK #{file}"
96
96
  fs.copyFileSync(src, dest)
97
97
  else
98
- console.log "MISSING #{src}"
98
+ LOG "MISSING #{src}"
99
99
  else
100
100
  fs.copyFileSync(src, dest)
101
101
 
package/src/fs_utils.js CHANGED
@@ -103,17 +103,17 @@ export var getFullPath = function(filepath) {
103
103
 
104
104
  // --- If report is true, missing source files are not an error
105
105
  // but both missing source files and successful copies
106
- // are reported via console.log
106
+ // are reported via LOG
107
107
  export var backup = function(file, from, to, report = false) {
108
108
  var dest, src;
109
109
  src = mkpath(from, file);
110
110
  dest = mkpath(to, file);
111
111
  if (report) {
112
112
  if (fs.existsSync(src)) {
113
- console.log(`OK ${file}`);
113
+ LOG(`OK ${file}`);
114
114
  return fs.copyFileSync(src, dest);
115
115
  } else {
116
- return console.log(`MISSING ${src}`);
116
+ return LOG(`MISSING ${src}`);
117
117
  }
118
118
  } else {
119
119
  return fs.copyFileSync(src, dest);