@jdeighan/coffee-utils 7.0.21 → 7.0.24

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.21",
4
+ "version": "7.0.24",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -202,6 +202,19 @@ export isInteger = (x) ->
202
202
 
203
203
  # ---------------------------------------------------------------------------
204
204
 
205
+ export isUniqueList = (lItems) ->
206
+
207
+ if ! lItems?
208
+ return true # empty list is unique
209
+ h = {}
210
+ for item in lItems
211
+ if h[item]
212
+ return false
213
+ h[item] = 1
214
+ return true
215
+
216
+ # ---------------------------------------------------------------------------
217
+
205
218
  export uniq = (lItems) ->
206
219
 
207
220
  return [...new Set(lItems)]
@@ -384,6 +397,17 @@ export strcat = (lItems...) ->
384
397
 
385
398
  export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
386
399
 
400
+ assert isHash(hVars), "replaceVars() hVars is not a hash"
401
+
387
402
  replacerFunc = (match, prefix, name) =>
388
- return if prefix then process.env[name] else hVars[name].toString()
403
+ if prefix
404
+ result = process.env[name]
405
+ else if ! hVars[name]?
406
+ result = 'undef'
407
+ else
408
+ result = hVars[name]
409
+ if ! isString(result)
410
+ result = JSON.stringify(result)
411
+ return result
412
+
389
413
  return line.replace(rx, replacerFunc)
@@ -202,6 +202,23 @@ export var isInteger = function(x) {
202
202
  }
203
203
  };
204
204
 
205
+ // ---------------------------------------------------------------------------
206
+ export var isUniqueList = function(lItems) {
207
+ var h, i, item, len;
208
+ if (lItems == null) {
209
+ return true; // empty list is unique
210
+ }
211
+ h = {};
212
+ for (i = 0, len = lItems.length; i < len; i++) {
213
+ item = lItems[i];
214
+ if (h[item]) {
215
+ return false;
216
+ }
217
+ h[item] = 1;
218
+ }
219
+ return true;
220
+ };
221
+
205
222
  // ---------------------------------------------------------------------------
