@jdeighan/coffee-utils 14.0.2 → 14.0.4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "14.0.2",
4
+ "version": "14.0.4",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
8
- ".": "./src/coffee-utils.js",
9
8
  "./server": "./src/server.js",
10
9
  "./browser": "./src/browser.js",
11
10
  "./fs": "./src/fs.js",
@@ -50,13 +49,13 @@
50
49
  },
51
50
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
52
51
  "dependencies": {
53
- "@jdeighan/base-utils": "^3.0.3",
52
+ "@jdeighan/base-utils": "^4.0.1",
54
53
  "cross-env": "^7.0.3",
55
54
  "n-readlines": "^1.0.1",
56
55
  "readline-sync": "^1.4.10",
57
56
  "svelte": "^3.55.0"
58
57
  },
59
58
  "devDependencies": {
60
- "@jdeighan/unit-tester": "^3.0.16"
59
+ "@jdeighan/unit-tester": "^3.0.18"
61
60
  }
62
61
  }
@@ -3,14 +3,13 @@
3
3
  import {
4
4
  undef, defined, notdefined, OL, deepCopy,
5
5
  isString, isNonEmptyString, isArray, isHash,
6
- isEmpty, nonEmpty,
6
+ isEmpty, nonEmpty, isArrayOfStrings,
7
7
  } from '@jdeighan/base-utils'
8
8
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
9
9
  import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
10
10
  import {
11
11
  dbgEnter, dbgReturn, dbg,
12
12
  } from '@jdeighan/base-utils/debug'
13
- import {isArrayOfStrings} from '@jdeighan/coffee-utils'
14
13
 
15
14
  # ---------------------------------------------------------------------------
16
15
 
package/src/KeyedSet.js CHANGED
@@ -11,7 +11,8 @@ import {
11
11
  isArray,
12
12
  isHash,
13
13
  isEmpty,
14
- nonEmpty
14
+ nonEmpty,
15
+ isArrayOfStrings
15
16
  } from '@jdeighan/base-utils';
16
17
 
