@jdeighan/coffee-utils 10.0.3 → 10.0.6

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "10.0.3",
4
+ "version": "10.0.6",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -10,6 +10,7 @@ import {arrayToBlock} from '@jdeighan/coffee-utils/block'
10
10
  import {indented} from '@jdeighan/coffee-utils/indent'
11
11
  import {LOG} from '@jdeighan/coffee-utils/log'
12
12
  import {debug} from '@jdeighan/coffee-utils/debug'
13
+ import {isTAML, taml} from '@jdeighan/coffee-utils/taml'
13
14
  import {Section} from '@jdeighan/coffee-utils/section'
14
15
 
15
16
  # ---------------------------------------------------------------------------
@@ -32,6 +33,10 @@ export class SectionMap
32
33
  # --- lSectionTree is a tree of section/set names
33
34
 
34
35
  debug "enter SectionMap()", @lSectionTree
36
+
37
+ if isTAML(@lSectionTree)
38
+ @SectionTree = taml(@lSectionTree)
39
+
35
40
  assert isArray(@lSectionTree), "not an array"
36
41
 
37
42
  # --- keys are section names, values are Section objects
package/src/SectionMap.js CHANGED
@@ -40,6 +40,11 @@ import {
40
40
  debug
41
41
  } from '@jdeighan/coffee-utils/debug';
42
42
 
43
+ import {
44
+ isTAML,
45
+ taml
46
+ } from '@jdeighan/coffee-utils/taml';
47
+
43
48
  import {
44
49
  Section
45
50
  } from '@jdeighan/coffee-utils/section';
