@jdeighan/coffee-utils 4.0.23 → 4.1.3

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.
@@ -0,0 +1,319 @@
1
+ // Generated by CoffeeScript 2.6.1
2
+ // utils.test.coffee
3
+ var hEsc, simple;
4
+
5
+ import {
6
+ UnitTester
7
+ } from '@jdeighan/coffee-utils/test';
8
+
9
+ import {
10
+ say,
11
+ undef,
12
+ error,
13
+ warn,
14
+ isString,
15
+ isObject,
16
+ isArray,
17
+ isHash,
18
+ isEmpty,
19
+ nonEmpty,
20
+ isComment,
21
+ getClassName,
22
+ isNumber,
23
+ isFunction,
24
+ isInteger,
25
+ rtrim,
26
+ ltrunc,
27
+ rtrunc,
28
+ extractMatches,
29
+ words,
30
+ escapeStr,
31
+ titleLine,
32
+ removeCR,
33
+ CWS,
34
+ isArrayOfHashes,
35
+ oneline,
36
+ croak,
37
+ isRegExp
38
+ } from '@jdeighan/coffee-utils';
39
+
40
+ import {
41
+ setLogger
42
+ } from '@jdeighan/coffee-utils/log';
43
+
44
+ import {
45
+ setDebugging
46
+ } from '@jdeighan/coffee-utils/debug';
47
+
48
+ import {
49
+ arrayToBlock
50
+ } from '@jdeighan/coffee-utils/block';
51
+
52
+ simple = new UnitTester();
53
+
54
+ // ---------------------------------------------------------------------------
55
+ simple.truthy(19, isEmpty(''));
56
+
57
+ simple.truthy(20, isEmpty(' \t\t'));
58
+
59
+ simple.truthy(21, isEmpty([]));
60
+
61
+ simple.truthy(22, isEmpty({}));
62
+
63
+ simple.truthy(24, nonEmpty('a'));
64
+
65
+ simple.truthy(25, nonEmpty('.'));
66
+
67
+ simple.truthy(26, nonEmpty([2]));
68
+
69
+ simple.truthy(27, nonEmpty({
70
+ width: 2
71
+ }));
72
+
73
+ simple.truthy(29, isComment("# a comment"));
74
+
75
+ simple.truthy(30, isComment("### a comment"));
76
+
77
+ simple.truthy(31, isComment("#\ta comment"));
78
+
79
+ simple.truthy(32, isComment("###\ta comment"));
80
+
81
+ simple.truthy(33, isComment(" # a comment"));
82
+
83
+ simple.truthy(34, isComment(" ### a comment"));
84
+
85
+ simple.falsy(35, isComment("not much"));
86
+
87
+ simple.falsy(36, isComment("#foreach x in lItems"));
88
+
89
+ simple.truthy(37, isComment('#'));
90
+
91
+ simple.truthy(38, isComment(' #'));
92
+
93
+ simple.truthy(39, isComment(' ###'));
94
+
95
+ simple.falsy(40, isComment('#for'));
96
+
97
+ simple.falsy(41, isComment(' #for'));
98
+
99
+ simple.falsy(42, isComment('#for line in lLines'));
100
+
101
+ simple.equal(44, titleLine('a thing').length, 42);
102
+
103
+ simple.equal(45, titleLine('a thing', '-', 5, 90).length, 90);
104
+
105
+ // ---------------------------------------------------------------------------
106
+ simple.equal(80, rtrim("abc"), "abc");
107
+
108
+ simple.equal(81, rtrim(" abc"), " abc");
109
+
110
+ simple.equal(82, rtrim("abc "), "abc");
111
+
112
+ simple.equal(83, rtrim(" abc "), " abc");
113
+
114
+ // ---------------------------------------------------------------------------
115
+ simple.equal(87, words('a b c'), ['a', 'b', 'c']);
116
+
117
+ simple.equal(88, words(' a b c '), ['a', 'b', 'c']);
118
+
119
+ // ---------------------------------------------------------------------------
120
+ simple.equal(92, escapeStr("\t\tXXX\n"), "\\t\\tXXX\\n");
121
+
122
+ hEsc = {
123
+ "\n": "\\n",
124
+ "\t": "\\t",
125
+ "\"": "\\\""
126
+ };
127
+
128
+ simple.equal(64, escapeStr("\thas quote: \"\nnext line", hEsc), "\\thas quote: \\\"\\nnext line");
129
+
130
+ // ---------------------------------------------------------------------------
131
+ simple.equal(130, rtrunc('/user/lib/.env', 5), '/user/lib');
132
+
133
+ simple.equal(131, ltrunc('abcdefg', 3), 'defg');
134
+
135
+ simple.equal(133, removeCR("abc\r\ndef\r\n"), "abc\ndef\n");
136
+
137
+ simple.equal(139, CWS(`abc
138
+ def
139
+ ghi`), "abc def ghi");
140
+
141
+ // ---------------------------------------------------------------------------
142
+ simple.truthy(147, isArrayOfHashes([]));
143
+
144
+ simple.truthy(148, isArrayOfHashes([{}, {}]));
145
+
146
+ simple.truthy(149, isArrayOfHashes([
147
+ {
148
+ a: 1,
149
+ b: 2
150
+ },
151
+ {}
152
+ ]));
153
+
154
+ simple.truthy(150, isArrayOfHashes([
155
+ {
156
+ a: 1,
157
+ b: 2,
158
+ c: [1,
159
+ 2,
160
+ 3]
161
+ },
162
+ {}
163
+ ]));
164
+
165
+ simple.falsy(152, isArrayOfHashes({}));
166
+
167
+ simple.falsy(153, isArrayOfHashes([1, 2, 3]));
168
+
169
+ simple.falsy(154, isArrayOfHashes([
170
+ {
171
+ a: 1,
172
+ b: 2,
173
+ c: [1,
174
+ 2,
175
+ 3]
176
+ },
177
+ 4
178
+ ]));
179
+
180
+ simple.falsy(155, isArrayOfHashes([
181
+ {
182
+ a: 1,
183
+ b: 2,
184
+ c: [1,
185
+ 2,
186
+ 3]
187
+ },
188
+ {},
189
+ [1,
190
+ 2]
191
+ ]));
192
+
193
+ // ---------------------------------------------------------------------------
194
+ (function() {
195
+ var NewClass, h, l, n, n2, o, s, s2;
196
+ NewClass = class NewClass {};
197
+ h = {
198
+ a: 1,
199
+ b: 2
200
+ };
201
+ l = [1, 2, 2];
202
+ o = new NewClass();
203
+ n = 42;
204
+ n2 = new Number(42);
205
+ s = 'simple';
206
+ s2 = new String('abc');
207
+ simple.truthy(170, isHash(h));
208
+ simple.falsy(171, isHash(l));
209
+ simple.falsy(172, isHash(o));
210
+ simple.falsy(173, isHash(n));
211
+ simple.falsy(174, isHash(n2));
212
+ simple.falsy(175, isHash(s));
213
+ simple.falsy(176, isHash(s2));
214
+ simple.falsy(178, isArray(h));
215
+ simple.truthy(179, isArray(l));
216
+ simple.falsy(180, isArray(o));
217
+ simple.falsy(181, isArray(n));
218
+ simple.falsy(182, isArray(n2));
219
+ simple.falsy(183, isArray(s));
220
+ simple.falsy(184, isArray(s2));
221
+ simple.falsy(186, isString(h));
222
+ simple.falsy(187, isString(l));
223
+ simple.falsy(188, isString(o));
224
+ simple.falsy(189, isString(n));
225
+ simple.falsy(190, isString(n2));
226
+ simple.truthy(191, isString(s));
227
+ simple.truthy(192, isString(s2));
228
+ simple.falsy(194, isObject(h));
229
+ simple.falsy(195, isObject(l));
230
+ simple.truthy(196, isObject(o));
231
+ simple.falsy(197, isObject(n));
232
+ simple.falsy(198, isObject(n2));
233
+ simple.falsy(199, isObject(s));
234
+ simple.falsy(200, isObject(s2));
235
+ simple.falsy(202, isNumber(h));
236
+ simple.falsy(203, isNumber(l));
237
+ simple.falsy(204, isNumber(o));
238
+ simple.truthy(205, isNumber(n));
239
+ simple.truthy(206, isNumber(n2));
240
+ simple.falsy(207, isNumber(s));
241
+ return simple.falsy(208, isNumber(s2));
242
+ })();
243
+
244
+ // ---------------------------------------------------------------------------
245
+ simple.truthy(214, isFunction(function() {
246
+ return pass;
247
+ }));
248
+
249
+ simple.falsy(215, isFunction(23));
250
+
251
+ simple.truthy(217, isInteger(42));
252
+
253
+ simple.truthy(218, isInteger(new Number(42)));
254
+
255
+ simple.falsy(219, isInteger('abc'));
256
+
257
+ simple.falsy(220, isInteger({}));
258
+
259
+ simple.falsy(221, isInteger([]));
260
+
261
+ // ---------------------------------------------------------------------------
262
+ simple.equal(233, oneline(undef), "undef");
263
+
264
+ simple.equal(234, oneline("\t\tabc\nxyz"), "'\\t\\tabc\\nxyz'");
265
+
266
+ simple.equal(235, oneline({
267
+ a: 1,
268
+ b: 'xyz'
269
+ }), '{"a":1,"b":"xyz"}');
270
+
271
+ // ---------------------------------------------------------------------------
272
+ simple.equal(239, CWS(`a simple
273
+ error message`), "a simple error message");
274
+
275
+ // ---------------------------------------------------------------------------
276
+ // test croak()
277
+ (function() {
278
+ var lLines, obj;
279
+ lLines = [];
280
+ setLogger(function(line) {
281
+ return lLines.push(line);
282
+ });
283
+ obj = {
284
+ a: 1,
285
+ b: 2
286
+ };
287
+ try {
288
+ croak("bad stuff", "An Object", obj);
289
+ } catch (error1) {}
290
+ simple.equal(255, arrayToBlock(lLines), `ERROR: bad stuff
291
+ An Object = {"a":1,"b":2}`);
292
+ return setLogger();
293
+ })();
294
+
295
+ // ---------------------------------------------------------------------------
296
+ // test isRegExp()
297
+ simple.truthy(265, isRegExp(/^abc$/));
298
+
299
+ simple.truthy(266, isRegExp(/^\s*where\s+areyou$/));
300
+
301
+ simple.falsy(272, isRegExp(42));
302
+
303
+ simple.falsy(272, isRegExp('abc'));
304
+
305
+ simple.falsy(272, isRegExp([1, 'a']));
306
+
307
+ simple.falsy(272, isRegExp({
308
+ a: 1,
309
+ b: 'ccc'
310
+ }));
311
+
312
+ simple.falsy(272, isRegExp(undef));
313
+
314
+ simple.truthy(278, isRegExp(/\.coffee/));
315
+
316
+ // ---------------------------------------------------------------------------
317
+ simple.equal(212, extractMatches("..3 and 4 plus 5", /\d+/g, parseInt), [3, 4, 5]);
318
+
319
+ simple.equal(214, extractMatches("And This Is A String", /A/g), ['A', 'A']);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "4.0.23",
4
+ "version": "4.1.3",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -20,10 +20,10 @@
20
20
  "node": ">=12.0.0"
