@jdeighan/coffee-utils 10.0.4 → 10.0.5

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": "10.0.4",
4
+ "version": "10.0.5",
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 = {};
@@ -33,14 +33,6 @@ export pass = () ->
33
33
 
34
34
  # ---------------------------------------------------------------------------
35
35
 
36
- export getClassName = (obj) ->
37
-
38
- if (typeof obj != 'object')
39
- return undef
40
- return obj.constructor.name
41
-
42
- # ---------------------------------------------------------------------------
43
-
44
36
  export patchStr = (bigstr, pos, str) ->
45
37
 
46
38
  endpos = pos + str.length
@@ -62,6 +54,55 @@ export charCount = (str, ch) ->
62
54
 
63
55
  # ---------------------------------------------------------------------------
64
56
 
57
+ export isConstructor = (f) ->
58
+
59
+ try
60
+ new f()
61
+ catch err
62
+ if (err.message.indexOf('is not a constructor') >= 0)
63
+ return false;
64
+ return true;
65
+
66
+ # ---------------------------------------------------------------------------
67
+
68
+ export jsType = (x) ->
69
+
70
+ if notdefined(x)
71
+ return [undef, undef]
72
+ else if isString(x)
73
+ if x.match(/^\s*$/)
74
+ return ['string', 'empty']
75
+ else
76
+ return ['string', undef]
77
+ else if isNumber(x)
78
+ if Number.isInteger(x)
79
+ return ['number', 'integer']
80
+ else
81
+ return ['number', undef]
82
+ else if isBoolean(x)
83
+ return ['boolean', undef]
84
+ else if isHash(x)
85
+ lKeys = Object.keys(x);
86
+ if (lKeys.length == 0)
87
+ return ['hash', 'empty']
88
+ else
89
+ return ['hash', undef]
90
+ else if isArray(x)
91
+ if (x.length == 0)
92
+ return ['array', 'empty']
93
+ else
94
+ return ['array', undef]
95
+ else if isConstructor(x)
96
+ return ['function', 'constructor']
97
+ else if isFunction(x)
98
+ return ['function', undef]
99
+ else if isObject(x)
100
+ return ['object', undef]
101
+ else
102
+ croak "Unknown type: #{OL(x)}"
103
+
104
+ # ---------------------------------------------------------------------------
105
+
65
106
  export isString = (x) ->
66
107
 
67
108
  return (typeof x == 'string') || (x instanceof String)
@@ -94,6 +135,14 @@ export isObject = (x) ->
94
135
 
95
136
  # ---------------------------------------------------------------------------
96
137
 
138
+ export getClassName = (obj) ->
139
+
140
+ if (typeof obj != 'object')
141
+ return undef
142
+ return obj.constructor.name
143
+
144
+ # ---------------------------------------------------------------------------
145
+
97
146
  export isArray = (x) ->
98
147
 
99
148
  return Array.isArray(x)
@@ -207,13 +256,15 @@ export isRegExp = (x) ->
207
256
 
208
257
  # ---------------------------------------------------------------------------
209
258
 
210
- export isNumber = (x, hOptions={}) ->
259
+ export isNumber = (x, hOptions=undef) ->
211
260
 
212
261
  result = (typeof x == 'number') || (x instanceof Number)
213
- if result
214
- if defined(hOptions.min) && (x < hOptions.min)
262
+ if result && defined(hOptions)
263
+ assert isHash(hOptions), "2nd arg not a hash: #{OL(hOptions)}"
264
+ {min, max} = hOptions
265
+ if defined(min) && (x < min)
215
266
  result = false
216
- if defined(hOptions.max) && (x > hOptions.max)
267
+ if defined(max) && (x > max)
217
268
  result = false
218
269
  return result
219
270
 
