@jdeighan/coffee-utils 11.0.4 → 11.0.6
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 +4 -4
- package/src/Section.coffee +1 -1
- package/src/Section.js +4 -4
- package/src/SectionMap.coffee +3 -2
- package/src/SectionMap.js +8 -5
- package/src/block.coffee +2 -32
- package/src/block.js +17 -44
- package/src/fs.coffee +2 -1
- package/src/fs.js +4 -1
- package/src/fsa.coffee +3 -2
- package/src/fsa.js +8 -5
- package/src/svelte.coffee +2 -1
- package/src/svelte.js +5 -2
- package/src/utils.coffee +20 -310
- package/src/utils.js +87 -366
- package/src/arrow.coffee +0 -65
- package/src/arrow.js +0 -83
- package/src/debug.coffee +0 -300
- package/src/debug.js +0 -329
- package/src/log.coffee +0 -186
- package/src/log.js +0 -206
- package/src/stack.coffee +0 -169
- package/src/stack.js +0 -191
package/src/utils.coffee
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
# utils.coffee
|
2
2
|
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
import {assert, croak} from '@jdeighan/exceptions'
|
4
|
+
import {LOG, sep_dash, sep_eq} from '@jdeighan/exceptions/log'
|
5
|
+
import {
|
6
|
+
undef, pass, defined, notdefined,
|
7
|
+
deepCopy, escapeStr, unescapeStr, hasChar, quoted, OL,
|
8
|
+
isString, isNumber, isInteger, isHash, isArray, isBoolean,
|
9
|
+
isConstructor, isFunction, isRegExp, isObject, getClassName,
|
10
|
+
jsType,
|
11
|
+
isEmpty, nonEmpty, chomp, rtrim, setCharsAt, words,
|
12
|
+
} from '@jdeighan/exceptions/utils'
|
13
|
+
|
14
|
+
export {
|
15
|
+
undef, pass, defined, notdefined, sep_dash, sep_eq,
|
16
|
+
deepCopy, escapeStr, unescapeStr, hasChar, quoted, OL,
|
17
|
+
isString, isNumber, isInteger, isHash, isArray, isBoolean,
|
18
|
+
isConstructor, isFunction, isRegExp, isObject, getClassName,
|
19
|
+
jsType,
|
20
|
+
isEmpty, nonEmpty, chomp, rtrim, setCharsAt, words,
|
21
|
+
}
|
8
22
|
|
9
23
|
# ---------------------------------------------------------------------------
|
10
24
|
# TEMP!!!!!
|
11
25
|
|
12
|
-
export isComment = (line)
|
26
|
+
export isComment = (line) =>
|
13
27
|
|
14
28
|
lMatches = line.match(///^
|
15
29
|
\s*
|
@@ -20,30 +34,6 @@ export isComment = (line) ->
|
|
20
34
|
|
21
35
|
# ---------------------------------------------------------------------------
|
22
36
|
|
23
|
-
export chomp = (str) ->
|
24
|
-
|
25
|
-
len = str.length
|
26
|
-
if (len == 0)
|
27
|
-
return ''
|
28
|
-
else if (len == 1)
|
29
|
-
if (str == "\r") || (str == "\n")
|
30
|
-
return ''
|
31
|
-
else
|
32
|
-
return str
|
33
|
-
else
|
34
|
-
# --- check the last 2 characters
|
35
|
-
tail = str.substring(len-2)
|
36
|
-
if (tail == "\r\n")
|
37
|
-
return str.substring(0, len-2)
|
38
|
-
else
|
39
|
-
tail = str.substring(len-1)
|
40
|
-
if (tail == "\n")
|
41
|
-
return str.substring(0, len-1)
|
42
|
-
else
|
43
|
-
return str
|
44
|
-
|
45
|
-
# ---------------------------------------------------------------------------
|
46
|
-
|
47
37
|
export isSubclassOf = (subClass, superClass) ->
|
48
38
|
|
49
39
|
return (subClass == superClass) \
|
@@ -56,11 +46,6 @@ export eval_expr = (str) ->
|
|
56
46
|
str = str.replace(/\bundef\b/g, 'undefined')
|
57
47
|
return Function('"use strict";return (' + str + ')')();
|
58
48
|
|
59
|
-
# ---------------------------------------------------------------------------
|
60
|
-
# pass - do nothing
|
61
|
-
|
62
|
-
export pass = () ->
|
63
|
-
|
64
49
|
# ---------------------------------------------------------------------------
|
65
50
|
|
66
51
|
export patchStr = (bigstr, pos, str) ->
|
@@ -90,61 +75,6 @@ export oneof = (word, lWords...) ->
|
|
90
75
|
|
91
76
|
# ---------------------------------------------------------------------------
|
92
77
|
|
93
|
-
export isConstructor = (f) ->
|
94
|
-
|
95
|
-
try
|
96
|
-
new f()
|
97
|
-
catch err
|
98
|
-
if (err.message.indexOf('is not a constructor') >= 0)
|
99
|
-
return false;
|
100
|
-
return true;
|
101
|
-
|
102
|
-
# ---------------------------------------------------------------------------
|
103
|
-
|
104
|
-
export jsType = (x) ->
|
105
|
-
|
106
|
-
if notdefined(x)
|
107
|
-
return [undef, undef]
|
108
|
-
else if isString(x)
|
109
|
-
if x.match(/^\s*$/)
|
110
|
-
return ['string', 'empty']
|
111
|
-
else
|
112
|
-
return ['string', undef]
|
113
|
-
else if isNumber(x)
|
114
|
-
if Number.isInteger(x)
|
115
|
-
return ['number', 'integer']
|
116
|
-
else
|
117
|
-
return ['number', undef]
|
118
|
-
else if isBoolean(x)
|
119
|
-
return ['boolean', undef]
|
120
|
-
else if isHash(x)
|
121
|
-
lKeys = Object.keys(x);
|
122
|
-
if (lKeys.length == 0)
|
123
|
-
return ['hash', 'empty']
|
124
|
-
else
|
125
|
-
return ['hash', undef]
|
126
|
-
else if isArray(x)
|
127
|
-
if (x.length == 0)
|
128
|
-
return ['array', 'empty']
|
129
|
-
else
|
130
|
-
return ['array', undef]
|
131
|
-
else if isConstructor(x)
|
132
|
-
return ['function', 'constructor']
|
133
|
-
else if isFunction(x)
|
134
|
-
return ['function', undef]
|
135
|
-
else if isObject(x)
|
136
|
-
return ['object', undef]
|
137
|
-
else
|
138
|
-
croak "Unknown type: #{OL(x)}"
|
139
|
-
|
140
|
-
# ---------------------------------------------------------------------------
|
141
|
-
|
142
|
-
export isString = (x) ->
|
143
|
-
|
144
|
-
return (typeof x == 'string') || (x instanceof String)
|
145
|
-
|
146
|
-
# ---------------------------------------------------------------------------
|
147
|
-
|
148
78
|
export isNonEmptyString = (x) ->
|
149
79
|
|
150
80
|
if typeof x != 'string' && x ! instanceof String
|
@@ -155,64 +85,12 @@ export isNonEmptyString = (x) ->
|
|
155
85
|
|
156
86
|
# ---------------------------------------------------------------------------
|
157
87
|
|
158
|
-
export isBoolean = (x) ->
|
159
|
-
|
160
|
-
return typeof x == 'boolean'
|
161
|
-
|
162
|
-
# ---------------------------------------------------------------------------
|
163
|
-
|
164
|
-
export isObject = (x, lReqKeys=undef) ->
|
165
|
-
|
166
|
-
result = (typeof x == 'object') \
|
167
|
-
&& ! isString(x) \
|
168
|
-
&& ! isArray(x) \
|
169
|
-
&& ! isHash(x) \
|
170
|
-
&& ! isNumber(x)
|
171
|
-
if result
|
172
|
-
if defined(lReqKeys)
|
173
|
-
assert isArray(lReqKeys), "lReqKeys is not an array"
|
174
|
-
for key in lReqKeys
|
175
|
-
if ! x.hasOwnProperty(key)
|
176
|
-
return false
|
177
|
-
return true
|
178
|
-
else
|
179
|
-
return false
|
180
|
-
|
181
|
-
# ---------------------------------------------------------------------------
|
182
|
-
|
183
|
-
export getClassName = (obj) ->
|
184
|
-
|
185
|
-
if (typeof obj != 'object')
|
186
|
-
return undef
|
187
|
-
return obj.constructor.name
|
188
|
-
|
189
|
-
# ---------------------------------------------------------------------------
|
190
|
-
|
191
|
-
export isArray = (x) ->
|
192
|
-
|
193
|
-
return Array.isArray(x)
|
194
|
-
|
195
|
-
# ---------------------------------------------------------------------------
|
196
|
-
|
197
88
|
export isNonEmptyArray = (x) ->
|
198
89
|
|
199
90
|
return isArray(x) && (x.length > 0)
|
200
91
|
|
201
92
|
# ---------------------------------------------------------------------------
|
202
93
|
|
203
|
-
export isHash = (x, lKeys) ->
|
204
|
-
|
205
|
-
if ! x || (getClassName(x) != 'Object')
|
206
|
-
return false
|
207
|
-
if defined(lKeys)
|
208
|
-
assert isArray(lKeys), "isHash(): lKeys not an array"
|
209
|
-
for key in lKeys
|
210
|
-
if ! x.hasOwnProperty(key)
|
211
|
-
return false
|
212
|
-
return true
|
213
|
-
|
214
|
-
# ---------------------------------------------------------------------------
|
215
|
-
|
216
94
|
export isNonEmptyHash = (x) ->
|
217
95
|
|
218
96
|
return isHash(x) && (Object.keys(x).length > 0)
|
@@ -225,40 +103,6 @@ export hashHasKey = (x, key) ->
|
|
225
103
|
assert isString(key), "hashHasKey(): key not a string"
|
226
104
|
return x.hasOwnProperty(key)
|
227
105
|
|
228
|
-
# ---------------------------------------------------------------------------
|
229
|
-
# isEmpty
|
230
|
-
# - string is whitespace, array has no elements, hash has no keys
|
231
|
-
|
232
|
-
export isEmpty = (x) ->
|
233
|
-
|
234
|
-
if (x == undef) || (x == null)
|
235
|
-
return true
|
236
|
-
if isString(x)
|
237
|
-
return x.match(/^\s*$/)
|
238
|
-
if isArray(x)
|
239
|
-
return x.length == 0
|
240
|
-
if isHash(x)
|
241
|
-
return Object.keys(x).length == 0
|
242
|
-
else
|
243
|
-
return false
|
244
|
-
|
245
|
-
# ---------------------------------------------------------------------------
|
246
|
-
# nonEmpty
|
247
|
-
# - string has non-whitespace, array has elements, hash has keys
|
248
|
-
|
249
|
-
export nonEmpty = (x) ->
|
250
|
-
|
251
|
-
if ! x?
|
252
|
-
return false
|
253
|
-
if isString(x)
|
254
|
-
return ! x.match(/^\s*$/)
|
255
|
-
if isArray(x)
|
256
|
-
return x.length > 0
|
257
|
-
if isHash(x)
|
258
|
-
return Object.keys(x).length > 0
|
259
|
-
else
|
260
|
-
croak "isEmpty(): Invalid parameter"
|
261
|
-
|
262
106
|
# ---------------------------------------------------------------------------
|
263
107
|
|
264
108
|
export notInArray = (lItems, item) ->
|
@@ -277,15 +121,6 @@ export pushCond = (lItems, item, doPush=notInArray) ->
|
|
277
121
|
|
278
122
|
# ---------------------------------------------------------------------------
|
279
123
|
|
280
|
-
export words = (str) ->
|
281
|
-
|
282
|
-
str = str.trim()
|
283
|
-
if (str == '')
|
284
|
-
return []
|
285
|
-
return str.split(/\s+/)
|
286
|
-
|
287
|
-
# ---------------------------------------------------------------------------
|
288
|
-
|
289
124
|
export isArrayOfHashes = (lItems) ->
|
290
125
|
|
291
126
|
if ! isArray(lItems)
|
@@ -308,50 +143,6 @@ export isArrayOfStrings = (lItems) ->
|
|
308
143
|
|
309
144
|
# ---------------------------------------------------------------------------
|
310
145
|
|
311
|
-
export isFunction = (x) ->
|
312
|
-
|
313
|
-
return typeof x == 'function'
|
314
|
-
|
315
|
-
# ---------------------------------------------------------------------------
|
316
|
-
|
317
|
-
export isRegExp = (x) ->
|
318
|
-
|
319
|
-
return x instanceof RegExp
|
320
|
-
|
321
|
-
# ---------------------------------------------------------------------------
|
322
|
-
|
323
|
-
export isNumber = (x, hOptions=undef) ->
|
324
|
-
|
325
|
-
result = (typeof x == 'number') || (x instanceof Number)
|
326
|
-
if result && defined(hOptions)
|
327
|
-
assert isHash(hOptions), "2nd arg not a hash: #{OL(hOptions)}"
|
328
|
-
{min, max} = hOptions
|
329
|
-
if defined(min) && (x < min)
|
330
|
-
result = false
|
331
|
-
if defined(max) && (x > max)
|
332
|
-
result = false
|
333
|
-
return result
|
334
|
-
|
335
|
-
# ---------------------------------------------------------------------------
|
336
|
-
|
337
|
-
export isInteger = (x, hOptions={}) ->
|
338
|
-
|
339
|
-
if (typeof x == 'number')
|
340
|
-
result = Number.isInteger(x)
|
341
|
-
else if (x instanceof Number)
|
342
|
-
result = Number.isInteger(x.valueOf())
|
343
|
-
else
|
344
|
-
result = false
|
345
|
-
|
346
|
-
if result
|
347
|
-
if defined(hOptions.min) && (x < hOptions.min)
|
348
|
-
result = false
|
349
|
-
if defined(hOptions.max) && (x > hOptions.max)
|
350
|
-
result = false
|
351
|
-
return result
|
352
|
-
|
353
|
-
# ---------------------------------------------------------------------------
|
354
|
-
|
355
146
|
export isUniqueList = (lItems, func=undef) ->
|
356
147
|
|
357
148
|
if ! lItems?
|
@@ -435,19 +226,6 @@ export titleLine = (title, char='=', padding=2, linelen=42) ->
|
|
435
226
|
strRight = char.repeat(nRight)
|
436
227
|
return strLeft + strMiddle + strRight
|
437
228
|
|
438
|
-
# ---------------------------------------------------------------------------
|
439
|
-
# rtrim - strip trailing whitespace
|
440
|
-
|
441
|
-
export rtrim = (line) ->
|
442
|
-
|
443
|
-
assert isString(line), "rtrim(): line is not a string"
|
444
|
-
lMatches = line.match(/\s+$/)
|
445
|
-
if lMatches?
|
446
|
-
n = lMatches[0].length # num chars to remove
|
447
|
-
return line.substring(0, line.length - n)
|
448
|
-
else
|
449
|
-
return line
|
450
|
-
|
451
229
|
# ---------------------------------------------------------------------------
|
452
230
|
# rtrunc - strip nChars chars from right of a string
|
453
231
|
|
@@ -462,51 +240,6 @@ export ltrunc = (str, nChars) ->
|
|
462
240
|
|
463
241
|
return str.substring(nChars)
|
464
242
|
|
465
|
-
# ---------------------------------------------------------------------------
|
466
|
-
# deepCopy - deep copy an array or object
|
467
|
-
|
468
|
-
export deepCopy = (obj) ->
|
469
|
-
|
470
|
-
if (obj == undef)
|
471
|
-
return undef
|
472
|
-
objStr = JSON.stringify(obj)
|
473
|
-
try
|
474
|
-
newObj = JSON.parse(objStr)
|
475
|
-
catch err
|
476
|
-
croak "ERROR: err.message", objStr
|
477
|
-
|
478
|
-
return newObj
|
479
|
-
|
480
|
-
# ---------------------------------------------------------------------------
|
481
|
-
# escapeStr - escape newlines, TAB chars, etc.
|
482
|
-
|
483
|
-
export hDefEsc = {
|
484
|
-
"\n": '®'
|
485
|
-
"\t": '→'
|
486
|
-
" ": '˳'
|
487
|
-
}
|
488
|
-
|
489
|
-
export escapeStr = (str, hEscape=hDefEsc) ->
|
490
|
-
|
491
|
-
assert isString(str), "escapeStr(): not a string"
|
492
|
-
lParts = for ch in str.split('')
|
493
|
-
if hEscape[ch]? then hEscape[ch] else ch
|
494
|
-
return lParts.join('')
|
495
|
-
|
496
|
-
# ---------------------------------------------------------------------------
|
497
|
-
|
498
|
-
export oneline = (obj) ->
|
499
|
-
|
500
|
-
if obj?
|
501
|
-
if isString(obj)
|
502
|
-
return "'#{escapeStr(obj)}'"
|
503
|
-
else
|
504
|
-
return JSON.stringify(obj)
|
505
|
-
else
|
506
|
-
return 'undef'
|
507
|
-
|
508
|
-
export OL = oneline
|
509
|
-
|
510
243
|
# ---------------------------------------------------------------------------
|
511
244
|
|
512
245
|
export removeCR = (str) ->
|
@@ -597,18 +330,6 @@ export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
|
|
597
330
|
|
598
331
|
# ---------------------------------------------------------------------------
|
599
332
|
|
600
|
-
export defined = (obj) ->
|
601
|
-
|
602
|
-
return (obj != undef) && (obj != null)
|
603
|
-
|
604
|
-
# ---------------------------------------------------------------------------
|
605
|
-
|
606
|
-
export notdefined = (obj) ->
|
607
|
-
|
608
|
-
return (obj == undef) || (obj == null)
|
609
|
-
|
610
|
-
# ---------------------------------------------------------------------------
|
611
|
-
|
612
333
|
export isIterable = (obj) ->
|
613
334
|
|
614
335
|
if (obj == undef) || (obj == null)
|
@@ -632,17 +353,6 @@ export range = (n) ->
|
|
632
353
|
|
633
354
|
# ---------------------------------------------------------------------------
|
634
355
|
|
635
|
-
export setCharsAt = (str, pos, str2) ->
|
636
|
-
|
637
|
-
assert (pos >= 0), "negative pos #{pos} not allowed"
|
638
|
-
assert (pos < str.length), "pos #{pos} not in #{OL(str)}"
|
639
|
-
if (pos + str2.length >= str.length)
|
640
|
-
return str.substring(0, pos) + str2
|
641
|
-
else
|
642
|
-
return str.substring(0, pos) + str2 + str.substring(pos + str2.length)
|
643
|
-
|
644
|
-
# ---------------------------------------------------------------------------
|
645
|
-
|
646
356
|
export getOptions = (hOptions, hDefault={}) ->
|
647
357
|
# --- If hOptions is a string, break into words and set each to true
|
648
358
|
|