@jdeighan/coffee-utils 6.0.2 → 6.0.3

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": "6.0.2",
4
+ "version": "6.0.3",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -14,6 +14,7 @@
14
14
  "./indent": "./src/indent_utils.js",
15
15
  "./stack": "./src/call_stack.js",
16
16
  "./debug": "./src/debug_utils.js",
17
+ "./arrow": "./src/arrow.js",
17
18
  "./svelte": "./src/svelte_utils.js",
18
19
  "./store": "./src/DataStores.js",
19
20
  "./taml": "./src/taml.js",
@@ -0,0 +1,39 @@
1
+ # arrow.coffee
2
+
3
+ vbar = '│' # unicode 2502
4
+ hbar = '─' # unicode 2500
5
+ corner = '└' # unicode 2514
6
+ arrowhead = '>'
7
+
8
+ oneIndent = vbar + ' '
9
+ export arrow = corner + hbar + arrowhead + ' '
10
+
11
+ # ---------------------------------------------------------------------------
12
+
13
+ export getPrefix = (level, withArrow) ->
14
+
15
+ if withArrow
16
+ return oneIndent.repeat(level-1) + arrow
17
+ else
18
+ return oneIndent.repeat(level)
19
+ return
20
+
21
+ # ---------------------------------------------------------------------------
22
+
23
+ export hasArrow = (str) ->
24
+
25
+ return str.indexOf(arrow) > -1
26
+
27
+ # ---------------------------------------------------------------------------
28
+
29
+ export removeArrow = (str, useVbar) ->
30
+
31
+ if hasArrow(str)
32
+ if useVbar
33
+ return str.replace(arrow, oneIndent)
34
+ else
35
+ return str.replace(arrow, ' ')
36
+ else
37
+ return str
38
+
39
+ # ---------------------------------------------------------------------------
package/src/arrow.js ADDED
@@ -0,0 +1,44 @@
1
+ // Generated by CoffeeScript 2.6.1
2
+ // arrow.coffee
3
+ var arrowhead, corner, hbar, oneIndent, vbar;
4
+
5
+ vbar = '│'; // unicode 2502
6
+
7
+ hbar = '─'; // unicode 2500
8
+
9
+ corner = '└'; // unicode 2514
10
+
11
+ arrowhead = '>';
12
+
13
+ oneIndent = vbar + ' ';
14
+
15
+ export var arrow = corner + hbar + arrowhead + ' ';
16
+
17
+ // ---------------------------------------------------------------------------
18
+ export var getPrefix = function(level, withArrow) {
19
+ if (withArrow) {
20
+ return oneIndent.repeat(level - 1) + arrow;
21
+ } else {
22
+ return oneIndent.repeat(level);
23
+ }
24
+ };
25
+
26
+ // ---------------------------------------------------------------------------
27
+ export var hasArrow = function(str) {
28
+ return str.indexOf(arrow) > -1;
29
+ };
30
+
31
+ // ---------------------------------------------------------------------------
32
+ export var removeArrow = function(str, useVbar) {
33
+ if (hasArrow(str)) {
34
+ if (useVbar) {
35
+ return str.replace(arrow, oneIndent);
36
+ } else {
37
+ return str.replace(arrow, ' ');
38
+ }
39
+ } else {
40
+ return str;
41
+ }
42
+ };
43
+
44
+ // ---------------------------------------------------------------------------
@@ -1,7 +1,7 @@
1
1
  # debug_utils.coffee
2
2
 
3
3
  import {
4
- assert, error, croak, warn, isString, isFunction, isBoolean,
4
+ assert, undef, error, croak, warn, isString, isFunction, isBoolean,
5
5
  oneline, escapeStr, isNumber, isArray, words,
6
6
  } from '@jdeighan/coffee-utils'
7
7
  import {blockToArray} from '@jdeighan/coffee-utils/block'
@@ -9,23 +9,21 @@ 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, setStringifier, orderedStringify,
12
+ log, LOG, setStringifier, orderedStringify,
13
13
  } from '@jdeighan/coffee-utils/log'
14
-
15
- undef = undefined
16
- vbar = '│' # unicode 2502
17
- hbar = '─' # unicode 2500
18
- corner = '└' # unicode 2514
19
- arrowhead = '>'
20
-
21
- indent = vbar + ' '
22
- arrow = corner + hbar + arrowhead + ' '
14
+ import {getPrefix, arrow, removeArrow} from '@jdeighan/coffee-utils/arrow'
23
15
 
24
16
  debugLevel = 0 # controls amount of indentation - we ensure it's never < 0
25
17
 
