@jdeighan/coffee-utils 10.0.7 → 10.0.10
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/block_utils.coffee +42 -15
- package/src/block_utils.js +50 -20
- package/src/coffee_utils.coffee +16 -0
- package/src/coffee_utils.js +15 -0
- package/src/indent_utils.coffee +2 -1
- package/src/indent_utils.js +2 -1
package/package.json
CHANGED
package/src/block_utils.coffee
CHANGED
@@ -5,11 +5,12 @@ import readline from 'readline'
|
|
5
5
|
|
6
6
|
import {assert, error, croak} from '@jdeighan/unit-tester/utils'
|
7
7
|
import {
|
8
|
-
undef, defined, isEmpty, isString, isArray, nonEmpty, rtrim,
|
8
|
+
undef, pass, defined, isEmpty, isString, isArray, nonEmpty, rtrim,
|
9
9
|
} from '@jdeighan/coffee-utils'
|
10
10
|
|
11
11
|
# ---------------------------------------------------------------------------
|
12
12
|
# blockToArray - split a block into lines
|
13
|
+
# DEPRECATED - use toArray()
|
13
14
|
|
14
15
|
export blockToArray = (block) ->
|
15
16
|
|
@@ -28,22 +29,48 @@ export blockToArray = (block) ->
|
|
28
29
|
# ---------------------------------------------------------------------------
|
29
30
|
# toArray - split a block or array into lines w/o newlines
|
30
31
|
|
31
|
-
export toArray = (item) ->
|
32
|
+
export toArray = (item, option=undef) ->
|
33
|
+
# --- Valid options:
|
34
|
+
# 'noEmptyLines'
|
35
|
+
# 'noLeadingEmptyLines'
|
32
36
|
|
33
37
|
if isString(item)
|
34
|
-
|
38
|
+
lLines = item.split(/\r?\n/)
|
35
39
|
else if isArray(item)
|
36
|
-
|
37
|
-
lLines = []
|
38
|
-
for str in item
|
39
|
-
for substr in toArray(str)
|
40
|
-
lLines.push substr
|
41
|
-
return lLines
|
40
|
+
lLines = item
|
42
41
|
else
|
43
42
|
croak "Not a string or array"
|
44
43
|
|
44
|
+
# --- We need to ensure that no strings contain newlines
|
45
|
+
# and possibly remove empty lines
|
46
|
+
lNewLines = []
|
47
|
+
nonEmptyFound = false
|
48
|
+
for line in lLines
|
49
|
+
if isEmpty(line)
|
50
|
+
if (option == 'noEmptyLines') \
|
51
|
+
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
52
|
+
pass
|
53
|
+
else
|
54
|
+
lNewLines.push ''
|
55
|
+
else if (line.indexOf("\n") > -1)
|
56
|
+
for substr in toArray(line)
|
57
|
+
if isEmpty(substr)
|
58
|
+
if (option == 'noEmptyLines') \
|
59
|
+
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
60
|
+
pass
|
61
|
+
else
|
62
|
+
lNewLines.push ''
|
63
|
+
else
|
64
|
+
nonEmptyFound = true
|
65
|
+
lNewLines.push substr
|
66
|
+
else
|
67
|
+
nonEmptyFound = true
|
68
|
+
lNewLines.push line
|
69
|
+
return lNewLines
|
70
|
+
|
45
71
|
# ---------------------------------------------------------------------------
|
46
|
-
# arrayToBlock - block will have no trailing whitespace
|
72
|
+
# arrayToBlock - block and lines in block will have no trailing whitespace
|
73
|
+
# DEPRECATED - use toBlock()
|
47
74
|
|
48
75
|
export arrayToBlock = (lLines) ->
|
49
76
|
|
@@ -65,11 +92,11 @@ export toBlock = (lLines) ->
|
|
65
92
|
if (lLines == undef)
|
66
93
|
return undef
|
67
94
|
assert isArray(lLines), "lLines is not an array"
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
95
|
+
lNewLines = []
|
96
|
+
for line in lLines
|
97
|
+
if defined(line)
|
98
|
+
lNewLines.push rtrim(line)
|
99
|
+
return lNewLines.join("\n")
|
73
100
|
|
74
101
|
# ---------------------------------------------------------------------------
|
75
102
|
|
package/src/block_utils.js
CHANGED
@@ -12,6 +12,7 @@ import {
|
|
12
12
|
|
13
13
|
import {
|
14
14
|
undef,
|
15
|
+
pass,
|
15
16
|
defined,
|
16
17
|
isEmpty,
|
17
18
|
isString,
|
@@ -22,6 +23,7 @@ import {
|
|
22
23
|
|
23
24
|
// ---------------------------------------------------------------------------
|
24
25
|
// blockToArray - split a block into lines
|
26
|
+
// DEPRECATED - use toArray()
|
25
27
|
export var blockToArray = function(block) {
|
26
28
|
var lLines, len;
|
27
29
|
if (isEmpty(block)) {
|
@@ -40,29 +42,56 @@ export var blockToArray = function(block) {
|
|
40
42
|
|
41
43
|
// ---------------------------------------------------------------------------
|
42
44
|
// toArray - split a block or array into lines w/o newlines
|
43
|
-
export var toArray = function(item) {
|
44
|
-
var i, j, lLines, len1, len2,
|
45
|
+
export var toArray = function(item, option = undef) {
|
46
|
+
var i, j, lLines, lNewLines, len1, len2, line, nonEmptyFound, ref, substr;
|
47
|
+
// --- Valid options:
|
48
|
+
// 'noEmptyLines'
|
49
|
+
// 'noLeadingEmptyLines'
|
45
50
|
if (isString(item)) {
|
46
|
-
|
51
|
+
lLines = item.split(/\r?\n/);
|
47
52
|
} else if (isArray(item)) {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
lLines = item;
|
54
|
+
} else {
|
55
|
+
croak("Not a string or array");
|
56
|
+
}
|
57
|
+
// --- We need to ensure that no strings contain newlines
|
58
|
+
// and possibly remove empty lines
|
59
|
+
lNewLines = [];
|
60
|
+
nonEmptyFound = false;
|
61
|
+
for (i = 0, len1 = lLines.length; i < len1; i++) {
|
62
|
+
line = lLines[i];
|
63
|
+
if (isEmpty(line)) {
|
64
|
+
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
65
|
+
pass;
|
66
|
+
} else {
|
67
|
+
lNewLines.push('');
|
68
|
+
}
|
69
|
+
} else if (line.indexOf("\n") > -1) {
|
70
|
+
ref = toArray(line);
|
53
71
|
for (j = 0, len2 = ref.length; j < len2; j++) {
|
54
72
|
substr = ref[j];
|
55
|
-
|
73
|
+
if (isEmpty(substr)) {
|
74
|
+
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
75
|
+
pass;
|
76
|
+
} else {
|
77
|
+
lNewLines.push('');
|
78
|
+
}
|
79
|
+
} else {
|
80
|
+
nonEmptyFound = true;
|
81
|
+
lNewLines.push(substr);
|
82
|
+
}
|
56
83
|
}
|
84
|
+
} else {
|
85
|
+
nonEmptyFound = true;
|
86
|
+
lNewLines.push(line);
|
57
87
|
}
|
58
|
-
return lLines;
|
59
|
-
} else {
|
60
|
-
return croak("Not a string or array");
|
61
88
|
}
|
89
|
+
return lNewLines;
|
62
90
|
};
|
63
91
|
|
64
92
|
// ---------------------------------------------------------------------------
|
65
|
-
// arrayToBlock - block will have no trailing whitespace
|
93
|
+
// arrayToBlock - block and lines in block will have no trailing whitespace
|
94
|
+
// DEPRECATED - use toBlock()
|
66
95
|
export var arrayToBlock = function(lLines) {
|
67
96
|
if (lLines === undef) {
|
68
97
|
return undef;
|
@@ -82,18 +111,19 @@ export var arrayToBlock = function(lLines) {
|
|
82
111
|
// toBlock - block may have trailing whitespace
|
83
112
|
// but undef items are ignored
|
84
113
|
export var toBlock = function(lLines) {
|
114
|
+
var i, lNewLines, len1, line;
|
85
115
|
if (lLines === undef) {
|
86
116
|
return undef;
|
87
117
|
}
|
88
118
|
assert(isArray(lLines), "lLines is not an array");
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
return lLines.join('\n');
|
119
|
+
lNewLines = [];
|
120
|
+
for (i = 0, len1 = lLines.length; i < len1; i++) {
|
121
|
+
line = lLines[i];
|
122
|
+
if (defined(line)) {
|
123
|
+
lNewLines.push(rtrim(line));
|
124
|
+
}
|
96
125
|
}
|
126
|
+
return lNewLines.join("\n");
|
97
127
|
};
|
98
128
|
|
99
129
|
// ---------------------------------------------------------------------------
|
package/src/coffee_utils.coffee
CHANGED
@@ -223,6 +223,22 @@ export nonEmpty = (x) ->
|
|
223
223
|
|
224
224
|
# ---------------------------------------------------------------------------
|
225
225
|
|
226
|
+
export notInArray = (lItems, item) ->
|
227
|
+
|
228
|
+
return (lItems.indexOf(item) == -1)
|
229
|
+
|
230
|
+
# ---------------------------------------------------------------------------
|
231
|
+
|
232
|
+
export pushCond = (lItems, item, doPush=notInArray) ->
|
233
|
+
|
234
|
+
if doPush(lItems, item)
|
235
|
+
lItems.push item
|
236
|
+
return true
|
237
|
+
else
|
238
|
+
return false
|
239
|
+
|
240
|
+
# ---------------------------------------------------------------------------
|
241
|
+
|
226
242
|
export words = (str) ->
|
227
243
|
|
228
244
|
return str.trim().split(/\s+/)
|
package/src/coffee_utils.js
CHANGED
@@ -236,6 +236,21 @@ export var nonEmpty = function(x) {
|
|
236
236
|
}
|
237
237
|
};
|
238
238
|
|
239
|
+
// ---------------------------------------------------------------------------
|
240
|
+
export var notInArray = function(lItems, item) {
|
241
|
+
return lItems.indexOf(item) === -1;
|
242
|
+
};
|
243
|
+
|
244
|
+
// ---------------------------------------------------------------------------
|
245
|
+
export var pushCond = function(lItems, item, doPush = notInArray) {
|
246
|
+
if (doPush(lItems, item)) {
|
247
|
+
lItems.push(item);
|
248
|
+
return true;
|
249
|
+
} else {
|
250
|
+
return false;
|
251
|
+
}
|
252
|
+
};
|
253
|
+
|
239
254
|
// ---------------------------------------------------------------------------
|
240
255
|
export var words = function(str) {
|
241
256
|
return str.trim().split(/\s+/);
|
package/src/indent_utils.coffee
CHANGED
@@ -88,6 +88,7 @@ export indented = (input, level=1, oneIndent="\t") ->
|
|
88
88
|
|
89
89
|
# ---------------------------------------------------------------------------
|
90
90
|
# undented - string with 1st line indentation removed for each line
|
91
|
+
# - ignore leading empty lines
|
91
92
|
# - unless level is set, in which case exactly that
|
92
93
|
# indentation is removed
|
93
94
|
# - returns same type as text, i.e. either string or array
|
@@ -97,7 +98,7 @@ export undented = (input, level=undef, oneIndent="\t") ->
|
|
97
98
|
if defined(level) && (level==0)
|
98
99
|
return input
|
99
100
|
|
100
|
-
lLines = toArray(input)
|
101
|
+
lLines = toArray(input, 'noLeadingEmptyLines')
|
101
102
|
if (lLines.length == 0)
|
102
103
|
if isString(input)
|
103
104
|
return ''
|
package/src/indent_utils.js
CHANGED
@@ -108,6 +108,7 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
|
|
108
108
|
|
109
109
|
// ---------------------------------------------------------------------------
|
110
110
|
// undented - string with 1st line indentation removed for each line
|
111
|
+
// - ignore leading empty lines
|
111
112
|
// - unless level is set, in which case exactly that
|
112
113
|
// indentation is removed
|
113
114
|
// - returns same type as text, i.e. either string or array
|
@@ -116,7 +117,7 @@ export var undented = function(input, level = undef, oneIndent = "\t") {
|
|
116
117
|
if (defined(level) && (level === 0)) {
|
117
118
|
return input;
|
118
119
|
}
|
119
|
-
lLines = toArray(input);
|
120
|
+
lLines = toArray(input, 'noLeadingEmptyLines');
|
120
121
|
if (lLines.length === 0) {
|
121
122
|
if (isString(input)) {
|
122
123
|
return '';
|