@jdeighan/coffee-utils 7.0.22 → 7.0.25

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.22",
4
+ "version": "7.0.25",
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
  # ---------------------------------------------------------------------------
@@ -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
 
@@ -130,7 +129,7 @@ var lineNum = 0
130
129
  for await (const line of rl) {
131
130
  lineNum += 1
132
131
  // Each line will be successively available here as 'line'
133
- if (! isComment(line) && func(line, lineNum)) {
132
+ if (func(line, lineNum)) {
134
133
  rl.close(); // close if true return value
135
134
  return;
136
135
  }
@@ -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+/)
@@ -202,6 +184,19 @@ export isInteger = (x) ->
202
184
 
203
185
  # ---------------------------------------------------------------------------
204
186
 
187
+ export isUniqueList = (lItems) ->
188
+
189
+ if ! lItems?
190
+ return true # empty list is unique
191
+ h = {}
192
+ for item in lItems
193
+ if h[item]
194
+ return false
195
+ h[item] = 1
196
+ return true
197
+
198
+ # ---------------------------------------------------------------------------
199
+
205
200
  export uniq = (lItems) ->
206
201
 
207
202
  return [...new Set(lItems)]
@@ -384,6 +379,25 @@ export strcat = (lItems...) ->
384
379
 
385
380
  export replaceVars = (line, hVars={}, rx=/__(env\.)?([A-Za-z_]\w*)__/g) ->
386
381
 
382
+ assert isHash(hVars), "replaceVars() hVars is not a hash"
383
+
387
384
  replacerFunc = (match, prefix, name) =>
388
- 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
+
389
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+/);
@@ -202,6 +184,23 @@ export var isInteger = function(x) {
202
184
  }
203
185
  };
204
186
 
187
+ // ---------------------------------------------------------------------------
188
+ export var isUniqueList = function(lItems) {
189
+ var h, i, item, len;
190
+ if (lItems == null) {
191
+ return true; // empty list is unique
192
+ }
193
+ h = {};
194
+ for (i = 0, len = lItems.length; i < len; i++) {
195
+ item = lItems[i];
196
+ if (h[item]) {
197
+ return false;
198
+ }
199
+ h[item] = 1;
200
+ }
201
+ return true;
202
+ };
203
+
205
204
  // ---------------------------------------------------------------------------
206
205
  export var uniq = function(lItems) {
207
206
  return [...new Set(lItems)];
@@ -412,12 +411,28 @@ export var strcat = function(...lItems) {
412
411
  // ---------------------------------------------------------------------------
413
412
  export var replaceVars = function(line, hVars = {}, rx = /__(env\.)?([A-Za-z_]\w*)__/g) {
414
413
  var replacerFunc;
414
+ assert(isHash(hVars), "replaceVars() hVars is not a hash");
415
415
  replacerFunc = (match, prefix, name) => {
416
+ var result;
416
417
  if (prefix) {
417
- return process.env[name];
418
+ result = process.env[name];
419
+ } else if (hVars[name] == null) {
420
+ result = 'undef';
418
421
  } else {
419
- return hVars[name].toString();
422
+ result = hVars[name];
423
+ if (!isString(result)) {
424
+ result = JSON.stringify(result);
425
+ }
420
426
  }
427
+ return result;
421
428
  };
422
429
  return line.replace(rx, replacerFunc);
423
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
+ };