@jdeighan/coffee-utils 3.0.2 → 3.0.6

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": "3.0.2",
4
+ "version": "3.0.6",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -301,26 +301,15 @@ export OL = oneline
301
301
 
302
302
  # ---------------------------------------------------------------------------
303
303
 
304
- export removeCR = (block) ->
304
+ export removeCR = (str) ->
305
305
 
306
- return block.replace(/\r/g, '')
306
+ return str.replace(/\r/g, '')
307
307
 
308
308
  # ---------------------------------------------------------------------------
309
309
 
310
- export splitBlock = (block) ->
310
+ export CWS = (str) ->
311
311
 
312
- block = removeCR(block)
313
- if pos = block.indexOf("\n")
314
- # --- pos is also the length of the 1st line
315
- # 2nd arg to substr() is number of characters to return
316
- return [block.substr(0, pos), block.substr(pos+1)]
317
- else
318
- return [block, '']
319
-
320
- # ---------------------------------------------------------------------------
321
-
322
- export CWS = (block) ->
323
-
324
- return block.trim().replace(/\s+/g, ' ')
312
+ assert isString(str), "CWS(): parameter not a string"
313
+ return str.trim().replace(/\s+/sg, ' ')
325
314
 
326
315
  # ---------------------------------------------------------------------------
