@jdeighan/coffee-utils 13.0.14 → 13.0.16
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +3 -3
- package/src/block.coffee +4 -60
- package/src/block.js +6 -70
- package/src/indent.coffee +11 -9
- package/src/indent.js +16 -20
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "13.0.
|
4
|
+
"version": "13.0.16",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -50,13 +50,13 @@
|
|
50
50
|
},
|
51
51
|
"homepage": "https://github.com/johndeighan/coffee-utils#readme",
|
52
52
|
"dependencies": {
|
53
|
-
"@jdeighan/base-utils": "^2.0.
|
53
|
+
"@jdeighan/base-utils": "^2.0.12",
|
54
54
|
"cross-env": "^7.0.3",
|
55
55
|
"n-readlines": "^1.0.1",
|
56
56
|
"readline-sync": "^1.4.10",
|
57
57
|
"svelte": "^3.55.0"
|
58
58
|
},
|
59
59
|
"devDependencies": {
|
60
|
-
"@jdeighan/unit-tester": "^3.0.
|
60
|
+
"@jdeighan/unit-tester": "^3.0.12"
|
61
61
|
}
|
62
62
|
}
|
package/src/block.coffee
CHANGED
@@ -4,72 +4,16 @@ import fs from 'fs'
|
|
4
4
|
import readline from 'readline'
|
5
5
|
|
6
6
|
import {assert, croak} from '@jdeighan/base-utils'
|
7
|
-
import {
|
7
|
+
import {
|
8
|
+
blockToArray, arrayToBlock, toArray, toBlock,
|
9
|
+
} from '@jdeighan/base-utils/utils'
|
8
10
|
import {
|
9
11
|
undef, pass, defined, notdefined,
|
10
12
|
isEmpty, isString, isArray, nonEmpty, isArrayOfStrings,
|
11
13
|
rtrim, OL,
|
12
14
|
} from '@jdeighan/coffee-utils'
|
13
15
|
|
14
|
-
export {blockToArray, arrayToBlock}
|
15
|
-
|
16
|
-
# ---------------------------------------------------------------------------
|
17
|
-
# toArray - split a block or array into lines w/o newlines
|
18
|
-
|
19
|
-
export toArray = (item, option=undef) ->
|
20
|
-
# --- Valid options:
|
21
|
-
# 'noEmptyLines'
|
22
|
-
# 'noLeadingEmptyLines'
|
23
|
-
|
24
|
-
if isString(item)
|
25
|
-
lLines = item.split(/\r?\n/)
|
26
|
-
else if isArray(item)
|
27
|
-
lLines = item
|
28
|
-
else
|
29
|
-
croak "Not a string or array"
|
30
|
-
|
31
|
-
# --- We need to ensure that no strings contain newlines
|
32
|
-
# and possibly remove empty lines
|
33
|
-
lNewLines = []
|
34
|
-
nonEmptyFound = false
|
35
|
-
for line in lLines
|
36
|
-
if isEmpty(line)
|
37
|
-
if (option == 'noEmptyLines') \
|
38
|
-
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
39
|
-
pass
|
40
|
-
else
|
41
|
-
lNewLines.push ''
|
42
|
-
else if (line.indexOf("\n") > -1)
|
43
|
-
for substr in toArray(line)
|
44
|
-
if isEmpty(substr)
|
45
|
-
if (option == 'noEmptyLines') \
|
46
|
-
|| ((option == 'noLeadingEmptyLines') && ! nonEmptyFound)
|
47
|
-
pass
|
48
|
-
else
|
49
|
-
lNewLines.push ''
|
50
|
-
else
|
51
|
-
nonEmptyFound = true
|
52
|
-
lNewLines.push substr
|
53
|
-
else
|
54
|
-
nonEmptyFound = true
|
55
|
-
lNewLines.push line
|
56
|
-
return lNewLines
|
57
|
-
|
58
|
-
# ---------------------------------------------------------------------------
|
59
|
-
# toBlock - block may have trailing whitespace
|
60
|
-
# but undef items are ignored
|
61
|
-
|
62
|
-
export toBlock = (lLines) ->
|
63
|
-
|
64
|
-
if notdefined(lLines)
|
65
|
-
return undef
|
66
|
-
assert isArrayOfStrings(lLines),
|
67
|
-
"lLines is not an array of strings: #{OL(lLines)}"
|
68
|
-
lNewLines = []
|
69
|
-
for line in lLines
|
70
|
-
if defined(line)
|
71
|
-
lNewLines.push rtrim(line)
|
72
|
-
return lNewLines.join("\n")
|
16
|
+
export {blockToArray, arrayToBlock, toArray, toBlock}
|
73
17
|
|
74
18
|
# ---------------------------------------------------------------------------
|
75
19
|
|
package/src/block.js
CHANGED
@@ -11,7 +11,9 @@ import {
|
|
11
11
|
|
12
12
|
import {
|
13
13
|
blockToArray,
|
14
|
-
arrayToBlock
|
14
|
+
arrayToBlock,
|
15
|
+
toArray,
|
16
|
+
toBlock
|
15
17
|
} from '@jdeighan/base-utils/utils';
|
16
18
|
|
17
19
|
import {
|
@@ -30,75 +32,9 @@ import {
|
|
30
32
|
|
31
33
|
export {
|
32
34
|
blockToArray,
|
33
|
-
arrayToBlock
|
34
|
-
|
35
|
-
|
36
|
-
// ---------------------------------------------------------------------------
|
37
|
-
// toArray - split a block or array into lines w/o newlines
|
38
|
-
export var toArray = function(item, option = undef) {
|
39
|
-
var i, j, lLines, lNewLines, len, len1, line, nonEmptyFound, ref, substr;
|
40
|
-
// --- Valid options:
|
41
|
-
// 'noEmptyLines'
|
42
|
-
// 'noLeadingEmptyLines'
|
43
|
-
if (isString(item)) {
|
44
|
-
lLines = item.split(/\r?\n/);
|
45
|
-
} else if (isArray(item)) {
|
46
|
-
lLines = item;
|
47
|
-
} else {
|
48
|
-
croak("Not a string or array");
|
49
|
-
}
|
50
|
-
// --- We need to ensure that no strings contain newlines
|
51
|
-
// and possibly remove empty lines
|
52
|
-
lNewLines = [];
|
53
|
-
nonEmptyFound = false;
|
54
|
-
for (i = 0, len = lLines.length; i < len; i++) {
|
55
|
-
line = lLines[i];
|
56
|
-
if (isEmpty(line)) {
|
57
|
-
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
58
|
-
pass;
|
59
|
-
} else {
|
60
|
-
lNewLines.push('');
|
61
|
-
}
|
62
|
-
} else if (line.indexOf("\n") > -1) {
|
63
|
-
ref = toArray(line);
|
64
|
-
for (j = 0, len1 = ref.length; j < len1; j++) {
|
65
|
-
substr = ref[j];
|
66
|
-
if (isEmpty(substr)) {
|
67
|
-
if ((option === 'noEmptyLines') || ((option === 'noLeadingEmptyLines') && !nonEmptyFound)) {
|
68
|
-
pass;
|
69
|
-
} else {
|
70
|
-
lNewLines.push('');
|
71
|
-
}
|
72
|
-
} else {
|
73
|
-
nonEmptyFound = true;
|
74
|
-
lNewLines.push(substr);
|
75
|
-
}
|
76
|
-
}
|
77
|
-
} else {
|
78
|
-
nonEmptyFound = true;
|
79
|
-
lNewLines.push(line);
|
80
|
-
}
|
81
|
-
}
|
82
|
-
return lNewLines;
|
83
|
-
};
|
84
|
-
|
85
|
-
// ---------------------------------------------------------------------------
|
86
|
-
// toBlock - block may have trailing whitespace
|
87
|
-
// but undef items are ignored
|
88
|
-
export var toBlock = function(lLines) {
|
89
|
-
var i, lNewLines, len, line;
|
90
|
-
if (notdefined(lLines)) {
|
91
|
-
return undef;
|
92
|
-
}
|
93
|
-
assert(isArrayOfStrings(lLines), `lLines is not an array of strings: ${OL(lLines)}`);
|
94
|
-
lNewLines = [];
|
95
|
-
for (i = 0, len = lLines.length; i < len; i++) {
|
96
|
-
line = lLines[i];
|
97
|
-
if (defined(line)) {
|
98
|
-
lNewLines.push(rtrim(line));
|
99
|
-
}
|
100
|
-
}
|
101
|
-
return lNewLines.join("\n");
|
35
|
+
arrayToBlock,
|
36
|
+
toArray,
|
37
|
+
toBlock
|
102
38
|
};
|
103
39
|
|
104
40
|
// ---------------------------------------------------------------------------
|
package/src/indent.coffee
CHANGED
@@ -90,32 +90,34 @@ export isUndented = (line) ->
|
|
90
90
|
|
91
91
|
export indented = (input, level=1, oneIndent="\t") ->
|
92
92
|
|
93
|
+
# --- level can be a string, in which case it is
|
94
|
+
# pre-pended to each line of input
|
93
95
|
if isString(level)
|
94
|
-
|
95
|
-
if (level == "")
|
96
|
+
if (level == '')
|
96
97
|
return input
|
97
98
|
toAdd = level
|
98
99
|
else if isInteger(level)
|
99
|
-
assert (level >= 0), "indented(): negative level"
|
100
100
|
if (level == 0)
|
101
101
|
return input
|
102
|
+
assert (level > 0), "Invalid level #{OL(level)}"
|
102
103
|
toAdd = indentation(level, oneIndent)
|
103
104
|
else
|
104
|
-
croak "level
|
105
|
+
croak "Invalid level #{OL(level)}"
|
105
106
|
|
106
107
|
# --- NOTE: toArray(input) just returns input if it's an array
|
107
108
|
# else it splits the string into an array of lines
|
108
|
-
lLines =
|
109
|
+
lLines = []
|
110
|
+
for line in toArray(input)
|
109
111
|
if isEmpty(line)
|
110
|
-
|
112
|
+
lLines.push ''
|
111
113
|
else
|
112
|
-
"#{toAdd}#{line}"
|
114
|
+
lLines.push "#{toAdd}#{line}"
|
115
|
+
|
113
116
|
if isArray(input)
|
114
117
|
return lLines
|
115
118
|
else if isString(input)
|
116
119
|
return toBlock(lLines)
|
117
|
-
|
118
|
-
croak "Invalid input; #{OL(input)}"
|
120
|
+
croak "Invalid input; #{OL(input)}"
|
119
121
|
|
120
122
|
# ---------------------------------------------------------------------------
|
121
123
|
# undented - string with 1st line indentation removed for each line
|
package/src/indent.js
CHANGED
@@ -102,45 +102,41 @@ export var isUndented = function(line) {
|
|
102
102
|
// indented - add indentation to each string in a block or array
|
103
103
|
// - returns the same type as input, i.e. array or string
|
104
104
|
export var indented = function(input, level = 1, oneIndent = "\t") {
|
105
|
-
var lLines, line, toAdd;
|
105
|
+
var i, lLines, len1, line, ref, toAdd;
|
106
|
+
// --- level can be a string, in which case it is
|
107
|
+
// pre-pended to each line of input
|
106
108
|
if (isString(level)) {
|
107
|
-
|
108
|
-
if (level === "") {
|
109
|
+
if (level === '') {
|
109
110
|
return input;
|
110
111
|
}
|
111
112
|
toAdd = level;
|
112
113
|
} else if (isInteger(level)) {
|
113
|
-
assert(level >= 0, "indented(): negative level");
|
114
114
|
if (level === 0) {
|
115
115
|
return input;
|
116
116
|
}
|
117
|
+
assert(level > 0, `Invalid level ${OL(level)}`);
|
117
118
|
toAdd = indentation(level, oneIndent);
|
118
119
|
} else {
|
119
|
-
croak(
|
120
|
+
croak(`Invalid level ${OL(level)}`);
|
120
121
|
}
|
121
122
|
// --- NOTE: toArray(input) just returns input if it's an array
|
122
123
|
// else it splits the string into an array of lines
|
123
|
-
lLines =
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
} else {
|
132
|
-
results.push(`${toAdd}${line}`);
|
133
|
-
}
|
124
|
+
lLines = [];
|
125
|
+
ref = toArray(input);
|
126
|
+
for (i = 0, len1 = ref.length; i < len1; i++) {
|
127
|
+
line = ref[i];
|
128
|
+
if (isEmpty(line)) {
|
129
|
+
lLines.push('');
|
130
|
+
} else {
|
131
|
+
lLines.push(`${toAdd}${line}`);
|
134
132
|
}
|
135
|
-
|
136
|
-
})();
|
133
|
+
}
|
137
134
|
if (isArray(input)) {
|
138
135
|
return lLines;
|
139
136
|
} else if (isString(input)) {
|
140
137
|
return toBlock(lLines);
|
141
|
-
} else {
|
142
|
-
return croak(`Invalid input; ${OL(input)}`);
|
143
138
|
}
|
139
|
+
return croak(`Invalid input; ${OL(input)}`);
|
144
140
|
};
|
145
141
|
|
146
142
|
// ---------------------------------------------------------------------------
|