@jdeighan/coffee-utils 7.0.48 → 7.0.51

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.
@@ -4,15 +4,28 @@ import yaml from 'js-yaml'
4
4
 
5
5
  import {
6
6
  assert, undef, isNumber, isInteger, isString, isHash, isFunction,
7
- escapeStr, sep_eq, sep_dash, pass
7
+ escapeStr, sep_eq, sep_dash, pass, OL,
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
- import {tabify, untabify, indentation} from '@jdeighan/coffee-utils/indent'
10
+ import {
11
+ tabify, untabify, indentation, indented,
12
+ } from '@jdeighan/coffee-utils/indent'
11
13
 
12
14
  # --- This logger only ever gets passed a single string argument
13
15
  putstr = undef
16
+ doDebugLog = false
14
17
 
15
18
  export stringify = undef
19
+ fourSpaces = ' '
20
+
21
+ # ---------------------------------------------------------------------------
22
+
23
+ export debugLog = (flag=true) ->
24
+
25
+ doDebugLog = flag
26
+ if doDebugLog
27
+ LOG "doDebugLog = #{flag}"
28
+ return
16
29
 
17
30
  # ---------------------------------------------------------------------------
18
31
  # This is useful for debugging
@@ -112,19 +125,6 @@ maxOneLine = 32
112
125
 
113
126
  # ---------------------------------------------------------------------------
114
127
 
115
- fixStr = (str) ->
116
-
117
- if !str
118
- return ''
119
-
120
- # --- If putstr is console.log, we'll convert TAB char to 3 spaces
121
- if putstr == console.log
122
- return untabify(str)
123
- else
124
- return str
125
-
126
- # ---------------------------------------------------------------------------
127
-
128
128
  export log = (str, hOptions={}) ->
129
129
  # --- valid options:
130
130
  # prefix
@@ -132,8 +132,11 @@ export log = (str, hOptions={}) ->
132
132
  assert isFunction(putstr), "putstr not properly set"
133
133
  assert isString(str), "log(): not a string"
134
134
  assert isHash(hOptions), "log(): arg 2 not a hash"
135
+ prefix = fixForTerminal(hOptions.prefix)
136
+
137
+ if doDebugLog
138
+ LOG "CALL log(#{OL(str)}), prefix = #{OL(prefix)}"
135
139
 
136
- prefix = fixStr(hOptions.prefix)
137
140
  putstr "#{prefix}#{str}"
138
141
  return true # to allow use in boolean expressions
139
142
 
@@ -141,15 +144,20 @@ export log = (str, hOptions={}) ->
141
144
 
142
145
  export logItem = (label, item, hOptions={}) ->
143
146
  # --- valid options:
144
- # prefix - not used
145
- # itemPrefix - always used
147
+ # prefix
146
148
 
147
149
  assert isFunction(putstr), "putstr not properly set"
148
150
  assert !label || isString(label), "label a non-string"
149
151
  assert isHash(hOptions), "arg 3 not a hash"
150
152
 
151
- label = fixStr(label)
152
- prefix = fixStr(hOptions.itemPrefix || hOptions.prefix)
153
+ label = fixForTerminal(label)
154
+ prefix = fixForTerminal(hOptions.prefix)
155
+ assert prefix.indexOf("\t") == -1, "prefix has TAB"
156
+
157
+ if doDebugLog
158
+ LOG "CALL logItem(#{OL(label)}, #{OL(item)})"
159
+ LOG "prefix = #{OL(prefix)}"
160
+
153
161
  labelStr = if label then "#{label} = " else ""
154
162
 
155
163
  if (item == undef)
@@ -162,21 +170,36 @@ export logItem = (label, item, hOptions={}) ->
162
170
  else
163
171
  if label
164
172
  putstr "#{prefix}#{label}:"
165
- putBlock item, prefix
173
+ putBlock item, prefix + fourSpaces
166
174
  else if isNumber(item)
167
175
  putstr "#{prefix}#{labelStr}#{item}"
168
176
  else
169
- putstr "#{prefix}#{sep_dash}"
170
177
  if label
171
178
  putstr "#{prefix}#{label}:"
172
179
  for str in blockToArray(stringify(item, true)) # escape special chars
173
- putstr "#{prefix}#{indentation(1)}#{fixStr(str)}"
174
- putstr "#{prefix}#{sep_dash}"
180
+ putstr "#{prefix + fourSpaces}#{fixForTerminal(str)}"
175
181
 
176
182
  return true
177
183
 
178
184
  # ---------------------------------------------------------------------------
179
185
 
186
+ export shortEnough = (label, value) ->
187
+
188
+ return (value == undef)
189
+
190
+ # ---------------------------------------------------------------------------
191
+ # --- needed because Windows Terminal handles TAB chars badly
192
+
193
+ fixForTerminal = (str) ->
194
+
195
+ if !str
196
+ return ''
197
+
198
+ # --- convert TAB char to 4 spaces
199
+ return str.replace(/\t/g, fourSpaces)
200
+
201
+ # ---------------------------------------------------------------------------
202
+
180
203
  putBlock = (item, prefix='') ->
181
204
 
182
205
  putstr "#{prefix}#{sep_eq}"
package/src/log_utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // log_utils.coffee
3
- var escReplacer, fixStr, loaded, maxOneLine, putBlock, putstr;
3
+ var doDebugLog, escReplacer, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -15,7 +15,8 @@ import {
15
15
  escapeStr,
16
16
  sep_eq,
17
17
  sep_dash,
18
- pass
18
+ pass,
19
+ OL
19
20
  } from '@jdeighan/coffee-utils';
20
21
 
21
22
  import {
@@ -25,14 +26,27 @@ import {
25
26
  import {
26
27
  tabify,
27
28
  untabify,
28
- indentation
29
+ indentation,
30
+ indented
29
31
  } from '@jdeighan/coffee-utils/indent';
30
32
 
31
33
  // --- This logger only ever gets passed a single string argument
32
34
  putstr = undef;
33
35
 
36
+ doDebugLog = false;
37
+
34
38
  export var stringify = undef;
35
39
 
40
+ fourSpaces = ' ';
41
+
42
+ // ---------------------------------------------------------------------------
43
+ export var debugLog = function(flag = true) {
44
+ doDebugLog = flag;
45
+ if (doDebugLog) {
46
+ LOG(`doDebugLog = ${flag}`);
47
+ }
48
+ };
49
+
36
50
  // ---------------------------------------------------------------------------
37
51
  // This is useful for debugging
38
52
  export var LOG = function(...lArgs) {
@@ -134,19 +148,6 @@ export var orderedStringify = function(obj, escape = false) {
134
148
  // ---------------------------------------------------------------------------
135
149
  maxOneLine = 32;
136
150
 
137
- // ---------------------------------------------------------------------------
138
- fixStr = function(str) {
139
- if (!str) {
140
- return '';
141
- }
142
- // --- If putstr is console.log, we'll convert TAB char to 3 spaces
143
- if (putstr === console.log) {
144
- return untabify(str);
145
- } else {
146
- return str;
147
- }
148
- };
149
-
150
151
  // ---------------------------------------------------------------------------
151
152
  export var log = function(str, hOptions = {}) {
152
153
  var prefix;
@@ -155,7 +156,10 @@ export var log = function(str, hOptions = {}) {
155
156
  assert(isFunction(putstr), "putstr not properly set");
156
157
  assert(isString(str), "log(): not a string");
157
158
  assert(isHash(hOptions), "log(): arg 2 not a hash");
158
- prefix = fixStr(hOptions.prefix);
159
+ prefix = fixForTerminal(hOptions.prefix);
160
+ if (doDebugLog) {
161
+ LOG(`CALL log(${OL(str)}), prefix = ${OL(prefix)}`);
162
+ }
159
163
  putstr(`${prefix}${str}`);
160
164
  return true; // to allow use in boolean expressions
161
165
  };
@@ -165,13 +169,17 @@ export var log = function(str, hOptions = {}) {
165
169
  export var logItem = function(label, item, hOptions = {}) {
166
170
  var i, labelStr, len, prefix, ref, str;
167
171
  // --- valid options:
168
- // prefix - not used
169
- // itemPrefix - always used
172
+ // prefix
170
173
  assert(isFunction(putstr), "putstr not properly set");
171
174
  assert(!label || isString(label), "label a non-string");
172
175
  assert(isHash(hOptions), "arg 3 not a hash");
173
- label = fixStr(label);
174
- prefix = fixStr(hOptions.itemPrefix || hOptions.prefix);
176
+ label = fixForTerminal(label);
177
+ prefix = fixForTerminal(hOptions.prefix);
178
+ assert(prefix.indexOf("\t") === -1, "prefix has TAB");
179
+ if (doDebugLog) {
180
+ LOG(`CALL logItem(${OL(label)}, ${OL(item)})`);
181
+ LOG(`prefix = ${OL(prefix)}`);
182
+ }
175
183
  labelStr = label ? `${label} = ` : "";
176
184
  if (item === undef) {
177
185
  putstr(`${prefix}${labelStr}undef`);
@@ -184,12 +192,11 @@ export var logItem = function(label, item, hOptions = {}) {
184
192
  if (label) {
185
193
  putstr(`${prefix}${label}:`);
186
194
  }
187
- putBlock(item, prefix);
195
+ putBlock(item, prefix + fourSpaces);
188
196
  }
189
197
  } else if (isNumber(item)) {
190
198
  putstr(`${prefix}${labelStr}${item}`);
191
199
  } else {
192
- putstr(`${prefix}${sep_dash}`);
193
200
  if (label) {
194
201
  putstr(`${prefix}${label}:`);
195
202
  }
@@ -197,13 +204,27 @@ export var logItem = function(label, item, hOptions = {}) {
197
204
  // escape special chars
198
205
  for (i = 0, len = ref.length; i < len; i++) {
199
206
  str = ref[i];
200
- putstr(`${prefix}${indentation(1)}${fixStr(str)}`);
207
+ putstr(`${prefix + fourSpaces}${fixForTerminal(str)}`);
201
208
  }
202
- putstr(`${prefix}${sep_dash}`);
203
209
  }
204
210
  return true;
205
211
  };
206
212
 
213
+ // ---------------------------------------------------------------------------
214
+ export var shortEnough = function(label, value) {
215
+ return value === undef;
216
+ };
217
+
218
+ // ---------------------------------------------------------------------------
219
+ // --- needed because Windows Terminal handles TAB chars badly
220
+ fixForTerminal = function(str) {
221
+ if (!str) {
222
+ return '';
223
+ }
224
+ // --- convert TAB char to 4 spaces
225
+ return str.replace(/\t/g, fourSpaces);
226
+ };
227
+
207
228
  // ---------------------------------------------------------------------------
208
229
  putBlock = function(item, prefix = '') {
209
230
  var i, len, line, ref;