@jdeighan/coffee-utils 8.0.5 → 8.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": "8.0.5",
4
+ "version": "8.0.6",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -66,4 +66,7 @@ export class Section
66
66
 
67
67
  getBlock: () ->
68
68
 
69
- return arrayToBlock(@lLines)
69
+ if (@lLines.length == 0)
70
+ return undef
71
+ else
72
+ return arrayToBlock(@lLines)
package/src/Section.js CHANGED
@@ -79,7 +79,11 @@ export var Section = class Section {
79
79
 
80
80
  // ..........................................................
81
81
  getBlock() {
82
- return arrayToBlock(this.lLines);
82
+ if (this.lLines.length === 0) {
83
+ return undef;
84
+ } else {
85
+ return arrayToBlock(this.lLines);
86
+ }
83
87
  }
84
88
 
85
89
  };
@@ -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,36 @@ export class SectionMap
119
120
  return
120
121
 
121
122
  # ..........................................................
123
+ # --- procFunc should be (name, text) -> return processedText
122
124
 
123
- getBlock: () ->
125
+ getBlock: (procFunc=undef, lTree=undef) ->
124
126
 
125
127
  debug "enter getBlock()"
128
+ if (lTree == undef)
129
+ lTree = @lSectionTree
130
+ else
131
+ assert isArray(lTree), "not an array #{OL(lTree)}"
132
+
126
133
  lParts = []
127
- for [_, sect] from @allSections()
128
- if sect.nonEmpty()
129
- lParts.push sect.getBlock()
134
+ for part in lTree
135
+ if isString(part)
136
+ text = @section(part).getBlock()
137
+ if nonEmpty(text) && defined(procFunc)
138
+ text = procFunc(part, text)
139
+ else if isNonEmptyArray(part)
140
+ if isSectionName(part[0])
141
+ text = @getBlock(procFunc, part)
142
+ else if isSetName(part[0])
143
+ text = @getBlock(procFunc, part.slice(1))
144
+ if nonEmpty(text) && defined(procFunc)
145
+ text = procFunc(part[0], text)
146
+ else
147
+ croak "Bad part: #{OL(part)}"
148
+ else
149
+ croak "Bad part: #{OL(part)}"
150
+ if defined(text)
151
+ lParts.push text
152
+
130
153
  debug 'lParts', lParts
131
154
  result = arrayToBlock(lParts)
132
155
  debug "return from getBlock()", result
@@ -183,33 +206,6 @@ export class SectionMap
183
206
 
184
207
  # ..........................................................
185
208
 
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
209
  getShape: () ->
214
210
 
215
211
  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,39 @@ 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, text) -> return processedText
151
+ getBlock(procFunc = undef, lTree = undef) {
152
+ var j, lParts, len, part, result, text;
151
153
  debug("enter getBlock()");
154
+ if (lTree === undef) {
155
+ lTree = this.lSectionTree;
156
+ } else {
157
+ assert(isArray(lTree), `not an array ${OL(lTree)}`);
158
+ }
152
159
  lParts = [];
153
- ref = this.allSections();
154
- for (x of ref) {
155
- [_, sect] = x;
156
- if (sect.nonEmpty()) {
157
- lParts.push(sect.getBlock());
160
+ for (j = 0, len = lTree.length; j < len; j++) {
161
+ part = lTree[j];
162
+ if (isString(part)) {
163
+ text = this.section(part).getBlock();
164
+ if (nonEmpty(text) && defined(procFunc)) {
165
+ text = procFunc(part, text);
166
+ }
167
+ } else if (isNonEmptyArray(part)) {
168
+ if (isSectionName(part[0])) {
169
+ text = this.getBlock(procFunc, part);
170
+ } else if (isSetName(part[0])) {
171
+ text = this.getBlock(procFunc, part.slice(1));
172
+ if (nonEmpty(text) && defined(procFunc)) {
173
+ text = procFunc(part[0], text);
174
+ }
175
+ } else {
176
+ croak(`Bad part: ${OL(part)}`);
177
+ }
178
+ } else {
179
+ croak(`Bad part: ${OL(part)}`);
180
+ }
181
+ if (defined(text)) {
182
+ lParts.push(text);
158
183
  }
159
184
  }
160
185
  debug('lParts', lParts);
@@ -213,37 +238,6 @@ export var SectionMap = class SectionMap {
213
238
  return this.length(desc) > 0;
214
239
  }
215
240
 
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
241
  // ..........................................................
248
242
  getShape() {
249
243
  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");
@@ -146,3 +146,10 @@ export tabify = (str, numSpaces=undef) ->
146
146
  export untabify = (str, numSpaces=3) ->
147
147
 
148
148
  return str.replace(/\t/g, ' '.repeat(numSpaces))
149
+
150
+ # ---------------------------------------------------------------------------
151
+ # enclose - indent text, surround with pre and post
152
+
153
+ export enclose = (text, pre, post) ->
154
+
155
+ return pre + "\n" + indented(text) + "\n" + post
@@ -175,3 +175,9 @@ export var tabify = function(str, numSpaces = undef) {
175
175
  export var untabify = function(str, numSpaces = 3) {
176
176
  return str.replace(/\t/g, ' '.repeat(numSpaces));
177
177
  };
178
+
179
+ // ---------------------------------------------------------------------------
180
+ // enclose - indent text, surround with pre and post
181
+ export var enclose = function(text, pre, post) {
182
+ return pre + "\n" + indented(text) + "\n" + post;
183
+ };