@jdeighan/coffee-utils 3.0.2 → 3.0.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 +5 -16
- package/src/coffee_utils.js +5 -17
- package/src/indent_utils.coffee +11 -8
- package/src/indent_utils.js +19 -24
- package/src/log_utils.coffee +1 -1
- package/src/log_utils.js +1 -1
package/package.json
CHANGED
package/src/coffee_utils.coffee
CHANGED
@@ -301,26 +301,15 @@ export OL = oneline
|
|
301
301
|
|
302
302
|
# ---------------------------------------------------------------------------
|
303
303
|
|
304
|
-
export removeCR = (
|
304
|
+
export removeCR = (str) ->
|
305
305
|
|
306
|
-
return
|
306
|
+
return str.replace(/\r/g, '')
|
307
307
|
|
308
308
|
# ---------------------------------------------------------------------------
|
309
309
|
|
310
|
-
export
|
310
|
+
export CWS = (str) ->
|
311
311
|
|
312
|
-
|
313
|
-
|
314
|
-
# --- pos is also the length of the 1st line
|
315
|
-
# 2nd arg to substr() is number of characters to return
|
316
|
-
return [block.substr(0, pos), block.substr(pos+1)]
|
317
|
-
else
|
318
|
-
return [block, '']
|
319
|
-
|
320
|
-
# ---------------------------------------------------------------------------
|
321
|
-
|
322
|
-
export CWS = (block) ->
|
323
|
-
|
324
|
-
return block.trim().replace(/\s+/g, ' ')
|
312
|
+
assert isString(str), "CWS(): parameter not a string"
|
313
|
+
return str.trim().replace(/\s+/sg, ' ')
|
325
314
|
|
326
315
|
# ---------------------------------------------------------------------------
|
package/src/coffee_utils.js
CHANGED
@@ -316,26 +316,14 @@ export var oneline = function(obj) {
|
|
316
316
|
export var OL = oneline;
|
317
317
|
|
318
318
|
// ---------------------------------------------------------------------------
|
319
|
-
export var removeCR = function(
|
320
|
-
return
|
319
|
+
export var removeCR = function(str) {
|
320
|
+
return str.replace(/\r/g, '');
|
321
321
|
};
|
322
322
|
|
323
323
|
// ---------------------------------------------------------------------------
|
324
|
-
export var
|
325
|
-
|
326
|
-
|
327
|
-
if (pos = block.indexOf("\n")) {
|
328
|
-
// --- pos is also the length of the 1st line
|
329
|
-
// 2nd arg to substr() is number of characters to return
|
330
|
-
return [block.substr(0, pos), block.substr(pos + 1)];
|
331
|
-
} else {
|
332
|
-
return [block, ''];
|
333
|
-
}
|
334
|
-
};
|
335
|
-
|
336
|
-
// ---------------------------------------------------------------------------
|
337
|
-
export var CWS = function(block) {
|
338
|
-
return block.trim().replace(/\s+/g, ' ');
|
324
|
+
export var CWS = function(str) {
|
325
|
+
assert(isString(str), "CWS(): parameter not a string");
|
326
|
+
return str.trim().replace(/\s+/sg, ' ');
|
339
327
|
};
|
340
328
|
|
341
329
|
// ---------------------------------------------------------------------------
|
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, escapeStr,
|
6
|
-
|
6
|
+
OL, isInteger, isString, isArray, isEmpty, rtrim,
|
7
7
|
} from '@jdeighan/coffee-utils'
|
8
8
|
import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
|
9
9
|
|
@@ -49,13 +49,16 @@ export indented = (input, level=0) ->
|
|
49
49
|
|
50
50
|
toAdd = indentation(level)
|
51
51
|
if isArray(input)
|
52
|
-
|
53
|
-
"#{toAdd}#{line}"
|
54
|
-
return lLines
|
52
|
+
lInputLines = input
|
55
53
|
else
|
56
|
-
|
54
|
+
lInputLines = blockToArray(input)
|
55
|
+
|
56
|
+
lLines = for line in lInputLines
|
57
|
+
if isEmpty(line)
|
58
|
+
""
|
59
|
+
else
|
57
60
|
"#{toAdd}#{line}"
|
58
|
-
|
61
|
+
return arrayToBlock(lLines)
|
59
62
|
|
60
63
|
# ---------------------------------------------------------------------------
|
61
64
|
# undented - string with 1st line indentation removed for each line
|
@@ -77,7 +80,7 @@ export undented = (text, level=undef) ->
|
|
77
80
|
if (lLines.length == 0)
|
78
81
|
return []
|
79
82
|
else
|
80
|
-
error "undented(): Not an array or string: #{
|
83
|
+
error "undented(): Not an array or string: #{OL(text)}"
|
81
84
|
|
82
85
|
# --- determine what to remove from beginning of each line
|
83
86
|
if level?
|
@@ -95,7 +98,7 @@ export undented = (text, level=undef) ->
|
|
95
98
|
else
|
96
99
|
assert (line.indexOf(toRemove)==0),
|
97
100
|
"undented(): Error removing '#{escapeStr(toRemove)}' \
|
98
|
-
from
|
101
|
+
from #{OL(text)}"
|
99
102
|
lNewLines.push(line.substr(nToRemove))
|
100
103
|
|
101
104
|
if isString(text)
|
package/src/indent_utils.js
CHANGED
@@ -8,7 +8,7 @@ import {
|
|
8
8
|
undef,
|
9
9
|
error,
|
10
10
|
escapeStr,
|
11
|
-
|
11
|
+
OL,
|
12
12
|
isInteger,
|
13
13
|
isString,
|
14
14
|
isArray,
|
@@ -54,36 +54,31 @@ export var indentLevel = function(str) {
|
|
54
54
|
// ---------------------------------------------------------------------------
|
55
55
|
// indented - add indentation to each string in a block
|
56
56
|
export var indented = function(input, level = 0) {
|
57
|
-
var lLines, line, toAdd;
|
57
|
+
var lInputLines, lLines, line, toAdd;
|
58
58
|
assert(level >= 0, "indented(): negative level");
|
59
59
|
if (level === 0) {
|
60
60
|
return input;
|
61
61
|
}
|
62
62
|
toAdd = indentation(level);
|
63
63
|
if (isArray(input)) {
|
64
|
-
|
65
|
-
var i, len, results;
|
66
|
-
results = [];
|
67
|
-
for (i = 0, len = input.length; i < len; i++) {
|
68
|
-
line = input[i];
|
69
|
-
results.push(`${toAdd}${line}`);
|
70
|
-
}
|
71
|
-
return results;
|
72
|
-
})();
|
73
|
-
return lLines;
|
64
|
+
lInputLines = input;
|
74
65
|
} else {
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
66
|
+
lInputLines = blockToArray(input);
|
67
|
+
}
|
68
|
+
lLines = (function() {
|
69
|
+
var i, len, results;
|
70
|
+
results = [];
|
71
|
+
for (i = 0, len = lInputLines.length; i < len; i++) {
|
72
|
+
line = lInputLines[i];
|
73
|
+
if (isEmpty(line)) {
|
74
|
+
results.push("");
|
75
|
+
} else {
|
81
76
|
results.push(`${toAdd}${line}`);
|
82
77
|
}
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
78
|
+
}
|
79
|
+
return results;
|
80
|
+
})();
|
81
|
+
return arrayToBlock(lLines);
|
87
82
|
};
|
88
83
|
|
89
84
|
// ---------------------------------------------------------------------------
|
@@ -107,7 +102,7 @@ export var undented = function(text, level = undef) {
|
|
107
102
|
return [];
|
108
103
|
}
|
109
104
|
} else {
|
110
|
-
error(`undented(): Not an array or string: ${
|
105
|
+
error(`undented(): Not an array or string: ${OL(text)}`);
|
111
106
|
}
|
112
107
|
// --- determine what to remove from beginning of each line
|
113
108
|
if (level != null) {
|
@@ -124,7 +119,7 @@ export var undented = function(text, level = undef) {
|
|
124
119
|
if (isEmpty(line)) {
|
125
120
|
lNewLines.push('');
|
126
121
|
} else {
|
127
|
-
assert(line.indexOf(toRemove) === 0, `undented(): Error removing '${escapeStr(toRemove)}' from
|
122
|
+
assert(line.indexOf(toRemove) === 0, `undented(): Error removing '${escapeStr(toRemove)}' from ${OL(text)}`);
|
128
123
|
lNewLines.push(line.substr(nToRemove));
|
129
124
|
}
|
130
125
|
}
|
package/src/log_utils.coffee
CHANGED
@@ -121,7 +121,7 @@ export log = (lArgs...) ->
|
|
121
121
|
else
|
122
122
|
logger "#{prefix}#{str}:"
|
123
123
|
for line in blockToArray(item)
|
124
|
-
logger "#{itemPrefix} #{escapeStr(line)}"
|
124
|
+
logger "#{itemPrefix} '#{escapeStr(line)}'"
|
125
125
|
else
|
126
126
|
# --- It's some type of object
|
127
127
|
json = JSON.stringify(item)
|
package/src/log_utils.js
CHANGED
@@ -139,7 +139,7 @@ export var log = function(...lArgs) {
|
|
139
139
|
ref = blockToArray(item);
|
140
140
|
for (i = 0, len = ref.length; i < len; i++) {
|
141
141
|
line = ref[i];
|
142
|
-
logger(`${itemPrefix} ${escapeStr(line)}`);
|
142
|
+
logger(`${itemPrefix} '${escapeStr(line)}'`);
|
143
143
|
}
|
144
144
|
}
|
145
145
|
} else {
|