@jdeighan/coffee-utils 10.0.6 → 10.0.9
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 +34 -9
- package/src/block_utils.js +39 -12
- package/src/coffee_utils.coffee +16 -0
- package/src/coffee_utils.js +15 -0
- package/src/indent_utils.coffee +6 -2
- package/src/indent_utils.js +7 -2
package/package.json
CHANGED
package/src/block_utils.coffee
CHANGED
@@ -5,7 +5,7 @@ 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
|
# ---------------------------------------------------------------------------
|
@@ -28,20 +28,45 @@ export blockToArray = (block) ->
|
|
28
28
|
# ---------------------------------------------------------------------------
|
29
29
|
# toArray - split a block or array into lines w/o newlines
|
30
30
|
|
31
|
-
export toArray = (item) ->
|
31
|
+
export toArray = (item, option=undef) ->
|
32
|
+
# --- Valid options:
|
33
|
+
# 'noEmptyLines'
|
34
|
+
# 'noLeadingEmptyLines'
|
32
35
|
|
33
36
|
if isString(item)
|
34
|
-
|
37
|
+
lLines = item.split(/\r?\n/)
|
35
38
|
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
|
39
|
+
lLines = item
|
42
40
|
else
|
43
41
|
croak "Not a string or array"
|
44
42
|
|
43
|
+
# --- We need to ensure that no strings contain newlines
|
44
|
+
# and possibly remove empty lines
|
45
|
+
lNewLines = []
|
46
|
+
nonEmptyFound = false
|
47
|
+
for line in lLines
|
48
|
+
if isEmpty(line)
|
49
|
+
if (option == 'noEmptyLines') \
|
50
|
+
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
51
|
+
pass
|
52
|
+
else
|
53
|
+
lNewLines.push ''
|
54
|
+
else if (line.indexOf("\n") > -1)
|
55
|
+
for substr in toArray(line)
|
56
|
+
if isEmpty(substr)
|
57
|
+
if (option == 'noEmptyLines') \
|
58
|
+
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
59
|
+
pass
|
60
|
+
else
|
61
|
+
lNewLines.push ''
|
62
|
+
else
|
63
|
+
nonEmptyFound = true
|
64
|
+
lNewLines.push substr
|
65
|
+
else
|
66
|
+
nonEmptyFound = true
|
67
|
+
lNewLines.push line
|
68
|
+
return lNewLines
|
69
|
+
|
45
70
|
# ---------------------------------------------------------------------------
|
46
71
|
# arrayToBlock - block will have no trailing whitespace
|
47
72
|
|
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,
|
@@ -40,25 +41,51 @@ export var blockToArray = function(block) {
|
|
40
41
|
|
41
42
|
// ---------------------------------------------------------------------------
|
42
43
|
// toArray - split a block or array into lines w/o newlines
|
43
|
-
export var toArray = function(item) {
|
44
|
-
var i, j, lLines, len1, len2,
|
44
|
+
export var toArray = function(item, option = undef) {
|
45
|
+
var i, j, lLines, lNewLines, len1, len2, line, nonEmptyFound, ref, substr;
|
46
|
+
// --- Valid options:
|
47
|
+
// 'noEmptyLines'
|
48
|
+
// 'noLeadingEmptyLines'
|
45
49
|
if (isString(item)) {
|
46
|
-
|
50
|
+
lLines = item.split(/\r?\n/);
|
47
51
|
} else if (isArray(item)) {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
52
|
+
lLines = item;
|
53
|
+
} else {
|
54
|
+
croak("Not a string or array");
|
55
|
+
}
|
56
|
+
// --- We need to ensure that no strings contain newlines
|
57
|
+
// and possibly remove empty lines
|
58
|
+
lNewLines = [];
|
59
|
+
nonEmptyFound = false;
|
60
|
+
for (i = 0, len1 = lLines.length; i < len1; i++) {
|
61
|
+
line = lLines[i];
|
62
|
+
if (isEmpty(line)) {
|
63
|
+
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
64
|
+
pass;
|
65
|
+
} else {
|
66
|
+
lNewLines.push('');
|
67
|
+
}
|
68
|
+
} else if (line.indexOf("\n") > -1) {
|
69
|
+
ref = toArray(line);
|
53
70
|
for (j = 0, len2 = ref.length; j < len2; j++) {
|
54
71
|
substr = ref[j];
|
55
|
-
|
72
|
+
if (isEmpty(substr)) {
|
73
|
+
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
74
|
+
pass;
|
75
|
+
} else {
|
76
|
+
lNewLines.push('');
|
77
|
+
}
|
78
|
+
} else {
|
79
|
+
nonEmptyFound = true;
|
80
|
+
lNewLines.push(substr);
|
81
|
+
}
|
56
82
|
}
|
83
|
+
} else {
|
84
|
+
nonEmptyFound = true;
|
85
|
+
lNewLines.push(line);
|
57
86
|
}
|
58
|
-
return lLines;
|
59
|
-
} else {
|
60
|
-
return croak("Not a string or array");
|
61
87
|
}
|
88
|
+
return lNewLines;
|
62
89
|
};
|
63
90
|
|
64
91
|
// ---------------------------------------------------------------------------
|
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,9 +98,12 @@ 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)
|
104
|
+
return ''
|
105
|
+
else
|
106
|
+
return []
|
103
107
|
|
104
108
|
# --- determine what to remove from beginning of each line
|
105
109
|
if defined(level)
|
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,9 +117,13 @@ 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)) {
|
123
|
+
return '';
|
124
|
+
} else {
|
125
|
+
return [];
|
126
|
+
}
|
122
127
|
}
|
123
128
|
// --- determine what to remove from beginning of each line
|
124
129
|
if (defined(level)) {
|