@jdeighan/coffee-utils 7.0.2 → 7.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.2",
4
+ "version": "7.0.5",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -49,7 +49,7 @@
49
49
  "js-yaml": "^4.1.0",
50
50
  "n-readlines": "^1.0.1",
51
51
  "readline-sync": "^1.4.10",
52
- "svelte": "^3.46.4"
52
+ "svelte": "^3.46.6"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@jdeighan/unit-tester": "^1.0.5"
@@ -168,7 +168,10 @@ export debug = (lArgs...) ->
168
168
  # when debugging is off
169
169
 
170
170
  [mainPre, auxPre, hEnv, type] = adjustStack(label)
171
- hOptions = {prefix: mainPre, itemPrefix: auxPre}
171
+ hOptions = {
172
+ prefix: mainPre
173
+ itemPrefix: auxPre
174
+ }
172
175
  switch type
173
176
  when 'enter'
174
177
  log label, hOptions
@@ -239,21 +239,26 @@ export pathTo = (fname, dir, direction="down") ->
239
239
 
240
240
  debug "enter pathTo('#{fname}','#{dir}','#{direction}')"
241
241
  assert fs.existsSync(dir), "Directory #{dir} does not exist"
242
- if fs.existsSync("#{dir}/#{fname}")
243
- debug "return from pathTo: #{dir}/#{fname} - file exists"
244
- return mkpath("#{dir}/#{fname}")
242
+ filepath = mkpath(dir, fname)
243
+ if fs.existsSync(filepath)
244
+ debug "return from pathTo: #{filepath} - file exists"
245
+ return filepath
245
246
  else if (direction == 'down')
246
247
  # --- Search all directories in this directory
248
+ # getSubDirs() returns dirs sorted alphabetically
247
249
  for subdir in getSubDirs(dir)
248
- if fpath = pathTo(fname, "#{dir}/#{subdir}")
250
+ dirpath = mkpath(dir, subdir)
251
+ debug "check #{dirpath}"
252
+ if fpath = pathTo(fname, dirpath)
249
253
  debug "return from pathTo: #{fpath}"
250
254
  return fpath
251
255
  else if (direction == 'up')
252
- while dir = getParentDir(dir)
253
- debug "check #{dir}"
254
- if fs.existsSync("#{dir}/#{fname}")
255
- debug "return from pathTo(): #{dir}/#{fname}"
256
- return "#{dir}/#{fname}"
256
+ while dirpath = getParentDir(dir)
257
+ debug "check #{dirpath}"
258
+ filepath = mkpath(dirpath, fname)
259
+ if fs.existsSync(filepath)
260
+ debug "return from pathTo(): #{filepath}"
261
+ return filepath
257
262
  else
258
263
  error "pathTo(): Invalid direction '#{direction}'"
259
264
  debug "return undef from pathTo - file not found"
@@ -322,34 +327,36 @@ export parseSource = (source) ->
322
327
 
323
328
  debug "enter parseSource()"
324
329
  if source == 'unit test'