@@ -60,6 +65,9 @@ export var SectionMap = class SectionMap {
60
65
  this.lSectionTree = lSectionTree;
61
66
  // --- lSectionTree is a tree of section/set names
62
67
  debug("enter SectionMap()", this.lSectionTree);
68
+ if (isTAML(this.lSectionTree)) {
69
+ this.SectionTree = taml(this.lSectionTree);
70
+ }
63
71
  assert(isArray(this.lSectionTree), "not an array");
64
72
  // --- keys are section names, values are Section objects
65
73
  this.hSections = {};
@@ -21,6 +21,13 @@ export isComment = (line) ->
21
21
 
22
22
  # ---------------------------------------------------------------------------
23
23
 
24
+ export isSubclassOf = (subClass, superClass) ->
25
+
26
+ return (subClass == superClass) \
27
+ || (subClass.prototype instanceof superClass)
28
+
29
+ # ---------------------------------------------------------------------------
30
+
24
31
  export eval_expr = (str) ->
25
32
 
26
33
  str = str.replace(/\bundef\b/g, 'undefined')
@@ -33,14 +40,6 @@ export pass = () ->
33
40
 
34
41
  # ---------------------------------------------------------------------------
35
42
 
36
- export getClassName = (obj) ->
37
-
38
- if (typeof obj != 'object')
39
- return undef
40
- return obj.constructor.name
41
-
42
- # ---------------------------------------------------------------------------
43
-
44
43
  export patchStr = (bigstr, pos, str) ->
45
44
 
46
45
  endpos = pos + str.length
@@ -62,6 +61,55 @@ export charCount = (str, ch) ->
62
61
 
63
62
  # ---------------------------------------------------------------------------
64
63
 
64
+ export isConstructor = (f) ->
65
+
66
+ try
67
+ new f()
68
+ catch err
69
+ if (err.message.indexOf('is not a constructor') >= 0)
70
+ return false;
71
+ return true;
72
+
73
+ # ---------------------------------------------------------------------------
74
+
75
+ export jsType = (x) ->
76
+
77
+ if notdefined(x)
78
+ return [undef, undef]
79
+ else if isString(x)
80
+ if x.match(/^\s*$/)
81
+ return ['string', 'empty']
82
+ else
83
+ return ['string', undef]
84
+ else if isNumber(x)
85
+ if Number.isInteger(x)
86
+ return ['number', 'integer']
87
+ else
88
+ return ['number', undef]
89
+ else if isBoolean(x)
90
+ return ['boolean', undef]
91
+ else if isHash(x)
92
+ lKeys = Object.keys(x);
93
+ if (lKeys.length == 0)
94
+ return ['hash', 'empty']
95
+ else
96
+ return ['hash', undef]
97
+ else if isArray(x)
98
+ if (x.length == 0)
99
+ return ['array', 'empty']
100
+ else
101
+ return ['array', undef]
102
+ else if isConstructor(x)
103
+ return ['function', 'constructor']
104
+ else if isFunction(x)
105
+ return ['function', undef]
106
+ else if isObject(x)
107
+ return ['object', undef]
108
+ else
109
+ croak "Unknown type: #{OL(x)}"
110
+
111
+ # ---------------------------------------------------------------------------
112
+
65
113
  export isString = (x) ->
66
114
 
67
115
  return (typeof x == 'string') || (x instanceof String)
@@ -94,6 +142,14 @@ export isObject = (x) ->
94
142
 
95
143
  # ---------------------------------------------------------------------------
96
144
 
145
+ export getClassName = (obj) ->
146
+
147
+ if (typeof obj != 'object')
148
+ return undef
149
+ return obj.constructor.name
150
+
151
+ # ---------------------------------------------------------------------------
152
+
97
153
  export isArray = (x) ->
98
154
 
99
155
  return Array.isArray(x)
@@ -207,13 +263,15 @@ export isRegExp = (x) ->
207
263
 
208
264
  # ---------------------------------------------------------------------------
209
265
 
210
- export isNumber = (x, hOptions={}) ->
266
+ export isNumber = (x, hOptions=undef) ->
211
267
 
212
268
  result = (typeof x == 'number') || (x instanceof Number)
213
- if result
214
- if defined(hOptions.min) && (x < hOptions.min)
269
+ if result && defined(hOptions)
270
+ assert isHash(hOptions), "2nd arg not a hash: #{OL(hOptions)}"
271
+ {min, max} = hOptions
272
+ if defined(min) && (x < min)
215
273
  result = false
216
- if defined(hOptions.max) && (x > hOptions.max)
274
+ if defined(max) && (x > max)
217
275
  result = false
218
276
  return result
219
277
 
@@ -27,6 +27,11 @@ export var isComment = function(line) {
27
27
  return defined(lMatches);
28
28
  };
29
29
 
30
+ // ---------------------------------------------------------------------------
31
+ export var isSubclassOf = function(subClass, superClass) {
32
+ return (subClass === superClass) || (subClass.prototype instanceof superClass);
33
+ };
34
+
30
35
  // ---------------------------------------------------------------------------
31
36
  export var eval_expr = function(str) {
32
37
  str = str.replace(/\bundef\b/g, 'undefined');
@@ -37,14 +42,6 @@ export var eval_expr = function(str) {
37
42
  // pass - do nothing
38
43
  export var pass = function() {};
39
44
 
40
- // ---------------------------------------------------------------------------
41
- export var getClassName = function(obj) {
42
- if (typeof obj !== 'object') {
43
- return undef;
44
- }
45
- return obj.constructor.name;
46
- };
47
-
48
45
  // ---------------------------------------------------------------------------
49
46
  export var patchStr = function(bigstr, pos, str) {
50
47
  var endpos;
@@ -68,6 +65,63 @@ export var charCount = function(str, ch) {
68
65
  return count;
69
66
  };
70
67
 
68
+ // ---------------------------------------------------------------------------
69
+ export var isConstructor = function(f) {
70
+ var err;
71
+ try {
72
+ new f();
73
+ } catch (error1) {
74
+ err = error1;
75
+ if (err.message.indexOf('is not a constructor') >= 0) {
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ };
81
+
82
+ // ---------------------------------------------------------------------------
83
+ export var jsType = function(x) {
84
+ var lKeys;
85
+ if (notdefined(x)) {
86
+ return [undef, undef];
87
+ } else if (isString(x)) {
88
+ if (x.match(/^\s*$/)) {
89
+ return ['string', 'empty'];
90
+ } else {
91
+ return ['string', undef];
92
+ }
93
+ } else if (isNumber(x)) {
94
+ if (Number.isInteger(x)) {
95
+ return ['number', 'integer'];
96
+ } else {
97
+ return ['number', undef];
98
+ }
99
+ } else if (isBoolean(x)) {
100
+ return ['boolean', undef];
101
+ } else if (isHash(x)) {
102
+ lKeys = Object.keys(x);
103
+ if (lKeys.length === 0) {
104
+ return ['hash', 'empty'];
105
+ } else {
106
+ return ['hash', undef];
107
+ }
108
+ } else if (isArray(x)) {
109
+ if (x.length === 0) {
110
+ return ['array', 'empty'];
111
+ } else {
112
+ return ['array', undef];
113
+ }
114
+ } else if (isConstructor(x)) {
115
+ return ['function', 'constructor'];
116
+ } else if (isFunction(x)) {
117
+ return ['function', undef];
118
+ } else if (isObject(x)) {
119
+ return ['object', undef];
120
+ } else {
121
+ return croak(`Unknown type: ${OL(x)}`);
122
+ }
123
+ };
124
+
71
125
  // ---------------------------------------------------------------------------
72
126
  export var isString = function(x) {
73
127
  return (typeof x === 'string') || (x instanceof String);
@@ -94,6 +148,14 @@ export var isObject = function(x) {
94
148
  return (typeof x === 'object') && !isString(x) && !isArray(x) && !isHash(x) && !isNumber(x);
95
149
  };
96
150
 
151
+ // ---------------------------------------------------------------------------
152
+ export var getClassName = function(obj) {
153
+ if (typeof obj !== 'object') {
154
+ return undef;
155
+ }
156
+ return obj.constructor.name;
157
+ };
158
+
97
159
  // ---------------------------------------------------------------------------
98
160
  export var isArray = function(x) {
99
161
  return Array.isArray(x);
@@ -220,14 +282,16 @@ export var isRegExp = function(x) {
220
282
  };
221
283
 
222
284
  // ---------------------------------------------------------------------------
223
- export var isNumber = function(x, hOptions = {}) {
224
- var result;
285
+ export var isNumber = function(x, hOptions = undef) {
286
+ var max, min, result;
225
287
  result = (typeof x === 'number') || (x instanceof Number);
226
- if (result) {
227
- if (defined(hOptions.min) && (x < hOptions.min)) {
288
+ if (result && defined(hOptions)) {
289
+ assert(isHash(hOptions), `2nd arg not a hash: ${OL(hOptions)}`);
290
+ ({min, max} = hOptions);
291
+ if (defined(min) && (x < min)) {
228
292
  result = false;
229
293
  }
230
- if (defined(hOptions.max) && (x > hOptions.max)) {
294
+ if (defined(max) && (x > max)) {
231
295
  result = false;
232
296
  }
233
297
  }
@@ -183,7 +183,12 @@ export logItem = (label, item, pre='', itemPre=undef) ->
183
183
  putstr "#{pre}#{labelStr}#{item}"
184
184
  else if isString(item)
185
185
  if (item.length <= maxOneLine)
186
- putstr "#{pre}#{labelStr}'#{escapeStr(item)}'"
186
+ result = escapeStr(item)
187
+ hasApos = (result.indexOf("'") >= 0)
188
+ if hasApos
189
+ putstr "#{pre}#{labelStr}\"#{result}\""
190
+ else
191
+ putstr "#{pre}#{labelStr}'#{result}'"
187
192
  else
188
193
  if label
189
194
  putstr "#{pre}#{label}:"
package/src/log_utils.js CHANGED
@@ -184,7 +184,7 @@ export var logBareItem = function(item, pre = '') {
184
184
 
185
185
  // ---------------------------------------------------------------------------
186
186
  export var logItem = function(label, item, pre = '', itemPre = undef) {
187
- var i, labelStr, len, ref, str;
187
+ var hasApos, i, labelStr, len, ref, result, str;
188
188
  assert(isString(pre), `not a string: ${OL(pre)}`);
189
189
  assert(isFunction(putstr), "putstr not properly set");
190
190
  assert(!label || isString(label), "label a non-string");
@@ -207,7 +207,13 @@ export var logItem = function(label, item, pre = '', itemPre = undef) {
207
207
  putstr(`${pre}${labelStr}${item}`);
208
208
  } else if (isString(item)) {
209
209
  if (item.length <= maxOneLine) {
210
- putstr(`${pre}${labelStr}'${escapeStr(item)}'`);
210
+ result = escapeStr(item);
211
+ hasApos = result.indexOf("'") >= 0;
212
+ if (hasApos) {
213
+ putstr(`${pre}${labelStr}\"${result}\"`);
214
+ } else {
215
+ putstr(`${pre}${labelStr}'${result}'`);
216
+ }
211
217
  } else {
212
218
  if (label) {
213
219
  putstr(`${pre}${label}:`);