26
18
  # --- These are saved/restored on the call stack
27
19
  export debugging = false
28
- shouldDebug = shouldLog = undef
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
26
+ shouldLogString = (str) -> debugging
29
27
 
30
28
  stack = new CallStack()
31
29
  DEBUGDEBUG = false
@@ -45,8 +43,8 @@ export resetDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
45
43
  debugging = false
46
44
  debugLevel = 0
47
45
  stack.reset()
48
- shouldDebug = (funcName) -> debugging
49
- shouldLog = (str) -> debugging
46
+ shouldDebugFunc = (func) -> debugging
47
+ shouldLogString = (str) -> debugging
50
48
  if funcDoDebug
51
49
  setDebugging funcDoDebug, funcDoLog
52
50
  return
@@ -63,12 +61,12 @@ export setDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
63
61
  debugging = false
64
62
  lFuncNames = words(funcDoDebug)
65
63
  assert isArray(lFuncNames), "words('#{funcDoDebug}') returned non-array"
66
- shouldDebug = (funcName) ->
64
+ shouldDebugFunc = (funcName) ->
67
65
  funcMatch(funcName, lFuncNames)
68
66
  if DEBUGDEBUG
69
67
  console.log "setDebugging FUNCS: #{lFuncNames.join(',')}"
70
68
  else if isFunction(funcDoDebug)
71
- shouldDebug = funcDoDebug
69
+ shouldDebugFunc = funcDoDebug
72
70
  if DEBUGDEBUG
73
71
  console.log "setDebugging to custom func"
74
72
  else
@@ -76,7 +74,7 @@ export setDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
76
74
 
77
75
  if funcDoLog
78
76
  assert isFunction(funcDoLog), "setDebugging: arg 2 not a function"
79
- shouldLog = funcDoLog
77
+ shouldLogString = funcDoLog
80
78
  return
81
79
 
82
80
  # ---------------------------------------------------------------------------
@@ -99,60 +97,37 @@ export funcMatch = (curFunc, lFuncNames) ->
99
97
 
100
98
  curEnv = () ->
101
99
 
102
- return {debugging, shouldDebug, shouldLog}
100
+ return {debugging, shouldDebugFunc, shouldLogString}
103
101
 
104
102
  # ---------------------------------------------------------------------------
105
103
 
106
104
  setEnv = (hEnv) ->
107
105
 
108
- {debugging, shouldDebug, shouldLog} = hEnv
109
- return
110
-
111
- # ---------------------------------------------------------------------------
112
- # --- 2 possible signatures:
113
- # (item) - just log out the string
114
- # (item, hOptions) - log out the object, with a label
115
-
116
- logger = (item, hOptions=undef) ->
117
-
118
- log item, hOptions
106
+ {debugging, shouldDebugFunc, shouldLogString} = hEnv
119
107
  return
120
108
 
121
109
  # ---------------------------------------------------------------------------
122
110
 
123
- getPrefix = (level) ->
124
-
125
- if (level < 0)
126
- warn "You have mismatched debug 'enter'/'return' somewhere!"
127
- return ''
128
- return ' '.repeat(level)
129
-
130
- # ---------------------------------------------------------------------------
131
-
132
111
  export debug = (lArgs...) ->
133
- # --- either 1 or 2 args
112
+
113
+ # --- We want to allow item to be undef. Therefore, we need to
114
+ # distinguish between 1 arg sent vs. 2+ args sent
115
+ nArgs = lArgs.length
116
+ if DEBUGDEBUG
117
+ LOG "debug() called with #{nArgs} args"
118
+ [label, item] = lArgs
134
119
 
135
120
  # --- We always need to manipulate the stack when we encounter
136
121
  # either "enter X" or "return from X", so we can't short-circuit
137
122
  # when debugging is off
138
123
 
139
- nArgs = lArgs.length
140
- assert ((nArgs == 1) || (nArgs == 2)), "debug(); Bad # args #{nArgs}"
141
- str = lArgs[0]
142
-
143
- # --- str must always be a string
144
- # if 2 args, then str is meant to be a label for the item
145
-
146
- assert isString(str),
147
- "debug(): 1st arg #{oneline(str)} should be a string"
148
-
149
- if (nArgs==2)
150
- item = lArgs[1]
124
+ assert isString(label),
125
+ "debug(): 1st arg #{oneline(label)} should be a string"
151
126
 
152
127
  # --- determine if we're entering or returning from a function
153
128
  entering = returning = false
154
129
  curFunc = undef