325
- debug "return 'unit test' from parseSource()"
326
- return {
330
+ hSourceInfo = {
327
331
  filename: 'unit test'
328
332
  stub: 'unit test'
329
333
  }
334
+ debug "return from parseSource()", hSourceInfo
335
+ return hSourceInfo
330
336
  try
331
337
  hInfo = pathlib.parse(source)
332
- debug "return from parseSource()", hInfo
333
- if hInfo.root
338
+ if hInfo.dir
334
339
  dir = mkpath(hInfo.dir) # change \ to /
335
- return {
336
- dir: dir
340
+ hSourceInfo = {
341
+ dir
337
342
  fullpath: mkpath(dir, hInfo.base)
338
343
  filename: hInfo.base
339
344
  stub: hInfo.name
340
345
  ext: hInfo.ext
341
346
  }
342
347
  else
343
- return {
344
- dir: mkpath(hInfo.dir) # change \ to /
348
+ hSourceInfo = {
345
349
  filename: hInfo.base
346
350
  stub: hInfo.name
347
351
  ext: hInfo.ext
348
352
  }
353
+ debug "return from parseSource()", hSourceInfo
354
+ return hSourceInfo
349
355
  catch err
350
- debug "return '#{err.message} from parseSource()"
351
- return {
356
+ hSourceInfo = {
352
357
  filename: source
353
358
  stub: source
354
359
  error: err.message
355
360
  }
361
+ debug "return '#{err.message} from parseSource()", hSourceInfo
362
+ return hSourceInfo
package/src/fs_utils.js CHANGED
@@ -281,28 +281,33 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
281
281
 
282
282
  // ---------------------------------------------------------------------------
283
283
  export var pathTo = function(fname, dir, direction = "down") {
284
- var fpath, i, len, ref, subdir;
284
+ var dirpath, filepath, fpath, i, len, ref, subdir;
285
285
  debug(`enter pathTo('${fname}','${dir}','${direction}')`);
286
286
  assert(fs.existsSync(dir), `Directory ${dir} does not exist`);
287
- if (fs.existsSync(`${dir}/${fname}`)) {
288
- debug(`return from pathTo: ${dir}/${fname} - file exists`);
289
- return mkpath(`${dir}/${fname}`);
287
+ filepath = mkpath(dir, fname);
288
+ if (fs.existsSync(filepath)) {
289
+ debug(`return from pathTo: ${filepath} - file exists`);
290
+ return filepath;
290
291
  } else if (direction === 'down') {
291
292
  ref = getSubDirs(dir);
292
293
  // --- Search all directories in this directory
294
+ // getSubDirs() returns dirs sorted alphabetically
293
295
  for (i = 0, len = ref.length; i < len; i++) {
294
296
  subdir = ref[i];
295
- if (fpath = pathTo(fname, `${dir}/${subdir}`)) {
297
+ dirpath = mkpath(dir, subdir);
298
+ debug(`check ${dirpath}`);
299
+ if (fpath = pathTo(fname, dirpath)) {
296
300
  debug(`return from pathTo: ${fpath}`);
297
301
  return fpath;
298
302
  }
299
303
  }
300
304
  } else if (direction === 'up') {
301
- while (dir = getParentDir(dir)) {
302
- debug(`check ${dir}`);
303
- if (fs.existsSync(`${dir}/${fname}`)) {
304
- debug(`return from pathTo(): ${dir}/${fname}`);
305
- return `${dir}/${fname}`;
305
+ while (dirpath = getParentDir(dir)) {
306
+ debug(`check ${dirpath}`);
307
+ filepath = mkpath(dirpath, fname);
308
+ if (fs.existsSync(filepath)) {
309
+ debug(`return from pathTo(): ${filepath}`);
310
+ return filepath;
306
311
  }
307
312
  }
308
313
  } else {
@@ -367,7 +372,7 @@ export var shortenPath = function(path) {
367
372
 
368
373
  // ---------------------------------------------------------------------------
369
374
  export var parseSource = function(source) {
370
- var dir, err, hInfo;
375
+ var dir, err, hInfo, hSourceInfo;
371
376
  // --- returns {
372
377
  // dir
373
378
  // filename # only this is guaranteed to be set
@@ -376,39 +381,41 @@ export var parseSource = function(source) {
376
381
  // }
377
382
  debug("enter parseSource()");
378
383
  if (source === 'unit test') {
379
- debug("return 'unit test' from parseSource()");
380
- return {
384
+ hSourceInfo = {
381
385
  filename: 'unit test',
382
386
  stub: 'unit test'
383
387
  };
388
+ debug("return from parseSource()", hSourceInfo);
389
+ return hSourceInfo;
384
390
  }
385
391
  try {
386
392
  hInfo = pathlib.parse(source);
387
- debug("return from parseSource()", hInfo);
388
- if (hInfo.root) {
393
+ if (hInfo.dir) {
389
394
  dir = mkpath(hInfo.dir); // change \ to /
390
- return {
391
- dir: dir,
395
+ hSourceInfo = {
396
+ dir,
392
397
  fullpath: mkpath(dir, hInfo.base),
393
398
  filename: hInfo.base,
394
399
  stub: hInfo.name,
395
400
  ext: hInfo.ext
396
401
  };
397
402
  } else {
398
- return {
399
- dir: mkpath(hInfo.dir), // change \ to /
403
+ hSourceInfo = {
400
404
  filename: hInfo.base,
401
405
  stub: hInfo.name,
402
406
  ext: hInfo.ext
403
407
  };
404
408
  }
409
+ debug("return from parseSource()", hSourceInfo);
410
+ return hSourceInfo;
405
411
  } catch (error1) {
406
412
  err = error1;
407
- debug(`return '${err.message} from parseSource()`);
408
- return {
413
+ hSourceInfo = {
409
414
  filename: source,
410
415
  stub: source,
411
416
  error: err.message
412
417
  };
418
+ debug(`return '${err.message} from parseSource()`, hSourceInfo);
419
+ return hSourceInfo;
413
420
  }
414
421
  };
@@ -130,22 +130,6 @@ export tabify = (str, numSpaces=undef) ->
130
130
  lLines.push '\t'.repeat(prefixLen) + theRest
131
131
  return arrayToBlock(lLines)
132
132
 
133
- # ---------------------------------------------------------------------------
134
- # untabify - convert leading TABs to spaces
135
-
136
- untabify_old = (str, numSpaces=3) ->
137
-
138
- oneIndent = ' '.repeat(numSpaces)
139
- lLines = []
140
- for str in blockToArray(str)
141
- lMatches = str.match(/^(\t*)(.*)$/)
142
- [_, prefix, theRest] = lMatches
143
- if prefix == ''
144
- lLines.push theRest
145
- else
146
- lLines.push oneIndent.repeat(prefix.length) + theRest
147
- return arrayToBlock(lLines)
148
-
149
133
  # ---------------------------------------------------------------------------
150
134
  # untabify - convert ALL TABs to spaces
151
135
 
@@ -1,7 +1,5 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
- // indent_utils.coffee
3
- var untabify_old;
4
-
2
+ // indent_utils.coffee
5
3
  import {
6
4
  assert,
7
5
  undef,
@@ -162,26 +160,6 @@ export var tabify = function(str, numSpaces = undef) {
162
160
  return arrayToBlock(lLines);
163
161
  };
164
162
 
165
- // ---------------------------------------------------------------------------
166
- // untabify - convert leading TABs to spaces
167
- untabify_old = function(str, numSpaces = 3) {
168
- var _, i, lLines, lMatches, len, oneIndent, prefix, ref, theRest;
169
- oneIndent = ' '.repeat(numSpaces);
170
- lLines = [];
171
- ref = blockToArray(str);
172
- for (i = 0, len = ref.length; i < len; i++) {
173
- str = ref[i];
174
- lMatches = str.match(/^(\t*)(.*)$/);
175
- [_, prefix, theRest] = lMatches;
176
- if (prefix === '') {
177
- lLines.push(theRest);
178
- } else {
179
- lLines.push(oneIndent.repeat(prefix.length) + theRest);
180
- }
181
- }
182
- return arrayToBlock(lLines);
183
- };
184
-
185
163
  // ---------------------------------------------------------------------------
186
164
  // untabify - convert ALL TABs to spaces
187
165
  export var untabify = function(str, numSpaces = 3) {
@@ -134,17 +134,15 @@ export log = (str, hOptions={}) ->
134
134
 
135
135
  export logItem = (label, item, hOptions={}) ->
136
136
  # --- valid options:
137
- # prefix
138
- # itemPrefix
137
+ # prefix - not used
138
+ # itemPrefix - always used
139
139
 
140
140
  assert isFunction(putstr), "putstr not properly set"
141
141
  assert !label || isString(label), "label a non-string"
142
142
  assert isHash(hOptions), "arg 3 not a hash"
143
143
 
144
144
  label = fixStr(label)
145
- prefix = fixStr(hOptions.prefix)
146
- itemPrefix = fixStr(hOptions.itemPrefix || prefix)
147
-
145
+ prefix = fixStr(hOptions.itemPrefix || hOptions.prefix)
148
146
  labelStr = if label then "#{label} = " else ""
149
147
 
150
148
  if (item == undef)
@@ -155,16 +153,16 @@ export logItem = (label, item, hOptions={}) ->
155
153
  else
156
154
  if label
157
155
  putstr "#{prefix}#{label}:"
158
- putBlock item, itemPrefix
156
+ putBlock item, prefix
159
157
  else if isNumber(item)
160
158
  putstr "#{prefix}#{labelStr}#{item}"
161
159
  else
162
- putstr "#{itemPrefix}#{sep_dash}"
160
+ putstr "#{prefix}#{sep_dash}"
163
161
  if label
164
162
  putstr "#{prefix}#{label}:"
165
163
  for str in blockToArray(stringify(item, true)) # escape special chars
166
- putstr "#{itemPrefix}#{indentation(1)}#{fixStr(str)}"
167
- putstr "#{itemPrefix}#{sep_dash}"
164
+ putstr "#{prefix}#{indentation(1)}#{fixStr(str)}"
165
+ putstr "#{prefix}#{sep_dash}"
168
166
 
169
167
  return
170
168
 
package/src/log_utils.js CHANGED
@@ -151,16 +151,15 @@ export var log = function(str, hOptions = {}) {
151
151
 
152
152
  // ---------------------------------------------------------------------------
153
153
  export var logItem = function(label, item, hOptions = {}) {
154
- var i, itemPrefix, labelStr, len, prefix, ref, str;
154
+ var i, labelStr, len, prefix, ref, str;
155
155
  // --- valid options:
156
- // prefix
157
- // itemPrefix
156
+ // prefix - not used
157
+ // itemPrefix - always used
158
158
  assert(isFunction(putstr), "putstr not properly set");
159
159
  assert(!label || isString(label), "label a non-string");
160
160
  assert(isHash(hOptions), "arg 3 not a hash");
161
161
  label = fixStr(label);
162
- prefix = fixStr(hOptions.prefix);
163
- itemPrefix = fixStr(hOptions.itemPrefix || prefix);
162
+ prefix = fixStr(hOptions.itemPrefix || hOptions.prefix);
164
163
  labelStr = label ? `${label} = ` : "";
165
164
  if (item === undef) {
166
165
  putstr(`${prefix}${labelStr}undef`);
@@ -171,12 +170,12 @@ export var logItem = function(label, item, hOptions = {}) {
171
170
  if (label) {
172
171
  putstr(`${prefix}${label}:`);
173
172
  }
174
- putBlock(item, itemPrefix);
173
+ putBlock(item, prefix);
175
174
  }
176
175
  } else if (isNumber(item)) {
177
176
  putstr(`${prefix}${labelStr}${item}`);
178
177
  } else {
179
- putstr(`${itemPrefix}${sep_dash}`);
178
+ putstr(`${prefix}${sep_dash}`);
180
179
  if (label) {
181
180
  putstr(`${prefix}${label}:`);
182
181
  }
@@ -184,9 +183,9 @@ export var logItem = function(label, item, hOptions = {}) {
184
183
  // escape special chars
185
184
  for (i = 0, len = ref.length; i < len; i++) {
186
185
  str = ref[i];
187
- putstr(`${itemPrefix}${indentation(1)}${fixStr(str)}`);
186
+ putstr(`${prefix}${indentation(1)}${fixStr(str)}`);
188
187
  }
189
- putstr(`${itemPrefix}${sep_dash}`);
188
+ putstr(`${prefix}${sep_dash}`);
190
189
  }
191
190
  };
192
191