@jdeighan/coffee-utils 11.0.3 → 11.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/src/log.coffee DELETED
@@ -1,186 +0,0 @@
1
- # log.coffee
2
-
3
- import {LOG, assert, croak} from '@jdeighan/exceptions'
4
- import {
5
- undef, isNumber, isInteger, isString, isHash, isFunction,
6
- escapeStr, sep_eq, sep_dash, pass, OL,
7
- } from '@jdeighan/coffee-utils'
8
- import {toArray} from '@jdeighan/coffee-utils/block'
9
- import {toTAML} from '@jdeighan/coffee-utils/taml'
10
-
11
- # --- This logger only ever gets passed a single string argument
12
- putstr = undef
13
- doDebugLog = false
14
-
15
- export stringify = undef
16
- fourSpaces = ' '
17
-
18
- # ---------------------------------------------------------------------------
19
-
20
- export debugLog = (flag=true) ->
21
-
22
- doDebugLog = flag
23
- if doDebugLog
24
- LOG "doDebugLog = #{flag}"
25
- return
26
-
27
- # ---------------------------------------------------------------------------
28
-
29
- export setStringifier = (func) ->
30
-
31
- orgStringifier = stringify
32
- assert isFunction(func), "setStringifier() arg is not a function"
33
- stringify = func
34
- return orgStringifier
35
-
36
- # ---------------------------------------------------------------------------
37
-
38
- export resetStringifier = () ->
39
-
40
- setStringifier orderedStringify
41
-
42
- # ---------------------------------------------------------------------------
43
-
44
- export setLogger = (func) ->
45
-
46
- assert isFunction(func), "setLogger() arg is not a function"
47
- orgLogger = putstr
48
- putstr = func
49
- return orgLogger
50
-
51
- # ---------------------------------------------------------------------------
52
-
53
- export resetLogger = () ->
54
-
55
- setLogger console.log
56
-
57
- # ---------------------------------------------------------------------------
58
-
59
- export tamlStringify = (obj, escape=false) ->
60
-
61
- return toTAML(obj, {
62
- useTabs: false
63
- sortKeys: false
64
- escape
65
- })
66
-
67
- # ---------------------------------------------------------------------------
68
-
69
- export orderedStringify = (obj, escape=false) ->
70
-
71
- return toTAML(obj, {
72
- useTabs: false
73
- sortKeys: true
74
- escape
75
- })
76
-
77
- # ---------------------------------------------------------------------------
78
-
79
- maxOneLine = 32
80
-
81
- # ---------------------------------------------------------------------------
82
-
83
- export log = (str, prefix='') ->
84
-
85
- assert isString(prefix), "not a string: #{OL(prefix)}"
86
- assert isString(str), "log(): not a string: #{OL(str)}"
87
- assert isFunction(putstr), "putstr not properly set"
88
- prefix = fixForTerminal(prefix)
89
-
90
- if doDebugLog
91
- LOG "CALL log(#{OL(str)}), prefix = #{OL(prefix)}"
92
-
93
- putstr "#{prefix}#{str}"
94
- return true # to allow use in boolean expressions
95
-
96
- # ---------------------------------------------------------------------------
97
-
98
- export logBareItem = (item, pre='') ->
99
-
100
- logItem undef, item, pre
101
- return
102
-
103
- # ---------------------------------------------------------------------------
104
-
105
- export logItem = (label, item, pre='', itemPre=undef) ->
106
-
107
- assert isString(pre), "not a string: #{OL(pre)}"
108
- assert isFunction(putstr), "putstr not properly set"
109
- assert !label || isString(label), "label a non-string"
110
- if (itemPre == undef)
111
- itemPre = pre
112
-
113
- assert pre.indexOf("\t") == -1, "pre has TAB"
114
- assert itemPre.indexOf("\t") == -1, "itemPre has TAB"
115
-
116
- if doDebugLog
117
- LOG "CALL logItem(#{OL(label)}, #{OL(item)})"
118
- LOG "pre = #{OL(pre)}"
119
- LOG "itemPre = #{OL(itemPre)}"
120
-
121
- if label
122
- labelStr = "#{label} = "
123
- else
124
- labelStr = ""
125
-
126
- if (item == undef)
127
- putstr "#{pre}#{labelStr}undef"
128
- else if (item == null)
129
- putstr "#{pre}#{labelStr}null"
130
- else if isNumber(item)
131
- putstr "#{pre}#{labelStr}#{item}"
132
- else if isString(item)
133
- if (item.length <= maxOneLine)
134
- result = escapeStr(item)
135
- hasApos = (result.indexOf("'") >= 0)
136
- if hasApos
137
- putstr "#{pre}#{labelStr}\"#{result}\""
138
- else
139
- putstr "#{pre}#{labelStr}'#{result}'"
140
- else
141
- if label
142
- putstr "#{pre}#{label}:"
143
- putBlock item, itemPre
144
- else
145
- if label
146
- putstr "#{pre}#{label}:"
147
-
148
- # --- escape special chars
149
- for str in toArray(stringify(item, true))
150
- putstr "#{itemPre}#{str}"
151
-
152
- return true
153
-
154
- # ---------------------------------------------------------------------------
155
-
156
- export shortEnough = (label, value) ->
157
-
158
- return (value == undef)
159
-
160
- # ---------------------------------------------------------------------------
161
- # --- needed because Windows Terminal handles TAB chars badly
162
-
163
- fixForTerminal = (str) ->
164
-
165
- if !str
166
- return ''
167
-
168
- # --- convert TAB char to 4 spaces
169
- return str.replace(/\t/g, fourSpaces)
170
-
171
- # ---------------------------------------------------------------------------
172
-
173
- putBlock = (item, prefix='') ->
174
-
175
- putstr "#{prefix}#{sep_eq}"
176
- for line in toArray(item)
177
- putstr "#{prefix}#{escapeStr(line)}"
178
- putstr "#{prefix}#{sep_eq}"
179
- return
180
-
181
- # ---------------------------------------------------------------------------
182
-
183
- if ! loaded
184
- setStringifier orderedStringify
185
- resetLogger()
186
- loaded = true
package/src/log.js DELETED
@@ -1,206 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // log.coffee
3
- var doDebugLog, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
4
-
5
- import {
6
- LOG,
7
- assert,
8
- croak
9
- } from '@jdeighan/exceptions';
10
-
11
- import {
12
- undef,
13
- isNumber,
14
- isInteger,
15
- isString,
16
- isHash,
17
- isFunction,
18
- escapeStr,
19
- sep_eq,
20
- sep_dash,
21
- pass,
22
- OL
23
- } from '@jdeighan/coffee-utils';
24
-
25
- import {
26
- toArray
27
- } from '@jdeighan/coffee-utils/block';
28
-
29
- import {
30
- toTAML
31
- } from '@jdeighan/coffee-utils/taml';
32
-
33
- // --- This logger only ever gets passed a single string argument
34
- putstr = undef;
35
-
36
- doDebugLog = false;
37
-
38
- export var stringify = undef;
39
-
40
- fourSpaces = ' ';
41
-
42
- // ---------------------------------------------------------------------------
43
- export var debugLog = function(flag = true) {
44
- doDebugLog = flag;
45
- if (doDebugLog) {
46
- LOG(`doDebugLog = ${flag}`);
47
- }
48
- };
49
-
50
- // ---------------------------------------------------------------------------
51
- export var setStringifier = function(func) {
52
- var orgStringifier;
53
- orgStringifier = stringify;
54
- assert(isFunction(func), "setStringifier() arg is not a function");
55
- stringify = func;
56
- return orgStringifier;
57
- };
58
-
59
- // ---------------------------------------------------------------------------
60
- export var resetStringifier = function() {
61
- return setStringifier(orderedStringify);
62
- };
63
-
64
- // ---------------------------------------------------------------------------
65
- export var setLogger = function(func) {
66
- var orgLogger;
67
- assert(isFunction(func), "setLogger() arg is not a function");
68
- orgLogger = putstr;
69
- putstr = func;
70
- return orgLogger;
71
- };
72
-
73
- // ---------------------------------------------------------------------------
74
- export var resetLogger = function() {
75
- return setLogger(console.log);
76
- };
77
-
78
- // ---------------------------------------------------------------------------
79
- export var tamlStringify = function(obj, escape = false) {
80
- return toTAML(obj, {
81
- useTabs: false,
82
- sortKeys: false,
83
- escape
84
- });
85
- };
86
-
87
- // ---------------------------------------------------------------------------
88
- export var orderedStringify = function(obj, escape = false) {
89
- return toTAML(obj, {
90
- useTabs: false,
91
- sortKeys: true,
92
- escape
93
- });
94
- };
95
-
96
- // ---------------------------------------------------------------------------
97
- maxOneLine = 32;
98
-
99
- // ---------------------------------------------------------------------------
100
- export var log = function(str, prefix = '') {
101
- assert(isString(prefix), `not a string: ${OL(prefix)}`);
102
- assert(isString(str), `log(): not a string: ${OL(str)}`);
103
- assert(isFunction(putstr), "putstr not properly set");
104
- prefix = fixForTerminal(prefix);
105
- if (doDebugLog) {
106
- LOG(`CALL log(${OL(str)}), prefix = ${OL(prefix)}`);
107
- }
108
- putstr(`${prefix}${str}`);
109
- return true; // to allow use in boolean expressions
110
- };
111
-
112
-
113
- // ---------------------------------------------------------------------------
114
- export var logBareItem = function(item, pre = '') {
115
- logItem(undef, item, pre);
116
- };
117
-
118
- // ---------------------------------------------------------------------------
119
- export var logItem = function(label, item, pre = '', itemPre = undef) {
120
- var hasApos, i, labelStr, len, ref, result, str;
121
- assert(isString(pre), `not a string: ${OL(pre)}`);
122
- assert(isFunction(putstr), "putstr not properly set");
123
- assert(!label || isString(label), "label a non-string");
124
- if (itemPre === undef) {
125
- itemPre = pre;
126
- }
127
- assert(pre.indexOf("\t") === -1, "pre has TAB");
128
- assert(itemPre.indexOf("\t") === -1, "itemPre has TAB");
129
- if (doDebugLog) {
130
- LOG(`CALL logItem(${OL(label)}, ${OL(item)})`);
131
- LOG(`pre = ${OL(pre)}`);
132
- LOG(`itemPre = ${OL(itemPre)}`);
133
- }
134
- if (label) {
135
- labelStr = `${label} = `;
136
- } else {
137
- labelStr = "";
138
- }
139
- if (item === undef) {
140
- putstr(`${pre}${labelStr}undef`);
141
- } else if (item === null) {
142
- putstr(`${pre}${labelStr}null`);
143
- } else if (isNumber(item)) {
144
- putstr(`${pre}${labelStr}${item}`);
145
- } else if (isString(item)) {
146
- if (item.length <= maxOneLine) {
147
- result = escapeStr(item);
148
- hasApos = result.indexOf("'") >= 0;
149
- if (hasApos) {
150
- putstr(`${pre}${labelStr}\"${result}\"`);
151
- } else {
152
- putstr(`${pre}${labelStr}'${result}'`);
153
- }
154
- } else {
155
- if (label) {
156
- putstr(`${pre}${label}:`);
157
- }
158
- putBlock(item, itemPre);
159
- }
160
- } else {
161
- if (label) {
162
- putstr(`${pre}${label}:`);
163
- }
164
- ref = toArray(stringify(item, true));
165
- // --- escape special chars
166
- for (i = 0, len = ref.length; i < len; i++) {
167
- str = ref[i];
168
- putstr(`${itemPre}${str}`);
169
- }
170
- }
171
- return true;
172
- };
173
-
174
- // ---------------------------------------------------------------------------
175
- export var shortEnough = function(label, value) {
176
- return value === undef;
177
- };
178
-
179
- // ---------------------------------------------------------------------------
180
- // --- needed because Windows Terminal handles TAB chars badly
181
- fixForTerminal = function(str) {
182
- if (!str) {
183
- return '';
184
- }
185
- // --- convert TAB char to 4 spaces
186
- return str.replace(/\t/g, fourSpaces);
187
- };
188
-
189
- // ---------------------------------------------------------------------------
190
- putBlock = function(item, prefix = '') {
191
- var i, len, line, ref;
192
- putstr(`${prefix}${sep_eq}`);
193
- ref = toArray(item);
194
- for (i = 0, len = ref.length; i < len; i++) {
195
- line = ref[i];
196
- putstr(`${prefix}${escapeStr(line)}`);
197
- }
198
- putstr(`${prefix}${sep_eq}`);
199
- };
200
-
201
- if (!loaded) {
202
- setStringifier(orderedStringify);
203
- resetLogger();
204
- }
205
-
206
- loaded = true;
package/src/stack.coffee DELETED
@@ -1,169 +0,0 @@
1
- # stack.coffee
2
-
3
- import {assert, croak} from '@jdeighan/exceptions'
4
- import {
5
- undef, defined, OL, escapeStr, deepCopy,
6
- isArray, isBoolean,
7
- } from '@jdeighan/coffee-utils'
8
-
9
- doDebugStack = false
10
-
11
- # ---------------------------------------------------------------------------
12
-
13
- export debugStack = (flag=true) ->
14
-
15
- doDebugStack = flag
16
- return
17
-
18
- # ---------------------------------------------------------------------------
19
-
20
- export class CallStack
21
-
22
- constructor: () ->
23
-
24
- @lStack = []
25
-
26
- # ........................................................................
27
-
28
- reset: () ->
29
-
30
- if doDebugStack
31
- console.log "RESET STACK"
32
- @lStack = []
33
- return
34
-
35
- # ........................................................................
36
-
37
- indent: () ->
38
-
39
- return ' '.repeat(@lStack.length)
40
-
41
- # ........................................................................
42
-
43
- enter: (funcName, lArgs=[], isLogged) ->
44
- # --- funcName might be <object>.<method>
45
-
46
- assert isArray(lArgs), "missing lArgs"
47
- assert isBoolean(isLogged), "missing isLogged"
48
-
49
- if doDebugStack
50
- console.log @indent() + "[--> ENTER #{funcName}]"
51
-
52
- lMatches = funcName.match(///^
53
- ([A-Za-z_][A-Za-z0-9_]*)
54
- (?:
55
- \.
56
- ([A-Za-z_][A-Za-z0-9_]*)
57
- )?
58
- $///)
59
- assert defined(lMatches), "Bad funcName: #{OL(funcName)}"
60
- [_, ident1, ident2] = lMatches
61
- if ident2
62
- hStackItem = {
63
- fullName: funcName # "#{ident1}.#{ident2}"
64
- funcName: ident2
65
- isLogged
66
- lArgs: deepCopy(lArgs)
67
- }
68
- else
69
- hStackItem = {
70
- fullName: funcName
71
- funcName: ident1
72
- isLogged
73
- lArgs: deepCopy(lArgs)
74
- }
75
- @lStack.push hStackItem
76
- return hStackItem
77
-
78
- # ........................................................................
79
-
80
- getLevel: () ->
81
-
82
- level = 0
83
- for item in @lStack
84
- if item.isLogged
85
- level += 1
86
- return level
87
-
88
- # ........................................................................
89
-
90
- isLogging: () ->
91
-
92
- if (@lStack.length == 0)
93
- return false
94
- else
95
- return @lStack[@lStack.length - 1].isLogged
96
-
97
- # ........................................................................
98
- # --- if stack is empty, log the error, but continue
99
-
100
- returnFrom: (fName) ->
101
-
102
- if @lStack.length == 0
103
- console.log "ERROR: returnFrom('#{fName}') but stack is empty"
104
- return
105
- {fullName, isLogged} = @lStack.pop()
106
- if doDebugStack
107
- console.log @indent() + "[<-- BACK #{fName}]"
108
- if (fullName != fName)
109
- console.log "ERROR: returnFrom('#{fName}') but TOS is #{fullName}"
110
- return
111
-
112
- return
113
-
114
- # ........................................................................
115
-
116
- curFunc: () ->
117
-
118
- if (@lStack.length == 0)
119
- return 'main'
120
- else
121
- return @lStack[@lStack.length - 1].funcName
122
-
123
- # ........................................................................
124
-
125
- isActive: (funcName) ->
126
- # --- funcName won't be <obj>.<method>
127
- # but the stack might contain that form
128
-
129
- for h in @lStack
130
- if (h.funcName == funcName)
131
- return true
132
- return false
133
-
134
- # ........................................................................
135
-
136
- dump: (label="CALL STACK") ->
137
-
138
- lLines = [label]
139
- if @lStack.length == 0
140
- lLines.push " <EMPTY>"
141
- else
142
- for item, i in @lStack
143
- lLines.push " " + @callStr(i, item)
144
- return lLines.join("\n")
145
-
146
- # ........................................................................
147
-
148
- callStr: (i, item) ->
149
-
150
- sym = if item.isLogged then '*' else '-'
151
- str = "#{i}: #{sym}#{item.fullName}"
152
- for arg in item.lArgs
153
- str += " #{OL(arg)}"
154
- return str
155
-
156
- # ........................................................................
157
-
158
- sdump: (label='CALL STACK') ->
159
-
160
- lFuncNames = []
161
- for item in @lStack
162
- if item.isLogged
163
- lFuncNames.push '*' + item.fullName
164
- else
165
- lFuncNames.push item.fullName
166
- if @lStack.length == 0
167
- return "#{label} <EMPTY>"
168
- else
169
- return "#{label} #{lFuncNames.join(' ')}"