@jdeighan/coffee-utils 2.1.2 → 2.1.6
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/coffee_utils.coffee +6 -0
- package/src/coffee_utils.js +5 -0
- package/src/debug_utils.coffee +47 -32
- package/src/debug_utils.js +42 -31
- package/src/fs_utils.coffee +29 -1
- package/src/fs_utils.js +44 -1
- package/src/indent_utils.coffee +6 -7
- package/src/indent_utils.js +8 -9
package/package.json
CHANGED
package/src/coffee_utils.coffee
CHANGED
@@ -188,6 +188,12 @@ export isFunction = (x) ->
|
|
188
188
|
|
189
189
|
# ---------------------------------------------------------------------------
|
190
190
|
|
191
|
+
export isRegExp = (x) ->
|
192
|
+
|
193
|
+
return x instanceof RegExp
|
194
|
+
|
195
|
+
# ---------------------------------------------------------------------------
|
196
|
+
|
191
197
|
export isInteger = (x) ->
|
192
198
|
|
193
199
|
if (typeof x == 'number')
|
package/src/coffee_utils.js
CHANGED
@@ -195,6 +195,11 @@ export var isFunction = function(x) {
|
|
195
195
|
return typeof x === 'function';
|
196
196
|
};
|
197
197
|
|
198
|
+
// ---------------------------------------------------------------------------
|
199
|
+
export var isRegExp = function(x) {
|
200
|
+
return x instanceof RegExp;
|
201
|
+
};
|
202
|
+
|
198
203
|
// ---------------------------------------------------------------------------
|
199
204
|
export var isInteger = function(x) {
|
200
205
|
if (typeof x === 'number') {
|
package/src/debug_utils.coffee
CHANGED
@@ -17,13 +17,12 @@ arrow = corner + hbar + arrowhead + ' '
|
|
17
17
|
|
18
18
|
debugLevel = 0 # controls amount of indentation - we ensure it's never < 0
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
ifMatches = undefined
|
20
|
+
lDebugStack = []
|
23
21
|
|
24
|
-
# ---
|
25
|
-
|
26
|
-
|
22
|
+
# --- These are saved/restored in lDebugStack
|
23
|
+
export debugging = false
|
24
|
+
ifMatches = undef
|
25
|
+
lDebugFuncs = undef
|
27
26
|
|
28
27
|
# ---------------------------------------------------------------------------
|
29
28
|
|
@@ -34,37 +33,43 @@ export debugIfLineMatches = (regexp=undef) ->
|
|
34
33
|
|
35
34
|
# ---------------------------------------------------------------------------
|
36
35
|
|
37
|
-
lDebugStack = []
|
38
|
-
|
39
36
|
saveDebugEnv = () ->
|
40
37
|
|
41
|
-
lDebugStack.push(
|
38
|
+
lDebugStack.push({
|
39
|
+
debugging,
|
40
|
+
ifMatches,
|
41
|
+
lDebugFuncs,
|
42
|
+
})
|
42
43
|
return
|
43
44
|
|
44
45
|
restoreDebugEnv = () ->
|
45
46
|
|
46
47
|
if (lDebugStack.length == 0)
|
47
48
|
debugging = false
|
49
|
+
ifMatches = undef
|
50
|
+
lDebugFuncs = undef
|
48
51
|
else
|
49
|
-
|
52
|
+
h = lDebugStack.pop()
|
53
|
+
{debugging, ifMatches, lDebugFuncs} = h
|
54
|
+
|
50
55
|
return
|
51
56
|
|
52
57
|
# ---------------------------------------------------------------------------
|
53
58
|
|
54
59
|
export setDebugging = (x) ->
|
55
60
|
|
56
|
-
if (x==
|
57
|
-
# --- save current setting
|
58
|
-
saveDebugEnv()
|
59
|
-
debugging = true
|
60
|
-
else if (x==false)
|
61
|
+
if (x==false)
|
61
62
|
restoreDebugEnv()
|
62
|
-
else if isString(x)
|
63
|
-
lDebugFuncs = words(x)
|
64
|
-
else if isArray(x)
|
65
|
-
lDebugFuncs = x
|
66
63
|
else
|
67
|
-
|
64
|
+
# --- save current setting
|
65
|
+
saveDebugEnv()
|
66
|
+
if (x==true)
|
67
|
+
debugging = true
|
68
|
+
else if isString(x)
|
69
|
+
debugging = false
|
70
|
+
lDebugFuncs = words(x)
|
71
|
+
else
|
72
|
+
croak "setDebugging(): bad parameter #{oneline(x)}"
|
68
73
|
return
|
69
74
|
|
70
75
|
# ---------------------------------------------------------------------------
|
@@ -78,6 +83,17 @@ getPrefix = (level) ->
|
|
78
83
|
|
79
84
|
# ---------------------------------------------------------------------------
|
80
85
|
|
86
|
+
export resetDebugging = () ->
|
87
|
+
|
88
|
+
debugging = false
|
89
|
+
debugLevel = 0
|
90
|
+
ifMatches = undef
|
91
|
+
lDebugFuncs = undef
|
92
|
+
lDebugStack = []
|
93
|
+
return
|
94
|
+
|
95
|
+
# ---------------------------------------------------------------------------
|
96
|
+
|
81
97
|
export debug = (lArgs...) ->
|
82
98
|
# --- either 1 or 2 args
|
83
99
|
|
@@ -100,7 +116,7 @@ export debug = (lArgs...) ->
|
|
100
116
|
# --- determine if we're entering or returning from a function
|
101
117
|
entering = exiting = false
|
102
118
|
curFunction = undef
|
103
|
-
if (lMatches=str.match(///^
|
119
|
+
if (lMatches = str.match(///^
|
104
120
|
\s*
|
105
121
|
enter
|
106
122
|
\s+
|
@@ -108,7 +124,7 @@ export debug = (lArgs...) ->
|
|
108
124
|
///))
|
109
125
|
entering = true
|
110
126
|
curFunction = lMatches[1]
|
111
|
-
else if (lMatches=str.match(///^
|
127
|
+
else if (lMatches = str.match(///^
|
112
128
|
\s*
|
113
129
|
return
|
114
130
|
.*
|
@@ -119,9 +135,8 @@ export debug = (lArgs...) ->
|
|
119
135
|
exiting = true
|
120
136
|
curFunction = lMatches[1]
|
121
137
|
|
122
|
-
if
|
123
|
-
|
124
|
-
setDebugging true
|
138
|
+
if entering && lDebugFuncs && lDebugFuncs.includes(curFunction)
|
139
|
+
setDebugging true
|
125
140
|
|
126
141
|
if debugging && (not ifMatches? || str.match(ifMatches))
|
127
142
|
|
@@ -139,12 +154,12 @@ export debug = (lArgs...) ->
|
|
139
154
|
else
|
140
155
|
log str, item, {prefix, logItem: true}
|
141
156
|
|
142
|
-
if
|
143
|
-
|
144
|
-
setDebugging false # revert to previous setting - might still be on
|
157
|
+
if exiting && lDebugFuncs && lDebugFuncs.includes(curFunction)
|
158
|
+
setDebugging false # revert to previous setting - might still be on
|
145
159
|
|
146
|
-
if
|
147
|
-
|
148
|
-
|
149
|
-
debugLevel
|
160
|
+
if debugging
|
161
|
+
if entering
|
162
|
+
debugLevel += 1
|
163
|
+
if exiting && (debugLevel > 0)
|
164
|
+
debugLevel -= 1
|
150
165
|
return
|
package/src/debug_utils.js
CHANGED
@@ -39,13 +39,14 @@ arrow = corner + hbar + arrowhead + ' ';
|
|
39
39
|
|
40
40
|
debugLevel = 0; // controls amount of indentation - we ensure it's never < 0
|
41
41
|
|
42
|
+
lDebugStack = [];
|
43
|
+
|
44
|
+
// --- These are saved/restored in lDebugStack
|
42
45
|
export var debugging = false;
|
43
46
|
|
44
|
-
ifMatches =
|
47
|
+
ifMatches = undef;
|
45
48
|
|
46
|
-
|
47
|
-
// when returning from one of these functions, restore previous setting
|
48
|
-
lDebugFuncs = void 0;
|
49
|
+
lDebugFuncs = undef;
|
49
50
|
|
50
51
|
// ---------------------------------------------------------------------------
|
51
52
|
export var debugIfLineMatches = function(regexp = undef) {
|
@@ -53,34 +54,37 @@ export var debugIfLineMatches = function(regexp = undef) {
|
|
53
54
|
};
|
54
55
|
|
55
56
|
// ---------------------------------------------------------------------------
|
56
|
-
lDebugStack = [];
|
57
|
-
|
58
57
|
saveDebugEnv = function() {
|
59
|
-
lDebugStack.push(debugging);
|
58
|
+
lDebugStack.push({debugging, ifMatches, lDebugFuncs});
|
60
59
|
};
|
61
60
|
|
62
61
|
restoreDebugEnv = function() {
|
62
|
+
var h;
|
63
63
|
if (lDebugStack.length === 0) {
|
64
64
|
debugging = false;
|
65
|
+
ifMatches = undef;
|
66
|
+
lDebugFuncs = undef;
|
65
67
|
} else {
|
66
|
-
|
68
|
+
h = lDebugStack.pop();
|
69
|
+
({debugging, ifMatches, lDebugFuncs} = h);
|
67
70
|
}
|
68
71
|
};
|
69
72
|
|
70
73
|
// ---------------------------------------------------------------------------
|
71
74
|
export var setDebugging = function(x) {
|
72
|
-
if (x ===
|
73
|
-
// --- save current setting
|
74
|
-
saveDebugEnv();
|
75
|
-
debugging = true;
|
76
|
-
} else if (x === false) {
|
75
|
+
if (x === false) {
|
77
76
|
restoreDebugEnv();
|
78
|
-
} else if (isString(x)) {
|
79
|
-
lDebugFuncs = words(x);
|
80
|
-
} else if (isArray(x)) {
|
81
|
-
lDebugFuncs = x;
|
82
77
|
} else {
|
83
|
-
|
78
|
+
// --- save current setting
|
79
|
+
saveDebugEnv();
|
80
|
+
if (x === true) {
|
81
|
+
debugging = true;
|
82
|
+
} else if (isString(x)) {
|
83
|
+
debugging = false;
|
84
|
+
lDebugFuncs = words(x);
|
85
|
+
} else {
|
86
|
+
croak(`setDebugging(): bad parameter ${oneline(x)}`);
|
87
|
+
}
|
84
88
|
}
|
85
89
|
};
|
86
90
|
|
@@ -93,6 +97,15 @@ getPrefix = function(level) {
|
|
93
97
|
return ' '.repeat(level);
|
94
98
|
};
|
95
99
|
|
100
|
+
// ---------------------------------------------------------------------------
|
101
|
+
export var resetDebugging = function() {
|
102
|
+
debugging = false;
|
103
|
+
debugLevel = 0;
|
104
|
+
ifMatches = undef;
|
105
|
+
lDebugFuncs = undef;
|
106
|
+
lDebugStack = [];
|
107
|
+
};
|
108
|
+
|
96
109
|
// ---------------------------------------------------------------------------
|
97
110
|
export var debug = function(...lArgs) {
|
98
111
|
var curFunction, entering, exiting, item, lMatches, nArgs, prefix, str;
|
@@ -119,10 +132,8 @@ export var debug = function(...lArgs) {
|
|
119
132
|
exiting = true;
|
120
133
|
curFunction = lMatches[1];
|
121
134
|
}
|
122
|
-
if (
|
123
|
-
|
124
|
-
setDebugging(true);
|
125
|
-
}
|
135
|
+
if (entering && lDebugFuncs && lDebugFuncs.includes(curFunction)) {
|
136
|
+
setDebugging(true);
|
126
137
|
}
|
127
138
|
if (debugging && ((ifMatches == null) || str.match(ifMatches))) {
|
128
139
|
// --- set the prefix, i.e. indentation to use
|
@@ -144,15 +155,15 @@ export var debug = function(...lArgs) {
|
|
144
155
|
});
|
145
156
|
}
|
146
157
|
}
|
147
|
-
if (
|
148
|
-
|
149
|
-
setDebugging(false); // revert to previous setting - might still be on
|
150
|
-
}
|
151
|
-
}
|
152
|
-
if (entering) {
|
153
|
-
debugLevel += 1;
|
158
|
+
if (exiting && lDebugFuncs && lDebugFuncs.includes(curFunction)) {
|
159
|
+
setDebugging(false); // revert to previous setting - might still be on
|
154
160
|
}
|
155
|
-
if (
|
156
|
-
|
161
|
+
if (debugging) {
|
162
|
+
if (entering) {
|
163
|
+
debugLevel += 1;
|
164
|
+
}
|
165
|
+
if (exiting && (debugLevel > 0)) {
|
166
|
+
debugLevel -= 1;
|
167
|
+
}
|
157
168
|
}
|
158
169
|
};
|
package/src/fs_utils.coffee
CHANGED
@@ -11,7 +11,8 @@ import {
|
|
11
11
|
} from 'fs'
|
12
12
|
|
13
13
|
import {
|
14
|
-
undef, pass, firstLine, rtrim, error,
|
14
|
+
undef, pass, firstLine, rtrim, error, nonEmpty,
|
15
|
+
isRegExp, isFunction, croak,
|
15
16
|
} from '@jdeighan/coffee-utils'
|
16
17
|
import {log} from '@jdeighan/coffee-utils/log'
|
17
18
|
import {debug} from '@jdeighan/coffee-utils/debug'
|
@@ -117,6 +118,33 @@ export getParentDir = (dir) ->
|
|
117
118
|
|
118
119
|
# ---------------------------------------------------------------------------
|
119
120
|
|
121
|
+
export forEachFile = (dir, cb, filt=undef, level=0) ->
|
122
|
+
# --- filt can be a regular expression or a function that gets:
|
123
|
+
# (filename, dir, level)
|
124
|
+
# callback will get parms (filename, dir, level)
|
125
|
+
|
126
|
+
lSubDirectories = []
|
127
|
+
for ent in readdirSync(dir, {withFileTypes: true})
|
128
|
+
if ent.isDirectory()
|
129
|
+
lSubDirectories.push ent
|
130
|
+
else if ent.isFile()
|
131
|
+
if not filt?
|
132
|
+
cb(ent.name, dir, level)
|
133
|
+
else if isRegExp(filt)
|
134
|
+
if ent.name.match(filt)
|
135
|
+
cb(ent.name, dir, level)
|
136
|
+
else if isFunction(filt)
|
137
|
+
if filt(ent.name, dir, level)
|
138
|
+
cb(ent.name, dir, level)
|
139
|
+
else
|
140
|
+
croak "forEachFile(): bad filter", 'filter', filt
|
141
|
+
if nonEmpty(lSubDirectories)
|
142
|
+
for subdir in lSubDirectories.sort()
|
143
|
+
forEachFile(mkpath(dir, subdir.name), cb, filt, level+1)
|
144
|
+
return
|
145
|
+
|
146
|
+
# ---------------------------------------------------------------------------
|
147
|
+
|
120
148
|
export pathTo = (fname, dir, direction="down") ->
|
121
149
|
|
122
150
|
debug "enter pathTo('#{fname}','#{dir}','#{direction}')"
|
package/src/fs_utils.js
CHANGED
@@ -28,7 +28,11 @@ import {
|
|
28
28
|
pass,
|
29
29
|
firstLine,
|
30
30
|
rtrim,
|
31
|
-
error
|
31
|
+
error,
|
32
|
+
nonEmpty,
|
33
|
+
isRegExp,
|
34
|
+
isFunction,
|
35
|
+
croak
|
32
36
|
} from '@jdeighan/coffee-utils';
|
33
37
|
|
34
38
|
import {
|
@@ -145,6 +149,45 @@ export var getParentDir = function(dir) {
|
|
145
149
|
return mkpath(resolve(dir, '..'));
|
146
150
|
};
|
147
151
|
|
152
|
+
// ---------------------------------------------------------------------------
|
153
|
+
export var forEachFile = function(dir, cb, filt = undef, level = 0) {
|
154
|
+
var ent, i, j, lSubDirectories, len, len1, ref, ref1, subdir;
|
155
|
+
// --- filt can be a regular expression or a function that gets:
|
156
|
+
// (filename, dir, level)
|
157
|
+
// callback will get parms (filename, dir, level)
|
158
|
+
lSubDirectories = [];
|
159
|
+
ref = readdirSync(dir, {
|
160
|
+
withFileTypes: true
|
161
|
+
});
|
162
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
163
|
+
ent = ref[i];
|
164
|
+
if (ent.isDirectory()) {
|
165
|
+
lSubDirectories.push(ent);
|
166
|
+
} else if (ent.isFile()) {
|
167
|
+
if (filt == null) {
|
168
|
+
cb(ent.name, dir, level);
|
169
|
+
} else if (isRegExp(filt)) {
|
170
|
+
if (ent.name.match(filt)) {
|
171
|
+
cb(ent.name, dir, level);
|
172
|
+
} else if (isFunction(filt)) {
|
173
|
+
if (filt(ent.name, dir, level)) {
|
174
|
+
cb(ent.name, dir, level);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
} else {
|
178
|
+
croak("forEachFile(): bad filter", 'filter', filt);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
if (nonEmpty(lSubDirectories)) {
|
183
|
+
ref1 = lSubDirectories.sort();
|
184
|
+
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
185
|
+
subdir = ref1[j];
|
186
|
+
forEachFile(mkpath(dir, subdir.name), cb, filt, level + 1);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
};
|
190
|
+
|
148
191
|
// ---------------------------------------------------------------------------
|
149
192
|
export var pathTo = function(fname, dir, direction = "down") {
|
150
193
|
var fpath, i, len, ref, subdir;
|
package/src/indent_utils.coffee
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
import {strict as assert} from 'assert'
|
4
4
|
import {
|
5
5
|
undef, error, arrayToString, stringToArray, escapeStr,
|
6
|
-
oneline, isInteger, isString, isArray, isEmpty,
|
6
|
+
oneline, isInteger, isString, isArray, isEmpty, rtrim,
|
7
7
|
} from '@jdeighan/coffee-utils'
|
8
8
|
|
9
9
|
# ---------------------------------------------------------------------------
|
@@ -15,8 +15,9 @@ export splitLine = (line) ->
|
|
15
15
|
|
16
16
|
assert line?, "splitLine(): line is undef"
|
17
17
|
assert (typeof line == 'string'), "splitLine(): line is not a string"
|
18
|
+
line = rtrim(line)
|
18
19
|
lMatches = line.match(/^(\s*)(.*)$/)
|
19
|
-
return [lMatches[1].length, lMatches[2]
|
20
|
+
return [lMatches[1].length, lMatches[2]]
|
20
21
|
|
21
22
|
# ---------------------------------------------------------------------------
|
22
23
|
# indentation - return appropriate indentation string for given level
|
@@ -129,15 +130,13 @@ export tabify = (str, numSpaces=undef) ->
|
|
129
130
|
|
130
131
|
export untabify = (str, numSpaces=3) ->
|
131
132
|
|
133
|
+
oneIndent = ' '.repeat(numSpaces)
|
132
134
|
lLines = []
|
133
135
|
for str in stringToArray(str)
|
134
|
-
lMatches = str.match(/^(\
|
136
|
+
lMatches = str.match(/^(\t*)(.*)$/)
|
135
137
|
[_, prefix, theRest] = lMatches
|
136
138
|
if prefix == ''
|
137
139
|
lLines.push theRest
|
138
140
|
else
|
139
|
-
|
140
|
-
if (prefix != '\t'.repeat(n))
|
141
|
-
error "untabify(): not all TABs: prefix='#{escapeStr(prefix)}'"
|
142
|
-
lLines.push ' '.repeat(n * numSpaces) + theRest
|
141
|
+
lLines.push oneIndent.repeat(prefix.length) + theRest
|
143
142
|
return arrayToString(lLines)
|
package/src/indent_utils.js
CHANGED
@@ -14,7 +14,8 @@ import {
|
|
14
14
|
isInteger,
|
15
15
|
isString,
|
16
16
|
isArray,
|
17
|
-
isEmpty
|
17
|
+
isEmpty,
|
18
|
+
rtrim
|
18
19
|
} from '@jdeighan/coffee-utils';
|
19
20
|
|
20
21
|
// ---------------------------------------------------------------------------
|
@@ -25,8 +26,9 @@ export var splitLine = function(line) {
|
|
25
26
|
var lMatches;
|
26
27
|
assert(line != null, "splitLine(): line is undef");
|
27
28
|
assert(typeof line === 'string', "splitLine(): line is not a string");
|
29
|
+
line = rtrim(line);
|
28
30
|
lMatches = line.match(/^(\s*)(.*)$/);
|
29
|
-
return [lMatches[1].length, lMatches[2]
|
31
|
+
return [lMatches[1].length, lMatches[2]];
|
30
32
|
};
|
31
33
|
|
32
34
|
// ---------------------------------------------------------------------------
|
@@ -163,21 +165,18 @@ export var tabify = function(str, numSpaces = undef) {
|
|
163
165
|
// ---------------------------------------------------------------------------
|
164
166
|
// untabify - convert leading TABs to spaces
|
165
167
|
export var untabify = function(str, numSpaces = 3) {
|
166
|
-
var _, i, lLines, lMatches, len,
|
168
|
+
var _, i, lLines, lMatches, len, oneIndent, prefix, ref, theRest;
|
169
|
+
oneIndent = ' '.repeat(numSpaces);
|
167
170
|
lLines = [];
|
168
171
|
ref = stringToArray(str);
|
169
172
|
for (i = 0, len = ref.length; i < len; i++) {
|
170
173
|
str = ref[i];
|
171
|
-
lMatches = str.match(/^(\
|
174
|
+
lMatches = str.match(/^(\t*)(.*)$/);
|
172
175
|
[_, prefix, theRest] = lMatches;
|
173
176
|
if (prefix === '') {
|
174
177
|
lLines.push(theRest);
|
175
178
|
} else {
|
176
|
-
|
177
|
-
if (prefix !== '\t'.repeat(n)) {
|
178
|
-
error(`untabify(): not all TABs: prefix='${escapeStr(prefix)}'`);
|
179
|
-
}
|
180
|
-
lLines.push(' '.repeat(n * numSpaces) + theRest);
|
179
|
+
lLines.push(oneIndent.repeat(prefix.length) + theRest);
|
181
180
|
}
|
182
181
|
}
|
183
182
|
return arrayToString(lLines);
|