@jdeighan/coffee-utils 8.0.5 → 8.0.8

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": "8.0.5",
4
+ "version": "8.0.8",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -12,34 +12,34 @@ export class Section
12
12
 
13
13
  constructor: (@name) ->
14
14
 
15
- @lLines = []
15
+ @lParts = []
16
16
 
17
17
  # ..........................................................
18
18
 
19
19
  length: () ->
20
20
 
21
- return @lLines.length
21
+ return @lParts.length
22
22
 
23
23
  # ..........................................................
24
24
 
25
25
  indent: (level=1) ->
26
26
 
27
- lNewLines = for line in @lLines
27
+ lNewLines = for line in @lParts
28
28
  indented(line, level)
29
- @lLines = lNewLines
29
+ @lParts = lNewLines
30
30
  return
31
31
 
32
32
  # ..........................................................
33
33
 
34
34
  isEmpty: () ->
35
35
 
36
- return (@lLines.length == 0)
36
+ return (@lParts.length == 0)
37
37
 
38
38
  # ..........................................................
39
39
 
40
40
  nonEmpty: () ->
41
41
 
42
- return (@lLines.length > 0)
42
+ return (@lParts.length > 0)
43
43
 
44
44
  # ..........................................................
45
45
 
@@ -47,9 +47,9 @@ export class Section
47
47
 
48
48
  if isArray(data)
49
49
  for line in data
50
- @lLines.push line
50
+ @lParts.push line
51
51
  else
52
- @lLines.push data
52
+ @lParts.push data
53
53
  return
54
54
 
55
55
  # ..........................................................
@@ -57,13 +57,22 @@ export class Section
57
57
  prepend: (data) ->
58
58
 
59
59
  if isArray(data)
60
- @lLines = [data..., @lLines...]
60
+ @lParts = [data..., @lParts...]
61
61
  else
62
- @lLines = [data, @lLines...]
62
+ @lParts = [data, @lParts...]
63
63
  return
64
64
 
65
65
  # ..........................................................
66
66
 
67
+ getParts: () ->
68
+
69
+ return @lParts
70
+
71
+ # ..........................................................
72
+
67
73
  getBlock: () ->
68
74
 
