@jdeighan/coffee-utils 7.0.30 → 7.0.33

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.30",
4
+ "version": "7.0.33",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -19,12 +19,13 @@ export error = (message) ->
19
19
 
20
20
  # ---------------------------------------------------------------------------
21
21
  # assert - mimic nodejs's assert
22
+ # return true so we can use it in boolean expressions
22
23
 
23
24
  export assert = (cond, msg) ->
24
25
 
25
26
  if ! cond
26
27
  error(msg)
27
- return
28
+ return true
28
29
 
29
30
  # ---------------------------------------------------------------------------
30
31
  # croak - throws an error after possibly printing useful info
@@ -85,9 +86,16 @@ export isArray = (x) ->
85
86
 
86
87
  # ---------------------------------------------------------------------------
87
88
 
88
- export isHash = (x) ->
89
+ export isHash = (x, lKeys) ->
89
90
 
90
- return (getClassName(x) == 'Object')
91
+ if ! x || (getClassName(x) != 'Object')
92
+ return false
93
+ if defined(lKeys)
94
+ assert isArray(lKeys), "isHash(): lKeys not an array"
95
+ for key in lKeys
96
+ if ! x.hasOwnProperty(key)
97
+ return false
98
+ return true
91
99
 
92
100
  # ---------------------------------------------------------------------------
93
101
 
@@ -103,7 +111,7 @@ export hashHasKey = (x, key) ->
103
111
 
104
112
  export isEmpty = (x) ->
105
113
 
106
- if ! x?
114
+ if (x == undef) || (x == null)
107
115
  return true
108
116
  if isString(x)
109
117
  return x.match(/^\s*$/)
@@ -112,7 +120,7 @@ export isEmpty = (x) ->
112
120
  if isHash(x)
113
121
  return Object.keys(x).length == 0
114
122
  else
115
- error "isEmpty(): Invalid parameter"
123
+ return false
116
124
 
117
125
  # ---------------------------------------------------------------------------
118
126
  # nonEmpty
@@ -383,15 +391,16 @@ export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
383
391
 
384
392
  replacerFunc = (match, prefix, name) =>
385
393
  if prefix
386
- result = process.env[name]
387
- else if ! hVars[name]?
388
- result = 'undef'
394
+ return process.env[name]
389
395
  else
390
- result = hVars[name]
391
- if ! isString(result)
392
- result = JSON.stringify(result)
393
- return result
394
-
396
+ value = hVars[name]
397
+ if defined(value)
398
+ if isString(value)
399
+ return value
400
+ else
401
+ return JSON.stringify(value)
402
+ else
403
+ return "__#{name}__"
395
404
  return line.replace(rx, replacerFunc)
396
405
 
397
406
  # ---------------------------------------------------------------------------