206
223
  export var uniq = function(lItems) {
207
224
  return [...new Set(lItems)];
@@ -412,12 +429,20 @@ export var strcat = function(...lItems) {
412
429
  // ---------------------------------------------------------------------------
413
430
  export var replaceVars = function(line, hVars = {}, rx = /__(env\.)?([A-Za-z_]\w*)__/g) {
414
431
  var replacerFunc;
432
+ assert(isHash(hVars), "replaceVars() hVars is not a hash");
415
433
  replacerFunc = (match, prefix, name) => {
434
+ var result;
416
435
  if (prefix) {
417
- return process.env[name];
436
+ result = process.env[name];
437
+ } else if (hVars[name] == null) {
438
+ result = 'undef';
418
439
  } else {
419
- return hVars[name].toString();
440
+ result = hVars[name];
441
+ if (!isString(result)) {
442
+ result = JSON.stringify(result);
443
+ }
420
444
  }
445
+ return result;
421
446
  };
422
447
  return line.replace(rx, replacerFunc);
423
448
  };
@@ -62,7 +62,12 @@ export isFile = (fullpath) ->
62
62
 
63
63
  export isDir = (fullpath) ->
64
64
 
65
- return fs.lstatSync(fullpath).isDirectory()
65
+ try
66
+ obj = fs.lstatSync(fullpath)
67
+ if !obj? then return false
68
+ return obj.isDirectory()
69
+ catch
70
+ return false
66
71
 
67
72
  # ---------------------------------------------------------------------------
68
73
 
@@ -73,6 +78,16 @@ export isSimpleFileName = (path) ->
73
78
 
74
79
  # ---------------------------------------------------------------------------
75
80
 
81
+ export fileStub = (path) ->
82
+
83
+ assert isString(path), "fileExt(): path not a string"
84
+ if lMatches = path.match(/^(.*)\.[A-Za-z0-9_]+$/)
85
+ return lMatches[1]
86
+ else
87
+ return ''
88
+
89
+ # ---------------------------------------------------------------------------
90
+
76
91
  export fileExt = (path) ->
77
92
 
78
93
  assert isString(path), "fileExt(): path not a string"
@@ -343,6 +358,7 @@ export parseSource = (source) ->
343
358
  # --- returns {
344
359
  # dir
345
360
  # filename
361
+ # fullpath
346
362
  # stub
347
363
  # ext
348
364
  # }
@@ -356,22 +372,28 @@ export parseSource = (source) ->
356
372
  if source.match(/^file\:\/\//)
357
373
  source = urllib.fileURLToPath(source)
358
374
 
359
- hInfo = pathlib.parse(source)
360
- if hInfo.dir
361
- dir = mkpath(hInfo.dir) # change \ to /
375
+ if isDir(source)
362
376
  hSourceInfo = {
363
- dir
364
- fullpath: mkpath(dir, hInfo.base)
365
- filename: hInfo.base
366
- stub: hInfo.name
367
- ext: hInfo.ext
377
+ dir: source
378
+ fullpath: source
368
379
  }
369
380
  else
370
- hSourceInfo = {
371
- filename: hInfo.base
372
- stub: hInfo.name
373
- ext: hInfo.ext
374
- }
381
+ hInfo = pathlib.parse(source)
382
+ if hInfo.dir
383
+ dir = mkpath(hInfo.dir) # change \ to /
384
+ hSourceInfo = {
385
+ dir
386
+ fullpath: mkpath(dir, hInfo.base)
387
+ filename: hInfo.base
388
+ stub: hInfo.name
389
+ ext: hInfo.ext
390
+ }
391
+ else
392
+ hSourceInfo = {
393
+ filename: hInfo.base
394
+ stub: hInfo.name
395
+ ext: hInfo.ext
396
+ }
375
397
  debug "return from parseSource()", hSourceInfo
376
398
  return hSourceInfo
377
399
 
package/src/fs_utils.js CHANGED
@@ -85,7 +85,16 @@ export var isFile = function(fullpath) {
85
85
 
86
86
  // ---------------------------------------------------------------------------
87
87
  export var isDir = function(fullpath) {
88
- return fs.lstatSync(fullpath).isDirectory();
88
+ var obj;
89
+ try {
90
+ obj = fs.lstatSync(fullpath);
91
+ if (obj == null) {
92
+ return false;
93
+ }
94
+ return obj.isDirectory();
95
+ } catch (error1) {
96
+ return false;
97
+ }
89
98
  };
90
99
 
91
100
  // ---------------------------------------------------------------------------
@@ -95,6 +104,17 @@ export var isSimpleFileName = function(path) {
95
104
  return !h.root && !h.dir && h.base;
96
105
  };
97
106
 
107
+ // ---------------------------------------------------------------------------
108
+ export var fileStub = function(path) {
109
+ var lMatches;
110
+ assert(isString(path), "fileExt(): path not a string");
111
+ if (lMatches = path.match(/^(.*)\.[A-Za-z0-9_]+$/)) {
112
+ return lMatches[1];
113
+ } else {
114
+ return '';
115
+ }
116
+ };
117
+
98
118
  // ---------------------------------------------------------------------------
99
119
  export var fileExt = function(path) {
100
120
  var lMatches;
@@ -406,6 +426,7 @@ export var parseSource = function(source) {
406
426
  // --- returns {
407
427
  // dir
408
428
  // filename
429
+ // fullpath
409
430
  // stub
410
431
  // ext
411
432
  // }
@@ -418,22 +439,29 @@ export var parseSource = function(source) {
418
439
  if (source.match(/^file\:\/\//)) {
419
440
  source = urllib.fileURLToPath(source);
420
441
  }
421
- hInfo = pathlib.parse(source);
422
- if (hInfo.dir) {
423
- dir = mkpath(hInfo.dir); // change \ to /
442
+ if (isDir(source)) {
424
443
  hSourceInfo = {
425
- dir,
426
- fullpath: mkpath(dir, hInfo.base),
427
- filename: hInfo.base,
428
- stub: hInfo.name,
429
- ext: hInfo.ext
444
+ dir: source,
445
+ fullpath: source
430
446
  };
431
447
  } else {
432
- hSourceInfo = {
433
- filename: hInfo.base,
434
- stub: hInfo.name,
435
- ext: hInfo.ext
436
- };
448
+ hInfo = pathlib.parse(source);
449
+ if (hInfo.dir) {
450
+ dir = mkpath(hInfo.dir); // change \ to /
451
+ hSourceInfo = {
452
+ dir,
453
+ fullpath: mkpath(dir, hInfo.base),
454
+ filename: hInfo.base,
455
+ stub: hInfo.name,
456
+ ext: hInfo.ext
457
+ };
458
+ } else {
459
+ hSourceInfo = {
460
+ filename: hInfo.base,
461
+ stub: hInfo.name,
462
+ ext: hInfo.ext
463
+ };
464
+ }
437
465
  }
438
466
  debug("return from parseSource()", hSourceInfo);
439
467
  return hSourceInfo;