@@ -37,14 +37,6 @@ export var eval_expr = function(str) {
37
37
  // pass - do nothing
38
38
  export var pass = function() {};
39
39
 
40
- // ---------------------------------------------------------------------------
41
- export var getClassName = function(obj) {
42
- if (typeof obj !== 'object') {
43
- return undef;
44
- }
45
- return obj.constructor.name;
46
- };
47
-
48
40
  // ---------------------------------------------------------------------------
49
41
  export var patchStr = function(bigstr, pos, str) {
50
42
  var endpos;
@@ -68,6 +60,63 @@ export var charCount = function(str, ch) {
68
60
  return count;
69
61
  };
70
62
 
63
+ // ---------------------------------------------------------------------------
64
+ export var isConstructor = function(f) {
65
+ var err;
66
+ try {
67
+ new f();
68
+ } catch (error1) {
69
+ err = error1;
70
+ if (err.message.indexOf('is not a constructor') >= 0) {
71
+ return false;
72
+ }
73
+ }
74
+ return true;
75
+ };
76
+
77
+ // ---------------------------------------------------------------------------
78
+ export var jsType = function(x) {
79
+ var lKeys;
80
+ if (notdefined(x)) {
81
+ return [undef, undef];
82
+ } else if (isString(x)) {
83
+ if (x.match(/^\s*$/)) {
84
+ return ['string', 'empty'];
85
+ } else {
86
+ return ['string', undef];
87
+ }
88
+ } else if (isNumber(x)) {
89
+ if (Number.isInteger(x)) {
90
+ return ['number', 'integer'];
91
+ } else {
92
+ return ['number', undef];
93
+ }
94
+ } else if (isBoolean(x)) {
95
+ return ['boolean', undef];
96
+ } else if (isHash(x)) {
97
+ lKeys = Object.keys(x);
98
+ if (lKeys.length === 0) {
99
+ return ['hash', 'empty'];
100
+ } else {
101
+ return ['hash', undef];
102
+ }
103
+ } else if (isArray(x)) {
104
+ if (x.length === 0) {
105
+ return ['array', 'empty'];
106
+ } else {
107
+ return ['array', undef];
108
+ }
109
+ } else if (isConstructor(x)) {
110
+ return ['function', 'constructor'];
111
+ } else if (isFunction(x)) {
112
+ return ['function', undef];
113
+ } else if (isObject(x)) {
114
+ return ['object', undef];
115
+ } else {
116
+ return croak(`Unknown type: ${OL(x)}`);
117
+ }
118
+ };
119
+
71
120
  // ---------------------------------------------------------------------------
72
121
  export var isString = function(x) {
73
122
  return (typeof x === 'string') || (x instanceof String);
@@ -94,6 +143,14 @@ export var isObject = function(x) {
94
143
  return (typeof x === 'object') && !isString(x) && !isArray(x) && !isHash(x) && !isNumber(x);
95
144
  };
96
145
 
146
+ // ---------------------------------------------------------------------------
147
+ export var getClassName = function(obj) {
148
+ if (typeof obj !== 'object') {
149
+ return undef;
150
+ }
151
+ return obj.constructor.name;
152
+ };
153
+
97
154
  // ---------------------------------------------------------------------------
98
155
  export var isArray = function(x) {
99
156
  return Array.isArray(x);
@@ -220,14 +277,16 @@ export var isRegExp = function(x) {
220
277
  };
221
278
 
222
279
  // ---------------------------------------------------------------------------
223
- export var isNumber = function(x, hOptions = {}) {
224
- var result;
280
+ export var isNumber = function(x, hOptions = undef) {
281
+ var max, min, result;
225
282
  result = (typeof x === 'number') || (x instanceof Number);
226
- if (result) {
227
- if (defined(hOptions.min) && (x < hOptions.min)) {
283
+ if (result && defined(hOptions)) {
284
+ assert(isHash(hOptions), `2nd arg not a hash: ${OL(hOptions)}`);
285
+ ({min, max} = hOptions);
286
+ if (defined(min) && (x < min)) {
228
287
  result = false;
229
288
  }
230
- if (defined(hOptions.max) && (x > hOptions.max)) {
289
+ if (defined(max) && (x > max)) {
231
290
  result = false;
232
291
  }
233
292
  }