@@ -316,26 +316,14 @@ export var oneline = function(obj) {
316
316
  export var OL = oneline;
317
317
 
318
318
  // ---------------------------------------------------------------------------
319
- export var removeCR = function(block) {
320
- return block.replace(/\r/g, '');
319
+ export var removeCR = function(str) {
320
+ return str.replace(/\r/g, '');
321
321
  };
322
322
 
323
323
  // ---------------------------------------------------------------------------
324
- export var splitBlock = function(block) {
325
- var pos;
326
- block = removeCR(block);
327
- if (pos = block.indexOf("\n")) {
328
- // --- pos is also the length of the 1st line
329
- // 2nd arg to substr() is number of characters to return
330
- return [block.substr(0, pos), block.substr(pos + 1)];
331
- } else {
332
- return [block, ''];
333
- }
334
- };
335
-
336
- // ---------------------------------------------------------------------------
337
- export var CWS = function(block) {
338
- return block.trim().replace(/\s+/g, ' ');
324
+ export var CWS = function(str) {
325
+ assert(isString(str), "CWS(): parameter not a string");
326
+ return str.trim().replace(/\s+/sg, ' ');
339
327
  };
340
328
 
341
329
  // ---------------------------------------------------------------------------
@@ -3,7 +3,7 @@
3
3
  import {strict as assert} from 'assert'
4
4
  import {
5
5
  undef, error, escapeStr,
6
- oneline, isInteger, isString, isArray, isEmpty, rtrim,
6
+ OL, isInteger, isString, isArray, isEmpty, rtrim,
7
7
  } from '@jdeighan/coffee-utils'
8
8
  import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
9
9
 
@@ -49,13 +49,16 @@ export indented = (input, level=0) ->
49
49
 
50
50
  toAdd = indentation(level)
51
51
  if isArray(input)
52
- lLines = for line in input
53
- "#{toAdd}#{line}"
54
- return lLines
52
+ lInputLines = input
55
53
  else
56
- lLines = for line in blockToArray(input)
54
+ lInputLines = blockToArray(input)
55
+
56
+ lLines = for line in lInputLines
57
+ if isEmpty(line)
58
+ ""
59
+ else
57
60
  "#{toAdd}#{line}"
58
- return arrayToBlock(lLines)
61
+ return arrayToBlock(lLines)
59
62
 
60
63
  # ---------------------------------------------------------------------------
61
64
  # undented - string with 1st line indentation removed for each line
@@ -77,7 +80,7 @@ export undented = (text, level=undef) ->
77
80
  if (lLines.length == 0)
78
81
  return []
79
82
  else
80
- error "undented(): Not an array or string: #{oneline(text)}"
83
+ error "undented(): Not an array or string: #{OL(text)}"
81
84
 
82
85
  # --- determine what to remove from beginning of each line
83
86
  if level?
@@ -95,7 +98,7 @@ export undented = (text, level=undef) ->
95
98
  else
96
99
  assert (line.indexOf(toRemove)==0),
97
100
  "undented(): Error removing '#{escapeStr(toRemove)}' \
98
- from '#{oneline(text)}'"
101
+ from #{OL(text)}"
99
102
  lNewLines.push(line.substr(nToRemove))
100
103
 
101
104
  if isString(text)
@@ -8,7 +8,7 @@ import {
8
8
  undef,
9
9
  error,
10
10
  escapeStr,
11
- oneline,
11
+ OL,
12
12
  isInteger,
13
13
  isString,
14
14
  isArray,
@@ -54,36 +54,31 @@ export var indentLevel = function(str) {
54
54
  // ---------------------------------------------------------------------------
55
55
  // indented - add indentation to each string in a block
56
56
  export var indented = function(input, level = 0) {
57
- var lLines, line, toAdd;
57
+ var lInputLines, lLines, line, toAdd;
58
58
  assert(level >= 0, "indented(): negative level");
59
59
  if (level === 0) {
60
60
  return input;
61
61
  }
62
62
  toAdd = indentation(level);
63
63
  if (isArray(input)) {
64
- lLines = (function() {
65
- var i, len, results;
66
- results = [];
67
- for (i = 0, len = input.length; i < len; i++) {
68
- line = input[i];
69
- results.push(`${toAdd}${line}`);
70
- }
71
- return results;
72
- })();
73
- return lLines;
64
+ lInputLines = input;
74
65
  } else {
75
- lLines = (function() {
76
- var i, len, ref, results;
77
- ref = blockToArray(input);
78
- results = [];
79
- for (i = 0, len = ref.length; i < len; i++) {
80
- line = ref[i];
66
+ lInputLines = blockToArray(input);
67
+ }
68
+ lLines = (function() {
69
+ var i, len, results;
70
+ results = [];
71
+ for (i = 0, len = lInputLines.length; i < len; i++) {
72
+ line = lInputLines[i];
73
+ if (isEmpty(line)) {
74
+ results.push("");
75
+ } else {
81
76
  results.push(`${toAdd}${line}`);
82
77
  }
83
- return results;
84
- })();
85
- return arrayToBlock(lLines);
86
- }
78
+ }
79
+ return results;
80
+ })();
81
+ return arrayToBlock(lLines);
87
82
  };
88
83
 
89
84
  // ---------------------------------------------------------------------------
@@ -107,7 +102,7 @@ export var undented = function(text, level = undef) {
107
102
  return [];
108
103
  }
109
104
  } else {
110
- error(`undented(): Not an array or string: ${oneline(text)}`);
105
+ error(`undented(): Not an array or string: ${OL(text)}`);
111
106
  }
112
107
  // --- determine what to remove from beginning of each line
113
108
  if (level != null) {
@@ -124,7 +119,7 @@ export var undented = function(text, level = undef) {
124
119
  if (isEmpty(line)) {
125
120
  lNewLines.push('');
126
121
  } else {
127
- assert(line.indexOf(toRemove) === 0, `undented(): Error removing '${escapeStr(toRemove)}' from '${oneline(text)}'`);
122
+ assert(line.indexOf(toRemove) === 0, `undented(): Error removing '${escapeStr(toRemove)}' from ${OL(text)}`);
128
123
  lNewLines.push(line.substr(nToRemove));
129
124
  }
130
125
  }
@@ -121,7 +121,7 @@ export log = (lArgs...) ->
121
121
  else
122
122
  logger "#{prefix}#{str}:"
123
123
  for line in blockToArray(item)
124
- logger "#{itemPrefix} #{escapeStr(line)}"
124
+ logger "#{itemPrefix} '#{escapeStr(line)}'"
125
125
  else
126
126
  # --- It's some type of object
127
127
  json = JSON.stringify(item)
package/src/log_utils.js CHANGED
@@ -139,7 +139,7 @@ export var log = function(...lArgs) {
139
139
  ref = blockToArray(item);
140
140
  for (i = 0, len = ref.length; i < len; i++) {
141
141
  line = ref[i];
142
- logger(`${itemPrefix} ${escapeStr(line)}`);
142
+ logger(`${itemPrefix} '${escapeStr(line)}'`);
143
143
  }
144
144
  }
145
145
  } else {