@jdeighan/coffee-utils 13.0.15 → 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/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
|
// ---------------------------------------------------------------------------
|