155
- if (lMatches = str.match(///^
130
+ if (lMatches = label.match(///^
156
131
  \s*
157
132
  enter
158
133
  \s+
@@ -161,8 +136,10 @@ export debug = (lArgs...) ->
161
136
  entering = true
162
137
  curFunc = lMatches[1]
163
138
  stack.call(curFunc, curEnv())
164
- debugging = shouldDebug(curFunc)
165
- else if (lMatches = str.match(///^
139
+ debugging = shouldDebugFunc(curFunc)
140
+ if DEBUGDEBUG
141
+ LOG "ENTER #{curFunc}, debugging = #{debugging}"
142
+ else if (lMatches = label.match(///^
166
143
  \s*
167
144
  return
168
145
  .+
@@ -173,31 +150,32 @@ export debug = (lArgs...) ->
173
150
  returning = true
174
151
  curFunc = lMatches[1]
175
152
  hInfo = stack.returnFrom(curFunc)
153
+ if DEBUGDEBUG
154
+ LOG "RETURN FROM #{curFunc}, debugging = #{hInfo.debugging}"
176
155
 
177
- if shouldLog(str)
178
-
156
+ if shouldLogString(label)
179
157
  # --- set the prefix, i.e. indentation to use
180
158
  if returning
181
159
  if (debugLevel==0)
182
160
  prefix = arrow
183
161
  else
184
- prefix = indent.repeat(debugLevel-1) + arrow
162
+ prefix = getPrefix(debugLevel, true) # with arrow
185
163
  else
186
- prefix = indent.repeat(debugLevel)
164
+ prefix = getPrefix(debugLevel, false) # no arrow
187
165
 
188
166
  if (nArgs==1)
189
- logger str, {
190
- prefix: prefix
191
- }
167
+ log label, {prefix}
192
168
  else
193
- itemPrefix = prefix.replace(arrow, ' ')
194
- logger item, {
195
- label: str
196
- prefix: prefix
169
+ itemPrefix = removeArrow(prefix, false)
170
+ log item, {
171
+ label
172
+ prefix
197
173
  itemPrefix
198
174
  }
175
+ else if DEBUGDEBUG
176
+ LOG "shouldLogString('#{label}') returned FALSE"
199
177
 
200
- # --- Adjust debug level
178
+ # --- Adjust debug level & contents of hInfo
201
179
  if returning
202
180
  if debugLevel > 0
203
181
  debugLevel -= 1
@@ -240,15 +218,15 @@ export checkTrace = (block) ->
240
218
  funcName = lMatches[1]
241
219
  len = lStack.length
242
220
  if (len == 0)
243
- logger "return from #{funcName} with empty stack"
221
+ log "return from #{funcName} with empty stack"
244
222
  else if (lStack[len-1] == funcName)
245
223
  lStack.pop()
246
224
  else if (lStack[len-2] == funcName)
247
- logger "missing return from #{lStack[len-2]}"
225
+ log "missing return from #{lStack[len-2]}"
248
226
  lStack.pop()
249
227
  lStack.pop()
250
228
  else
251
- logger "return from #{funcName} - not found on stack"
229
+ log "return from #{funcName} - not found on stack"
252
230
  return
253
231
 
254
232
  # ---------------------------------------------------------------------------
@@ -1,9 +1,10 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // debug_utils.coffee
3
- var DEBUGDEBUG, arrow, arrowhead, corner, curEnv, debugLevel, getPrefix, hbar, indent, logger, reMethod, setEnv, shouldDebug, shouldLog, stack, undef, vbar;
3
+ var DEBUGDEBUG, curEnv, debugLevel, reMethod, setEnv, shouldDebugFunc, shouldLogString, stack;
4
4
 
5
5
  import {
6
6
  assert,
7
+ undef,
7
8
  error,
8
9
  croak,
9
10
  warn,
@@ -35,23 +36,16 @@ import {
35
36
 
36
37
  import {
37
38
  log,
39
+ LOG,
38
40
  setStringifier,
39
41
  orderedStringify
40
42
  } from '@jdeighan/coffee-utils/log';
41
43
 
42
- undef = void 0;
43
-
44
- vbar = '│'; // unicode 2502
45
-
46
- hbar = ''; // unicode 2500
47
-
48
- corner = '└'; // unicode 2514
49
-
50
- arrowhead = '>';
51
-
52
- indent = vbar + ' ';
53
-
54
- arrow = corner + hbar + arrowhead + ' ';
44
+ import {
45
+ getPrefix,
46
+ arrow,
47
+ removeArrow
48
+ } from '@jdeighan/coffee-utils/arrow';
55
49
 
56
50
  debugLevel = 0; // controls amount of indentation - we ensure it's never < 0
57
51
 
@@ -59,7 +53,16 @@ debugLevel = 0; // controls amount of indentation - we ensure it's never < 0
59
53
  // --- These are saved/restored on the call stack
60
54
  export var debugging = false;
61
55
 
62
- shouldDebug = shouldLog = undef;
56
+ // --- By default, when entering a function, keep the debugging flag
57
+ // as it was
58
+ shouldDebugFunc = function(func) {
59
+ return debugging;
60
+ };
61
+
62
+ // --- By default, log everything when debugging flag is on
63
+ shouldLogString = function(str) {
64
+ return debugging;
65
+ };
63
66
 
64
67
  stack = new CallStack();
65
68
 
@@ -76,10 +79,10 @@ export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
76
79
  debugging = false;
77
80
  debugLevel = 0;
78
81
  stack.reset();
79
- shouldDebug = function(funcName) {
82
+ shouldDebugFunc = function(func) {
80
83
  return debugging;
81
84
  };
82
- shouldLog = function(str) {
85
+ shouldLogString = function(str) {
83
86
  return debugging;
84
87
  };
85
88
  if (funcDoDebug) {
@@ -99,14 +102,14 @@ export var setDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
99
102
  debugging = false;
100
103
  lFuncNames = words(funcDoDebug);
101
104
  assert(isArray(lFuncNames), `words('${funcDoDebug}') returned non-array`);
102
- shouldDebug = function(funcName) {
105
+ shouldDebugFunc = function(funcName) {
103
106
  return funcMatch(funcName, lFuncNames);
104
107
  };
105
108
  if (DEBUGDEBUG) {
106
109
  console.log(`setDebugging FUNCS: ${lFuncNames.join(',')}`);
107
110
  }
108
111
  } else if (isFunction(funcDoDebug)) {
109
- shouldDebug = funcDoDebug;
112
+ shouldDebugFunc = funcDoDebug;
110
113
  if (DEBUGDEBUG) {
111
114
  console.log("setDebugging to custom func");
112
115
  }
@@ -115,7 +118,7 @@ export var setDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
115
118
  }
116
119
  if (funcDoLog) {
117
120
  assert(isFunction(funcDoLog), "setDebugging: arg 2 not a function");
118
- shouldLog = funcDoLog;
121
+ shouldLogString = funcDoLog;
119
122
  }
120
123
  };
121
124
 
@@ -136,86 +139,68 @@ export var funcMatch = function(curFunc, lFuncNames) {
136
139
 
137
140
  // ---------------------------------------------------------------------------
138
141
  curEnv = function() {
139
- return {debugging, shouldDebug, shouldLog};
142
+ return {debugging, shouldDebugFunc, shouldLogString};
140
143
  };
141
144
 
142
145
  // ---------------------------------------------------------------------------
143
146
  setEnv = function(hEnv) {
144
- ({debugging, shouldDebug, shouldLog} = hEnv);
145
- };
146
-
147
- // ---------------------------------------------------------------------------
148
- // --- 2 possible signatures:
149
- // (item) - just log out the string
150
- // (item, hOptions) - log out the object, with a label
151
- logger = function(item, hOptions = undef) {
152
- log(item, hOptions);
153
- };
154
-
155
- // ---------------------------------------------------------------------------
156
- getPrefix = function(level) {
157
- if (level < 0) {
158
- warn("You have mismatched debug 'enter'/'return' somewhere!");
159
- return '';
160
- }
161
- return ' '.repeat(level);
147
+ ({debugging, shouldDebugFunc, shouldLogString} = hEnv);
162
148
  };
163
149
 
164
150
  // ---------------------------------------------------------------------------
165
151
  export var debug = function(...lArgs) {
166
- var curFunc, entering, hInfo, item, itemPrefix, lMatches, nArgs, prefix, returning, str;
167
- // --- either 1 or 2 args
168
-
152
+ var curFunc, entering, hInfo, item, itemPrefix, lMatches, label, nArgs, prefix, returning;
153
+ // --- We want to allow item to be undef. Therefore, we need to
154
+ // distinguish between 1 arg sent vs. 2+ args sent
155
+ nArgs = lArgs.length;
156
+ if (DEBUGDEBUG) {
157
+ LOG(`debug() called with ${nArgs} args`);
158
+ }
159
+ [label, item] = lArgs;
169
160
  // --- We always need to manipulate the stack when we encounter
170
161
  // either "enter X" or "return from X", so we can't short-circuit
171
162
  // when debugging is off
172
- nArgs = lArgs.length;
173
- assert((nArgs === 1) || (nArgs === 2), `debug(); Bad # args ${nArgs}`);
174
- str = lArgs[0];
175
- // --- str must always be a string
176
- // if 2 args, then str is meant to be a label for the item
177
- assert(isString(str), `debug(): 1st arg ${oneline(str)} should be a string`);
178
- if (nArgs === 2) {
179
- item = lArgs[1];
180
- }
163
+ assert(isString(label), `debug(): 1st arg ${oneline(label)} should be a string`);
181
164
  // --- determine if we're entering or returning from a function
182
165
  entering = returning = false;
183
166
  curFunc = undef;
184
- if ((lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
167
+ if ((lMatches = label.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
185
168
  entering = true;
186
169
  curFunc = lMatches[1];
187
170
  stack.call(curFunc, curEnv());
188
- debugging = shouldDebug(curFunc);
189
- } else if ((lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
171
+ debugging = shouldDebugFunc(curFunc);
172
+ if (DEBUGDEBUG) {
173
+ LOG(`ENTER ${curFunc}, debugging = ${debugging}`);
174
+ }
175
+ } else if ((lMatches = label.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
190
176
  returning = true;
191
177
  curFunc = lMatches[1];
192
178
  hInfo = stack.returnFrom(curFunc);
179
+ if (DEBUGDEBUG) {
180
+ LOG(`RETURN FROM ${curFunc}, debugging = ${hInfo.debugging}`);
181
+ }
193
182
  }
194
- if (shouldLog(str)) {
183
+ if (shouldLogString(label)) {
195
184
  // --- set the prefix, i.e. indentation to use
196
185
  if (returning) {
197
186
  if (debugLevel === 0) {
198
187
  prefix = arrow;
199
188
  } else {
200
- prefix = indent.repeat(debugLevel - 1) + arrow;
189
+ prefix = getPrefix(debugLevel, true); // with arrow
201
190
  }
202
191
  } else {
203
- prefix = indent.repeat(debugLevel);
192
+ prefix = getPrefix(debugLevel, false); // no arrow
204
193
  }
205
194
  if (nArgs === 1) {
206
- logger(str, {
207
- prefix: prefix
208
- });
195
+ log(label, {prefix});
209
196
  } else {
210
- itemPrefix = prefix.replace(arrow, ' ');
211
- logger(item, {
212
- label: str,
213
- prefix: prefix,
214
- itemPrefix
215
- });
197
+ itemPrefix = removeArrow(prefix, false);
198
+ log(item, {label, prefix, itemPrefix});
216
199
  }
200
+ } else if (DEBUGDEBUG) {
201
+ LOG(`shouldLogString('${label}') returned FALSE`);
217
202
  }
218
- // --- Adjust debug level
203
+ // --- Adjust debug level & contents of hInfo
219
204
  if (returning) {
220
205
  if (debugLevel > 0) {
221
206
  debugLevel -= 1;
@@ -246,15 +231,15 @@ export var checkTrace = function(block) {
246
231
  funcName = lMatches[1];
247
232
  len = lStack.length;
248
233
  if (len === 0) {
249
- logger(`return from ${funcName} with empty stack`);
234
+ log(`return from ${funcName} with empty stack`);
250
235
  } else if (lStack[len - 1] === funcName) {
251
236
  lStack.pop();
252
237
  } else if (lStack[len - 2] === funcName) {
253
- logger(`missing return from ${lStack[len - 2]}`);
238
+ log(`missing return from ${lStack[len - 2]}`);
254
239
  lStack.pop();
255
240
  lStack.pop();
256
241
  } else {
257
- logger(`return from ${funcName} - not found on stack`);
242
+ log(`return from ${funcName} - not found on stack`);
258
243
  }
259
244
  }
260
245
  }
@@ -133,7 +133,7 @@ export tabify = (str, numSpaces=undef) ->
133
133
  # ---------------------------------------------------------------------------
134
134
  # untabify - convert leading TABs to spaces
135
135
 
136
- export untabify = (str, numSpaces=3) ->
136
+ untabify_old = (str, numSpaces=3) ->
137
137
 
138
138
  oneIndent = ' '.repeat(numSpaces)
139
139
  lLines = []
@@ -145,3 +145,10 @@ export untabify = (str, numSpaces=3) ->
145
145
  else
146
146
  lLines.push oneIndent.repeat(prefix.length) + theRest
147
147
  return arrayToBlock(lLines)
148
+
149
+ # ---------------------------------------------------------------------------
150
+ # untabify - convert ALL TABs to spaces
151
+
152
+ export untabify = (str, numSpaces=3) ->
153
+
154
+ return str.replace(/\t/g, ' '.repeat(numSpaces))
@@ -1,5 +1,7 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
- // indent_utils.coffee
2
+ // indent_utils.coffee
3
+ var untabify_old;
4
+
3
5
  import {
4
6
  assert,
5
7
  undef,
@@ -162,7 +164,7 @@ export var tabify = function(str, numSpaces = undef) {
162
164
 
163
165
  // ---------------------------------------------------------------------------
164
166
  // untabify - convert leading TABs to spaces
165
- export var untabify = function(str, numSpaces = 3) {
167
+ untabify_old = function(str, numSpaces = 3) {
166
168
  var _, i, lLines, lMatches, len, oneIndent, prefix, ref, theRest;
167
169
  oneIndent = ' '.repeat(numSpaces);
168
170
  lLines = [];
@@ -179,3 +181,9 @@ export var untabify = function(str, numSpaces = 3) {
179
181
  }
180
182
  return arrayToBlock(lLines);
181
183
  };
184
+
185
+ // ---------------------------------------------------------------------------
186
+ // untabify - convert ALL TABs to spaces
187
+ export var untabify = function(str, numSpaces = 3) {
188
+ return str.replace(/\t/g, ' '.repeat(numSpaces));
189
+ };
@@ -7,23 +7,29 @@ import {
7
7
  escapeStr, sep_eq, sep_dash
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
- import {tabify, untabify} from '@jdeighan/coffee-utils/indent'
10
+ import {tabify, untabify, indentation} from '@jdeighan/coffee-utils/indent'
11
+ import {arrow, hasArrow, removeArrow} from '@jdeighan/coffee-utils/arrow'
11
12
 
12
13
  # --- This logger only ever gets passed a single string argument
13
- logger = undef
14
+ putstr = undef
15
+
14
16
  export stringify = undef
15
- export id = 42
17
+ objSep = '-'.repeat(42)
16
18
 
17
19
  # ---------------------------------------------------------------------------
18
20
  # This is useful for debugging and easy to remove after debugging
19
21
 
20
- export LOG = (label, item, ch='=') ->
22
+ export LOG = (lArgs...) ->
21
23
 
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)
24
+ [label, item] = lArgs
25
+ if lArgs.length > 1
26
+ console.log objSep
27
+ if item?
28
+ console.log "#{label}:"
29
+ console.log untabify(orderedStringify(item))
30
+ else
31
+ console.log "[#{label}]: UNDEFINED"
32
+ console.log objSep
27
33
  else
28
34
  console.log label
29
35
  return
@@ -47,9 +53,9 @@ export resetStringifier = () ->
47
53
 
48
54
  export setLogger = (func) ->
49
55
 
50
- orgLogger = logger
51
56
  assert isFunction(func), "setLogger() arg is not a function"
52
- logger = func
57
+ orgLogger = putstr
58
+ putstr = func
53
59
  return orgLogger
54
60
 
55
61
  # ---------------------------------------------------------------------------
@@ -99,55 +105,61 @@ maxOneLine = 32
99
105
 
100
106
  # ---------------------------------------------------------------------------
101
107
 
102
- export log = (item, hOptions=undef) ->
108
+ export log = (item, hOptions={}) ->
103
109
  # --- valid options:
104
- # label
105
- # prefix
106
- # escape
107
-
108
- assert isFunction(logger), "logger not properly set"
109
- prefix = itemPrefix = label = ''
110
- if hOptions?
111
- if isString(hOptions)
112
- label = hOptions
113
- else
114
- assert isHash(hOptions), "log(): 2nd arg must be a string or hash"
115
- if hOptions.prefix?
116
- prefix = hOptions.prefix
117
- if hOptions.itemPrefix?
118
- itemPrefix = hOptions.itemPrefix
119
- else
120
- itemPrefix = prefix
121
- if hOptions.label?
122
- label = hOptions.label
110
+ # label
111
+ # prefix
112
+ # itemPrefix
113
+ # escape
114
+
115
+ assert isFunction(putstr), "putstr not properly set"
116
+ if isString(hOptions)
117
+ label = hOptions
118
+ prefix = itemPrefix = ''
119
+ else
120
+ assert isHash(hOptions), "log(): 2nd arg must be a string or hash"
121
+ label = hOptions.label || ''
122
+ prefix = hOptions.prefix || ''
123
+ itemPrefix = hOptions.itemPrefix || prefix || ''
124
+
125
+ # --- If putstr is console.log, we'll convert TAB char to 3 spaces
126
+ if putstr == console.log
127
+ label = untabify(label)
128
+ prefix = untabify(prefix)
129
+ itemPrefix = untabify(itemPrefix)
123
130
 
124
131
  if isString(item) && (label == '')
125
- if hOptions? && hOptions.escape
126
- logger "#{prefix}#{escapeStr(item)}"
132
+ if hOptions.escape
133
+ putstr "#{prefix}#{escapeStr(item)}"
127
134
  else
128
- logger "#{prefix}#{item}"
135
+ putstr "#{prefix}#{item}"
129
136
  return
130
137
 
131
138
  if (label == '')
132
139
  label = 'ITEM'
133
140
 
134
141
  if (item == undef)
135
- logger "#{prefix}#{label} = undef"
142
+ putstr "#{prefix}#{label} = undef"
136
143
  else if isString(item)
137
144
  if (item.length <= maxOneLine)
138
- logger "#{prefix}#{label} = '#{escapeStr(item)}'"
145
+ putstr "#{prefix}#{label} = '#{escapeStr(item)}'"
139
146
  else
140
- logger "#{prefix}#{label}:"
141
- logger "#{itemPrefix}#{sep_eq}"
147
+ putstr "#{prefix}#{label}:"
148
+ putstr "#{itemPrefix}#{sep_eq}"
142
149
  for line in blockToArray(item)
143
- logger "#{itemPrefix}#{escapeStr(line)}"
144
- logger "#{itemPrefix}#{sep_eq}"
150
+ putstr "#{itemPrefix}#{escapeStr(line)}"
151
+ putstr "#{itemPrefix}#{sep_eq}"
145
152
  else if isNumber(item)
146
- logger "#{prefix}#{label} = #{item}"
153
+ putstr "#{prefix}#{label} = #{item}"
147
154
  else
148
- logger "#{prefix}#{label}:"
155
+ putstr "#{removeArrow(prefix, true)}#{objSep}"
156
+ putstr "#{prefix}#{label}:"
149
157
  for str in blockToArray(stringify(item, true))
150
- logger "#{itemPrefix}\t#{str}"
158
+ if putstr == console.log
159
+ putstr "#{itemPrefix} #{untabify(str)}"
160
+ else
161
+ putstr "#{itemPrefix}#{indentation(1)}#{str}"
162
+ putstr "#{removeArrow(prefix, false)}#{objSep}"
151
163
  return
152
164
 
153
165
  # ---------------------------------------------------------------------------
package/src/log_utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // log_utils.coffee
3
- var escReplacer, loaded, logger, maxOneLine;
3
+ var escReplacer, loaded, maxOneLine, objSep, putstr;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -23,24 +23,37 @@ import {
23
23
 
24
24
  import {
25
25
  tabify,
26
- untabify
26
+ untabify,
27
+ indentation
27
28
  } from '@jdeighan/coffee-utils/indent';
28
29
 
30
+ import {
31
+ arrow,
32
+ hasArrow,
33
+ removeArrow
34
+ } from '@jdeighan/coffee-utils/arrow';
35
+
29
36
  // --- This logger only ever gets passed a single string argument
30
- logger = undef;
37
+ putstr = undef;
31
38
 
32
39
  export var stringify = undef;
33
40
 
34
- export var id = 42;
41
+ objSep = '-'.repeat(42);
35
42
 
36
43
  // ---------------------------------------------------------------------------
37
44
  // This is useful for debugging and easy to remove after debugging
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));
45
+ export var LOG = function(...lArgs) {
46
+ var item, label;
47
+ [label, item] = lArgs;
48
+ if (lArgs.length > 1) {
49
+ console.log(objSep);
50
+ if (item != null) {
51
+ console.log(`${label}:`);
52
+ console.log(untabify(orderedStringify(item)));
53
+ } else {
54
+ console.log(`[${label}]: UNDEFINED`);
55
+ }
56
+ console.log(objSep);
44
57
  } else {
45
58
  console.log(label);
46
59
  }
@@ -63,9 +76,9 @@ export var resetStringifier = function() {
63
76
  // ---------------------------------------------------------------------------
64
77
  export var setLogger = function(func) {
65
78
  var orgLogger;
66
- orgLogger = logger;
67
79
  assert(isFunction(func), "setLogger() arg is not a function");
68
- logger = func;
80
+ orgLogger = putstr;
81
+ putstr = func;
69
82
  return orgLogger;
70
83
  };
71
84
 
@@ -116,37 +129,34 @@ export var orderedStringify = function(obj, escape = false) {
116
129
  maxOneLine = 32;
117
130
 
118
131
  // ---------------------------------------------------------------------------
119
- export var log = function(item, hOptions = undef) {
132
+ export var log = function(item, hOptions = {}) {
120
133
  var i, itemPrefix, j, label, len, len1, line, prefix, ref, ref1, str;
121
134
  // --- valid options:
122
- // label
123
- // prefix
124
- // escape
125
- assert(isFunction(logger), "logger not properly set");
126
- prefix = itemPrefix = label = '';
127
- if (hOptions != null) {
128
- if (isString(hOptions)) {
129
- label = hOptions;
130
- } else {
131
- assert(isHash(hOptions), "log(): 2nd arg must be a string or hash");
132
- if (hOptions.prefix != null) {
133
- prefix = hOptions.prefix;
134
- }
135
- if (hOptions.itemPrefix != null) {
136
- itemPrefix = hOptions.itemPrefix;
137
- } else {
138
- itemPrefix = prefix;
139
- }
140
- if (hOptions.label != null) {
141
- label = hOptions.label;
142
- }
143
- }
135
+ // label
136
+ // prefix
137
+ // itemPrefix
138
+ // escape
139
+ assert(isFunction(putstr), "putstr not properly set");
140
+ if (isString(hOptions)) {
141
+ label = hOptions;
142
+ prefix = itemPrefix = '';
143
+ } else {
144
+ assert(isHash(hOptions), "log(): 2nd arg must be a string or hash");
145
+ label = hOptions.label || '';
146
+ prefix = hOptions.prefix || '';
147
+ itemPrefix = hOptions.itemPrefix || prefix || '';
148
+ }
149
+ // --- If putstr is console.log, we'll convert TAB char to 3 spaces
150
+ if (putstr === console.log) {
151
+ label = untabify(label);
152
+ prefix = untabify(prefix);
153
+ itemPrefix = untabify(itemPrefix);
144
154
  }
145
155
  if (isString(item) && (label === '')) {
146
- if ((hOptions != null) && hOptions.escape) {
147
- logger(`${prefix}${escapeStr(item)}`);
156
+ if (hOptions.escape) {
157
+ putstr(`${prefix}${escapeStr(item)}`);
148
158
  } else {
149
- logger(`${prefix}${item}`);
159
+ putstr(`${prefix}${item}`);
150
160
  }
151
161
  return;
152
162
  }
@@ -154,29 +164,35 @@ export var log = function(item, hOptions = undef) {
154
164
  label = 'ITEM';
155
165
  }
156
166
  if (item === undef) {
157
- logger(`${prefix}${label} = undef`);
167
+ putstr(`${prefix}${label} = undef`);
158
168
  } else if (isString(item)) {
159
169
  if (item.length <= maxOneLine) {
160
- logger(`${prefix}${label} = '${escapeStr(item)}'`);
170
+ putstr(`${prefix}${label} = '${escapeStr(item)}'`);
161
171
  } else {
162
- logger(`${prefix}${label}:`);
163
- logger(`${itemPrefix}${sep_eq}`);
172
+ putstr(`${prefix}${label}:`);
173
+ putstr(`${itemPrefix}${sep_eq}`);
164
174
  ref = blockToArray(item);
165
175
  for (i = 0, len = ref.length; i < len; i++) {
166
176
  line = ref[i];
167
- logger(`${itemPrefix}${escapeStr(line)}`);
177
+ putstr(`${itemPrefix}${escapeStr(line)}`);
168
178
  }
169
- logger(`${itemPrefix}${sep_eq}`);
179
+ putstr(`${itemPrefix}${sep_eq}`);
170
180
  }
171
181
  } else if (isNumber(item)) {
172
- logger(`${prefix}${label} = ${item}`);
182
+ putstr(`${prefix}${label} = ${item}`);
173
183
  } else {
174
- logger(`${prefix}${label}:`);
184
+ putstr(`${removeArrow(prefix, true)}${objSep}`);
185
+ putstr(`${prefix}${label}:`);
175
186
  ref1 = blockToArray(stringify(item, true));
176
187
  for (j = 0, len1 = ref1.length; j < len1; j++) {
177
188
  str = ref1[j];
178
- logger(`${itemPrefix}\t${str}`);
189
+ if (putstr === console.log) {
190
+ putstr(`${itemPrefix} ${untabify(str)}`);
191
+ } else {
192
+ putstr(`${itemPrefix}${indentation(1)}${str}`);
193
+ }
179
194
  }
195
+ putstr(`${removeArrow(prefix, false)}${objSep}`);
180
196
  }
181
197
  };
182
198