@jdeighan/coffee-utils 9.0.2 → 9.0.5

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": "9.0.2",
4
+ "version": "9.0.5",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -55,6 +55,6 @@
55
55
  "svelte": "^3.49.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@jdeighan/unit-tester": "^2.0.20"
58
+ "@jdeighan/unit-tester": "^2.0.21"
59
59
  }
60
60
  }
@@ -23,10 +23,10 @@ export class Section
23
23
 
24
24
  # ..........................................................
25
25
 
26
- indent: (level=1) ->
26
+ indent: (level=1, oneIndent="\t") ->
27
27
 
28
28
  lNewLines = for line in @lParts
29
- indented(line, level)
29
+ indented(line, level, oneIndent)
30
30
  @lParts = lNewLines
31
31
  return
32
32
 
package/src/Section.js CHANGED
@@ -34,7 +34,7 @@ export var Section = class Section {
34
34
  }
35
35
 
36
36
  // ..........................................................
37
- indent(level = 1) {
37
+ indent(level = 1, oneIndent = "\t") {
38
38
  var lNewLines, line;
39
39
  lNewLines = (function() {
40
40
  var i, len, ref, results;
@@ -42,7 +42,7 @@ export var Section = class Section {
42
42
  results = [];
43
43
  for (i = 0, len = ref.length; i < len; i++) {
44
44
  line = ref[i];
45
- results.push(indented(line, level));
45
+ results.push(indented(line, level, oneIndent));
46
46
  }
47
47
  return results;
48
48
  }).call(this);
@@ -121,10 +121,11 @@ export class SectionMap
121
121
  return
122
122
 
123
123
  # ..........................................................
124
- # --- procFunc should be (name, block) ->
125
- # return processed block
124
+ # --- hProc should be <name> -> <function>
125
+ # <function> should be <block> -> <block>
126
+ # --- lTree allows you to get just a section
126
127
 
127
- getBlock: (procFunc=undef, lTree=undef) ->
128
+ getBlock: (hProc={}, lTree=undef) ->
128
129
 
129
130
  debug "enter getBlock()"
130
131
  if (lTree == undef)
@@ -135,22 +136,23 @@ export class SectionMap
135
136
  lParts = []
136
137
  for part in lTree
137
138
  if isString(part)
138
- text = @section(part).getBlock()
139
- if nonEmpty(text) && defined(procFunc)
140
- text = procFunc(part, text)
139
+ block = @section(part).getBlock()
140
+ if defined(hProc[part])
141
+ # --- called even if block is empty
142
+ block = hProc[part](block)
141
143
  else if isNonEmptyArray(part)
142
144
  if isSectionName(part[0])
143
- text = @getBlock(procFunc, part)
145
+ block = @getBlock(hProc, part)
144
146
  else if isSetName(part[0])
145
- text = @getBlock(procFunc, part.slice(1))
146
- if nonEmpty(text) && defined(procFunc)
147
- text = procFunc(part[0], text)
147
+ block = @getBlock(hProc, part.slice(1))
148
+ if defined(hProc[part[0]])
149
+ block = hProc[part[0]](block)
148
150
  else
149
151
  croak "Bad part: #{OL(part)}"
150
152
  else
151
153
  croak "Bad part: #{OL(part)}"
152
- if defined(text)
153
- lParts.push text
154
+ if defined(block)
155
+ lParts.push block
154
156
 
155
157
  debug 'lParts', lParts
156
158
  result = arrayToBlock(lParts)
package/src/SectionMap.js CHANGED
@@ -151,10 +151,11 @@ export var SectionMap = class SectionMap {
151
151
  }
152
152
 
153
153
  // ..........................................................
154
- // --- procFunc should be (name, block) ->
155
- // return processed block
156
- getBlock(procFunc = undef, lTree = undef) {
157
- var j, lParts, len, part, result, text;
154
+ // --- hProc should be <name> -> <function>
155
+ // <function> should be <block> -> <block>
156
+ // --- lTree allows you to get just a section
157
+ getBlock(hProc = {}, lTree = undef) {
158
+ var block, j, lParts, len, part, result;
158
159
  debug("enter getBlock()");
159
160
  if (lTree === undef) {
160
161
  lTree = this.lSectionTree;
@@ -165,17 +166,18 @@ export var SectionMap = class SectionMap {
165
166
  for (j = 0, len = lTree.length; j < len; j++) {
166
167
  part = lTree[j];
167
168
  if (isString(part)) {
168
- text = this.section(part).getBlock();
169
- if (nonEmpty(text) && defined(procFunc)) {
170
- text = procFunc(part, text);
169
+ block = this.section(part).getBlock();
170
+ if (defined(hProc[part])) {
171
+ // --- called even if block is empty
172
+ block = hProc[part](block);
171
173
  }
172
174
  } else if (isNonEmptyArray(part)) {
173
175
  if (isSectionName(part[0])) {
174
- text = this.getBlock(procFunc, part);
176
+ block = this.getBlock(hProc, part);
175
177
  } else if (isSetName(part[0])) {
176
- text = this.getBlock(procFunc, part.slice(1));
177
- if (nonEmpty(text) && defined(procFunc)) {
178
- text = procFunc(part[0], text);
178
+ block = this.getBlock(hProc, part.slice(1));
179
+ if (defined(hProc[part[0]])) {
180
+ block = hProc[part[0]](block);
179
181
  }
180
182
  } else {
181
183
  croak(`Bad part: ${OL(part)}`);
@@ -183,8 +185,8 @@ export var SectionMap = class SectionMap {
183
185
  } else {
184
186
  croak(`Bad part: ${OL(part)}`);
185
187
  }
186
- if (defined(text)) {
187
- lParts.push(text);
188
+ if (defined(block)) {
189
+ lParts.push(block);
188
190
  }
189
191
  }
190
192
  debug('lParts', lParts);
@@ -25,6 +25,23 @@ export blockToArray = (block) ->
25
25
  len -= 1
26
26
  return lLines
27
27
 
28
+ # ---------------------------------------------------------------------------
29
+ # toArray - split a block or array into lines w/o newlines
30
+
31
+ export toArray = (item) ->
32
+
33
+ if isString(item)
34
+ return item.split(/\r?\n/)
35
+ else if isArray(item)
36
+ # --- We still need to ensure that no strings contain newlines
37
+ lLines = []
38
+ for str in item
39
+ for substr in toArray(str)
40
+ lLines.push substr
41
+ return lLines
42
+ else
43
+ croak "Not a string or array"
44
+
28
45
  # ---------------------------------------------------------------------------
29
46
  # arrayToBlock - block will have no trailing whitespace
30
47
 
@@ -39,6 +56,21 @@ export arrayToBlock = (lLines) ->
39
56
  else
40
57
  return rtrim(lLines.join('\n'))
41
58
 
59
+ # ---------------------------------------------------------------------------
60
+ # toBlock - block may have trailing whitespace
61
+ # but undef items are ignored
62
+
63
+ export toBlock = (lLines) ->
64
+
65
+ if (lLines == undef)
66
+ return undef
67
+ assert isArray(lLines), "lLines is not an array"
68
+ lLines = lLines.filter((line) => defined(line));
69
+ if lLines.length == 0
70
+ return undef
71
+ else
72
+ return lLines.join('\n')
73
+
42
74
  # ---------------------------------------------------------------------------
43
75
 
44
76
  export splitBlock = (block) ->
@@ -37,6 +37,29 @@ export var blockToArray = function(block) {
37
37
  }
38
38
  };
39
39
 
40
+ // ---------------------------------------------------------------------------
41
+ // toArray - split a block or array into lines w/o newlines
42
+ export var toArray = function(item) {
43
+ var i, j, lLines, len1, len2, ref, str, substr;
44
+ if (isString(item)) {
45
+ return item.split(/\r?\n/);
46
+ } else if (isArray(item)) {
47
+ // --- We still need to ensure that no strings contain newlines
48
+ lLines = [];
49
+ for (i = 0, len1 = item.length; i < len1; i++) {
50
+ str = item[i];
51
+ ref = toArray(str);
52
+ for (j = 0, len2 = ref.length; j < len2; j++) {
53
+ substr = ref[j];
54
+ lLines.push(substr);
55
+ }
56
+ }
57
+ return lLines;
58
+ } else {
59
+ return croak("Not a string or array");
60
+ }
61
+ };
62
+
40
63
  // ---------------------------------------------------------------------------
41
64
  // arrayToBlock - block will have no trailing whitespace
42
65
  export var arrayToBlock = function(lLines) {
@@ -54,6 +77,24 @@ export var arrayToBlock = function(lLines) {
54
77
  }
55
78
  };
56
79
 
80
+ // ---------------------------------------------------------------------------
81
+ // toBlock - block may have trailing whitespace
82
+ // but undef items are ignored
83
+ export var toBlock = function(lLines) {
84
+ if (lLines === undef) {
85
+ return undef;
86
+ }
87
+ assert(isArray(lLines), "lLines is not an array");
88
+ lLines = lLines.filter((line) => {
89
+ return defined(line);
90
+ });
91
+ if (lLines.length === 0) {
92
+ return undef;
93
+ } else {
94
+ return lLines.join('\n');
95
+ }
96
+ };
97
+
57
98
  // ---------------------------------------------------------------------------
58
99
  export var splitBlock = function(block) {
59
100
  var pos;
@@ -5,7 +5,9 @@ import {
5
5
  undef, escapeStr, defined,
6
6
  OL, isInteger, isString, isArray, isEmpty, rtrim,
7
7
  } from '@jdeighan/coffee-utils'
8
- import {arrayToBlock, blockToArray} from '@jdeighan/coffee-utils/block'
8
+ import {
9
+ arrayToBlock, blockToArray, toArray, toBlock,
10
+ } from '@jdeighan/coffee-utils/block'
9
11
 
10
12
  # ---------------------------------------------------------------------------
11
13
 
@@ -71,14 +73,11 @@ export isUndented = (line) ->
71
73
  export indented = (input, level=1, oneIndent="\t") ->
72
74
 
73
75
  assert (level >= 0), "indented(): negative level"
74
- if level == 0
76
+ if (level == 0)
75
77
  return input
76
78
 
77
79
  toAdd = indentation(level, oneIndent)
78
- if isArray(input)
79
- lInputLines = input
80
- else
81
- lInputLines = blockToArray(input)
80
+ lInputLines = toArray(input)
82
81
 
83
82
  lLines = for line in lInputLines
84
83
  if isEmpty(line)
@@ -93,27 +92,18 @@ export indented = (input, level=1, oneIndent="\t") ->
93
92
  # indentation is removed
94
93
  # - returns same type as text, i.e. either string or array
95
94
 
96
- export undented = (text, level=undef, oneIndent="\t") ->
95
+ export undented = (input, level=undef, oneIndent="\t") ->
97
96
 
98
97
  if defined(level) && (level==0)
99
- return text
100
-
101
- if isString(text)
102
- lLines = blockToArray(text)
103
- if (lLines.length == 0)
104
- return ''
105
- else if isArray(text)
106
- lLines = text
107
- for line in lLines
108
- assert isString(line), "undented(): array not all strings"
109
- if (lLines.length == 0)
110
- return []
111
- else
112
- error "undented(): Not an array or string: #{OL(text)}"
98
+ return input
99
+
100
+ lLines = toArray(input)
101
+ if (lLines.length == 0)
102
+ return ''
113
103
 
114
104
  # --- determine what to remove from beginning of each line
115
105
  if defined(level)
116
- assert isInteger(level), "undented(): level must be an integer"
106
+ assert isInteger(level), "level must be an integer"
117
107
  toRemove = indentation(level, oneIndent)
118
108
  else
119
109
  lMatches = lLines[0].match(/^\s*/)
@@ -129,8 +119,8 @@ export undented = (text, level=undef, oneIndent="\t") ->
129
119
  throw new Error("remove #{OL(toRemove)} from #{OL(text)}")
130
120
  lNewLines.push(line.substr(nToRemove))
131
121
 
132
- if isString(text)
133
- return arrayToBlock(lNewLines)
122
+ if isString(input)
123
+ return toBlock(lNewLines)
134
124
  else
135
125
  return lNewLines
136
126
 
@@ -19,7 +19,9 @@ import {
19
19
 
20
20
  import {
21
21
  arrayToBlock,
22
- blockToArray
22
+ blockToArray,
23
+ toArray,
24
+ toBlock
23
25
  } from '@jdeighan/coffee-utils/block';
24
26
 
25
27
  // ---------------------------------------------------------------------------
@@ -87,11 +89,7 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
87
89
  return input;
88
90
  }
89
91
  toAdd = indentation(level, oneIndent);
90
- if (isArray(input)) {
91
- lInputLines = input;
92
- } else {
93
- lInputLines = blockToArray(input);
94
- }
92
+ lInputLines = toArray(input);
95
93
  lLines = (function() {
96
94
  var i, len1, results;
97
95
  results = [];
@@ -113,31 +111,18 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
113
111
  // - unless level is set, in which case exactly that
114
112
  // indentation is removed
115
113
  // - returns same type as text, i.e. either string or array
116
- export var undented = function(text, level = undef, oneIndent = "\t") {
117
- var i, j, lLines, lMatches, lNewLines, len1, len2, line, nToRemove, toRemove;
114
+ export var undented = function(input, level = undef, oneIndent = "\t") {
115
+ var i, lLines, lMatches, lNewLines, len1, line, nToRemove, toRemove;
118
116
  if (defined(level) && (level === 0)) {
119
- return text;
117
+ return input;
120
118
  }
121
- if (isString(text)) {
122
- lLines = blockToArray(text);
123
- if (lLines.length === 0) {
124
- return '';
125
- }
126
- } else if (isArray(text)) {
127
- lLines = text;
128
- for (i = 0, len1 = lLines.length; i < len1; i++) {
129
- line = lLines[i];
130
- assert(isString(line), "undented(): array not all strings");
131
- }
132
- if (lLines.length === 0) {
133
- return [];
134
- }
135
- } else {
136
- error(`undented(): Not an array or string: ${OL(text)}`);
119
+ lLines = toArray(input);
120
+ if (lLines.length === 0) {
121
+ return '';
137
122
  }
138
123
  // --- determine what to remove from beginning of each line
139
124
  if (defined(level)) {
140
- assert(isInteger(level), "undented(): level must be an integer");
125
+ assert(isInteger(level), "level must be an integer");
141
126
  toRemove = indentation(level, oneIndent);
142
127
  } else {
143
128
  lMatches = lLines[0].match(/^\s*/);
@@ -145,8 +130,8 @@ export var undented = function(text, level = undef, oneIndent = "\t") {
145
130
  }
146
131
  nToRemove = indentLevel(toRemove);
147
132
  lNewLines = [];
148
- for (j = 0, len2 = lLines.length; j < len2; j++) {
149
- line = lLines[j];
133
+ for (i = 0, len1 = lLines.length; i < len1; i++) {
134
+ line = lLines[i];
150
135
  if (isEmpty(line)) {
151
136
  lNewLines.push('');
152
137
  } else {
@@ -156,8 +141,8 @@ export var undented = function(text, level = undef, oneIndent = "\t") {
156
141
  lNewLines.push(line.substr(nToRemove));
157
142
  }
158
143
  }
159
- if (isString(text)) {
160
- return arrayToBlock(lNewLines);
144
+ if (isString(input)) {
145
+ return toBlock(lNewLines);
161
146
  } else {
162
147
  return lNewLines;
163
148
  }