@jdeighan/coffee-utils 7.0.23 → 7.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.23",
4
+ "version": "7.0.26",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -4,7 +4,7 @@ import fs from 'fs'
4
4
  import readline from 'readline'
5
5
 
6
6
  import {
7
- assert, isEmpty, isString, nonEmpty, error, isComment, rtrim,
7
+ assert, isEmpty, isString, nonEmpty, error, rtrim,
8
8
  } from '@jdeighan/coffee-utils'
9
9
 
10
10
  # ---------------------------------------------------------------------------
@@ -84,7 +84,7 @@ export truncateBlock = (str, numLines) ->
84
84
  export joinBlocks = (lBlocks...) ->
85
85
 
86
86
  lNonEmptyBlocks = []
87
- for block in lBlocks
87
+ for block in lBlocks.flat(999)
88
88
  assert isString(block), "joinBlocks(): #{block} is not a string"
89
89
  if nonEmpty(block)
90
90
  lNonEmptyBlocks.push block
@@ -108,7 +108,7 @@ var lineNum = 0
108
108
  for await (const line of rl) {
109
109
  lineNum += 1
110
110
  // Each line will be successively available here as 'line'
111
- if (! isComment(line) && func(line, lineNum)) {
111
+ if (func(line, lineNum)) {
112
112
  rl.close(); // close if true return value
113
113
  return;
114
114
  }
@@ -10,7 +10,6 @@ import {
10
10
  isString,
11
11
  nonEmpty,
12
12
  error,
13
- isComment,
14
13
  rtrim
15
14
  } from '@jdeighan/coffee-utils';
16
15
 
@@ -102,10 +101,11 @@ export var truncateBlock = function(str, numLines) {
102
101
 
103
102
  // ---------------------------------------------------------------------------
104
103
  export var joinBlocks = function(...lBlocks) {
105
- var block, i, lNonEmptyBlocks, len1;
104
+ var block, i, lNonEmptyBlocks, len1, ref;
106
105
  lNonEmptyBlocks = [];
107
- for (i = 0, len1 = lBlocks.length; i < len1; i++) {
108
- block = lBlocks[i];
106
+ ref = lBlocks.flat(999);
107
+ for (i = 0, len1 = ref.length; i < len1; i++) {
108
+ block = ref[i];
109
109
  assert(isString(block), `joinBlocks(): ${block} is not a string`);
110
110
  if (nonEmpty(block)) {
111
111
  lNonEmptyBlocks.push(block);
@@ -130,7 +130,7 @@ var lineNum = 0
130
130
  for await (const line of rl) {
131
131
  lineNum += 1
132
132
  // Each line will be successively available here as 'line'
133
- if (! isComment(line) && func(line, lineNum)) {
133
+ if (func(line, lineNum)) {
134
134
  rl.close(); // close if true return value
135
135
  return;
136
136
  }
@@ -133,24 +133,6 @@ export nonEmpty = (x) ->
133
133
 
134
134
  # ---------------------------------------------------------------------------
135
135
 
136
- commentRegExp = /^\s*\#+(?:\s|$)/
137
-
138
- # ---------------------------------------------------------------------------
139
-
140
- export setCommentRegexp = (regexp) ->
141
-
142
- commentRegExp = regexp
143
- return
144
-
145
- # ---------------------------------------------------------------------------
146
-
147
- export isComment = (str) ->
148
-
149
- assert isString(str), "isComment(): not a string"
150
- return if str.match(commentRegExp) then true else false
151
-
152
- # ---------------------------------------------------------------------------
153
-
154
136
  export words = (str) ->
155
137
 
156
138
  return str.trim().split(/\s+/)
@@ -397,6 +379,25 @@ export strcat = (lItems...) ->
397
379
 
398
380
  export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
399
381
 
382
+ assert isHash(hVars), "replaceVars() hVars is not a hash"
383
+
400
384
  replacerFunc = (match, prefix, name) =>
401
- return if prefix then process.env[name] else hVars[name].toString()
385
+ if prefix
386
+ result = process.env[name]
387
+ else if ! hVars[name]?
388
+ result = 'undef'
389
+ else
390
+ result = hVars[name]
391
+ if ! isString(result)
392
+ result = JSON.stringify(result)
393
+ return result
394
+
402
395
  return line.replace(rx, replacerFunc)
396
+
397
+ # ---------------------------------------------------------------------------
398
+
399
+ export isIterable = (object) ->
400
+
401
+ if (object == undef) || (object == null)
402
+ return false
403
+ return typeof object[Symbol.iterator] == 'function'
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // coffee_utils.coffee
3
- var LOG, commentRegExp;
3
+ var LOG;
4
4
 
5
5
  export var sep_dash = '-'.repeat(42);
6
6
 
@@ -128,24 +128,6 @@ export var nonEmpty = function(x) {
128
128
  }
129
129
  };
130
130
 
131
- // ---------------------------------------------------------------------------
132
- commentRegExp = /^\s*\#+(?:\s|$)/;
133
-
134
- // ---------------------------------------------------------------------------
135
- export var setCommentRegexp = function(regexp) {
136
- commentRegExp = regexp;
137
- };
138
-
139
- // ---------------------------------------------------------------------------
140
- export var isComment = function(str) {
141
- assert(isString(str), "isComment(): not a string");
142
- if (str.match(commentRegExp)) {
143
- return true;
144
- } else {
145
- return false;
146
- }
147
- };
148
-
149
131
  // ---------------------------------------------------------------------------
150
132
  export var words = function(str) {
151
133
  return str.trim().split(/\s+/);
@@ -429,12 +411,28 @@ export var strcat = function(...lItems) {
429
411
  // ---------------------------------------------------------------------------
430
412
  export var replaceVars = function(line, hVars = {}, rx = /__(env\.)?([A-Za-z_]\w*)__/g) {
431
413
  var replacerFunc;
414
+ assert(isHash(hVars), "replaceVars() hVars is not a hash");
432
415
  replacerFunc = (match, prefix, name) => {
416
+ var result;
433
417
  if (prefix) {
434
- return process.env[name];
418
+ result = process.env[name];
419
+ } else if (hVars[name] == null) {
420
+ result = 'undef';
435
421
  } else {
436
- return hVars[name].toString();
422
+ result = hVars[name];
423
+ if (!isString(result)) {
424
+ result = JSON.stringify(result);
425
+ }
437
426
  }
427
+ return result;
438
428
  };
439
429
  return line.replace(rx, replacerFunc);
440
430
  };
431
+
432
+ // ---------------------------------------------------------------------------
433
+ export var isIterable = function(object) {
434
+ if ((object === undef) || (object === null)) {
435
+ return false;
436
+ }
437
+ return typeof object[Symbol.iterator] === 'function';
438
+ };