@@ -25,10 +25,12 @@ export var error = function(message) {
25
25
 
26
26
  // ---------------------------------------------------------------------------
27
27
  // assert - mimic nodejs's assert
28
+ // return true so we can use it in boolean expressions
28
29
  export var assert = function(cond, msg) {
29
30
  if (!cond) {
30
31
  error(msg);
31
32
  }
33
+ return true;
32
34
  };
33
35
 
34
36
  // ---------------------------------------------------------------------------
@@ -77,8 +79,21 @@ export var isArray = function(x) {
77
79
  };
78
80
 
79
81
  // ---------------------------------------------------------------------------
80
- export var isHash = function(x) {
81
- return getClassName(x) === 'Object';
82
+ export var isHash = function(x, lKeys) {
83
+ var i, key, len;
84
+ if (!x || (getClassName(x) !== 'Object')) {
85
+ return false;
86
+ }
87
+ if (defined(lKeys)) {
88
+ assert(isArray(lKeys), "isHash(): lKeys not an array");
89
+ for (i = 0, len = lKeys.length; i < len; i++) {
90
+ key = lKeys[i];
91
+ if (!x.hasOwnProperty(key)) {
92
+ return false;
93
+ }
94
+ }
95
+ }
96
+ return true;
82
97
  };
83
98
 
84
99
  // ---------------------------------------------------------------------------
@@ -92,7 +107,7 @@ export var hashHasKey = function(x, key) {
92
107
  // isEmpty
93
108
  // - string is whitespace, array has no elements, hash has no keys
94
109
  export var isEmpty = function(x) {
95
- if (x == null) {
110
+ if ((x === undef) || (x === null)) {
96
111
  return true;
97
112
  }
98
113
  if (isString(x)) {
@@ -104,7 +119,7 @@ export var isEmpty = function(x) {
104
119
  if (isHash(x)) {
105
120
  return Object.keys(x).length === 0;
106
121
  } else {
107
- return error("isEmpty(): Invalid parameter");
122
+ return false;
108
123
  }
109
124
  };
110
125
 
@@ -413,18 +428,21 @@ export var replaceVars = function(line, hVars = {}, rx = /__(env\.)?([A-Za-z_]\w
413
428
  var replacerFunc;
414
429
  assert(isHash(hVars), "replaceVars() hVars is not a hash");
415
430
  replacerFunc = (match, prefix, name) => {
416
- var result;
431
+ var value;
417
432
  if (prefix) {
418
- result = process.env[name];
419
- } else if (hVars[name] == null) {
420
- result = 'undef';
433
+ return process.env[name];
421
434
  } else {
422
- result = hVars[name];
423
- if (!isString(result)) {
424
- result = JSON.stringify(result);
435
+ value = hVars[name];
436
+ if (defined(value)) {
437
+ if (isString(value)) {
438
+ return value;
439
+ } else {
440
+ return JSON.stringify(value);
441
+ }
442
+ } else {
443
+ return `__${name}__`;
425
444
  }
426
445
  }
427
- return result;
428
446
  };
429
447
  return line.replace(rx, replacerFunc);
430
448
  };
@@ -361,6 +361,7 @@ export parseSource = (source) ->
361
361
  # fullpath
362
362
  # stub
363
363
  # ext
364
+ # purpose
364
365
  # }
365
366
  # --- NOTE: source may be a file URL, e.g. import.meta.url
366
367
 
@@ -394,6 +395,13 @@ export parseSource = (source) ->
394
395
  stub: hInfo.name
395
396
  ext: hInfo.ext
396
397
  }
398
+
399
+ # --- check for a 'purpose'
400
+ if lMatches = hSourceInfo.stub.match(///
401
+ \.
402
+ ([A-Za-z_]+)
403
+ $///)
404
+ hSourceInfo.purpose = lMatches[1]
397
405
  debug "return from parseSource()", hSourceInfo
398
406
  return hSourceInfo
399
407
 
package/src/fs_utils.js CHANGED
@@ -422,13 +422,14 @@ export var shortenPath = function(path) {
422
422
 
423
423
  // ---------------------------------------------------------------------------
424
424
  export var parseSource = function(source) {
425
- var dir, hInfo, hSourceInfo;
425
+ var dir, hInfo, hSourceInfo, lMatches;
426
426
  // --- returns {
427
427
  // dir
428
428
  // filename
429
429
  // fullpath
430
430
  // stub
431
431
  // ext
432
+ // purpose
432
433
  // }
433
434
  // --- NOTE: source may be a file URL, e.g. import.meta.url
434
435
  debug("enter parseSource()");
@@ -462,6 +463,10 @@ export var parseSource = function(source) {
462
463
  ext: hInfo.ext
463
464
  };
464
465
  }
466
+ // --- check for a 'purpose'
467
+ if (lMatches = hSourceInfo.stub.match(/\.([A-Za-z_]+)$/)) {
468
+ hSourceInfo.purpose = lMatches[1];
469
+ }
465
470
  }
466
471
  debug("return from parseSource()", hSourceInfo);
467
472
  return hSourceInfo;
@@ -9,7 +9,7 @@ import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
9
9
  # ---------------------------------------------------------------------------
10
10
  # NOTE: Currently, only TAB indentation is supported
11
11
  # ---------------------------------------------------------------------------
12
- # splitLine - separate a line into {level, line}
12
+ # splitLine - separate a line into [level, line]
13
13
 
14
14
  export splitLine = (line) ->
15
15
 
@@ -21,7 +21,7 @@ import {
21
21
  // ---------------------------------------------------------------------------
22
22
  // NOTE: Currently, only TAB indentation is supported
23
23
  // ---------------------------------------------------------------------------
24
- // splitLine - separate a line into {level, line}
24
+ // splitLine - separate a line into [level, line]
25
25
  export var splitLine = function(line) {
26
26
  var lMatches;
27
27
  assert(line != null, "splitLine(): line is undef");