69
- return arrayToBlock(@lLines)
75
+ if (@lParts.length == 0)
76
+ return undef
77
+ else
78
+ return arrayToBlock(@lParts)
package/src/Section.js CHANGED
@@ -21,12 +21,12 @@ import {
21
21
  export var Section = class Section {
22
22
  constructor(name) {
23
23
  this.name = name;
24
- this.lLines = [];
24
+ this.lParts = [];
25
25
  }
26
26
 
27
27
  // ..........................................................
28
28
  length() {
29
- return this.lLines.length;
29
+ return this.lParts.length;
30
30
  }
31
31
 
32
32
  // ..........................................................
@@ -34,7 +34,7 @@ export var Section = class Section {
34
34
  var lNewLines, line;
35
35
  lNewLines = (function() {
36
36
  var i, len, ref, results;
37
- ref = this.lLines;
37
+ ref = this.lParts;
38
38
  results = [];
39
39
  for (i = 0, len = ref.length; i < len; i++) {
40
40
  line = ref[i];
@@ -42,17 +42,17 @@ export var Section = class Section {
42
42
  }
43
43
  return results;
44
44
  }).call(this);
45
- this.lLines = lNewLines;
45
+ this.lParts = lNewLines;
46
46
  }
47
47
 
48
48
  // ..........................................................
49
49
  isEmpty() {
50
- return this.lLines.length === 0;
50
+ return this.lParts.length === 0;
51
51
  }
52
52
 
53
53
  // ..........................................................
54
54
  nonEmpty() {
55
- return this.lLines.length > 0;
55
+ return this.lParts.length > 0;
56
56
  }
57
57
 
58
58
  // ..........................................................
@@ -61,25 +61,34 @@ export var Section = class Section {
61
61
  if (isArray(data)) {
62
62
  for (i = 0, len = data.length; i < len; i++) {
63
63
  line = data[i];
64
- this.lLines.push(line);
64
+ this.lParts.push(line);
65
65
  }
66
66
  } else {
67
- this.lLines.push(data);
67
+ this.lParts.push(data);
68
68
  }
69
69
  }
70
70
 
71
71
  // ..........................................................
72
72
  prepend(data) {
73
73
  if (isArray(data)) {
74
- this.lLines = [...data, ...this.lLines];
74
+ this.lParts = [...data, ...this.lParts];
75
75
  } else {
76
- this.lLines = [data, ...this.lLines];
76
+ this.lParts = [data, ...this.lParts];
77
77
  }
78
78
  }
79
79
 
80
+ // ..........................................................
81
+ getParts() {
82
+ return this.lParts;
83
+ }
84
+
80
85
  // ..........................................................
81
86
  getBlock() {
82
- return arrayToBlock(this.lLines);
87
+ if (this.lParts.length === 0) {
88
+ return undef;
89
+ } else {
90
+ return arrayToBlock(this.lParts);
91
+ }
83
92
  }
84
93
 
85
94
  };
@@ -3,6 +3,7 @@
3
3
  import {
4
4
  assert, pass, undef, defined, croak, OL, isEmpty, nonEmpty,
5
5
  isString, isHash, isArray, isUniqueTree, isNonEmptyString,
6
+ isNonEmptyArray,
6
7
  } from '@jdeighan/coffee-utils'
7
8
  import {arrayToBlock} from '@jdeighan/coffee-utils/block'
8
9
  import {indented} from '@jdeighan/coffee-utils/indent'
@@ -119,14 +120,37 @@ export class SectionMap
119
120
  return
120
121
 
121
122
  # ..........................................................
123
+ # --- procFunc should be (name, block) ->
124
+ # return processed block
122
125
 
123
- getBlock: () ->
126
+ getBlock: (procFunc=undef, lTree=undef) ->
124
127
 
125
128
  debug "enter getBlock()"
129
+ if (lTree == undef)
130
+ lTree = @lSectionTree
131
+ else
132
+ assert isArray(lTree), "not an array #{OL(lTree)}"
133
+
126
134
  lParts = []
127
- for [_, sect] from @allSections()
128
- if sect.nonEmpty()
129
- lParts.push sect.getBlock()
135
+ for part in lTree
136
+ if isString(part)
137
+ text = @section(part).getBlock()
138
+ if nonEmpty(text) && defined(procFunc)
139
+ text = procFunc(part, text)
140
+ else if isNonEmptyArray(part)
141
+ if isSectionName(part[0])
142
+ text = @getBlock(procFunc, part)
143
+ else if isSetName(part[0])
144
+ text = @getBlock(procFunc, part.slice(1))
145
+ if nonEmpty(text) && defined(procFunc)
146
+ text = procFunc(part[0], text)
147
+ else
148
+ croak "Bad part: #{OL(part)}"
149
+ else
150
+ croak "Bad part: #{OL(part)}"
151
+ if defined(text)
152
+ lParts.push text
153
+
130
154
  debug 'lParts', lParts
131
155
  result = arrayToBlock(lParts)
132
156
  debug "return from getBlock()", result
@@ -183,33 +207,6 @@ export class SectionMap
183
207
 
184
208
  # ..........................................................
185
209
 
186
- indent: (desc=undef, level=1) ->
187
-
188
- for [_, sect] from @allSections(desc)
189
- sect.indent(level)
190
- return
191
-
192
- # ..........................................................
193
-
194
- enclose: (name, pre, post) ->
195
-
196
- if isSectionName(name)
197
- sect = @section(name)
198
- if sect.nonEmpty()
199
- sect.indent()
200
- sect.prepend(pre)
201
- sect.add(post)
202
- else if isSetName(name)
203
- if @nonEmpty(name)
204
- @indent(name)
205
- @firstSection(name).prepend(pre)
206
- @lastSection(name).add(post)
207
- else
208
- croak "Bad name param #{OL(name)}"
209
- return
210
-
211
- # ..........................................................
212
-
213
210
  getShape: () ->
214
211
 
215
212
  debug "enter getShape()"
package/src/SectionMap.js CHANGED
@@ -15,7 +15,8 @@ import {
15
15
  isHash,
16
16
  isArray,
17
17
  isUniqueTree,
18
- isNonEmptyString
18
+ isNonEmptyString,
19
+ isNonEmptyArray
19
20
  } from '@jdeighan/coffee-utils';
20
21
 
21
22
  import {
@@ -146,15 +147,40 @@ export var SectionMap = class SectionMap {
146
147
  }
147
148
 
148
149
  // ..........................................................
149
- getBlock() {
150
- var _, lParts, ref, result, sect, x;
150
+ // --- procFunc should be (name, block) ->
151
+ // return processed block
152
+ getBlock(procFunc = undef, lTree = undef) {
153
+ var j, lParts, len, part, result, text;
151
154
  debug("enter getBlock()");
155
+ if (lTree === undef) {
156
+ lTree = this.lSectionTree;
157
+ } else {
158
+ assert(isArray(lTree), `not an array ${OL(lTree)}`);
159
+ }
152
160
  lParts = [];
153
- ref = this.allSections();
154
- for (x of ref) {
155
- [_, sect] = x;
156
- if (sect.nonEmpty()) {
157
- lParts.push(sect.getBlock());
161
+ for (j = 0, len = lTree.length; j < len; j++) {
162
+ part = lTree[j];
163
+ if (isString(part)) {
164
+ text = this.section(part).getBlock();
165
+ if (nonEmpty(text) && defined(procFunc)) {
166
+ text = procFunc(part, text);
167
+ }
168
+ } else if (isNonEmptyArray(part)) {
169
+ if (isSectionName(part[0])) {
170
+ text = this.getBlock(procFunc, part);
171
+ } else if (isSetName(part[0])) {
172
+ text = this.getBlock(procFunc, part.slice(1));
173
+ if (nonEmpty(text) && defined(procFunc)) {
174
+ text = procFunc(part[0], text);
175
+ }
176
+ } else {
177
+ croak(`Bad part: ${OL(part)}`);
178
+ }
179
+ } else {
180
+ croak(`Bad part: ${OL(part)}`);
181
+ }
182
+ if (defined(text)) {
183
+ lParts.push(text);
158
184
  }
159
185
  }
160
186
  debug('lParts', lParts);
@@ -213,37 +239,6 @@ export var SectionMap = class SectionMap {
213
239
  return this.length(desc) > 0;
214
240
  }
215
241
 
216
- // ..........................................................
217
- indent(desc = undef, level = 1) {
218
- var _, ref, sect, x;
219
- ref = this.allSections(desc);
220
- for (x of ref) {
221
- [_, sect] = x;
222
- sect.indent(level);
223
- }
224
- }
225
-
226
- // ..........................................................
227
- enclose(name, pre, post) {
228
- var sect;
229
- if (isSectionName(name)) {
230
- sect = this.section(name);
231
- if (sect.nonEmpty()) {
232
- sect.indent();
233
- sect.prepend(pre);
234
- sect.add(post);
235
- }
236
- } else if (isSetName(name)) {
237
- if (this.nonEmpty(name)) {
238
- this.indent(name);
239
- this.firstSection(name).prepend(pre);
240
- this.lastSection(name).add(post);
241
- }
242
- } else {
243
- croak(`Bad name param ${OL(name)}`);
244
- }
245
- }
246
-
247
242
  // ..........................................................
248
243
  getShape() {
249
244
  var lParts, level, ref, result, sect, x;
@@ -173,6 +173,12 @@ export isArray = (x) ->
173
173
 
174
174
  # ---------------------------------------------------------------------------
175
175
 
176
+ export isNonEmptyArray = (x) ->
177
+
178
+ return isArray(x) && (x.length > 0)
179
+
180
+ # ---------------------------------------------------------------------------
181
+
176
182
  export isHash = (x, lKeys) ->
177
183
 
178
184
  if ! x || (getClassName(x) != 'Object')
@@ -186,6 +192,12 @@ export isHash = (x, lKeys) ->
186
192
 
187
193
  # ---------------------------------------------------------------------------
188
194
 
195
+ export isNonEmptyHash = (x) ->
196
+
197
+ return isHash(x) && (Object.keys(x).length > 0)
198
+
199
+ # ---------------------------------------------------------------------------
200
+
189
201
  export hashHasKey = (x, key) ->
190
202
 
191
203
  assert isHash(x), "hashHasKey(): not a hash"
@@ -159,6 +159,11 @@ export var isArray = function(x) {
159
159
  return Array.isArray(x);
160
160
  };
161
161
 
162
+ // ---------------------------------------------------------------------------
163
+ export var isNonEmptyArray = function(x) {
164
+ return isArray(x) && (x.length > 0);
165
+ };
166
+
162
167
  // ---------------------------------------------------------------------------
163
168
  export var isHash = function(x, lKeys) {
164
169
  var i, key, len;
@@ -177,6 +182,11 @@ export var isHash = function(x, lKeys) {
177
182
  return true;
178
183
  };
179
184
 
185
+ // ---------------------------------------------------------------------------
186
+ export var isNonEmptyHash = function(x) {
187
+ return isHash(x) && (Object.keys(x).length > 0);
188
+ };
189
+
180
190
  // ---------------------------------------------------------------------------
181
191
  export var hashHasKey = function(x, key) {
182
192
  assert(isHash(x), "hashHasKey(): not a hash");
@@ -6,6 +6,15 @@ import {
6
6
  } from '@jdeighan/coffee-utils'
7
7
  import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
8
8
 
9
+ # ---------------------------------------------------------------------------
10
+
11
+ export splitPrefix = (line) ->
12
+
13
+ assert isString(line), "non-string #{OL(line)}"
14
+ line = rtrim(line)
15
+ lMatches = line.match(/^(\s*)(.*)$/)
16
+ return [lMatches[1], lMatches[2]]
17
+
9
18
  # ---------------------------------------------------------------------------
10
19
  # NOTE: Currently, only TAB indentation is supported
11
20
  # ---------------------------------------------------------------------------
@@ -13,11 +22,8 @@ import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
13
22
 
14
23
  export splitLine = (line) ->
15
24
 
16
- assert defined(line), "splitLine(): line is undef"
17
- assert isString(line), "splitLine(): non-string #{OL(line)}"
18
- line = rtrim(line)
19
- lMatches = line.match(/^(\s*)(.*)$/)
20
- return [lMatches[1].length, lMatches[2]]
25
+ [prefix, str] = splitPrefix(line)
26
+ return [indentLevel(prefix), str]
21
27
 
22
28
  # ---------------------------------------------------------------------------
23
29
  # indentation - return appropriate indentation string for given level
@@ -146,3 +152,10 @@ export tabify = (str, numSpaces=undef) ->
146
152
  export untabify = (str, numSpaces=3) ->
147
153
 
148
154
  return str.replace(/\t/g, ' '.repeat(numSpaces))
155
+
156
+ # ---------------------------------------------------------------------------
157
+ # enclose - indent text, surround with pre and post
158
+
159
+ export enclose = (text, pre, post) ->
160
+
161
+ return pre + "\n" + indented(text) + "\n" + post
@@ -19,17 +19,23 @@ import {
19
19
  blockToArray
20
20
  } from '@jdeighan/coffee-utils/block';
21
21
 
22
+ // ---------------------------------------------------------------------------
23
+ export var splitPrefix = function(line) {
24
+ var lMatches;
25
+ assert(isString(line), `non-string ${OL(line)}`);
26
+ line = rtrim(line);
27
+ lMatches = line.match(/^(\s*)(.*)$/);
28
+ return [lMatches[1], lMatches[2]];
29
+ };
30
+
22
31
  // ---------------------------------------------------------------------------
23
32
  // NOTE: Currently, only TAB indentation is supported
24
33
  // ---------------------------------------------------------------------------
25
34
  // splitLine - separate a line into [level, line]
26
35
  export var splitLine = function(line) {
27
- var lMatches;
28
- assert(defined(line), "splitLine(): line is undef");
29
- assert(isString(line), `splitLine(): non-string ${OL(line)}`);
30
- line = rtrim(line);
31
- lMatches = line.match(/^(\s*)(.*)$/);
32
- return [lMatches[1].length, lMatches[2]];
36
+ var prefix, str;
37
+ [prefix, str] = splitPrefix(line);
38
+ return [indentLevel(prefix), str];
33
39
  };
34
40
 
35
41
  // ---------------------------------------------------------------------------
@@ -175,3 +181,9 @@ export var tabify = function(str, numSpaces = undef) {
175
181
  export var untabify = function(str, numSpaces = 3) {
176
182
  return str.replace(/\t/g, ' '.repeat(numSpaces));
177
183
  };
184
+
185
+ // ---------------------------------------------------------------------------
186
+ // enclose - indent text, surround with pre and post
187
+ export var enclose = function(text, pre, post) {
188
+ return pre + "\n" + indented(text) + "\n" + post;
189
+ };