@jdeighan/coffee-utils 11.0.4 → 11.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/src/arrow.js DELETED
@@ -1,83 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // arrow.coffee
3
- import {
4
- assert
5
- } from '@jdeighan/exceptions';
6
-
7
- import {
8
- undef,
9
- OL,
10
- setCharsAt
11
- } from '@jdeighan/coffee-utils';
12
-
13
- // --- We use spaces here because Windows Terminal handles TAB chars badly
14
- export var vbar = '│'; // unicode 2502
15
-
16
- export var hbar = '─'; // unicode 2500
17
-
18
- export var corner = '└'; // unicode 2514
19
-
20
- export var arrowhead = '>';
21
-
22
- export var space = ' ';
23
-
24
- export var dot = '.';
25
-
26
- export var oneIndent = vbar + space + space + space;
27
-
28
- export var arrow = corner + hbar + arrowhead + space;
29
-
30
- export var clearIndent = space + space + space + space;
31
-
32
- export var dotIndent = dot + space + space + space;
33
-
34
- // ---------------------------------------------------------------------------
35
- export var prefix = function(level, option = 'none') {
36
- switch (option) {
37
- case 'withArrow':
38
- if (level === 0) {
39
- return arrow;
40
- } else {
41
- return oneIndent.repeat(level - 1) + arrow;
42
- }
43
- break;
44
- case 'noLastVbar':
45
- assert(level >= 1, `prefix(), noLastVbar but level=${OL(level)}`);
46
- return oneIndent.repeat(level - 1) + clearIndent;
47
- case 'noLast2Vbars':
48
- assert(level >= 2, `prefix(), noLast2Vbars but level=${OL(level)}`);
49
- return oneIndent.repeat(level - 2) + clearIndent + clearIndent;
50
- case 'dotLastVbar':
51
- assert(level >= 1, `prefix(), dotLastVbar but level=${OL(level)}`);
52
- return oneIndent.repeat(level - 1) + dotIndent;
53
- case 'dotLast2Vbars':
54
- assert(level >= 2, `prefix(), dotLast2Vbars but level=${OL(level)}`);
55
- return oneIndent.repeat(level - 2) + dotIndent + clearIndent;
56
- default:
57
- return oneIndent.repeat(level);
58
- }
59
- };
60
-
61
- // ---------------------------------------------------------------------------
62
- export var addArrow = function(prefix) {
63
- var pos, result;
64
- pos = prefix.lastIndexOf(vbar);
65
- if (pos === -1) {
66
- result = prefix;
67
- } else {
68
- result = setCharsAt(prefix, pos, arrow);
69
- }
70
- return result;
71
- };
72
-
73
- // ---------------------------------------------------------------------------
74
- export var removeLastVbar = function(prefix) {
75
- var pos, result;
76
- pos = prefix.lastIndexOf(vbar);
77
- if (pos === -1) {
78
- result = prefix;
79
- } else {
80
- result = setCharsAt(prefix, pos, ' ');
81
- }
82
- return result;
83
- };
package/src/debug.coffee DELETED
@@ -1,300 +0,0 @@
1
- # debug.coffee
2
-
3
- import {LOG, assert, croak} from '@jdeighan/exceptions'
4
- import {
5
- undef, defined, notdefined,
6
- isString, isFunction, isBoolean, isArray,
7
- OL, words, pass,
8
- } from '@jdeighan/coffee-utils'
9
- import {CallStack} from '@jdeighan/coffee-utils/stack'
10
- import {prefix} from '@jdeighan/coffee-utils/arrow'
11
- import {
12
- log, logItem, logBareItem, shortEnough,
13
- } from '@jdeighan/coffee-utils/log'
14
-
15
- # --- set in resetDebugging() and setDebugging()
16
- export callStack = new CallStack()
17
- export shouldLog = () -> undef
18
-
19
- lFuncList = [] # names of functions being debugged
20
- strFuncList = undef # original string
21
-
22
- # ---------------------------------------------------------------------------
23
-
24
- export interp = (label) ->
25
-
26
- return label.replace(/// \$ (\@)? ([A-Za-z_][A-Za-z0-9_]*) ///g,
27
- (_, atSign, varName) ->
28
- if atSign
29
- return "\#{OL(@#{varName})\}"
30
- else
31
- return "\#{OL(#{varName})\}"
32
- )
33
-
34
- # ---------------------------------------------------------------------------
35
-
36
- export debug = (orgLabel, lObjects...) ->
37
-
38
- assert isString(orgLabel), "1st arg #{OL(orgLabel)} should be a string"
39
-
40
- [type, funcName] = getType(orgLabel, lObjects)
41
- label = shouldLog(orgLabel, type, funcName, callStack)
42
- if defined(label)
43
- label = interp(label)
44
-
45
- switch type
46
-
47
- when 'enter'
48
- if defined(label)
49
- doTheLogging type, label, lObjects
50
- callStack.enter funcName, lObjects, defined(label)
51
-
52
- debug2 "enter debug()", orgLabel, lObjects
53
- debug2 "type = #{OL(type)}, funcName = #{OL(funcName)}"
54
- debug2 "return from debug()"
55
-
56
- when 'return'
57
- debug2 "enter debug()", orgLabel, lObjects
58
- debug2 "type = #{OL(type)}, funcName = #{OL(funcName)}"
59
- debug2 "return from debug()"
60
-
61
- if defined(label)
62
- doTheLogging type, label, lObjects
63
- callStack.returnFrom funcName
64
-
65
- when 'string'
66
- debug2 "enter debug()", orgLabel, lObjects
67
- debug2 "type = #{OL(type)}, funcName = #{OL(funcName)}"
68
-
69
- if defined(label)
70
- doTheLogging type, label, lObjects
71
- debug2 "return from debug()"
72
-
73
- return true # allow use in boolean expressions
74
-
75
- # ---------------------------------------------------------------------------
76
-
77
- export debug2 = (orgLabel, lObjects...) ->
78
-
79
- [type, funcName] = getType(orgLabel, lObjects)
80
- label = shouldLog(orgLabel, type, funcName, callStack)
81
-
82
- switch type
83
- when 'enter'
84
- if defined(label)
85
- doTheLogging 'enter', label, lObjects
86
- callStack.enter funcName, lObjects, defined(label)
87
-
88
- when 'return'
89
- if defined(label)
90
- doTheLogging 'return', label, lObjects
91
- callStack.returnFrom funcName
92
-
93
- when 'string'
94
- if defined(label)
95
- doTheLogging 'string', label, lObjects
96
-
97
- return true # allow use in boolean expressions
98
-
99
- # ---------------------------------------------------------------------------
100
-
101
- export doTheLogging = (type, label, lObjects) ->
102
-
103
- assert isString(label), "non-string label #{OL(label)}"
104
- level = callStack.getLevel()
105
-
106
- switch type
107
-
108
- when 'enter'
109
- log label, prefix(level)
110
- pre = prefix(level+1, 'dotLastVbar')
111
- itemPre = prefix(level+2, 'dotLast2Vbars')
112
- for obj,i in lObjects
113
- logItem "arg[#{i}]", obj, pre, itemPre
114
-
115
- when 'return'
116
- log label, prefix(level, 'withArrow')
117
- pre = prefix(level, 'noLastVbar')
118
- itemPre = prefix(level+1, 'noLast2Vbars')
119
- for obj,i in lObjects
120
- logItem "ret[#{i}]", obj, pre, itemPre
121
-
122
- when 'string'
123
- pre = prefix(level)
124
- itemPre = prefix(level+1, 'noLastVbar')
125
- if (lObjects.length==0)
126
- log label, pre
127
- else if (lObjects.length==1) && shortEnough(label, lObjects[0])
128
- logItem label, lObjects[0], pre
129
- else
130
- if (label.indexOf(':') != label.length - 1)
131
- label += ':'
132
- log label, pre
133
- for obj in lObjects
134
- logBareItem obj, itemPre
135
- return
136
-
137
- # ---------------------------------------------------------------------------
138
-
139
- export stdShouldLog = (label, type, funcName, stack) ->
140
- # --- if type is 'enter', then funcName won't be on the stack yet
141
- # returns the (possibly modified) label to log
142
-
143
- assert isString(label), "label #{OL(label)} not a string"
144
- assert isString(type), "type #{OL(type)} not a string"
145
- assert stack instanceof CallStack, "not a call stack object"
146
- if (type == 'enter') || (type == 'return')
147
- assert isString(funcName), "func name #{OL(funcName)} not a string"
148
- else
149
- assert funcName == undef, "func name #{OL(funcName)} not undef"
150
-
151
- if funcMatch(funcName || stack.curFunc())
152
- return label
153
-
154
- if (type == 'enter') && ! isMyFunc(funcName)
155
- # --- As a special case, if we enter a function where we will not
156
- # be logging, but we were logging in the calling function,
157
- # we'll log out the call itself
158
-
159
- if funcMatch(stack.curFunc())
160
- result = label.replace('enter', 'call')
161
- return result
162
- return undef
163
-
164
- # ---------------------------------------------------------------------------
165
-
166
- export isMyFunc = (funcName) ->
167
-
168
- return funcName in words('debug debug2 doTheLogging
169
- stdShouldLog setDebugging getFuncList funcMatch
170
- getType dumpCallStack')
171
-
172
- # ---------------------------------------------------------------------------
173
-
174
- export trueShouldLog = (label, type, funcName, stack) ->
175
-
176
- if isMyFunc(funcName || stack.curFunc())
177
- return undef
178
- else
179
- return label
180
-
181
- # ---------------------------------------------------------------------------
182
-
183
- export setDebugging = (option) ->
184
-
185
- callStack.reset()
186
- if isBoolean(option)
187
- if option
188
- shouldLog = trueShouldLog
189
- else
190
- shouldLog = () -> return undef
191
- else if isString(option)
192
- lFuncList = getFuncList(option)
193
- shouldLog = stdShouldLog
194
- else if isFunction(option)
195
- shouldLog = option
196
- else
197
- croak "bad parameter #{OL(option)}"
198
- return
199
-
200
- # ---------------------------------------------------------------------------
201
- # --- export only to allow unit tests
202
-
203
- export getFuncList = (str) ->
204
-
205
- strFuncList = str # store original string for debugging
206
- lFuncList = []
207
- for word in words(str)
208
- if lMatches = word.match(///^
209
- ([A-Za-z_][A-Za-z0-9_]*)
210
- (?:
211
- \.
212
- ([A-Za-z_][A-Za-z0-9_]*)
213
- )?
214
- (\+)?
215
- $///)
216
- [_, ident1, ident2, plus] = lMatches
217
- if ident2
218
- lFuncList.push {
219
- name: ident2
220
- object: ident1
221
- plus: (plus == '+')
222
- }
223
- else
224
- lFuncList.push {
225
- name: ident1
226
- plus: (plus == '+')
227
- }
228
- else
229
- croak "Bad word in func list: #{OL(word)}"
230
- return lFuncList
231
-
232
- # ---------------------------------------------------------------------------
233
- # --- export only to allow unit tests
234
-
235
- export funcMatch = (funcName) ->
236
-
237
- assert isArray(lFuncList), "not an array #{OL(lFuncList)}"
238
- for h in lFuncList
239
- {name, object, plus} = h
240
- if (name == funcName)
241
- return true
242
- if plus && callStack.isActive(name)
243
- return true
244
- return false
245
-
246
- # ---------------------------------------------------------------------------
247
- # --- type is one of: 'enter', 'return', 'string'
248
-
249
- export getType = (str, lObjects) ->
250
-
251
- if lMatches = str.match(///^
252
- \s*
253
- ( enter | (?: return .+ from ) )
254
- \s+
255
- ([A-Za-z_][A-Za-z0-9_]*)
256
- (?:
257
- \.
258
- ([A-Za-z_][A-Za-z0-9_]*)
259
- )?
260
- ///)
261
- [_, type, ident1, ident2] = lMatches
262
-
263
- if ident2
264
- funcName = ident2
265
- else
266
- funcName = ident1
267
-
268
- if (type == 'enter')
269
- return ['enter', funcName]
270
- else
271
- return ['return', funcName]
272
- else
273
- return ['string', undef]
274
-
275
- # ---------------------------------------------------------------------------
276
-
277
- reMethod = ///^
278
- ([A-Za-z_][A-Za-z0-9_]*)
279
- \.
280
- ([A-Za-z_][A-Za-z0-9_]*)
281
- $///
282
-
283
- # ---------------------------------------------------------------------------
284
-
285
- export dumpDebugGlobals = () ->
286
-
287
- LOG '='.repeat(40)
288
- LOG callStack.dump()
289
- if shouldLog == stdShouldLog
290
- LOG "using stdShouldLog"
291
- else if shouldLog == trueShouldLog
292
- LOG "using trueShouldLog"
293
- else
294
- LOG "using custom shouldLog"
295
- LOG "lFuncList:"
296
- for funcName in lFuncList
297
- LOG " #{OL(funcName)}"
298
- LOG '='.repeat(40)
299
-
300
- # ---------------------------------------------------------------------------
package/src/debug.js DELETED
@@ -1,329 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // debug.coffee
3
- var lFuncList, reMethod, strFuncList,
4
- indexOf = [].indexOf;
5
-
6
- import {
7
- LOG,
8
- assert,
9
- croak
10
- } from '@jdeighan/exceptions';
11
-
12
- import {
13
- undef,
14
- defined,
15
- notdefined,
16
- isString,
17
- isFunction,
18
- isBoolean,
19
- isArray,
20
- OL,
21
- words,
22
- pass
23
- } from '@jdeighan/coffee-utils';
24
-
25
- import {
26
- CallStack
27
- } from '@jdeighan/coffee-utils/stack';
28
-
29
- import {
30
- prefix
31
- } from '@jdeighan/coffee-utils/arrow';
32
-
33
- import {
34
- log,
35
- logItem,
36
- logBareItem,
37
- shortEnough
38
- } from '@jdeighan/coffee-utils/log';
39
-
40
- // --- set in resetDebugging() and setDebugging()
41
- export var callStack = new CallStack();
42
-
43
- export var shouldLog = function() {
44
- return undef;
45
- };
46
-
47
- lFuncList = []; // names of functions being debugged
48
-
49
- strFuncList = undef; // original string
50
-
51
-
52
- // ---------------------------------------------------------------------------
53
- export var interp = function(label) {
54
- return label.replace(/\$(\@)?([A-Za-z_][A-Za-z0-9_]*)/g, function(_, atSign, varName) {
55
- if (atSign) {
56
- return `\#{OL(@${varName})\}`;
57
- } else {
58
- return `\#{OL(${varName})\}`;
59
- }
60
- });
61
- };
62
-
63
- // ---------------------------------------------------------------------------
64
- export var debug = function(orgLabel, ...lObjects) {
65
- var funcName, label, type;
66
- assert(isString(orgLabel), `1st arg ${OL(orgLabel)} should be a string`);
67
- [type, funcName] = getType(orgLabel, lObjects);
68
- label = shouldLog(orgLabel, type, funcName, callStack);
69
- if (defined(label)) {
70
- label = interp(label);
71
- }
72
- switch (type) {
73
- case 'enter':
74
- if (defined(label)) {
75
- doTheLogging(type, label, lObjects);
76
- }
77
- callStack.enter(funcName, lObjects, defined(label));
78
- debug2("enter debug()", orgLabel, lObjects);
79
- debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
80
- debug2("return from debug()");
81
- break;
82
- case 'return':
83
- debug2("enter debug()", orgLabel, lObjects);
84
- debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
85
- debug2("return from debug()");
86
- if (defined(label)) {
87
- doTheLogging(type, label, lObjects);
88
- }
89
- callStack.returnFrom(funcName);
90
- break;
91
- case 'string':
92
- debug2("enter debug()", orgLabel, lObjects);
93
- debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
94
- if (defined(label)) {
95
- doTheLogging(type, label, lObjects);
96
- }
97
- debug2("return from debug()");
98
- }
99
- return true; // allow use in boolean expressions
100
- };
101
-
102
-
103
- // ---------------------------------------------------------------------------
104
- export var debug2 = function(orgLabel, ...lObjects) {
105
- var funcName, label, type;
106
- [type, funcName] = getType(orgLabel, lObjects);
107
- label = shouldLog(orgLabel, type, funcName, callStack);
108
- switch (type) {
109
- case 'enter':
110
- if (defined(label)) {
111
- doTheLogging('enter', label, lObjects);
112
- }
113
- callStack.enter(funcName, lObjects, defined(label));
114
- break;
115
- case 'return':
116
- if (defined(label)) {
117
- doTheLogging('return', label, lObjects);
118
- }
119
- callStack.returnFrom(funcName);
120
- break;
121
- case 'string':
122
- if (defined(label)) {
123
- doTheLogging('string', label, lObjects);
124
- }
125
- }
126
- return true; // allow use in boolean expressions
127
- };
128
-
129
-
130
- // ---------------------------------------------------------------------------
131
- export var doTheLogging = function(type, label, lObjects) {
132
- var i, itemPre, j, k, l, len, len1, len2, level, obj, pre;
133
- assert(isString(label), `non-string label ${OL(label)}`);
134
- level = callStack.getLevel();
135
- switch (type) {
136
- case 'enter':
137
- log(label, prefix(level));
138
- pre = prefix(level + 1, 'dotLastVbar');
139
- itemPre = prefix(level + 2, 'dotLast2Vbars');
140
- for (i = j = 0, len = lObjects.length; j < len; i = ++j) {
141
- obj = lObjects[i];
142
- logItem(`arg[${i}]`, obj, pre, itemPre);
143
- }
144
- break;
145
- case 'return':
146
- log(label, prefix(level, 'withArrow'));
147
- pre = prefix(level, 'noLastVbar');
148
- itemPre = prefix(level + 1, 'noLast2Vbars');
149
- for (i = k = 0, len1 = lObjects.length; k < len1; i = ++k) {
150
- obj = lObjects[i];
151
- logItem(`ret[${i}]`, obj, pre, itemPre);
152
- }
153
- break;
154
- case 'string':
155
- pre = prefix(level);
156
- itemPre = prefix(level + 1, 'noLastVbar');
157
- if (lObjects.length === 0) {
158
- log(label, pre);
159
- } else if ((lObjects.length === 1) && shortEnough(label, lObjects[0])) {
160
- logItem(label, lObjects[0], pre);
161
- } else {
162
- if (label.indexOf(':') !== label.length - 1) {
163
- label += ':';
164
- }
165
- log(label, pre);
166
- for (l = 0, len2 = lObjects.length; l < len2; l++) {
167
- obj = lObjects[l];
168
- logBareItem(obj, itemPre);
169
- }
170
- }
171
- }
172
- };
173
-
174
- // ---------------------------------------------------------------------------
175
- export var stdShouldLog = function(label, type, funcName, stack) {
176
- var result;
177
- // --- if type is 'enter', then funcName won't be on the stack yet
178
- // returns the (possibly modified) label to log
179
- assert(isString(label), `label ${OL(label)} not a string`);
180
- assert(isString(type), `type ${OL(type)} not a string`);
181
- assert(stack instanceof CallStack, "not a call stack object");
182
- if ((type === 'enter') || (type === 'return')) {
183
- assert(isString(funcName), `func name ${OL(funcName)} not a string`);
184
- } else {
185
- assert(funcName === undef, `func name ${OL(funcName)} not undef`);
186
- }
187
- if (funcMatch(funcName || stack.curFunc())) {
188
- return label;
189
- }
190
- if ((type === 'enter') && !isMyFunc(funcName)) {
191
- // --- As a special case, if we enter a function where we will not
192
- // be logging, but we were logging in the calling function,
193
- // we'll log out the call itself
194
- if (funcMatch(stack.curFunc())) {
195
- result = label.replace('enter', 'call');
196
- return result;
197
- }
198
- }
199
- return undef;
200
- };
201
-
202
- // ---------------------------------------------------------------------------
203
- export var isMyFunc = function(funcName) {
204
- return indexOf.call(words('debug debug2 doTheLogging stdShouldLog setDebugging getFuncList funcMatch getType dumpCallStack'), funcName) >= 0;
205
- };
206
-
207
- // ---------------------------------------------------------------------------
208
- export var trueShouldLog = function(label, type, funcName, stack) {
209
- if (isMyFunc(funcName || stack.curFunc())) {
210
- return undef;
211
- } else {
212
- return label;
213
- }
214
- };
215
-
216
- // ---------------------------------------------------------------------------
217
- export var setDebugging = function(option) {
218
- callStack.reset();
219
- if (isBoolean(option)) {
220
- if (option) {
221
- shouldLog = trueShouldLog;
222
- } else {
223
- shouldLog = function() {
224
- return undef;
225
- };
226
- }
227
- } else if (isString(option)) {
228
- lFuncList = getFuncList(option);
229
- shouldLog = stdShouldLog;
230
- } else if (isFunction(option)) {
231
- shouldLog = option;
232
- } else {
233
- croak(`bad parameter ${OL(option)}`);
234
- }
235
- };
236
-
237
- // ---------------------------------------------------------------------------
238
- // --- export only to allow unit tests
239
- export var getFuncList = function(str) {
240
- var _, ident1, ident2, j, lMatches, len, plus, ref, word;
241
- strFuncList = str; // store original string for debugging
242
- lFuncList = [];
243
- ref = words(str);
244
- for (j = 0, len = ref.length; j < len; j++) {
245
- word = ref[j];
246
- if (lMatches = word.match(/^([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?(\+)?$/)) {
247
- [_, ident1, ident2, plus] = lMatches;
248
- if (ident2) {
249
- lFuncList.push({
250
- name: ident2,
251
- object: ident1,
252
- plus: plus === '+'
253
- });
254
- } else {
255
- lFuncList.push({
256
- name: ident1,
257
- plus: plus === '+'
258
- });
259
- }
260
- } else {
261
- croak(`Bad word in func list: ${OL(word)}`);
262
- }
263
- }
264
- return lFuncList;
265
- };
266
-
267
- // ---------------------------------------------------------------------------
268
- // --- export only to allow unit tests
269
- export var funcMatch = function(funcName) {
270
- var h, j, len, name, object, plus;
271
- assert(isArray(lFuncList), `not an array ${OL(lFuncList)}`);
272
- for (j = 0, len = lFuncList.length; j < len; j++) {
273
- h = lFuncList[j];
274
- ({name, object, plus} = h);
275
- if (name === funcName) {
276
- return true;
277
- }
278
- if (plus && callStack.isActive(name)) {
279
- return true;
280
- }
281
- }
282
- return false;
283
- };
284
-
285
- // ---------------------------------------------------------------------------
286
- // --- type is one of: 'enter', 'return', 'string'
287
- export var getType = function(str, lObjects) {
288
- var _, funcName, ident1, ident2, lMatches, type;
289
- if (lMatches = str.match(/^\s*(enter|(?:return.+from))\s+([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?/)) {
290
- [_, type, ident1, ident2] = lMatches;
291
- if (ident2) {
292
- funcName = ident2;
293
- } else {
294
- funcName = ident1;
295
- }
296
- if (type === 'enter') {
297
- return ['enter', funcName];
298
- } else {
299
- return ['return', funcName];
300
- }
301
- } else {
302
- return ['string', undef];
303
- }
304
- };
305
-
306
- // ---------------------------------------------------------------------------
307
- reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
308
-
309
- // ---------------------------------------------------------------------------
310
- export var dumpDebugGlobals = function() {
311
- var funcName, j, len;
312
- LOG('='.repeat(40));
313
- LOG(callStack.dump());
314
- if (shouldLog === stdShouldLog) {
315
- LOG("using stdShouldLog");
316
- } else if (shouldLog === trueShouldLog) {
317
- LOG("using trueShouldLog");
318
- } else {
319
- LOG("using custom shouldLog");
320
- }
321
- LOG("lFuncList:");
322
- for (j = 0, len = lFuncList.length; j < len; j++) {
323
- funcName = lFuncList[j];
324
- LOG(` ${OL(funcName)}`);
325
- }
326
- return LOG('='.repeat(40));
327
- };
328
-
329
- // ---------------------------------------------------------------------------