21
21
  },
22
22
  "scripts": {
23
- "build": "cls && coffee -c .",
24
- "pretest": "cls && coffee -c .",
23
+ "build": "cls && rm -f ./src/*.js && coffee -c ./src",
24
+ "pretest": "npm run build && rm -f ./test/*.js && rm -f ./test/*.coffee && cielo -fq ./test",
25
25
  "test": "ava ./test/*.test.js",
26
- "prefinaltest": "cls && coffee -c .",
26
+ "prefinaltest": "npm run pretest",
27
27
  "finaltest": "cross-env FINALTEST=yes ava ./test/*.test.js"
28
28
  },
29
29
  "repository": {
@@ -42,6 +42,7 @@
42
42
  "dependencies": {
43
43
  "ava": "^3.15.0",
44
44
  "cross-env": "^7.0.3",
45
- "js-yaml": "^4.1.0"
45
+ "js-yaml": "^4.1.0",
46
+ "readline-sync": "^1.4.10"
46
47
  }
47
48
  }
@@ -8,9 +8,7 @@ import {
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
10
  import {log, currentLogger, setLogger} from '@jdeighan/coffee-utils/log'
11
- import {
12
- debug, debugging, setDebugging,
13
- } from '@jdeighan/coffee-utils/debug'
11
+ import {debug, debugging, setDebugging} from '@jdeighan/coffee-utils/debug'
14
12
 
15
13
  # ---------------------------------------------------------------------------
16
14
 
@@ -175,6 +173,9 @@ export class UnitTester
175
173
 
176
174
  test: (lineNum, input, expected) ->
177
175
 
176
+ assert isInteger(lineNum),
177
+ "UnitTester.test(): arg 1 must be an integer"
178
+
178
179
  @initialize()
179
180
  @lineNum = lineNum # set an object property
180
181
 
@@ -187,9 +188,6 @@ export class UnitTester
187
188
  if lineNum < -100000
188
189
  setDebugging true
189
190
 
190
- assert isInteger(lineNum),
191
- "UnitTester.test(): arg 1 must be an integer"
192
-
193
191
  lineNum = @getLineNum(lineNum) # corrects for duplicates
194
192
  errMsg = undef
195
193
  try
@@ -200,6 +198,7 @@ export class UnitTester
200
198
  errMsg = err.message || 'UNKNOWN ERROR'
201
199
  log "got ERROR: #{errMsg}"
202
200
 
201
+ expected = @transformExpected(expected)
203
202
  if isString(expected)
204
203
  expected = @normalize(expected)
205
204
 
@@ -236,6 +235,11 @@ export class UnitTester
236
235
 
237
236
  # ........................................................................
238
237
 
238
+ transformExpected: (input) ->
239
+ return input
240
+
241
+ # ........................................................................
242
+
239
243
  getLineNum: (lineNum) ->
240
244
 
241
245
  if @fulltest && (lineNum < 0)
package/src/UnitTester.js CHANGED
@@ -185,6 +185,7 @@ export var UnitTester = class UnitTester {
185
185
  // ........................................................................
186
186
  test(lineNum, input, expected) {
187
187
  var err, errMsg, got, whichTest;
188
+ assert(isInteger(lineNum), "UnitTester.test(): arg 1 must be an integer");
188
189
  this.initialize();
189
190
  this.lineNum = lineNum; // set an object property
190
191
  if ((lineNum < 0) && process.env.FINALTEST) {
@@ -196,7 +197,6 @@ export var UnitTester = class UnitTester {
196
197
  if (lineNum < -100000) {
197
198
  setDebugging(true);
198
199
  }
199
- assert(isInteger(lineNum), "UnitTester.test(): arg 1 must be an integer");
200
200
  lineNum = this.getLineNum(lineNum); // corrects for duplicates
201
201
  errMsg = undef;
202
202
  try {
@@ -209,6 +209,7 @@ export var UnitTester = class UnitTester {
209
209
  errMsg = err.message || 'UNKNOWN ERROR';
210
210
  log(`got ERROR: ${errMsg}`);
211
211
  }
212
+ expected = this.transformExpected(expected);
212
213
  if (isString(expected)) {
213
214
  expected = this.normalize(expected);
214
215
  }
@@ -248,6 +249,11 @@ export var UnitTester = class UnitTester {
248
249
  return input;
249
250
  }
250
251
 
252
+ // ........................................................................
253
+ transformExpected(input) {
254
+ return input;
255
+ }
256
+
251
257
  // ........................................................................
252
258
  getLineNum(lineNum) {
253
259
  if (this.fulltest && (lineNum < 0)) {
@@ -1,5 +1,6 @@
1
1
  # coffee_utils.coffee
2
2
 
3
+ import getline from 'readline-sync'
3
4
  import {log} from '@jdeighan/coffee-utils/log'
4
5
 
5
6
  export sep_dash = '-'.repeat(42)
@@ -212,9 +213,9 @@ export warn = (message) ->
212
213
  # say - print to the console (for now)
213
214
  # later, on a web page, call alert(str)
214
215
 
215
- export say = (str, obj=undef) ->
216
+ export say = (str) ->
216
217
 
217
- log str, obj
218
+ console.log str
218
219
  return
219
220
 
220
221
  # ---------------------------------------------------------------------------
@@ -223,7 +224,8 @@ export say = (str, obj=undef) ->
223
224
 
224
225
  export ask = (prompt) ->
225
226
 
226
- return 'yes'
227
+ answer = getline.question("{prompt}? ")
228
+ return answer
227
229
 
228
230
  # ---------------------------------------------------------------------------
229
231
 
@@ -2,6 +2,8 @@
2
2
  // coffee_utils.coffee
3
3
  var commentRegExp;
4
4
 
5
+ import getline from 'readline-sync';
6
+
5
7
  import {
6
8
  log
7
9
  } from '@jdeighan/coffee-utils/log';
@@ -217,15 +219,17 @@ export var warn = function(message) {
217
219
  // ---------------------------------------------------------------------------
218
220
  // say - print to the console (for now)
219
221
  // later, on a web page, call alert(str)
220
- export var say = function(str, obj = undef) {
221
- log(str, obj);
222
+ export var say = function(str) {
223
+ console.log(str);
222
224
  };
223
225
 
224
226
  // ---------------------------------------------------------------------------
225
227
  // ask - ask a question
226
228
  // later, on a web page, prompt the user for answer to question
227
229
  export var ask = function(prompt) {
228
- return 'yes';
230
+ var answer;
231
+ answer = getline.question("{prompt}? ");
232
+ return answer;
229
233
  };
230
234
 
231
235
  // ---------------------------------------------------------------------------
@@ -5,7 +5,7 @@ import {
5
5
  oneline, escapeStr, isNumber, isArray,
6
6
  } from '@jdeighan/coffee-utils'
7
7
  import {blockToArray} from '@jdeighan/coffee-utils/block'
8
- import {log} from '@jdeighan/coffee-utils/log'
8
+ import {log, setLogger} from '@jdeighan/coffee-utils/log'
9
9
  import {slurp} from '@jdeighan/coffee-utils/fs'
10
10
 
11
11
  vbar = '│' # unicode 2502
@@ -23,7 +23,28 @@ lDebugStack = []
23
23
  # --- These are saved/restored in lDebugStack
24
24
  export debugging = false
25
25
  ifMatches = undef
26
- lDebugFuncs = undef
26
+ lDebugFuncs = undef # --- names of functions to debug
27
+
28
+ stdLogger = false
29
+
30
+ # ---------------------------------------------------------------------------
31
+
32
+ export useStdLogger = (flag=true) ->
33
+
34
+ stdLogger = flag
35
+ return
36
+
37
+ # ---------------------------------------------------------------------------
38
+
39
+ logger = (lArgs...) ->
40
+
41
+ if stdLogger
42
+ log lArgs...
43
+ else
44
+ orgLogger = setLogger(console.log)
45
+ log lArgs...
46
+ setLogger(orgLogger)
47
+ return
27
48
 
28
49
  # ---------------------------------------------------------------------------
29
50
 
@@ -158,9 +179,9 @@ export debug = (lArgs...) ->
158
179
  prefix = indent.repeat(debugLevel)
159
180
 
160
181
  if (nArgs==1)
161
- log str, item, {prefix}
182
+ logger str, item, {prefix}
162
183
  else
163
- log str, item, {
184
+ logger str, item, {
164
185
  prefix,
165
186
  logItem: true,
166
187
  itemPrefix: stripArrow(prefix),
@@ -222,15 +243,15 @@ export checkTrace = (block) ->
222
243
  funcName = lMatches[1]
223
244
  len = lStack.length
224
245
  if (len == 0)
225
- log "return from #{funcName} with empty stack"
246
+ logger "return from #{funcName} with empty stack"
226
247
  else if (lStack[len-1] == funcName)
227
248
  lStack.pop()
228
249
  else if (lStack[len-2] == funcName)
229
- log "missing return from #{lStack[len-2]}"
250
+ logger "missing return from #{lStack[len-2]}"
230
251
  lStack.pop()
231
252
  lStack.pop()
232
253
  else
233
- log "return from #{funcName} - not found on stack"
254
+ logger "return from #{funcName} - not found on stack"
234
255
  return
235
256
 
236
257
  # ---------------------------------------------------------------------------
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // debug_utils.coffee
3
- var arrow, arrowhead, corner, debugLevel, getPrefix, hbar, ifMatches, indent, lDebugFuncs, lDebugStack, reMethod, restoreDebugEnv, saveDebugEnv, stripArrow, vbar;
3
+ var arrow, arrowhead, corner, debugLevel, getPrefix, hbar, ifMatches, indent, lDebugFuncs, lDebugStack, logger, reMethod, restoreDebugEnv, saveDebugEnv, stdLogger, stripArrow, vbar;
4
4
 
5
5
  import {
6
6
  assert,
@@ -22,7 +22,8 @@ import {
22
22
  } from '@jdeighan/coffee-utils/block';
23
23
 
24
24
  import {
25
- log
25
+ log,
26
+ setLogger
26
27
  } from '@jdeighan/coffee-utils/log';
27
28
 
28
29
  import {
@@ -50,7 +51,26 @@ export var debugging = false;
50
51
 
51
52
  ifMatches = undef;
52
53
 
53
- lDebugFuncs = undef;
54
+ lDebugFuncs = undef; // --- names of functions to debug
55
+
56
+ stdLogger = false;
57
+
58
+ // ---------------------------------------------------------------------------
59
+ export var useStdLogger = function(flag = true) {
60
+ stdLogger = flag;
61
+ };
62
+
63
+ // ---------------------------------------------------------------------------
64
+ logger = function(...lArgs) {
65
+ var orgLogger;
66
+ if (stdLogger) {
67
+ log(...lArgs);
68
+ } else {
69
+ orgLogger = setLogger(console.log);
70
+ log(...lArgs);
71
+ setLogger(orgLogger);
72
+ }
73
+ };
54
74
 
55
75
  // ---------------------------------------------------------------------------
56
76
  stripArrow = function(prefix) {
@@ -157,9 +177,9 @@ export var debug = function(...lArgs) {
157
177
  prefix = indent.repeat(debugLevel);
158
178
  }
159
179
  if (nArgs === 1) {
160
- log(str, item, {prefix});
180
+ logger(str, item, {prefix});
161
181
  } else {
162
- log(str, item, {
182
+ logger(str, item, {
163
183
  prefix,
164
184
  logItem: true,
165
185
  itemPrefix: stripArrow(prefix)
@@ -208,15 +228,15 @@ export var checkTrace = function(block) {
208
228
  funcName = lMatches[1];
209
229
  len = lStack.length;
210
230
  if (len === 0) {
211
- log(`return from ${funcName} with empty stack`);
231
+ logger(`return from ${funcName} with empty stack`);
212
232
  } else if (lStack[len - 1] === funcName) {
213
233
  lStack.pop();
214
234
  } else if (lStack[len - 2] === funcName) {
215
- log(`missing return from ${lStack[len - 2]}`);
235
+ logger(`missing return from ${lStack[len - 2]}`);
216
236
  lStack.pop();
217
237
  lStack.pop();
218
238
  } else {
219
- log(`return from ${funcName} - not found on stack`);
239
+ logger(`return from ${funcName} - not found on stack`);
220
240
  }
221
241
  }
222
242
  }