@jdeighan/coffee-utils 6.0.9 → 7.0.0

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": "6.0.9",
4
+ "version": "7.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -8,7 +8,6 @@ import {
8
8
  assert, undef, pass, error, isEmpty,
9
9
  } from '@jdeighan/coffee-utils'
10
10
  import {localStore} from '@jdeighan/coffee-utils/browser'
11
- import {log} from '@jdeighan/coffee-utils/log'
12
11
  import {
13
12
  withExt, slurp, barf, newerDestFileExists,
14
13
  } from '@jdeighan/coffee-utils/fs'
package/src/DataStores.js CHANGED
@@ -22,10 +22,6 @@ import {
22
22
  localStore
23
23
  } from '@jdeighan/coffee-utils/browser';
24
24
 
25
- import {
26
- log
27
- } from '@jdeighan/coffee-utils/log';
28
-
29
25
  import {
30
26
  withExt,
31
27
  slurp,
package/src/arrow.coffee CHANGED
@@ -1,39 +1,29 @@
1
1
  # arrow.coffee
2
2
 
3
- vbar = '│' # unicode 2502
4
- hbar = '─' # unicode 2500
5
- corner = '└' # unicode 2514
6
- arrowhead = '>'
3
+ export vbar = '│' # unicode 2502
4
+ export hbar = '─' # unicode 2500
5
+ export corner = '└' # unicode 2514
6
+ export arrowhead = '>'
7
+ export space = ' '
7
8
 
8
- oneIndent = vbar + ' '
9
- export arrow = corner + hbar + arrowhead + ' '
9
+ export oneIndent = vbar + space + space + space
10
+ export arrow = corner + hbar + arrowhead + space
11
+ export fourSpaces = space + space + space + space
10
12
 
11
13
  # ---------------------------------------------------------------------------
12
14
 
13
- export getPrefix = (level, withArrow) ->
15
+ export getPrefix = (level, option='none') ->
14
16
 
15
- if withArrow
16
- return oneIndent.repeat(level-1) + arrow
17
- else
18
- return oneIndent.repeat(level)
19
- return
20
-
21
- # ---------------------------------------------------------------------------
22
-
23
- export hasArrow = (str) ->
24
-
25
- return str.indexOf(arrow) > -1
26
-
27
- # ---------------------------------------------------------------------------
28
-
29
- export removeArrow = (str, useVbar) ->
30
-
31
- if hasArrow(str)
32
- if useVbar
33
- return str.replace(arrow, oneIndent)
17
+ if level==0 then return ''
18
+ switch option
19
+ when 'withArrow'
20
+ result = oneIndent.repeat(level-1) + arrow
21
+ when 'returnVal'
22
+ result = oneIndent.repeat(level-1) + fourSpaces
23
+ when 'none'
24
+ result = oneIndent.repeat(level)
34
25
  else
35
- return str.replace(arrow, ' ')
36
- else
37
- return str
38
-
39
- # ---------------------------------------------------------------------------
26
+ throw new Error("getPrefix(): Bad option: '#{option}'")
27
+ if result.length % 4 != 0
28
+ throw new Error("getPrefix(): Bad prefix '#{result}'")
29
+ return result
package/src/arrow.js CHANGED
@@ -1,44 +1,42 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // arrow.coffee
3
- var arrowhead, corner, hbar, oneIndent, vbar;
3
+ export var vbar = '│'; // unicode 2502
4
4
 
5
- vbar = ''; // unicode 2502
5
+ export var hbar = ''; // unicode 2500
6
6
 
7
- hbar = ''; // unicode 2500
7
+ export var corner = ''; // unicode 2514
8
8
 
9
- corner = ''; // unicode 2514
9
+ export var arrowhead = '>';
10
10
 
11
- arrowhead = '>';
11
+ export var space = ' ';
12
12
 
13
- oneIndent = vbar + ' ';
13
+ export var oneIndent = vbar + space + space + space;
14
14
 
15
- export var arrow = corner + hbar + arrowhead + ' ';
15
+ export var arrow = corner + hbar + arrowhead + space;
16
16
 
17
- // ---------------------------------------------------------------------------
18
- export var getPrefix = function(level, withArrow) {
19
- if (withArrow) {
20
- return oneIndent.repeat(level - 1) + arrow;
21
- } else {
22
- return oneIndent.repeat(level);
23
- }
24
- };
25
-
26
- // ---------------------------------------------------------------------------
27
- export var hasArrow = function(str) {
28
- return str.indexOf(arrow) > -1;
29
- };
17
+ export var fourSpaces = space + space + space + space;
30
18
 
31
19
  // ---------------------------------------------------------------------------
32
- export var removeArrow = function(str, useVbar) {
33
- if (hasArrow(str)) {
34
- if (useVbar) {
35
- return str.replace(arrow, oneIndent);
36
- } else {
37
- return str.replace(arrow, ' ');
38
- }
39
- } else {
40
- return str;
20
+ export var getPrefix = function(level, option = 'none') {
21
+ var result;
22
+ if (level === 0) {
23
+ return '';
24
+ }
25
+ switch (option) {
26
+ case 'withArrow':
27
+ result = oneIndent.repeat(level - 1) + arrow;
28
+ break;
29
+ case 'returnVal':
30
+ result = oneIndent.repeat(level - 1) + fourSpaces;
31
+ break;
32
+ case 'none':
33
+ result = oneIndent.repeat(level);
34
+ break;
35
+ default:
36
+ throw new Error(`getPrefix(): Bad option: '${option}'`);
37
+ }
38
+ if (result.length % 4 !== 0) {
39
+ throw new Error(`getPrefix(): Bad prefix '${result}'`);
41
40
  }
41
+ return result;
42
42
  };
43
-
44
- // ---------------------------------------------------------------------------
@@ -2,6 +2,7 @@
2
2
 
3
3
  import {undef, croak, assert} from '@jdeighan/coffee-utils'
4
4
  import {log, LOG} from '@jdeighan/coffee-utils/log'
5
+ import {getPrefix} from '@jdeighan/coffee-utils/arrow'
5
6
 
6
7
  doDebugStack = false
7
8
 
@@ -22,49 +23,95 @@ export class CallStack
22
23
 
23
24
  # ........................................................................
24
25
 
25
- call: (funcName, hInfo) ->
26
+ reset: () ->
26
27
 
27
28
  if doDebugStack
28
- prefix = ' '.repeat(@lStack.length)
29
- LOG "#{prefix}[--> CALL #{funcName}]"
30
- @lStack.push({funcName, hInfo})
29
+ LOG "RESET STACK"
30
+
31
+ @lStack = []
32
+ @level = 0
31
33
  return
32
34
 
33
35
  # ........................................................................
34
36
 
35
- returnFrom: (fName) ->
37
+ addCall: (funcName, hInfo, isLogged) ->
36
38
 
37
- if @lStack.length == 0
38
- LOG "returnFrom('#{fName}') but stack is empty"
39
- return undef
40
- {funcName, hInfo} = @lStack.pop()
39
+ @lStack.push({funcName, hInfo, isLogged})
40
+ if isLogged
41
+ @level += 1
42
+ return
43
+
44
+ # ........................................................................
45
+
46
+ removeCall: (fName) ->
47
+
48
+ {funcName, hInfo, isLogged} = @lStack.pop()
49
+ if isLogged && (@level > 0)
50
+ @level -= 1
41
51
  while (funcName != fName) && (@lStack.length > 0)
42
52
  LOG "[MISSING RETURN FROM #{funcName} (return from #{fName})]"
43
- {funcName, hInfo} = @lStack.pop()
53
+ {funcName, hInfo, isLogged} = @lStack.pop()
54
+ if isLogged && (@level > 0)
55
+ @level -= 1
44
56
 
45
- if doDebugStack
46
- prefix = ' '.repeat(@lStack.length)
47
- LOG "#{prefix}[<-- BACK #{fName}]"
48
- if (funcName == fName)
57
+ if funcName == fName
49
58
  return hInfo
50
59
  else
51
60
  @dump()
52
- LOG "BAD returnFrom('#{fName}')"
61
+ LOG "BAD BAD BAD BAD returnFrom('#{fName}')"
53
62
  return undef
54
63
 
55
64
  # ........................................................................
65
+ # ........................................................................
56
66
 
57
- reset: () ->
67
+ call: (funcName, hInfo, isLogged=undef) ->
68
+
69
+ assert isLogged != undef, "CallStack.call(): 3 args required"
70
+ mainPre = getPrefix(@level)
58
71
 
59
72
  if doDebugStack
60
- LOG "RESET STACK"
61
- @lStack = []
73
+ prefix = ' '.repeat(@lStack.length)
74
+ LOG "#{prefix}[--> CALL #{funcName}]"
75
+
76
+ @addCall funcName, hInfo, isLogged
77
+ auxPre = getPrefix(@level)
78
+ return [mainPre, auxPre, undef]
79
+
80
+ # ........................................................................
81
+
82
+ logStr: () ->
83
+
84
+ pre = getPrefix(@level)
85
+ return [pre, pre, undef]
86
+
87
+ # ........................................................................
88
+
89
+ returnFrom: (funcName) ->
90
+
91
+ # --- Prefixes are based on level before stack adjustment
92
+ mainPre = getPrefix(@level, 'withArrow')
93
+ auxPre = getPrefix(@level, 'returnVal')
62
94
 
95
+ if @lStack.length == 0
96
+ LOG "returnFrom('#{fName}') but stack is empty"
97
+ return [mainPre, auxPre, undef]
98
+
99
+ hInfo = @removeCall(funcName)
100
+ if doDebugStack
101
+ prefix = ' '.repeat(@lStack.length)
102
+ LOG "#{prefix}[<-- BACK #{fName}]"
103
+
104
+ return [mainPre, auxPre, hInfo]
105
+
106
+ # ........................................................................
63
107
  # ........................................................................
64
108
 
65
109
  dump: (label='CALL STACK') ->
66
110
 
67
- console.log "#{label}:"
68
- for item, i in @lStack
69
- LOG "#{i}: #{JSON.stringify(item)}"
111
+ LOG "#{label}:"
112
+ if @lStack.length == 0
113
+ LOG " <EMPTY>"
114
+ else
115
+ for item, i in @lStack
116
+ LOG " #{i}: #{JSON.stringify(item)}"
70
117
  return
package/src/call_stack.js CHANGED
@@ -13,6 +13,10 @@ import {
13
13
  LOG
14
14
  } from '@jdeighan/coffee-utils/log';
15
15
 
16
+ import {
17
+ getPrefix
18
+ } from '@jdeighan/coffee-utils/arrow';
19
+
16
20
  doDebugStack = false;
17
21
 
18
22
  // ---------------------------------------------------------------------------
@@ -27,56 +31,98 @@ export var CallStack = class CallStack {
27
31
  }
28
32
 
29
33
  // ........................................................................
30
- call(funcName, hInfo) {
31
- var prefix;
34
+ reset() {
32
35
  if (doDebugStack) {
33
- prefix = ' '.repeat(this.lStack.length);
34
- LOG(`${prefix}[--> CALL ${funcName}]`);
36
+ LOG("RESET STACK");
35
37
  }
36
- this.lStack.push({funcName, hInfo});
38
+ this.lStack = [];
39
+ this.level = 0;
37
40
  }
38
41
 
39
42
  // ........................................................................
40
- returnFrom(fName) {
41
- var funcName, hInfo, prefix;
42
- if (this.lStack.length === 0) {
43
- LOG(`returnFrom('${fName}') but stack is empty`);
44
- return undef;
43
+ addCall(funcName, hInfo, isLogged) {
44
+ this.lStack.push({funcName, hInfo, isLogged});
45
+ if (isLogged) {
46
+ this.level += 1;
47
+ }
48
+ }
49
+
50
+ // ........................................................................
51
+ removeCall(fName) {
52
+ var funcName, hInfo, isLogged;
53
+ ({funcName, hInfo, isLogged} = this.lStack.pop());
54
+ if (isLogged && (this.level > 0)) {
55
+ this.level -= 1;
45
56
  }
46
- ({funcName, hInfo} = this.lStack.pop());
47
57
  while ((funcName !== fName) && (this.lStack.length > 0)) {
48
58
  LOG(`[MISSING RETURN FROM ${funcName} (return from ${fName})]`);
49
- ({funcName, hInfo} = this.lStack.pop());
50
- }
51
- if (doDebugStack) {
52
- prefix = ' '.repeat(this.lStack.length);
53
- LOG(`${prefix}[<-- BACK ${fName}]`);
59
+ ({funcName, hInfo, isLogged} = this.lStack.pop());
60
+ if (isLogged && (this.level > 0)) {
61
+ this.level -= 1;
62
+ }
54
63
  }
55
64
  if (funcName === fName) {
56
65
  return hInfo;
57
66
  } else {
58
67
  this.dump();
59
- LOG(`BAD returnFrom('${fName}')`);
68
+ LOG(`BAD BAD BAD BAD returnFrom('${fName}')`);
60
69
  return undef;
61
70
  }
62
71
  }
63
72
 
64
73
  // ........................................................................
65
- reset() {
74
+ // ........................................................................
75
+ call(funcName, hInfo, isLogged = undef) {
76
+ var auxPre, mainPre, prefix;
77
+ assert(isLogged !== undef, "CallStack.call(): 3 args required");
78
+ mainPre = getPrefix(this.level);
66
79
  if (doDebugStack) {
67
- LOG("RESET STACK");
80
+ prefix = ' '.repeat(this.lStack.length);
81
+ LOG(`${prefix}[--> CALL ${funcName}]`);
68
82
  }
69
- return this.lStack = [];
83
+ this.addCall(funcName, hInfo, isLogged);
84
+ auxPre = getPrefix(this.level);
85
+ return [mainPre, auxPre, undef];
86
+ }
87
+
88
+ // ........................................................................
89
+ logStr() {
90
+ var pre;
91
+ pre = getPrefix(this.level);
92
+ return [pre, pre, undef];
70
93
  }
71
94
 
95
+ // ........................................................................
96
+ returnFrom(funcName) {
97
+ var auxPre, hInfo, mainPre, prefix;
98
+ // --- Prefixes are based on level before stack adjustment
99
+ mainPre = getPrefix(this.level, 'withArrow');
100
+ auxPre = getPrefix(this.level, 'returnVal');
101
+ if (this.lStack.length === 0) {
102
+ LOG(`returnFrom('${fName}') but stack is empty`);
103
+ return [mainPre, auxPre, undef];
104
+ }
105
+ hInfo = this.removeCall(funcName);
106
+ if (doDebugStack) {
107
+ prefix = ' '.repeat(this.lStack.length);
108
+ LOG(`${prefix}[<-- BACK ${fName}]`);
109
+ }
110
+ return [mainPre, auxPre, hInfo];
111
+ }
112
+
113
+ // ........................................................................
72
114
  // ........................................................................
73
115
  dump(label = 'CALL STACK') {
74
116
  var i, item, j, len, ref;
75
- console.log(`${label}:`);
76
- ref = this.lStack;
77
- for (i = j = 0, len = ref.length; j < len; i = ++j) {
78
- item = ref[i];
79
- LOG(`${i}: ${JSON.stringify(item)}`);
117
+ LOG(`${label}:`);
118
+ if (this.lStack.length === 0) {
119
+ LOG(" <EMPTY>");
120
+ } else {
121
+ ref = this.lStack;
122
+ for (i = j = 0, len = ref.length; j < len; i = ++j) {
123
+ item = ref[i];
124
+ LOG(` ${i}: ${JSON.stringify(item)}`);
125
+ }
80
126
  }
81
127
  }
82
128
 
@@ -3,6 +3,7 @@
3
3
  export sep_dash = '-'.repeat(42)
4
4
  export sep_eq = '='.repeat(42)
5
5
  `export const undef = undefined`
6
+ LOG = (lArgs...) -> console.log lArgs... # synonym for console.log()
6
7
 
7
8
  # ---------------------------------------------------------------------------
8
9
  # pass - do nothing
@@ -226,9 +227,9 @@ export hashToStr = (h) ->
226
227
  export say = (x) ->
227
228
 
228
229
  if isHash(x)
229
- console.log hashToStr(x)
230
+ LOG hashToStr(x)
230
231
  else
231
- console.log x
232
+ LOG x
232
233
  return
233
234
 
234
235
  # ---------------------------------------------------------------------------
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // coffee_utils.coffee
3
- var commentRegExp;
3
+ var LOG, commentRegExp;
4
4
 
5
5
  export var sep_dash = '-'.repeat(42);
6
6
 
@@ -8,6 +8,11 @@ export var sep_eq = '='.repeat(42);
8
8
 
9
9
  export const undef = undefined;
10
10
 
11
+ LOG = function(...lArgs) {
12
+ return console.log(...lArgs); // synonym for console.log()
13
+ };
14
+
15
+
11
16
  // ---------------------------------------------------------------------------
12
17
  // pass - do nothing
13
18
  export var pass = function() {};
@@ -219,9 +224,9 @@ export var hashToStr = function(h) {
219
224
  // later, on a web page, call alert(str)
220
225
  export var say = function(x) {
221
226
  if (isHash(x)) {
222
- console.log(hashToStr(x));
227
+ LOG(hashToStr(x));
223
228
  } else {
224
- console.log(x);
229
+ LOG(x);
225
230
  }
226
231
  };
227
232