@jdeighan/coffee-utils 8.0.13 → 8.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/coffee_utils.coffee +24 -11
- package/src/coffee_utils.js +30 -10
- package/src/indent_utils.coffee +28 -18
- package/src/indent_utils.js +36 -24
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "8.0.
|
4
|
+
"version": "8.0.16",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -55,6 +55,6 @@
|
|
55
55
|
"svelte": "^3.49.0"
|
56
56
|
},
|
57
57
|
"devDependencies": {
|
58
|
-
"@jdeighan/unit-tester": "^2.0.
|
58
|
+
"@jdeighan/unit-tester": "^2.0.16"
|
59
59
|
}
|
60
60
|
}
|
package/src/coffee_utils.coffee
CHANGED
@@ -151,12 +151,6 @@ export isBoolean = (x) ->
|
|
151
151
|
|
152
152
|
# ---------------------------------------------------------------------------
|
153
153
|
|
154
|
-
export isNumber = (x) ->
|
155
|
-
|
156
|
-
return typeof x == 'number' || x instanceof Number
|
157
|
-
|
158
|
-
# ---------------------------------------------------------------------------
|
159
|
-
|
160
154
|
export isObject = (x) ->
|
161
155
|
|
162
156
|
return (typeof x == 'object') \
|
@@ -280,14 +274,33 @@ export isRegExp = (x) ->
|
|
280
274
|
|
281
275
|
# ---------------------------------------------------------------------------
|
282
276
|
|
283
|
-
export
|
277
|
+
export isNumber = (x, hOptions={}) ->
|
278
|
+
|
279
|
+
result = (typeof x == 'number') || (x instanceof Number)
|
280
|
+
if result
|
281
|
+
if defined(hOptions.min) && (x < hOptions.min)
|
282
|
+
result = false
|
283
|
+
if defined(hOptions.max) && (x > hOptions.max)
|
284
|
+
result = false
|
285
|
+
return result
|
286
|
+
|
287
|
+
# ---------------------------------------------------------------------------
|
288
|
+
|
289
|
+
export isInteger = (x, hOptions={}) ->
|
284
290
|
|
285
291
|
if (typeof x == 'number')
|
286
|
-
|
287
|
-
else if (
|
288
|
-
|
292
|
+
result = Number.isInteger(x)
|
293
|
+
else if (x instanceof Number)
|
294
|
+
result = Number.isInteger(x.valueOf())
|
289
295
|
else
|
290
|
-
|
296
|
+
result = false
|
297
|
+
|
298
|
+
if result
|
299
|
+
if defined(hOptions.min) && (x < hOptions.min)
|
300
|
+
result = false
|
301
|
+
if defined(hOptions.max) && (x > hOptions.max)
|
302
|
+
result = false
|
303
|
+
return result
|
291
304
|
|
292
305
|
# ---------------------------------------------------------------------------
|
293
306
|
|
package/src/coffee_utils.js
CHANGED
@@ -144,11 +144,6 @@ export var isBoolean = function(x) {
|
|
144
144
|
return typeof x === 'boolean';
|
145
145
|
};
|
146
146
|
|
147
|
-
// ---------------------------------------------------------------------------
|
148
|
-
export var isNumber = function(x) {
|
149
|
-
return typeof x === 'number' || x instanceof Number;
|
150
|
-
};
|
151
|
-
|
152
147
|
// ---------------------------------------------------------------------------
|
153
148
|
export var isObject = function(x) {
|
154
149
|
return (typeof x === 'object') && !isString(x) && !isArray(x) && !isHash(x) && !isNumber(x);
|
@@ -280,14 +275,39 @@ export var isRegExp = function(x) {
|
|
280
275
|
};
|
281
276
|
|
282
277
|
// ---------------------------------------------------------------------------
|
283
|
-
export var
|
278
|
+
export var isNumber = function(x, hOptions = {}) {
|
279
|
+
var result;
|
280
|
+
result = (typeof x === 'number') || (x instanceof Number);
|
281
|
+
if (result) {
|
282
|
+
if (defined(hOptions.min) && (x < hOptions.min)) {
|
283
|
+
result = false;
|
284
|
+
}
|
285
|
+
if (defined(hOptions.max) && (x > hOptions.max)) {
|
286
|
+
result = false;
|
287
|
+
}
|
288
|
+
}
|
289
|
+
return result;
|
290
|
+
};
|
291
|
+
|
292
|
+
// ---------------------------------------------------------------------------
|
293
|
+
export var isInteger = function(x, hOptions = {}) {
|
294
|
+
var result;
|
284
295
|
if (typeof x === 'number') {
|
285
|
-
|
286
|
-
} else if (
|
287
|
-
|
296
|
+
result = Number.isInteger(x);
|
297
|
+
} else if (x instanceof Number) {
|
298
|
+
result = Number.isInteger(x.valueOf());
|
288
299
|
} else {
|
289
|
-
|
300
|
+
result = false;
|
301
|
+
}
|
302
|
+
if (result) {
|
303
|
+
if (defined(hOptions.min) && (x < hOptions.min)) {
|
304
|
+
result = false;
|
305
|
+
}
|
306
|
+
if (defined(hOptions.max) && (x > hOptions.max)) {
|
307
|
+
result = false;
|
308
|
+
}
|
290
309
|
}
|
310
|
+
return result;
|
291
311
|
};
|
292
312
|
|
293
313
|
// ---------------------------------------------------------------------------
|
package/src/indent_utils.coffee
CHANGED
@@ -15,34 +15,45 @@ export splitPrefix = (line) ->
|
|
15
15
|
lMatches = line.match(/^(\s*)(.*)$/)
|
16
16
|
return [lMatches[1], lMatches[2]]
|
17
17
|
|
18
|
-
# ---------------------------------------------------------------------------
|
19
|
-
# NOTE: Currently, only TAB indentation is supported
|
20
18
|
# ---------------------------------------------------------------------------
|
21
19
|
# splitLine - separate a line into [level, line]
|
22
20
|
|
23
|
-
export splitLine = (line) ->
|
21
|
+
export splitLine = (line, oneIndent="\t") ->
|
24
22
|
|
25
23
|
[prefix, str] = splitPrefix(line)
|
26
|
-
return [indentLevel(prefix), str]
|
24
|
+
return [indentLevel(prefix, oneIndent), str]
|
27
25
|
|
28
26
|
# ---------------------------------------------------------------------------
|
29
27
|
# indentation - return appropriate indentation string for given level
|
30
28
|
# export only to allow unit testing
|
31
29
|
|
32
|
-
export indentation = (level) ->
|
30
|
+
export indentation = (level, oneIndent="\t") ->
|
33
31
|
|
34
32
|
assert (level >= 0), "indentation(): negative level"
|
35
|
-
return
|
33
|
+
return oneIndent.repeat(level)
|
36
34
|
|
37
35
|
# ---------------------------------------------------------------------------
|
38
36
|
# indentLevel - determine indent level of a string
|
39
37
|
# it's OK if the string is ONLY indentation
|
40
38
|
|
41
|
-
export indentLevel = (line) ->
|
39
|
+
export indentLevel = (line, oneIndent="\t") ->
|
40
|
+
|
41
|
+
len = oneIndent.length
|
42
|
+
|
43
|
+
# --- This will always match
|
44
|
+
if lMatches = line.match(/^(\s*)/)
|
45
|
+
prefix = lMatches[1]
|
46
|
+
prefixLen = prefix.length
|
47
|
+
|
48
|
+
remain = prefixLen % len
|
49
|
+
if (remain != 0)
|
50
|
+
throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
|
51
|
+
|
52
|
+
level = prefixLen / len
|
53
|
+
if (prefix != oneIndent.repeat(level))
|
54
|
+
throw new Error("prefix #{OL(prefix)} not a mult of #{OL(oneIndent)}")
|
42
55
|
|
43
|
-
|
44
|
-
lMatches = line.match(/^\t*/)
|
45
|
-
return lMatches[0].length
|
56
|
+
return level
|
46
57
|
|
47
58
|
# ---------------------------------------------------------------------------
|
48
59
|
# isUndented - true iff indentLevel(line) == 0
|
@@ -50,19 +61,19 @@ export indentLevel = (line) ->
|
|
50
61
|
export isUndented = (line) ->
|
51
62
|
|
52
63
|
assert isString(line), "non-string #{OL(line)}"
|
53
|
-
lMatches = line.match(/^\
|
64
|
+
lMatches = line.match(/^\s*/)
|
54
65
|
return (lMatches[0].length == 0)
|
55
66
|
|
56
67
|
# ---------------------------------------------------------------------------
|
57
68
|
# indented - add indentation to each string in a block
|
58
69
|
|
59
|
-
export indented = (input, level=1) ->
|
70
|
+
export indented = (input, level=1, oneIndent="\t") ->
|
60
71
|
|
61
72
|
assert (level >= 0), "indented(): negative level"
|
62
73
|
if level == 0
|
63
74
|
return input
|
64
75
|
|
65
|
-
toAdd = indentation(level)
|
76
|
+
toAdd = indentation(level, oneIndent)
|
66
77
|
if isArray(input)
|
67
78
|
lInputLines = input
|
68
79
|
else
|
@@ -81,7 +92,7 @@ export indented = (input, level=1) ->
|
|
81
92
|
# indentation is removed
|
82
93
|
# - returns same type as text, i.e. either string or array
|
83
94
|
|
84
|
-
export undented = (text, level=undef) ->
|
95
|
+
export undented = (text, level=undef, oneIndent="\t") ->
|
85
96
|
|
86
97
|
if defined(level) && (level==0)
|
87
98
|
return text
|
@@ -102,7 +113,7 @@ export undented = (text, level=undef) ->
|
|
102
113
|
# --- determine what to remove from beginning of each line
|
103
114
|
if defined(level)
|
104
115
|
assert isInteger(level), "undented(): level must be an integer"
|
105
|
-
toRemove = indentation(level)
|
116
|
+
toRemove = indentation(level, oneIndent)
|
106
117
|
else
|
107
118
|
lMatches = lLines[0].match(/^\s*/)
|
108
119
|
toRemove = lMatches[0]
|
@@ -113,9 +124,8 @@ export undented = (text, level=undef) ->
|
|
113
124
|
if isEmpty(line)
|
114
125
|
lNewLines.push('')
|
115
126
|
else
|
116
|
-
|
117
|
-
"
|
118
|
-
from #{OL(text)}"
|
127
|
+
if (line.indexOf(toRemove) != 0)
|
128
|
+
throw new Error("remove #{OL(toRemove)} from #{OL(text)}")
|
119
129
|
lNewLines.push(line.substr(nToRemove))
|
120
130
|
|
121
131
|
if isString(text)
|
package/src/indent_utils.js
CHANGED
@@ -28,32 +28,42 @@ export var splitPrefix = function(line) {
|
|
28
28
|
return [lMatches[1], lMatches[2]];
|
29
29
|
};
|
30
30
|
|
31
|
-
// ---------------------------------------------------------------------------
|
32
|
-
// NOTE: Currently, only TAB indentation is supported
|
33
31
|
// ---------------------------------------------------------------------------
|
34
32
|
// splitLine - separate a line into [level, line]
|
35
|
-
export var splitLine = function(line) {
|
33
|
+
export var splitLine = function(line, oneIndent = "\t") {
|
36
34
|
var prefix, str;
|
37
35
|
[prefix, str] = splitPrefix(line);
|
38
|
-
return [indentLevel(prefix), str];
|
36
|
+
return [indentLevel(prefix, oneIndent), str];
|
39
37
|
};
|
40
38
|
|
41
39
|
// ---------------------------------------------------------------------------
|
42
40
|
// indentation - return appropriate indentation string for given level
|
43
41
|
// export only to allow unit testing
|
44
|
-
export var indentation = function(level) {
|
42
|
+
export var indentation = function(level, oneIndent = "\t") {
|
45
43
|
assert(level >= 0, "indentation(): negative level");
|
46
|
-
return
|
44
|
+
return oneIndent.repeat(level);
|
47
45
|
};
|
48
46
|
|
49
47
|
// ---------------------------------------------------------------------------
|
50
48
|
// indentLevel - determine indent level of a string
|
51
49
|
// it's OK if the string is ONLY indentation
|
52
|
-
export var indentLevel = function(line) {
|
53
|
-
var lMatches;
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
export var indentLevel = function(line, oneIndent = "\t") {
|
51
|
+
var lMatches, len, level, prefix, prefixLen, remain;
|
52
|
+
len = oneIndent.length;
|
53
|
+
// --- This will always match
|
54
|
+
if (lMatches = line.match(/^(\s*)/)) {
|
55
|
+
prefix = lMatches[1];
|
56
|
+
prefixLen = prefix.length;
|
57
|
+
}
|
58
|
+
remain = prefixLen % len;
|
59
|
+
if (remain !== 0) {
|
60
|
+
throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
|
61
|
+
}
|
62
|
+
level = prefixLen / len;
|
63
|
+
if (prefix !== oneIndent.repeat(level)) {
|
64
|
+
throw new Error(`prefix ${OL(prefix)} not a mult of ${OL(oneIndent)}`);
|
65
|
+
}
|
66
|
+
return level;
|
57
67
|
};
|
58
68
|
|
59
69
|
// ---------------------------------------------------------------------------
|
@@ -61,28 +71,28 @@ export var indentLevel = function(line) {
|
|
61
71
|
export var isUndented = function(line) {
|
62
72
|
var lMatches;
|
63
73
|
assert(isString(line), `non-string ${OL(line)}`);
|
64
|
-
lMatches = line.match(/^\
|
74
|
+
lMatches = line.match(/^\s*/);
|
65
75
|
return lMatches[0].length === 0;
|
66
76
|
};
|
67
77
|
|
68
78
|
// ---------------------------------------------------------------------------
|
69
79
|
// indented - add indentation to each string in a block
|
70
|
-
export var indented = function(input, level = 1) {
|
80
|
+
export var indented = function(input, level = 1, oneIndent = "\t") {
|
71
81
|
var lInputLines, lLines, line, toAdd;
|
72
82
|
assert(level >= 0, "indented(): negative level");
|
73
83
|
if (level === 0) {
|
74
84
|
return input;
|
75
85
|
}
|
76
|
-
toAdd = indentation(level);
|
86
|
+
toAdd = indentation(level, oneIndent);
|
77
87
|
if (isArray(input)) {
|
78
88
|
lInputLines = input;
|
79
89
|
} else {
|
80
90
|
lInputLines = blockToArray(input);
|
81
91
|
}
|
82
92
|
lLines = (function() {
|
83
|
-
var i,
|
93
|
+
var i, len1, results;
|
84
94
|
results = [];
|
85
|
-
for (i = 0,
|
95
|
+
for (i = 0, len1 = lInputLines.length; i < len1; i++) {
|
86
96
|
line = lInputLines[i];
|
87
97
|
if (isEmpty(line)) {
|
88
98
|
results.push("");
|
@@ -100,8 +110,8 @@ export var indented = function(input, level = 1) {
|
|
100
110
|
// - unless level is set, in which case exactly that
|
101
111
|
// indentation is removed
|
102
112
|
// - returns same type as text, i.e. either string or array
|
103
|
-
export var undented = function(text, level = undef) {
|
104
|
-
var i, j, lLines, lMatches, lNewLines,
|
113
|
+
export var undented = function(text, level = undef, oneIndent = "\t") {
|
114
|
+
var i, j, lLines, lMatches, lNewLines, len1, len2, line, nToRemove, toRemove;
|
105
115
|
if (defined(level) && (level === 0)) {
|
106
116
|
return text;
|
107
117
|
}
|
@@ -112,7 +122,7 @@ export var undented = function(text, level = undef) {
|
|
112
122
|
}
|
113
123
|
} else if (isArray(text)) {
|
114
124
|
lLines = text;
|
115
|
-
for (i = 0,
|
125
|
+
for (i = 0, len1 = lLines.length; i < len1; i++) {
|
116
126
|
line = lLines[i];
|
117
127
|
assert(isString(line), "undented(): array not all strings");
|
118
128
|
}
|
@@ -125,19 +135,21 @@ export var undented = function(text, level = undef) {
|
|
125
135
|
// --- determine what to remove from beginning of each line
|
126
136
|
if (defined(level)) {
|
127
137
|
assert(isInteger(level), "undented(): level must be an integer");
|
128
|
-
toRemove = indentation(level);
|
138
|
+
toRemove = indentation(level, oneIndent);
|
129
139
|
} else {
|
130
140
|
lMatches = lLines[0].match(/^\s*/);
|
131
141
|
toRemove = lMatches[0];
|
132
142
|
}
|
133
143
|
nToRemove = indentLevel(toRemove);
|
134
144
|
lNewLines = [];
|
135
|
-
for (j = 0,
|
145
|
+
for (j = 0, len2 = lLines.length; j < len2; j++) {
|
136
146
|
line = lLines[j];
|
137
147
|
if (isEmpty(line)) {
|
138
148
|
lNewLines.push('');
|
139
149
|
} else {
|
140
|
-
|
150
|
+
if (line.indexOf(toRemove) !== 0) {
|
151
|
+
throw new Error(`remove ${OL(toRemove)} from ${OL(text)}`);
|
152
|
+
}
|
141
153
|
lNewLines.push(line.substr(nToRemove));
|
142
154
|
}
|
143
155
|
}
|
@@ -153,10 +165,10 @@ export var undented = function(text, level = undef) {
|
|
153
165
|
// if numSpaces is not defined, then the first line
|
154
166
|
// that contains at least one space sets it
|
155
167
|
export var tabify = function(str, numSpaces = undef) {
|
156
|
-
var _, i, lLines,
|
168
|
+
var _, i, lLines, len1, level, prefix, prefixLen, ref, result, theRest;
|
157
169
|
lLines = [];
|
158
170
|
ref = blockToArray(str);
|
159
|
-
for (i = 0,
|
171
|
+
for (i = 0, len1 = ref.length; i < len1; i++) {
|
160
172
|
str = ref[i];
|
161
173
|
[_, prefix, theRest] = str.match(/^(\s*)(.*)$/);
|
162
174
|
prefixLen = prefix.length;
|