@jdeighan/coffee-utils 7.0.20 → 7.0.23

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.20",
4
+ "version": "7.0.23",
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)]
@@ -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)];
@@ -7,7 +7,7 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
9
  assert, undef, pass, rtrim, error, isEmpty, nonEmpty,
10
- isString, isArray, isRegExp, isFunction, croak,
10
+ isString, isArray, isRegExp, isFunction, croak, OL,
11
11
  } from '@jdeighan/coffee-utils'
12
12
  import {log, LOG} from '@jdeighan/coffee-utils/log'
13
13
  import {debug} from '@jdeighan/coffee-utils/debug'
@@ -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,34 +358,42 @@ export parseSource = (source) ->
343
358
  # --- returns {
344
359
  # dir
345
360
  # filename
361
+ # fullpath
346
362
  # stub
347
363
  # ext
348
364
  # }
349
365
  # --- NOTE: source may be a file URL, e.g. import.meta.url
350
366
 
351
367
  debug "enter parseSource()"
352
- assert isString(source), "parseSource(): source not a string"
368
+ assert isString(source),\
369
+ "parseSource(): source not a string: #{OL(source)}"
353
370
  if source == 'unit test'
354
371
  croak "A source of 'unit test' is deprecated"
355
372
  if source.match(/^file\:\/\//)
356
373
  source = urllib.fileURLToPath(source)
357
374
 
358
- hInfo = pathlib.parse(source)
359
- if hInfo.dir
360
- dir = mkpath(hInfo.dir) # change \ to /
375
+ if isDir(source)
361
376
  hSourceInfo = {
362
- dir
363
- fullpath: mkpath(dir, hInfo.base)
364
- filename: hInfo.base
365
- stub: hInfo.name
366
- ext: hInfo.ext
377
+ dir: source
378
+ fullpath: source
367
379
  }
368
380
  else
369
- hSourceInfo = {
370
- filename: hInfo.base
371
- stub: hInfo.name
372
- ext: hInfo.ext
373
- }
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
+ }
374
397
  debug "return from parseSource()", hSourceInfo
375
398
  return hSourceInfo
376
399
 
package/src/fs_utils.js CHANGED
@@ -22,7 +22,8 @@ import {
22
22
  isArray,
23
23
  isRegExp,
24
24
  isFunction,
25
- croak
25
+ croak,
26
+ OL
26
27
  } from '@jdeighan/coffee-utils';
27
28
 
28
29
  import {
@@ -84,7 +85,16 @@ export var isFile = function(fullpath) {
84
85
 
85
86
  // ---------------------------------------------------------------------------
86
87
  export var isDir = function(fullpath) {
87
- 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
+ }
88
98
  };
89
99
 
90
100
  // ---------------------------------------------------------------------------
@@ -94,6 +104,17 @@ export var isSimpleFileName = function(path) {
94
104
  return !h.root && !h.dir && h.base;
95
105
  };
96
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
+
97
118
  // ---------------------------------------------------------------------------
98
119
  export var fileExt = function(path) {
99
120
  var lMatches;
@@ -405,34 +426,42 @@ export var parseSource = function(source) {
405
426
  // --- returns {
406
427
  // dir
407
428
  // filename
429
+ // fullpath
408
430
  // stub
409
431
  // ext
410
432
  // }
411
433
  // --- NOTE: source may be a file URL, e.g. import.meta.url
412
434
  debug("enter parseSource()");
413
- assert(isString(source), "parseSource(): source not a string");
435
+ assert(isString(source), `parseSource(): source not a string: ${OL(source)}`);
414
436
  if (source === 'unit test') {
415
437
  croak("A source of 'unit test' is deprecated");
416
438
  }
417
439
  if (source.match(/^file\:\/\//)) {
418
440
  source = urllib.fileURLToPath(source);
419
441
  }
420
- hInfo = pathlib.parse(source);
421
- if (hInfo.dir) {
422
- dir = mkpath(hInfo.dir); // change \ to /
442
+ if (isDir(source)) {
423
443
  hSourceInfo = {
424
- dir,
425
- fullpath: mkpath(dir, hInfo.base),
426
- filename: hInfo.base,
427
- stub: hInfo.name,
428
- ext: hInfo.ext
444
+ dir: source,
445
+ fullpath: source
429
446
  };
430
447
  } else {
431
- hSourceInfo = {
432
- filename: hInfo.base,
433
- stub: hInfo.name,
434
- ext: hInfo.ext
435
- };
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
+ }
436
465
  }
437
466
  debug("return from parseSource()", hSourceInfo);
438
467
  return hSourceInfo;