@jdeighan/coffee-utils 7.0.63 → 7.0.66
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +2 -2
- package/src/debug_utils.coffee +30 -13
- package/src/debug_utils.js +27 -6
- package/src/fs_utils.coffee +15 -12
- package/src/fs_utils.js +16 -12
- package/src/indent_utils.coffee +5 -4
- package/src/indent_utils.js +6 -6
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "7.0.
|
4
|
+
"version": "7.0.66",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -52,6 +52,6 @@
|
|
52
52
|
"svelte": "^3.48.0"
|
53
53
|
},
|
54
54
|
"devDependencies": {
|
55
|
-
"@jdeighan/unit-tester": "^2.0.
|
55
|
+
"@jdeighan/unit-tester": "^2.0.8"
|
56
56
|
}
|
57
57
|
}
|
package/src/debug_utils.coffee
CHANGED
@@ -25,12 +25,26 @@ strFuncList = undef # original string
|
|
25
25
|
|
26
26
|
# ---------------------------------------------------------------------------
|
27
27
|
|
28
|
+
export interp = (label) ->
|
29
|
+
|
30
|
+
return label.replace(/// \$ (\@)? ([A-Za-z_][A-Za-z0-9_]*) ///g,
|
31
|
+
(_, atSign, varName) ->
|
32
|
+
if atSign
|
33
|
+
return "\#{OL(@#{varName})\}"
|
34
|
+
else
|
35
|
+
return "\#{OL(#{varName})\}"
|
36
|
+
)
|
37
|
+
|
38
|
+
# ---------------------------------------------------------------------------
|
39
|
+
|
28
40
|
export debug = (orgLabel, lObjects...) ->
|
29
41
|
|
30
42
|
assert isString(orgLabel), "1st arg #{OL(orgLabel)} should be a string"
|
31
43
|
|
32
44
|
[type, funcName] = getType(orgLabel, lObjects)
|
33
45
|
label = shouldLog(orgLabel, type, funcName, callStack)
|
46
|
+
if defined(label)
|
47
|
+
label = interp(label)
|
34
48
|
|
35
49
|
switch type
|
36
50
|
|
@@ -243,22 +257,25 @@ export getType = (str, lObjects) ->
|
|
243
257
|
|
244
258
|
if lMatches = str.match(///^
|
245
259
|
\s*
|
246
|
-
enter
|
260
|
+
( enter | (?: return .+ from ) )
|
247
261
|
\s+
|
248
|
-
([A-Za-z_][A-Za-z0-9_
|
262
|
+
([A-Za-z_][A-Za-z0-9_]*)
|
263
|
+
(?:
|
264
|
+
\.
|
265
|
+
([A-Za-z_][A-Za-z0-9_]*)
|
266
|
+
)?
|
249
267
|
///)
|
268
|
+
[_, type, ident1, ident2] = lMatches
|
250
269
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
///)
|
261
|
-
return ['return', lMatches[1]]
|
270
|
+
if ident2
|
271
|
+
funcName = ident2
|
272
|
+
else
|
273
|
+
funcName = ident1
|
274
|
+
|
275
|
+
if (type == 'enter')
|
276
|
+
return ['enter', funcName]
|
277
|
+
else
|
278
|
+
return ['return', funcName]
|
262
279
|
else
|
263
280
|
return ['string', undef]
|
264
281
|
|
package/src/debug_utils.js
CHANGED
@@ -65,12 +65,26 @@ lFuncList = []; // names of functions being debugged
|
|
65
65
|
strFuncList = undef; // original string
|
66
66
|
|
67
67
|
|
68
|
+
// ---------------------------------------------------------------------------
|
69
|
+
export var interp = function(label) {
|
70
|
+
return label.replace(/\$(\@)?([A-Za-z_][A-Za-z0-9_]*)/g, function(_, atSign, varName) {
|
71
|
+
if (atSign) {
|
72
|
+
return `\#{OL(@${varName})\}`;
|
73
|
+
} else {
|
74
|
+
return `\#{OL(${varName})\}`;
|
75
|
+
}
|
76
|
+
});
|
77
|
+
};
|
78
|
+
|
68
79
|
// ---------------------------------------------------------------------------
|
69
80
|
export var debug = function(orgLabel, ...lObjects) {
|
70
81
|
var funcName, label, type;
|
71
82
|
assert(isString(orgLabel), `1st arg ${OL(orgLabel)} should be a string`);
|
72
83
|
[type, funcName] = getType(orgLabel, lObjects);
|
73
84
|
label = shouldLog(orgLabel, type, funcName, callStack);
|
85
|
+
if (defined(label)) {
|
86
|
+
label = interp(label);
|
87
|
+
}
|
74
88
|
switch (type) {
|
75
89
|
case 'enter':
|
76
90
|
if (defined(label)) {
|
@@ -292,12 +306,19 @@ export var funcMatch = function(funcName) {
|
|
292
306
|
// ---------------------------------------------------------------------------
|
293
307
|
// --- type is one of: 'enter', 'return', 'string'
|
294
308
|
export var getType = function(str, lObjects) {
|
295
|
-
var lMatches;
|
296
|
-
if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
309
|
+
var _, funcName, ident1, ident2, lMatches, type;
|
310
|
+
if (lMatches = str.match(/^\s*(enter|(?:return.+from))\s+([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?/)) {
|
311
|
+
[_, type, ident1, ident2] = lMatches;
|
312
|
+
if (ident2) {
|
313
|
+
funcName = ident2;
|
314
|
+
} else {
|
315
|
+
funcName = ident1;
|
316
|
+
}
|
317
|
+
if (type === 'enter') {
|
318
|
+
return ['enter', funcName];
|
319
|
+
} else {
|
320
|
+
return ['return', funcName];
|
321
|
+
}
|
301
322
|
} else {
|
302
323
|
return ['string', undef];
|
303
324
|
}
|
package/src/fs_utils.coffee
CHANGED
@@ -6,7 +6,7 @@ import fs from 'fs'
|
|
6
6
|
import NReadLines from 'n-readlines'
|
7
7
|
|
8
8
|
import {
|
9
|
-
assert, undef, pass, rtrim, error, isEmpty, nonEmpty,
|
9
|
+
assert, undef, pass, defined, rtrim, error, isEmpty, nonEmpty,
|
10
10
|
isString, isArray, isRegExp, isFunction, croak, OL,
|
11
11
|
} from '@jdeighan/coffee-utils'
|
12
12
|
import {log, LOG} from '@jdeighan/coffee-utils/log'
|
@@ -270,30 +270,33 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
|
|
270
270
|
|
271
271
|
export pathTo = (fname, searchDir, direction="down") ->
|
272
272
|
|
273
|
-
debug "enter pathTo(
|
273
|
+
debug "enter pathTo()", fname, searchDir, direction
|
274
274
|
if ! searchDir
|
275
275
|
searchDir = process.cwd()
|
276
|
-
assert fs.existsSync(searchDir), "
|
276
|
+
assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
|
277
277
|
filepath = mkpath(searchDir, fname)
|
278
278
|
if fs.existsSync(filepath)
|
279
|
-
debug "return from pathTo
|
279
|
+
debug "return from pathTo() - file exists", filepath
|
280
280
|
return filepath
|
281
|
-
|
281
|
+
|
282
|
+
if (direction == 'down')
|
282
283
|
# --- Search all directories in this directory
|
283
284
|
# getSubDirs() returns dirs sorted alphabetically
|
284
285
|
for subdir in getSubDirs(searchDir)
|
285
286
|
dirpath = mkpath(searchDir, subdir)
|
286
|
-
debug "check #{
|
287
|
-
if fpath = pathTo(fname, dirpath)
|
288
|
-
debug "return from pathTo
|
287
|
+
debug "check #{subdir}"
|
288
|
+
if defined(fpath = pathTo(fname, dirpath))
|
289
|
+
debug "return from pathTo()", fpath
|
289
290
|
return fpath
|
290
291
|
else if (direction == 'up')
|
291
|
-
while
|
292
|
-
debug "check #{
|
293
|
-
filepath = mkpath(
|
292
|
+
while defined(dirPath = getParentDir(searchDir))
|
293
|
+
debug "check #{dirPath}"
|
294
|
+
filepath = mkpath(dirPath, fname)
|
295
|
+
debug "check for #{filepath}"
|
294
296
|
if fs.existsSync(filepath)
|
295
|
-
debug "return from pathTo()
|
297
|
+
debug "return from pathTo()", filepath
|
296
298
|
return filepath
|
299
|
+
searchDir = dirPath
|
297
300
|
else
|
298
301
|
error "pathTo(): Invalid direction '#{direction}'"
|
299
302
|
debug "return undef from pathTo - file not found"
|
package/src/fs_utils.js
CHANGED
@@ -14,6 +14,7 @@ import {
|
|
14
14
|
assert,
|
15
15
|
undef,
|
16
16
|
pass,
|
17
|
+
defined,
|
17
18
|
rtrim,
|
18
19
|
error,
|
19
20
|
isEmpty,
|
@@ -326,37 +327,40 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
|
|
326
327
|
|
327
328
|
// ---------------------------------------------------------------------------
|
328
329
|
export var pathTo = function(fname, searchDir, direction = "down") {
|
329
|
-
var dirpath, filepath, fpath, i, len, ref, subdir;
|
330
|
-
debug(
|
330
|
+
var dirPath, dirpath, filepath, fpath, i, len, ref, subdir;
|
331
|
+
debug("enter pathTo()", fname, searchDir, direction);
|
331
332
|
if (!searchDir) {
|
332
333
|
searchDir = process.cwd();
|
333
334
|
}
|
334
|
-
assert(fs.existsSync(searchDir), `
|
335
|
+
assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
|
335
336
|
filepath = mkpath(searchDir, fname);
|
336
337
|
if (fs.existsSync(filepath)) {
|
337
|
-
debug(
|
338
|
+
debug("return from pathTo() - file exists", filepath);
|
338
339
|
return filepath;
|
339
|
-
}
|
340
|
+
}
|
341
|
+
if (direction === 'down') {
|
340
342
|
ref = getSubDirs(searchDir);
|
341
343
|
// --- Search all directories in this directory
|
342
344
|
// getSubDirs() returns dirs sorted alphabetically
|
343
345
|
for (i = 0, len = ref.length; i < len; i++) {
|
344
346
|
subdir = ref[i];
|
345
347
|
dirpath = mkpath(searchDir, subdir);
|
346
|
-
debug(`check ${
|
347
|
-
if (fpath = pathTo(fname, dirpath)) {
|
348
|
-
debug(
|
348
|
+
debug(`check ${subdir}`);
|
349
|
+
if (defined(fpath = pathTo(fname, dirpath))) {
|
350
|
+
debug("return from pathTo()", fpath);
|
349
351
|
return fpath;
|
350
352
|
}
|
351
353
|
}
|
352
354
|
} else if (direction === 'up') {
|
353
|
-
while (
|
354
|
-
debug(`check ${
|
355
|
-
filepath = mkpath(
|
355
|
+
while (defined(dirPath = getParentDir(searchDir))) {
|
356
|
+
debug(`check ${dirPath}`);
|
357
|
+
filepath = mkpath(dirPath, fname);
|
358
|
+
debug(`check for ${filepath}`);
|
356
359
|
if (fs.existsSync(filepath)) {
|
357
|
-
debug(
|
360
|
+
debug("return from pathTo()", filepath);
|
358
361
|
return filepath;
|
359
362
|
}
|
363
|
+
searchDir = dirPath;
|
360
364
|
}
|
361
365
|
} else {
|
362
366
|
error(`pathTo(): Invalid direction '${direction}'`);
|
package/src/indent_utils.coffee
CHANGED
@@ -131,13 +131,14 @@ export tabify = (str, numSpaces=undef) ->
|
|
131
131
|
if prefixLen == 0
|
132
132
|
lLines.push theRest
|
133
133
|
else
|
134
|
-
|
135
|
-
error "tabify(): leading TAB characters not allowed"
|
134
|
+
assert (prefix.indexOf('\t') == -1), "found TAB"
|
136
135
|
if numSpaces == undef
|
137
136
|
numSpaces = prefixLen
|
138
137
|
assert (prefixLen % numSpaces == 0), "Bad prefix"
|
139
|
-
|
140
|
-
|
138
|
+
level = prefixLen / numSpaces
|
139
|
+
lLines.push '\t'.repeat(level) + theRest
|
140
|
+
result = arrayToBlock(lLines)
|
141
|
+
return result
|
141
142
|
|
142
143
|
# ---------------------------------------------------------------------------
|
143
144
|
# untabify - convert ALL TABs to spaces
|
package/src/indent_utils.js
CHANGED
@@ -147,7 +147,7 @@ export var undented = function(text, level = undef) {
|
|
147
147
|
// if numSpaces is not defined, then the first line
|
148
148
|
// that contains at least one space sets it
|
149
149
|
export var tabify = function(str, numSpaces = undef) {
|
150
|
-
var _, i, lLines, len, prefix, prefixLen, ref, theRest;
|
150
|
+
var _, i, lLines, len, level, prefix, prefixLen, ref, result, theRest;
|
151
151
|
lLines = [];
|
152
152
|
ref = blockToArray(str);
|
153
153
|
for (i = 0, len = ref.length; i < len; i++) {
|
@@ -157,17 +157,17 @@ export var tabify = function(str, numSpaces = undef) {
|
|
157
157
|
if (prefixLen === 0) {
|
158
158
|
lLines.push(theRest);
|
159
159
|
} else {
|
160
|
-
|
161
|
-
error("tabify(): leading TAB characters not allowed");
|
162
|
-
}
|
160
|
+
assert(prefix.indexOf('\t') === -1, "found TAB");
|
163
161
|
if (numSpaces === undef) {
|
164
162
|
numSpaces = prefixLen;
|
165
163
|
}
|
166
164
|
assert(prefixLen % numSpaces === 0, "Bad prefix");
|
167
|
-
|
165
|
+
level = prefixLen / numSpaces;
|
166
|
+
lLines.push('\t'.repeat(level) + theRest);
|
168
167
|
}
|
169
168
|
}
|
170
|
-
|
169
|
+
result = arrayToBlock(lLines);
|
170
|
+
return result;
|
171
171
|
};
|
172
172
|
|
173
173
|
// ---------------------------------------------------------------------------
|