@jdeighan/coffee-utils 4.1.35 → 5.0.0
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 +6 -4
- package/src/block_utils.coffee +0 -1
- package/src/block_utils.js +0 -4
- package/src/coffee_utils.coffee +15 -22
- package/src/coffee_utils.js +25 -43
- package/src/debug_utils.coffee +21 -30
- package/src/debug_utils.js +27 -33
- package/src/indent_utils.coffee +7 -9
- package/src/indent_utils.js +8 -11
- package/src/log_utils.coffee +83 -91
- package/src/log_utils.js +97 -100
- package/src/UnitTester.coffee +0 -258
- package/src/UnitTester.js +0 -278
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
|
6
6
|
"main": "coffee_utils.js",
|
|
7
7
|
"exports": {
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"./stack": "./src/call_stack.js",
|
|
16
16
|
"./debug": "./src/debug_utils.js",
|
|
17
17
|
"./svelte": "./src/svelte_utils.js",
|
|
18
|
-
"./test": "./src/UnitTester.js",
|
|
19
18
|
"./store": "./src/DataStores.js",
|
|
20
19
|
"./taml": "./src/taml.js",
|
|
20
|
+
"./templib": "./src/templib.js",
|
|
21
21
|
"./package.json": "./package.json"
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "cls && rm -f ./src/*.js && coffee -c ./src",
|
|
31
31
|
"pretest": "cls && cielo -qfc ./test && coffee -c .",
|
|
32
|
-
"test": "ava ./test/*.test.js",
|
|
32
|
+
"test": "npx ava ./test/*.test.js",
|
|
33
33
|
"prefinaltest": "npm run pretest",
|
|
34
34
|
"finaltest": "cross-env FINALTEST=yes ava ./test/*.test.js"
|
|
35
35
|
},
|
|
@@ -47,10 +47,12 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/johndeighan/coffee-utils#readme",
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"ava": "^3.15.0",
|
|
51
50
|
"cross-env": "^7.0.3",
|
|
52
51
|
"js-yaml": "^4.1.0",
|
|
53
52
|
"readline-sync": "^1.4.10",
|
|
54
53
|
"svelte": "^3.46.4"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@jdeighan/unit-tester": "^1.0.4"
|
|
55
57
|
}
|
|
56
58
|
}
|
package/src/block_utils.coffee
CHANGED
|
@@ -6,7 +6,6 @@ import readline from 'readline'
|
|
|
6
6
|
import {
|
|
7
7
|
assert, isEmpty, isString, nonEmpty, error, isComment, rtrim,
|
|
8
8
|
} from '@jdeighan/coffee-utils'
|
|
9
|
-
import {log} from '@jdeighan/coffee-utils/log'
|
|
10
9
|
|
|
11
10
|
# ---------------------------------------------------------------------------
|
|
12
11
|
# blockToArray - split a block into lines
|
package/src/block_utils.js
CHANGED
|
@@ -14,10 +14,6 @@ import {
|
|
|
14
14
|
rtrim
|
|
15
15
|
} from '@jdeighan/coffee-utils';
|
|
16
16
|
|
|
17
|
-
import {
|
|
18
|
-
log
|
|
19
|
-
} from '@jdeighan/coffee-utils/log';
|
|
20
|
-
|
|
21
17
|
// ---------------------------------------------------------------------------
|
|
22
18
|
// blockToArray - split a block into lines
|
|
23
19
|
export var blockToArray = function(block) {
|
package/src/coffee_utils.coffee
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# coffee_utils.coffee
|
|
2
2
|
|
|
3
|
-
import {log} from '@jdeighan/coffee-utils/log'
|
|
4
|
-
|
|
5
3
|
export sep_dash = '-'.repeat(42)
|
|
6
4
|
export sep_eq = '='.repeat(42)
|
|
7
5
|
`export const undef = undefined`
|
|
@@ -33,8 +31,10 @@ export assert = (cond, msg) ->
|
|
|
33
31
|
export croak = (err, label, obj) ->
|
|
34
32
|
|
|
35
33
|
message = if (typeof err == 'object') then err.message else err
|
|
36
|
-
log "ERROR (croak): #{message}"
|
|
37
|
-
log
|
|
34
|
+
console.log "ERROR (croak): #{message}"
|
|
35
|
+
console.log obj, label
|
|
36
|
+
|
|
37
|
+
# --- re-throw the error
|
|
38
38
|
if (typeof err == 'object')
|
|
39
39
|
throw err
|
|
40
40
|
else
|
|
@@ -227,7 +227,7 @@ export uniq = (lItems) ->
|
|
|
227
227
|
|
|
228
228
|
export warn = (message) ->
|
|
229
229
|
|
|
230
|
-
|
|
230
|
+
say "WARNING: #{message}"
|
|
231
231
|
|
|
232
232
|
# ---------------------------------------------------------------------------
|
|
233
233
|
# hashToStr - stringify a hash
|
|
@@ -301,24 +301,17 @@ export deepCopy = (obj) ->
|
|
|
301
301
|
# ---------------------------------------------------------------------------
|
|
302
302
|
# escapeStr - escape newlines, TAB chars, etc.
|
|
303
303
|
|
|
304
|
-
export
|
|
304
|
+
export hDefEsc = {
|
|
305
|
+
"\n": '®'
|
|
306
|
+
"\t": '→'
|
|
307
|
+
" ": '˳'
|
|
308
|
+
}
|
|
305
309
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
hEscape[ch]
|
|
312
|
-
else
|
|
313
|
-
ch
|
|
314
|
-
else
|
|
315
|
-
lParts = for ch in str.split('')
|
|
316
|
-
if ch == '\n'
|
|
317
|
-
'\\n'
|
|
318
|
-
else if ch == '\t'
|
|
319
|
-
'\\t'
|
|
320
|
-
else
|
|
321
|
-
ch
|
|
310
|
+
export escapeStr = (str, hEscape=hDefEsc) ->
|
|
311
|
+
|
|
312
|
+
assert isString(str), "escapeStr(): not a string"
|
|
313
|
+
lParts = for ch in str.split('')
|
|
314
|
+
if hEscape[ch]? then hEscape[ch] else ch
|
|
322
315
|
return lParts.join('')
|
|
323
316
|
|
|
324
317
|
# ---------------------------------------------------------------------------
|
package/src/coffee_utils.js
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
// coffee_utils.coffee
|
|
3
3
|
var commentRegExp;
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
log
|
|
7
|
-
} from '@jdeighan/coffee-utils/log';
|
|
8
|
-
|
|
9
5
|
export var sep_dash = '-'.repeat(42);
|
|
10
6
|
|
|
11
7
|
export var sep_eq = '='.repeat(42);
|
|
@@ -35,8 +31,9 @@ export var assert = function(cond, msg) {
|
|
|
35
31
|
export var croak = function(err, label, obj) {
|
|
36
32
|
var message;
|
|
37
33
|
message = (typeof err === 'object') ? err.message : err;
|
|
38
|
-
log(`ERROR (croak): ${message}`);
|
|
39
|
-
log(
|
|
34
|
+
console.log(`ERROR (croak): ${message}`);
|
|
35
|
+
console.log(obj, label);
|
|
36
|
+
// --- re-throw the error
|
|
40
37
|
if (typeof err === 'object') {
|
|
41
38
|
throw err;
|
|
42
39
|
} else {
|
|
@@ -229,7 +226,7 @@ export var uniq = function(lItems) {
|
|
|
229
226
|
// ---------------------------------------------------------------------------
|
|
230
227
|
// warn - issue a warning
|
|
231
228
|
export var warn = function(message) {
|
|
232
|
-
return
|
|
229
|
+
return say(`WARNING: ${message}`);
|
|
233
230
|
};
|
|
234
231
|
|
|
235
232
|
// ---------------------------------------------------------------------------
|
|
@@ -298,44 +295,29 @@ export var deepCopy = function(obj) {
|
|
|
298
295
|
|
|
299
296
|
// ---------------------------------------------------------------------------
|
|
300
297
|
// escapeStr - escape newlines, TAB chars, etc.
|
|
301
|
-
export var
|
|
298
|
+
export var hDefEsc = {
|
|
299
|
+
"\n": '®',
|
|
300
|
+
"\t": '→',
|
|
301
|
+
" ": '˳'
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export var escapeStr = function(str, hEscape = hDefEsc) {
|
|
302
305
|
var ch, lParts;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
results.push(hEscape[ch]);
|
|
315
|
-
} else {
|
|
316
|
-
results.push(ch);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return results;
|
|
320
|
-
})();
|
|
321
|
-
} else {
|
|
322
|
-
lParts = (function() {
|
|
323
|
-
var i, len, ref, results;
|
|
324
|
-
ref = str.split('');
|
|
325
|
-
results = [];
|
|
326
|
-
for (i = 0, len = ref.length; i < len; i++) {
|
|
327
|
-
ch = ref[i];
|
|
328
|
-
if (ch === '\n') {
|
|
329
|
-
results.push('\\n');
|
|
330
|
-
} else if (ch === '\t') {
|
|
331
|
-
results.push('\\t');
|
|
332
|
-
} else {
|
|
333
|
-
results.push(ch);
|
|
334
|
-
}
|
|
306
|
+
assert(isString(str), "escapeStr(): not a string");
|
|
307
|
+
lParts = (function() {
|
|
308
|
+
var i, len, ref, results;
|
|
309
|
+
ref = str.split('');
|
|
310
|
+
results = [];
|
|
311
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
|
312
|
+
ch = ref[i];
|
|
313
|
+
if (hEscape[ch] != null) {
|
|
314
|
+
results.push(hEscape[ch]);
|
|
315
|
+
} else {
|
|
316
|
+
results.push(ch);
|
|
335
317
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
318
|
+
}
|
|
319
|
+
return results;
|
|
320
|
+
})();
|
|
339
321
|
return lParts.join('');
|
|
340
322
|
};
|
|
341
323
|
|
package/src/debug_utils.coffee
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
# debug_utils.coffee
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
assert,
|
|
4
|
+
assert, 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'
|
|
8
|
-
import {
|
|
8
|
+
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
|
+
import {
|
|
12
|
+
log, setStringifier, orderedStringify,
|
|
13
|
+
} from '@jdeighan/coffee-utils/log'
|
|
11
14
|
|
|
15
|
+
undef = undefined
|
|
12
16
|
vbar = '│' # unicode 2502
|
|
13
17
|
hbar = '─' # unicode 2500
|
|
14
18
|
corner = '└' # unicode 2514
|
|
@@ -18,7 +22,6 @@ indent = vbar + ' '
|
|
|
18
22
|
arrow = corner + hbar + arrowhead + ' '
|
|
19
23
|
|
|
20
24
|
debugLevel = 0 # controls amount of indentation - we ensure it's never < 0
|
|
21
|
-
stdLogger = false
|
|
22
25
|
|
|
23
26
|
# --- These are saved/restored on the call stack
|
|
24
27
|
export debugging = false
|
|
@@ -34,7 +37,7 @@ export resetDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
|
|
|
34
37
|
debugLevel = 0
|
|
35
38
|
stack.reset()
|
|
36
39
|
shouldDebug = (funcName, curDebugging) -> curDebugging
|
|
37
|
-
shouldLog = (str)
|
|
40
|
+
shouldLog = (str) -> debugging || process.env.DEBUG
|
|
38
41
|
if funcDoDebug
|
|
39
42
|
setDebugging funcDoDebug, funcDoLog
|
|
40
43
|
return
|
|
@@ -91,32 +94,17 @@ setEnv = (hEnv) ->
|
|
|
91
94
|
return
|
|
92
95
|
|
|
93
96
|
# ---------------------------------------------------------------------------
|
|
97
|
+
# --- 2 possible signatures:
|
|
98
|
+
# (item) - just log out the string
|
|
99
|
+
# (item, hOptions) - log out the object, with a label
|
|
94
100
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
stdLogger = flag
|
|
98
|
-
return
|
|
99
|
-
|
|
100
|
-
# ---------------------------------------------------------------------------
|
|
101
|
-
|
|
102
|
-
logger = (lArgs...) ->
|
|
101
|
+
logger = (item, hOptions=undef) ->
|
|
103
102
|
|
|
104
|
-
|
|
105
|
-
log lArgs...
|
|
106
|
-
else
|
|
107
|
-
orgLogger = setLogger(console.log)
|
|
108
|
-
log lArgs...
|
|
109
|
-
setLogger(orgLogger)
|
|
103
|
+
log item, hOptions
|
|
110
104
|
return
|
|
111
105
|
|
|
112
106
|
# ---------------------------------------------------------------------------
|
|
113
107
|
|
|
114
|
-
stripArrow = (prefix) ->
|
|
115
|
-
|
|
116
|
-
return prefix.replace(arrow, ' ')
|
|
117
|
-
|
|
118
|
-
# ---------------------------------------------------------------------------
|
|
119
|
-
|
|
120
108
|
getPrefix = (level) ->
|
|
121
109
|
|
|
122
110
|
if (level < 0)
|
|
@@ -182,7 +170,7 @@ export debug = (lArgs...) ->
|
|
|
182
170
|
curFunc = lMatches[1]
|
|
183
171
|
hInfo = stack.returnFrom(curFunc)
|
|
184
172
|
|
|
185
|
-
if
|
|
173
|
+
if shouldLog(str)
|
|
186
174
|
|
|
187
175
|
# --- set the prefix, i.e. indentation to use
|
|
188
176
|
if returning
|
|
@@ -194,12 +182,15 @@ export debug = (lArgs...) ->
|
|
|
194
182
|
prefix = indent.repeat(debugLevel)
|
|
195
183
|
|
|
196
184
|
if (nArgs==1)
|
|
197
|
-
logger str,
|
|
185
|
+
logger str, {
|
|
186
|
+
prefix: prefix
|
|
187
|
+
}
|
|
198
188
|
else
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
189
|
+
itemPrefix = prefix.replace(arrow, ' ')
|
|
190
|
+
logger item, {
|
|
191
|
+
label: str
|
|
192
|
+
prefix: prefix
|
|
193
|
+
itemPrefix
|
|
203
194
|
}
|
|
204
195
|
|
|
205
196
|
# --- Adjust debug level
|
package/src/debug_utils.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.6.1
|
|
2
2
|
// debug_utils.coffee
|
|
3
|
-
var arrow, arrowhead, corner, curEnv, debugLevel, envSaysDebug, getPrefix, hbar, indent, logger, reMethod, setEnv, shouldDebug, shouldLog, stack,
|
|
3
|
+
var arrow, arrowhead, corner, curEnv, debugLevel, envSaysDebug, getPrefix, hbar, indent, logger, reMethod, setEnv, shouldDebug, shouldLog, stack, undef, vbar;
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
assert,
|
|
7
|
-
undef,
|
|
8
7
|
error,
|
|
9
8
|
croak,
|
|
10
9
|
warn,
|
|
@@ -23,9 +22,8 @@ import {
|
|
|
23
22
|
} from '@jdeighan/coffee-utils/block';
|
|
24
23
|
|
|
25
24
|
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} from '@jdeighan/coffee-utils/log';
|
|
25
|
+
untabify
|
|
26
|
+
} from '@jdeighan/coffee-utils/indent';
|
|
29
27
|
|
|
30
28
|
import {
|
|
31
29
|
slurp
|
|
@@ -35,6 +33,14 @@ import {
|
|
|
35
33
|
CallStack
|
|
36
34
|
} from '@jdeighan/coffee-utils/stack';
|
|
37
35
|
|
|
36
|
+
import {
|
|
37
|
+
log,
|
|
38
|
+
setStringifier,
|
|
39
|
+
orderedStringify
|
|
40
|
+
} from '@jdeighan/coffee-utils/log';
|
|
41
|
+
|
|
42
|
+
undef = void 0;
|
|
43
|
+
|
|
38
44
|
vbar = '│'; // unicode 2502
|
|
39
45
|
|
|
40
46
|
hbar = '─'; // unicode 2500
|
|
@@ -49,7 +55,6 @@ arrow = corner + hbar + arrowhead + ' ';
|
|
|
49
55
|
|
|
50
56
|
debugLevel = 0; // controls amount of indentation - we ensure it's never < 0
|
|
51
57
|
|
|
52
|
-
stdLogger = false;
|
|
53
58
|
|
|
54
59
|
// --- These are saved/restored on the call stack
|
|
55
60
|
export var debugging = false;
|
|
@@ -67,7 +72,7 @@ export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
|
|
|
67
72
|
return curDebugging;
|
|
68
73
|
};
|
|
69
74
|
shouldLog = function(str) {
|
|
70
|
-
return debugging;
|
|
75
|
+
return debugging || process.env.DEBUG;
|
|
71
76
|
};
|
|
72
77
|
if (funcDoDebug) {
|
|
73
78
|
setDebugging(funcDoDebug, funcDoLog);
|
|
@@ -123,25 +128,11 @@ setEnv = function(hEnv) {
|
|
|
123
128
|
};
|
|
124
129
|
|
|
125
130
|
// ---------------------------------------------------------------------------
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
logger = function(...lArgs) {
|
|
132
|
-
var orgLogger;
|
|
133
|
-
if (stdLogger) {
|
|
134
|
-
log(...lArgs);
|
|
135
|
-
} else {
|
|
136
|
-
orgLogger = setLogger(console.log);
|
|
137
|
-
log(...lArgs);
|
|
138
|
-
setLogger(orgLogger);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
stripArrow = function(prefix) {
|
|
144
|
-
return prefix.replace(arrow, ' ');
|
|
131
|
+
// --- 2 possible signatures:
|
|
132
|
+
// (item) - just log out the string
|
|
133
|
+
// (item, hOptions) - log out the object, with a label
|
|
134
|
+
logger = function(item, hOptions = undef) {
|
|
135
|
+
log(item, hOptions);
|
|
145
136
|
};
|
|
146
137
|
|
|
147
138
|
// ---------------------------------------------------------------------------
|
|
@@ -166,7 +157,7 @@ envSaysDebug = function(curFunc) {
|
|
|
166
157
|
|
|
167
158
|
// ---------------------------------------------------------------------------
|
|
168
159
|
export var debug = function(...lArgs) {
|
|
169
|
-
var curFunc, entering, hInfo, item, lMatches, nArgs, prefix, returning, str;
|
|
160
|
+
var curFunc, entering, hInfo, item, itemPrefix, lMatches, nArgs, prefix, returning, str;
|
|
170
161
|
// --- either 1 or 2 args
|
|
171
162
|
|
|
172
163
|
// --- We always need to manipulate the stack when we encounter
|
|
@@ -194,7 +185,7 @@ export var debug = function(...lArgs) {
|
|
|
194
185
|
curFunc = lMatches[1];
|
|
195
186
|
hInfo = stack.returnFrom(curFunc);
|
|
196
187
|
}
|
|
197
|
-
if (
|
|
188
|
+
if (shouldLog(str)) {
|
|
198
189
|
// --- set the prefix, i.e. indentation to use
|
|
199
190
|
if (returning) {
|
|
200
191
|
if (debugLevel === 0) {
|
|
@@ -206,12 +197,15 @@ export var debug = function(...lArgs) {
|
|
|
206
197
|
prefix = indent.repeat(debugLevel);
|
|
207
198
|
}
|
|
208
199
|
if (nArgs === 1) {
|
|
209
|
-
logger(str,
|
|
200
|
+
logger(str, {
|
|
201
|
+
prefix: prefix
|
|
202
|
+
});
|
|
210
203
|
} else {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
204
|
+
itemPrefix = prefix.replace(arrow, ' ');
|
|
205
|
+
logger(item, {
|
|
206
|
+
label: str,
|
|
207
|
+
prefix: prefix,
|
|
208
|
+
itemPrefix
|
|
215
209
|
});
|
|
216
210
|
}
|
|
217
211
|
}
|
package/src/indent_utils.coffee
CHANGED
|
@@ -117,19 +117,17 @@ export tabify = (str, numSpaces=undef) ->
|
|
|
117
117
|
|
|
118
118
|
lLines = []
|
|
119
119
|
for str in blockToArray(str)
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if
|
|
120
|
+
[_, prefix, theRest] = str.match(/^(\s*)(.*)$/)
|
|
121
|
+
prefixLen = prefix.length
|
|
122
|
+
if prefixLen == 0
|
|
123
123
|
lLines.push theRest
|
|
124
124
|
else
|
|
125
|
-
n = prefix.length
|
|
126
125
|
if (prefix.indexOf('\t') != -1)
|
|
127
126
|
error "tabify(): leading TAB characters not allowed"
|
|
128
|
-
if
|
|
129
|
-
numSpaces =
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
lLines.push '\t'.repeat(n / numSpaces) + theRest
|
|
127
|
+
if numSpaces == undef
|
|
128
|
+
numSpaces = prefixLen
|
|
129
|
+
assert (prefixLen % numSpaces == 0), "Bad prefix"
|
|
130
|
+
lLines.push '\t'.repeat(prefixLen) + theRest
|
|
133
131
|
return arrayToBlock(lLines)
|
|
134
132
|
|
|
135
133
|
# ---------------------------------------------------------------------------
|
package/src/indent_utils.js
CHANGED
|
@@ -137,27 +137,24 @@ export var undented = function(text, level = undef) {
|
|
|
137
137
|
// if numSpaces is not defined, then the first line
|
|
138
138
|
// that contains at least one space sets it
|
|
139
139
|
export var tabify = function(str, numSpaces = undef) {
|
|
140
|
-
var _, i, lLines,
|
|
140
|
+
var _, i, lLines, len, prefix, prefixLen, ref, theRest;
|
|
141
141
|
lLines = [];
|
|
142
142
|
ref = blockToArray(str);
|
|
143
143
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
144
144
|
str = ref[i];
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
145
|
+
[_, prefix, theRest] = str.match(/^(\s*)(.*)$/);
|
|
146
|
+
prefixLen = prefix.length;
|
|
147
|
+
if (prefixLen === 0) {
|
|
148
148
|
lLines.push(theRest);
|
|
149
149
|
} else {
|
|
150
|
-
n = prefix.length;
|
|
151
150
|
if (prefix.indexOf('\t') !== -1) {
|
|
152
151
|
error("tabify(): leading TAB characters not allowed");
|
|
153
152
|
}
|
|
154
|
-
if (numSpaces
|
|
155
|
-
numSpaces =
|
|
156
|
-
}
|
|
157
|
-
if (n % numSpaces !== 0) {
|
|
158
|
-
error("tabify(): Invalid # of leading space chars");
|
|
153
|
+
if (numSpaces === undef) {
|
|
154
|
+
numSpaces = prefixLen;
|
|
159
155
|
}
|
|
160
|
-
|
|
156
|
+
assert(prefixLen % numSpaces === 0, "Bad prefix");
|
|
157
|
+
lLines.push('\t'.repeat(prefixLen) + theRest);
|
|
161
158
|
}
|
|
162
159
|
}
|
|
163
160
|
return arrayToBlock(lLines);
|