17
18
  import {
@@ -30,10 +31,6 @@ import {
30
31
  dbg
31
32
  } from '@jdeighan/base-utils/debug';
32
33
 
33
- import {
34
- isArrayOfStrings
35
- } from '@jdeighan/coffee-utils';
36
-
37
34
  // ---------------------------------------------------------------------------
38
35
  export var KeyedSet = class KeyedSet extends Map {
39
36
  constructor(setName, lKeyNames, sep = '|') {
@@ -1,19 +1,17 @@
1
1
  # SectionMap.coffee
2
2
 
3
- import {assert, croak} from '@jdeighan/base-utils/exceptions'
4
- import {LOG, LOGVALUE, LOGTAML} from '@jdeighan/base-utils/log'
5
- import {isTAML, fromTAML} from '@jdeighan/base-utils/taml'
6
- import {
7
- dbg, dbgEnter, dbgReturn, dbgYield, dbgResume,
8
- } from '@jdeighan/base-utils/debug'
9
3
  import {
10
4
  pass, undef, defined, notdefined, OL, isEmpty, nonEmpty,
11
5
  isString, isHash, isArray, isNonEmptyString,
12
- isFunction, jsType, toBlock,
6
+ isFunction, jsType, toBlock, isArrayOfStrings, isNonEmptyArray,
13
7
  } from '@jdeighan/base-utils'
8
+ import {assert, croak} from '@jdeighan/base-utils/exceptions'
9
+ import {LOG, LOGVALUE, LOGTAML} from '@jdeighan/base-utils/log'
14
10
  import {
15
- isArrayOfStrings, isNonEmptyArray, isUniqueTree,
16
- } from '@jdeighan/coffee-utils'
11
+ dbg, dbgEnter, dbgReturn, dbgYield, dbgResume,
12
+ } from '@jdeighan/base-utils/debug'
13
+ import {isTAML, fromTAML} from '@jdeighan/base-utils/taml'
14
+
17
15
  import {Section} from '@jdeighan/coffee-utils/section'
18
16
 
19
17
  # ---------------------------------------------------------------------------
package/src/SectionMap.js CHANGED
@@ -2,6 +2,25 @@
2
2
  // SectionMap.coffee
3
3
  var isSectionName, isSetName;
4
4
 
5
+ import {
6
+ pass,
7
+ undef,
8
+ defined,
9
+ notdefined,
10
+ OL,
11
+ isEmpty,
12
+ nonEmpty,
13
+ isString,
14
+ isHash,
15
+ isArray,
16
+ isNonEmptyString,
17
+ isFunction,
18
+ jsType,
19
+ toBlock,
20
+ isArrayOfStrings,
21
+ isNonEmptyArray
22
+ } from '@jdeighan/base-utils';
23
+
5
24
  import {
6
25
  assert,
7
26
  croak
@@ -13,11 +32,6 @@ import {
13
32
  LOGTAML
14
33
  } from '@jdeighan/base-utils/log';
15
34
 
16
- import {
17
- isTAML,
18
- fromTAML
19
- } from '@jdeighan/base-utils/taml';
20
-
21
35
  import {
22
36
  dbg,
23
37
  dbgEnter,
@@ -27,27 +41,9 @@ import {
27
41
  } from '@jdeighan/base-utils/debug';
28
42
 
29
43
  import {
30
- pass,
31
- undef,
32
- defined,
33
- notdefined,
34
- OL,
35
- isEmpty,
36
- nonEmpty,
37
- isString,
38
- isHash,
39
- isArray,
40
- isNonEmptyString,
41
- isFunction,
42
- jsType,
43
- toBlock
44
- } from '@jdeighan/base-utils';
45
-
46
- import {
47
- isArrayOfStrings,
48
- isNonEmptyArray,
49
- isUniqueTree
50
- } from '@jdeighan/coffee-utils';
44
+ isTAML,
45
+ fromTAML
46
+ } from '@jdeighan/base-utils/taml';
51
47
 
52
48
  import {
53
49
  Section
package/src/block.coffee CHANGED
@@ -5,10 +5,9 @@ import readline from 'readline'
5
5
 
6
6
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
7
7
  import {
8
- undef, pass, defined, notdefined, rtrim, OL,
8
+ undef, pass, defined, notdefined, rtrim, OL, isArrayOfStrings,
9
9
  isEmpty, isString, isArray, nonEmpty, toArray, toBlock,
10
10
  } from '@jdeighan/base-utils'
11
- import {isArrayOfStrings} from '@jdeighan/coffee-utils'
12
11
 
13
12
  # ---------------------------------------------------------------------------
14
13
 
package/src/block.js CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  notdefined,
17
17
  rtrim,
18
18
  OL,
19
+ isArrayOfStrings,
19
20
  isEmpty,
20
21
  isString,
21
22
  isArray,
@@ -24,10 +25,6 @@ import {
24
25
  toBlock
25
26
  } from '@jdeighan/base-utils';
26
27
 
27
- import {
28
- isArrayOfStrings
29
- } from '@jdeighan/coffee-utils';
30
-
31
28
  // ---------------------------------------------------------------------------
32
29
  export var splitBlock = function(block) {
33
30
  var pos;
package/src/indent.coffee CHANGED
@@ -152,7 +152,7 @@ export undented = (input, level=undef, oneIndent="\t") ->
152
152
  lNewLines.push('')
153
153
  else
154
154
  if (line.indexOf(toRemove) != 0)
155
- throw new Error("remove #{OL(toRemove)} from #{OL(text)}")
155
+ throw new Error("remove #{OL(toRemove)} from #{OL(line)}")
156
156
  lNewLines.push(line.substr(nToRemove))
157
157
 
158
158
  if isString(input)
@@ -160,36 +160,6 @@ export undented = (input, level=undef, oneIndent="\t") ->
160
160
  else
161
161
  return lNewLines
162
162
 
163
- # ---------------------------------------------------------------------------
164
- # tabify - convert leading spaces to TAB characters
165
- # if numSpaces is not defined, then the first line
166
- # that contains at least one space sets it
167
-
168
- export tabify = (str, numSpaces=undef) ->
169
-
170
- lLines = []
171
- for str in toArray(str)
172
- [_, prefix, theRest] = str.match(/^(\s*)(.*)$/)
173
- prefixLen = prefix.length
174
- if prefixLen == 0
175
- lLines.push theRest
176
- else
177
- assert (prefix.indexOf('\t') == -1), "found TAB"
178
- if numSpaces == undef
179
- numSpaces = prefixLen
180
- assert (prefixLen % numSpaces == 0), "Bad prefix"
181
- level = prefixLen / numSpaces
182
- lLines.push '\t'.repeat(level) + theRest
183
- result = toBlock(lLines)
184
- return result
185
-
186
- # ---------------------------------------------------------------------------
187
- # untabify - convert ALL TABs to spaces
188
-
189
- export untabify = (str, numSpaces=3) ->
190
-
191
- return str.replace(/\t/g, ' '.repeat(numSpaces))
192
-
193
163
  # ---------------------------------------------------------------------------
194
164
  # enclose - indent text, surround with pre and post
195
165
 
package/src/indent.js CHANGED
@@ -171,7 +171,7 @@ export var undented = function(input, level = undef, oneIndent = "\t") {
171
171
  lNewLines.push('');
172
172
  } else {
173
173
  if (line.indexOf(toRemove) !== 0) {
174
- throw new Error(`remove ${OL(toRemove)} from ${OL(text)}`);
174
+ throw new Error(`remove ${OL(toRemove)} from ${OL(line)}`);
175
175
  }
176
176
  lNewLines.push(line.substr(nToRemove));
177
177
  }
@@ -183,40 +183,6 @@ export var undented = function(input, level = undef, oneIndent = "\t") {
183
183
  }
184
184
  };
185
185
 
186
- // ---------------------------------------------------------------------------
187
- // tabify - convert leading spaces to TAB characters
188
- // if numSpaces is not defined, then the first line
189
- // that contains at least one space sets it
190
- export var tabify = function(str, numSpaces = undef) {
191
- var _, i, lLines, len1, level, prefix, prefixLen, ref, result, theRest;
192
- lLines = [];
193
- ref = toArray(str);
194
- for (i = 0, len1 = ref.length; i < len1; i++) {
195
- str = ref[i];
196
- [_, prefix, theRest] = str.match(/^(\s*)(.*)$/);
197
- prefixLen = prefix.length;
198
- if (prefixLen === 0) {
199
- lLines.push(theRest);
200
- } else {
201
- assert(prefix.indexOf('\t') === -1, "found TAB");
202
- if (numSpaces === undef) {
203
- numSpaces = prefixLen;
204
- }
205
- assert(prefixLen % numSpaces === 0, "Bad prefix");
206
- level = prefixLen / numSpaces;
207
- lLines.push('\t'.repeat(level) + theRest);
208
- }
209
- }
210
- result = toBlock(lLines);
211
- return result;
212
- };
213
-
214
- // ---------------------------------------------------------------------------
215
- // untabify - convert ALL TABs to spaces
216
- export var untabify = function(str, numSpaces = 3) {
217
- return str.replace(/\t/g, ' '.repeat(numSpaces));
218
- };
219
-
220
186
  // ---------------------------------------------------------------------------
221
187
  // enclose - indent text, surround with pre and post
222
188
  export var enclose = function(text, pre, post, oneIndent = "\t") {
@@ -1,351 +0,0 @@
1
- # coffee-utils.coffee
2
-
3
- import {
4
- undef, pass, defined, notdefined,
5
- deepCopy, escapeStr, unescapeStr, hasChar, quoted, OL,
6
- isString, isNumber, isInteger, isHash, isArray, isBoolean,
7
- isConstructor, isFunction, isRegExp, isObject, getClassName,
8
- jsType, oneof, isIterable,
9
- isEmpty, nonEmpty, chomp, rtrim, setCharsAt, words, getOptions,
10
- } from '@jdeighan/base-utils'
11
- import {assert, croak} from '@jdeighan/base-utils/exceptions'
12
- import {
13
- LOG, LOGVALUE, sep_dash, sep_eq,
14
- } from '@jdeighan/base-utils/log'
15
- import {
16
- dbgEnter, dbgReturn, dbgYield, dbgResume, dbg, setDebugging,
17
- } from '@jdeighan/base-utils/debug'
18
-
19
- # ---------------------------------------------------------------------------
20
-
21
- export isHashComment = (line) =>
22
-
23
- lMatches = line.match(///^
24
- \s*
25
- \#
26
- (\s|$)
27
- ///)
28
- return defined(lMatches)
29
-
30
- # ---------------------------------------------------------------------------
31
-
32
- export isSubclassOf = (subClass, superClass) ->
33
-
34
- return (subClass == superClass) \
35
- || (subClass.prototype instanceof superClass)
36
-
37
- # ---------------------------------------------------------------------------
38
-
39
- export eval_expr = (str) ->
40
-
41
- str = str.replace(/\bundef\b/g, 'undefined')
42
- return Function('"use strict";return (' + str + ')')();
43
-
44
- # ---------------------------------------------------------------------------
45
-
46
- export patchStr = (bigstr, pos, str) ->
47
-
48
- endpos = pos + str.length
49
- if (endpos < bigstr.length)
50
- return bigstr.substring(0, pos) + str + bigstr.substring(endpos)
51
- else
52
- return bigstr.substring(0, pos) + str
53
-
54
- # ---------------------------------------------------------------------------
55
-
56
- export charCount = (str, ch) ->
57
-
58
- count = 0
59
- pos = str.indexOf(ch, 0)
60
- while (pos >= 0)
61
- count += 1
62
- pos = str.indexOf(ch, pos+1)
63
- return count
64
-
65
- # ---------------------------------------------------------------------------
66
-
67
- export removeKeys = (h, lKeys) =>
68
-
69
- for key in lKeys
70
- delete h[key]
71
- for own key,value of h
72
- if defined(value)
73
- if isArray(value)
74
- for item in value
75
- if isHash(item)
76
- removeKeys(item, lKeys)
77
- else if (typeof value == 'object')
78
- removeKeys value, lKeys
79
- return
80
-
81
- # ---------------------------------------------------------------------------
82
-
83
- export isNonEmptyString = (x) ->
84
-
85
- if typeof x != 'string' && x ! instanceof String
86
- return false
87
- if x.match(/^\s*$/)
88
- return false
89
- return true
90
-
91
- # ---------------------------------------------------------------------------
92
-
93
- export isNonEmptyArray = (x) ->
94
-
95
- return isArray(x) && (x.length > 0)
96
-
97
- # ---------------------------------------------------------------------------
98
-
99
- export isNonEmptyHash = (x) ->
100
-
101
- return isHash(x) && (Object.keys(x).length > 0)
102
-
103
- # ---------------------------------------------------------------------------
104
-
105
- export hashHasKey = (x, key) ->
106
-
107
- assert isHash(x), "hashHasKey(): not a hash"
108
- assert isString(key), "hashHasKey(): key not a string"
109
- return x.hasOwnProperty(key)
110
-
111
- # ---------------------------------------------------------------------------
112
-
113
- export notInArray = (lItems, item) ->
114
-
115
- return (lItems.indexOf(item) == -1)
116
-
117
- # ---------------------------------------------------------------------------
118
-
119
- export pushCond = (lItems, item, doPush=notInArray) ->
120
-
121
- if doPush(lItems, item)
122
- lItems.push item
123
- return true
124
- else
125
- return false
126
-
127
- # ---------------------------------------------------------------------------
128
-
129
- export isArrayOfHashes = (lItems) ->
130
- # --- undefined items are allowed
131
-
132
- if ! isArray(lItems)
133
- return false
134
- for item in lItems
135
- if defined(item) && ! isHash(item)
136
- return false
137
- return true
138
-
139
- # ---------------------------------------------------------------------------
140
-
141
- export isArrayOfStrings = (lItems) ->
142
- # --- undefined items are allowed
143
-
144
- if ! isArray(lItems)
145
- return false
146
- for item in lItems
147
- if defined(item) && ! isString(item)
148
- return false
149
- return true
150
-
151
- # ---------------------------------------------------------------------------
152
-
153
- export isUniqueList = (lItems, func=undef) ->
154
-
155
- if notdefined(lItems)
156
- return true # empty list is unique
157
- if defined(func)
158
- assert isFunction(func), "Not a function: #{OL(func)}"
159
- h = {}
160
- for item in lItems
161
- if defined(func) && !func(item)
162
- return false
163
- if defined(h[item])
164
- return false
165
- h[item] = 1
166
- return true
167
-
168
- # ---------------------------------------------------------------------------
169
-
170
- export isUniqueTree = (lItems, func=undef, hFound={}) ->
171
-
172
- if isEmpty(lItems)
173
- return true # empty list is unique
174
- if defined(func)
175
- assert isFunction(func), "Not a function: #{OL(func)}"
176
- for item in lItems
177
- if isArray(item)
178
- if ! isUniqueTree(item, func, hFound)
179
- return false
180
- else
181
- if defined(func) && !func(item)
182
- return false
183
- if defined(hFound[item])
184
- return false
185
- hFound[item] = 1
186
- return true
187
-
188
- # ---------------------------------------------------------------------------
189
-
190
- export uniq = (lItems) ->
191
-
192
- return [...new Set(lItems)]
193
-
194
- # ---------------------------------------------------------------------------
195
- # warn - issue a warning
196
-
197
- export warn = (message) ->
198
-
199
- say "WARNING: #{message}"
200
-
201
- # ---------------------------------------------------------------------------
202
- # hashToStr - stringify a hash
203
-
204
- export hashToStr = (h) ->
205
-
206
- return JSON.stringify(h, Object.keys(h).sort(), 3)
207
-
208
- # ---------------------------------------------------------------------------
209
- # say - print to the console (for now)
210
- # later, on a web page, call alert(str)
211
-
212
- export say = (x) ->
213
-
214
- if isHash(x)
215
- LOG hashToStr(x)
216
- else
217
- LOG x
218
- return
219
-
220
- # ---------------------------------------------------------------------------
221
-
222
- export titleLine = (title, char='=', padding=2, linelen=42) ->
223
- # --- used in logger
224
-
225
- if ! title
226
- return char.repeat(linelen)
227
-
228
- titleLen = title.length + 2 * padding
229
- nLeft = Math.floor((linelen - titleLen) / 2)
230
- nRight = linelen - nLeft - titleLen
231
- strLeft = char.repeat(nLeft)
232
- strMiddle = ' '.repeat(padding) + title + ' '.repeat(padding)
233
- strRight = char.repeat(nRight)
234
- return strLeft + strMiddle + strRight
235
-
236
- # ---------------------------------------------------------------------------
237
- # rtrunc - strip nChars chars from right of a string
238
-
239
- export rtrunc = (str, nChars) ->
240
-
241
- return str.substring(0, str.length - nChars)
242
-
243
- # ---------------------------------------------------------------------------
244
- # ltrunc - strip nChars chars from left of a string
245
-
246
- export ltrunc = (str, nChars) ->
247
-
248
- return str.substring(nChars)
249
-
250
- # ---------------------------------------------------------------------------
251
-
252
- export removeCR = (str) ->
253
-
254
- return str.replace(/\r/g, '')
255
-
256
- # ---------------------------------------------------------------------------
257
-
258
- export CWS = (str) ->
259
-
260
- assert isString(str), "CWS(): parameter not a string"
261
- return str.trim().replace(/\s+/sg, ' ')
262
-
263
- # ---------------------------------------------------------------------------
264
-
265
- export extractMatches = (line, regexp, convertFunc=undef) ->
266
-
267
- lStrings = [...line.matchAll(regexp)]
268
- lStrings = for str in lStrings
269
- str[0]
270
- if defined(convertFunc)
271
- lConverted = for str in lStrings
272
- convertFunc(str)
273
- return lConverted
274
- else
275
- return lStrings
276
-
277
- # ---------------------------------------------------------------------------
278
-
279
- export envVarsWithPrefix = (prefix, hOptions={}) ->
280
- # --- valid options:
281
- # stripPrefix
282
-
283
- assert prefix, "envVarsWithPrefix: empty prefix!"
284
- plen = prefix.length
285
- h = {}
286
- for key in Object.keys(process.env)
287
- if key.indexOf(prefix) == 0
288
- if hOptions.stripPrefix
289
- h[key.substr(plen)] = process.env[key]
290
- else
291
- h[key] = process.env[key]
292
- return h
293
-
294
- # ---------------------------------------------------------------------------
295
-
296
- export getTimeStr = (date=undef) ->
297
-
298
- if date == undef
299
- date = new Date()
300
- return date.toLocaleTimeString('en-US')
301
-
302
- # ---------------------------------------------------------------------------
303
-
304
- export getDateStr = (date=undef) ->
305
-
306
- if date == undef
307
- date = new Date()
308
- return date.toLocaleDateString('en-US')
309
-
310
- # ---------------------------------------------------------------------------
311
-
312
- export strcat = (lItems...) ->
313
- str = ''
314
- for item in lItems
315
- str += item.toString()
316
- return str
317
-
318
- # ---------------------------------------------------------------------------
319
-
320
- export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
321
-
322
- assert isHash(hVars), "replaceVars() hVars is not a hash"
323
-
324
- replacerFunc = (match, prefix, name) =>
325
- if prefix
326
- return process.env[name]
327
- else
328
- value = hVars[name]
329
- if defined(value)
330
- if isString(value)
331
- return value
332
- else
333
- return JSON.stringify(value)
334
- else
335
- return "__#{name}__"
336
- return line.replace(rx, replacerFunc)
337
-
338
- # ---------------------------------------------------------------------------
339
-
340
- export className = (aClass) ->
341
-
342
- if lMatches = aClass.toString().match(/class\s+(\w+)/)
343
- return lMatches[1]
344
- else
345
- croak "className(): Bad input class"
346
-
347
- # ---------------------------------------------------------------------------
348
-
349
- export timestamp = () ->
350
-
351
- return new Date().toLocaleTimeString("en-US")
@@ -1,431 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // coffee-utils.coffee
3
- var hasProp = {}.hasOwnProperty;
4
-
5
- import {
6
- undef,
7
- pass,
8
- defined,
9
- notdefined,
10
- deepCopy,
11
- escapeStr,
12
- unescapeStr,
13
- hasChar,
14
- quoted,
15
- OL,
16
- isString,
17
- isNumber,
18
- isInteger,
19
- isHash,
20
- isArray,
21
- isBoolean,
22
- isConstructor,
23
- isFunction,
24
- isRegExp,
25
- isObject,
26
- getClassName,
27
- jsType,
28
- oneof,
29
- isIterable,
30
- isEmpty,
31
- nonEmpty,
32
- chomp,
33
- rtrim,
34
- setCharsAt,
35
- words,
36
- getOptions
37
- } from '@jdeighan/base-utils';
38
-
39
- import {
40
- assert,
41
- croak
42
- } from '@jdeighan/base-utils/exceptions';
43
-
44
- import {
45
- LOG,
46
- LOGVALUE,
47
- sep_dash,
48
- sep_eq
49
- } from '@jdeighan/base-utils/log';
50
-
51
- import {
52
- dbgEnter,
53
- dbgReturn,
54
- dbgYield,
55
- dbgResume,
56
- dbg,
57
- setDebugging
58
- } from '@jdeighan/base-utils/debug';
59
-
60
- // ---------------------------------------------------------------------------
61
- export var isHashComment = (line) => {
62
- var lMatches;
63
- lMatches = line.match(/^\s*\#(\s|$)/);
64
- return defined(lMatches);
65
- };
66
-
67
- // ---------------------------------------------------------------------------
68
- export var isSubclassOf = function(subClass, superClass) {
69
- return (subClass === superClass) || (subClass.prototype instanceof superClass);
70
- };
71
-
72
- // ---------------------------------------------------------------------------
73
- export var eval_expr = function(str) {
74
- str = str.replace(/\bundef\b/g, 'undefined');
75
- return Function('"use strict";return (' + str + ')')();
76
- };
77
-
78
- // ---------------------------------------------------------------------------
79
- export var patchStr = function(bigstr, pos, str) {
80
- var endpos;
81
- endpos = pos + str.length;
82
- if (endpos < bigstr.length) {
83
- return bigstr.substring(0, pos) + str + bigstr.substring(endpos);
84
- } else {
85
- return bigstr.substring(0, pos) + str;
86
- }
87
- };
88
-
89
- // ---------------------------------------------------------------------------
90
- export var charCount = function(str, ch) {
91
- var count, pos;
92
- count = 0;
93
- pos = str.indexOf(ch, 0);
94
- while (pos >= 0) {
95
- count += 1;
96
- pos = str.indexOf(ch, pos + 1);
97
- }
98
- return count;
99
- };
100
-
101
- // ---------------------------------------------------------------------------
102
- export var removeKeys = (h, lKeys) => {
103
- var i, item, j, key, len, len1, value;
104
- for (i = 0, len = lKeys.length; i < len; i++) {
105
- key = lKeys[i];
106
- delete h[key];
107
- }
108
- for (key in h) {
109
- if (!hasProp.call(h, key)) continue;
110
- value = h[key];
111
- if (defined(value)) {
112
- if (isArray(value)) {
113
- for (j = 0, len1 = value.length; j < len1; j++) {
114
- item = value[j];
115
- if (isHash(item)) {
116
- removeKeys(item, lKeys);
117
- }
118
- }
119
- } else if (typeof value === 'object') {
120
- removeKeys(value, lKeys);
121
- }
122
- }
123
- }
124
- };
125
-
126
- // ---------------------------------------------------------------------------
127
- export var isNonEmptyString = function(x) {
128
- if (typeof x !== 'string' && !(x instanceof String)) {
129
- return false;
130
- }
131
- if (x.match(/^\s*$/)) {
132
- return false;
133
- }
134
- return true;
135
- };
136
-
137
- // ---------------------------------------------------------------------------
138
- export var isNonEmptyArray = function(x) {
139
- return isArray(x) && (x.length > 0);
140
- };
141
-
142
- // ---------------------------------------------------------------------------
143
- export var isNonEmptyHash = function(x) {
144
- return isHash(x) && (Object.keys(x).length > 0);
145
- };
146
-
147
- // ---------------------------------------------------------------------------
148
- export var hashHasKey = function(x, key) {
149
- assert(isHash(x), "hashHasKey(): not a hash");
150
- assert(isString(key), "hashHasKey(): key not a string");
151
- return x.hasOwnProperty(key);
152
- };
153
-
154
- // ---------------------------------------------------------------------------
155
- export var notInArray = function(lItems, item) {
156
- return lItems.indexOf(item) === -1;
157
- };
158
-
159
- // ---------------------------------------------------------------------------
160
- export var pushCond = function(lItems, item, doPush = notInArray) {
161
- if (doPush(lItems, item)) {
162
- lItems.push(item);
163
- return true;
164
- } else {
165
- return false;
166
- }
167
- };
168
-
169
- // ---------------------------------------------------------------------------
170
- export var isArrayOfHashes = function(lItems) {
171
- var i, item, len;
172
- if (!isArray(lItems)) {
173
- return false;
174
- }
175
- for (i = 0, len = lItems.length; i < len; i++) {
176
- item = lItems[i];
177
- if (defined(item) && !isHash(item)) {
178
- return false;
179
- }
180
- }
181
- return true;
182
- };
183
-
184
- // ---------------------------------------------------------------------------
185
- export var isArrayOfStrings = function(lItems) {
186
- var i, item, len;
187
- if (!isArray(lItems)) {
188
- return false;
189
- }
190
- for (i = 0, len = lItems.length; i < len; i++) {
191
- item = lItems[i];
192
- if (defined(item) && !isString(item)) {
193
- return false;
194
- }
195
- }
196
- return true;
197
- };
198
-
199
- // ---------------------------------------------------------------------------
200
- export var isUniqueList = function(lItems, func = undef) {
201
- var h, i, item, len;
202
- if (notdefined(lItems)) {
203
- return true; // empty list is unique
204
- }
205
- if (defined(func)) {
206
- assert(isFunction(func), `Not a function: ${OL(func)}`);
207
- }
208
- h = {};
209
- for (i = 0, len = lItems.length; i < len; i++) {
210
- item = lItems[i];
211
- if (defined(func) && !func(item)) {
212
- return false;
213
- }
214
- if (defined(h[item])) {
215
- return false;
216
- }
217
- h[item] = 1;
218
- }
219
- return true;
220
- };
221
-
222
- // ---------------------------------------------------------------------------
223
- export var isUniqueTree = function(lItems, func = undef, hFound = {}) {
224
- var i, item, len;
225
- if (isEmpty(lItems)) {
226
- return true; // empty list is unique
227
- }
228
- if (defined(func)) {
229
- assert(isFunction(func), `Not a function: ${OL(func)}`);
230
- }
231
- for (i = 0, len = lItems.length; i < len; i++) {
232
- item = lItems[i];
233
- if (isArray(item)) {
234
- if (!isUniqueTree(item, func, hFound)) {
235
- return false;
236
- }
237
- } else {
238
- if (defined(func) && !func(item)) {
239
- return false;
240
- }
241
- if (defined(hFound[item])) {
242
- return false;
243
- }
244
- hFound[item] = 1;
245
- }
246
- }
247
- return true;
248
- };
249
-
250
- // ---------------------------------------------------------------------------
251
- export var uniq = function(lItems) {
252
- return [...new Set(lItems)];
253
- };
254
-
255
- // ---------------------------------------------------------------------------
256
- // warn - issue a warning
257
- export var warn = function(message) {
258
- return say(`WARNING: ${message}`);
259
- };
260
-
261
- // ---------------------------------------------------------------------------
262
- // hashToStr - stringify a hash
263
- export var hashToStr = function(h) {
264
- return JSON.stringify(h, Object.keys(h).sort(), 3);
265
- };
266
-
267
- // ---------------------------------------------------------------------------
268
- // say - print to the console (for now)
269
- // later, on a web page, call alert(str)
270
- export var say = function(x) {
271
- if (isHash(x)) {
272
- LOG(hashToStr(x));
273
- } else {
274
- LOG(x);
275
- }
276
- };
277
-
278
- // ---------------------------------------------------------------------------
279
- export var titleLine = function(title, char = '=', padding = 2, linelen = 42) {
280
- var nLeft, nRight, strLeft, strMiddle, strRight, titleLen;
281
- if (!title) {
282
- return char.repeat(linelen);
283
- }
284
- titleLen = title.length + 2 * padding;
285
- nLeft = Math.floor((linelen - titleLen) / 2);
286
- nRight = linelen - nLeft - titleLen;
287
- strLeft = char.repeat(nLeft);
288
- strMiddle = ' '.repeat(padding) + title + ' '.repeat(padding);
289
- strRight = char.repeat(nRight);
290
- return strLeft + strMiddle + strRight;
291
- };
292
-
293
- // ---------------------------------------------------------------------------
294
- // rtrunc - strip nChars chars from right of a string
295
- export var rtrunc = function(str, nChars) {
296
- return str.substring(0, str.length - nChars);
297
- };
298
-
299
- // ---------------------------------------------------------------------------
300
- // ltrunc - strip nChars chars from left of a string
301
- export var ltrunc = function(str, nChars) {
302
- return str.substring(nChars);
303
- };
304
-
305
- // ---------------------------------------------------------------------------
306
- export var removeCR = function(str) {
307
- return str.replace(/\r/g, '');
308
- };
309
-
310
- // ---------------------------------------------------------------------------
311
- export var CWS = function(str) {
312
- assert(isString(str), "CWS(): parameter not a string");
313
- return str.trim().replace(/\s+/sg, ' ');
314
- };
315
-
316
- // ---------------------------------------------------------------------------
317
- export var extractMatches = function(line, regexp, convertFunc = undef) {
318
- var lConverted, lStrings, str;
319
- lStrings = [...line.matchAll(regexp)];
320
- lStrings = (function() {
321
- var i, len, results;
322
- results = [];
323
- for (i = 0, len = lStrings.length; i < len; i++) {
324
- str = lStrings[i];
325
- results.push(str[0]);
326
- }
327
- return results;
328
- })();
329
- if (defined(convertFunc)) {
330
- lConverted = (function() {
331
- var i, len, results;
332
- results = [];
333
- for (i = 0, len = lStrings.length; i < len; i++) {
334
- str = lStrings[i];
335
- results.push(convertFunc(str));
336
- }
337
- return results;
338
- })();
339
- return lConverted;
340
- } else {
341
- return lStrings;
342
- }
343
- };
344
-
345
- // ---------------------------------------------------------------------------
346
- export var envVarsWithPrefix = function(prefix, hOptions = {}) {
347
- var h, i, key, len, plen, ref;
348
- // --- valid options:
349
- // stripPrefix
350
- assert(prefix, "envVarsWithPrefix: empty prefix!");
351
- plen = prefix.length;
352
- h = {};
353
- ref = Object.keys(process.env);
354
- for (i = 0, len = ref.length; i < len; i++) {
355
- key = ref[i];
356
- if (key.indexOf(prefix) === 0) {
357
- if (hOptions.stripPrefix) {
358
- h[key.substr(plen)] = process.env[key];
359
- } else {
360
- h[key] = process.env[key];
361
- }
362
- }
363
- }
364
- return h;
365
- };
366
-
367
- // ---------------------------------------------------------------------------
368
- export var getTimeStr = function(date = undef) {
369
- if (date === undef) {
370
- date = new Date();
371
- }
372
- return date.toLocaleTimeString('en-US');
373
- };
374
-
375
- // ---------------------------------------------------------------------------
376
- export var getDateStr = function(date = undef) {
377
- if (date === undef) {
378
- date = new Date();
379
- }
380
- return date.toLocaleDateString('en-US');
381
- };
382
-
383
- // ---------------------------------------------------------------------------
384
- export var strcat = function(...lItems) {
385
- var i, item, len, str;
386
- str = '';
387
- for (i = 0, len = lItems.length; i < len; i++) {
388
- item = lItems[i];
389
- str += item.toString();
390
- }
391
- return str;
392
- };
393
-
394
- // ---------------------------------------------------------------------------
395
- export var replaceVars = function(line, hVars = {}, rx = /__(env\.)?([A-Za-z_]\w*)__/g) {
396
- var replacerFunc;
397
- assert(isHash(hVars), "replaceVars() hVars is not a hash");
398
- replacerFunc = (match, prefix, name) => {
399
- var value;
400
- if (prefix) {
401
- return process.env[name];
402
- } else {
403
- value = hVars[name];
404
- if (defined(value)) {
405
- if (isString(value)) {
406
- return value;
407
- } else {
408
- return JSON.stringify(value);
409
- }
410
- } else {
411
- return `__${name}__`;
412
- }
413
- }
414
- };
415
- return line.replace(rx, replacerFunc);
416
- };
417
-
418
- // ---------------------------------------------------------------------------
419
- export var className = function(aClass) {
420
- var lMatches;
421
- if (lMatches = aClass.toString().match(/class\s+(\w+)/)) {
422
- return lMatches[1];
423
- } else {
424
- return croak("className(): Bad input class");
425
- }
426
- };
427
-
428
- // ---------------------------------------------------------------------------
429
- export var timestamp = function() {
430
- return new Date().toLocaleTimeString("en-US");
431
- };