@jdeighan/coffee-utils 15.0.1 → 16.0.0

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": "15.0.1",
4
+ "version": "16.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
package/src/indent.coffee CHANGED
@@ -146,27 +146,22 @@ export indented = (input, level=1, oneIndent="\t") =>
146
146
  # ---------------------------------------------------------------------------
147
147
  # undented - string with 1st line indentation removed for each line
148
148
  # - ignore leading empty lines
149
- # - unless level is set, in which case exactly that
150
- # indentation is removed
151
149
  # - returns same type as text, i.e. either string or array
152
150
 
153
- export undented = (input, level=undef, oneIndent="\t") =>
151
+ export undented = (input) =>
154
152
 
155
- if defined(level) && (level==0)
156
- return input
157
-
158
- # --- Remove any leading blank lines, set lLines
153
+ # --- If a string, convert to an array
159
154
  if isString(input)
160
- if lMatches = input.match(///^ [\r\n]+ (.*) $///s)
161
- input = lMatches[1]
162
155
  lLines = toArray(input)
163
156
  else if isArray(input)
164
157
  lLines = input
165
- while (lLines.length > 0) && isEmpty(lLines[0])
166
- lLines.shift()
167
158
  else
168
159
  croak "input not a string or array"
169
160
 
161
+ # --- Remove leading blank lines
162
+ while (lLines.length > 0) && isEmpty(lLines[0])
163
+ lLines.shift() # remove
164
+
170
165
  if (lLines.length == 0)
171
166
  if isString(input)
172
167
  return ''
@@ -174,27 +169,23 @@ export undented = (input, level=undef, oneIndent="\t") =>
174
169
  return []
175
170
 
176
171
  # --- determine what to remove from beginning of each line
177
- if defined(level)
178
- assert isInteger(level), "level must be an integer"
179
- toRemove = indentation(level, oneIndent)
180
- else
181
- lMatches = lLines[0].match(/^\s*/)
182
- toRemove = lMatches[0]
183
- nToRemove = indentLevel(toRemove)
184
-
185
- lNewLines = []
186
- for line in lLines
187
- if isEmpty(line)
188
- lNewLines.push('')
189
- else
190
- if (line.indexOf(toRemove) != 0)
191
- throw new Error("remove #{OL(toRemove)} from #{OL(line)}")
192
- lNewLines.push(line.substr(nToRemove))
172
+ lMatches = lLines[0].match(/^\s*/)
173
+ toRemove = lMatches[0]
174
+ nToRemove = toRemove.length
175
+ if (nToRemove > 0)
176
+ lLines = lLines.map( (line) =>
177
+ if isEmpty(line)
178
+ return ''
179
+ else
180
+ assert (line.indexOf(toRemove)==0),
181
+ "can't remove #{OL(toRemove)} from #{OL(line)}"
182
+ return line.substr(nToRemove)
183
+ )
193
184
 
194
185
  if isString(input)
195
- return toBlock(lNewLines)
186
+ return toBlock(lLines)
196
187
  else
197
- return lNewLines
188
+ return lLines
198
189
 
199
190
  # ---------------------------------------------------------------------------
200
191
  # enclose - indent text, surround with pre and post
package/src/indent.js CHANGED
@@ -166,28 +166,21 @@ export var indented = (input, level = 1, oneIndent = "\t") => {
166
166
  // ---------------------------------------------------------------------------
167
167
  // undented - string with 1st line indentation removed for each line
168
168
  // - ignore leading empty lines
169
- // - unless level is set, in which case exactly that
170
- // indentation is removed
171
169
  // - returns same type as text, i.e. either string or array
172
- export var undented = (input, level = undef, oneIndent = "\t") => {
173
- var i, lLines, lMatches, lNewLines, len, line, nToRemove, toRemove;
174
- if (defined(level) && (level === 0)) {
175
- return input;
176
- }
177
- // --- Remove any leading blank lines, set lLines
170
+ export var undented = (input) => {
171
+ var lLines, lMatches, nToRemove, toRemove;
172
+ // --- If a string, convert to an array
178
173
  if (isString(input)) {
179
- if (lMatches = input.match(/^[\r\n]+(.*)$/s)) {
180
- input = lMatches[1];
181
- }
182
174
  lLines = toArray(input);
183
175
  } else if (isArray(input)) {
184
176
  lLines = input;
185
- while ((lLines.length > 0) && isEmpty(lLines[0])) {
186
- lLines.shift();
187
- }
188
177
  } else {
189
178
  croak("input not a string or array");
190
179
  }
180
+ // --- Remove leading blank lines
181
+ while ((lLines.length > 0) && isEmpty(lLines[0])) {
182
+ lLines.shift(); // remove
183
+ }
191
184
  if (lLines.length === 0) {
192
185
  if (isString(input)) {
193
186
  return '';
@@ -196,30 +189,23 @@ export var undented = (input, level = undef, oneIndent = "\t") => {
196
189
  }
197
190
  }
198
191
  // --- determine what to remove from beginning of each line
199
- if (defined(level)) {
200
- assert(isInteger(level), "level must be an integer");
201
- toRemove = indentation(level, oneIndent);
202
- } else {
203
- lMatches = lLines[0].match(/^\s*/);
204
- toRemove = lMatches[0];
205
- }
206
- nToRemove = indentLevel(toRemove);
207
- lNewLines = [];
208
- for (i = 0, len = lLines.length; i < len; i++) {
209
- line = lLines[i];
210
- if (isEmpty(line)) {
211
- lNewLines.push('');
212
- } else {
213
- if (line.indexOf(toRemove) !== 0) {
214
- throw new Error(`remove ${OL(toRemove)} from ${OL(line)}`);
192
+ lMatches = lLines[0].match(/^\s*/);
193
+ toRemove = lMatches[0];
194
+ nToRemove = toRemove.length;
195
+ if (nToRemove > 0) {
196
+ lLines = lLines.map((line) => {
197
+ if (isEmpty(line)) {
198
+ return '';
199
+ } else {
200
+ assert(line.indexOf(toRemove) === 0, `can't remove ${OL(toRemove)} from ${OL(line)}`);
201
+ return line.substr(nToRemove);
215
202
  }
216
- lNewLines.push(line.substr(nToRemove));
217
- }
203
+ });
218
204
  }
219
205
  if (isString(input)) {
220
- return toBlock(lNewLines);
206
+ return toBlock(lLines);
221
207
  } else {
222
- return lNewLines;
208
+ return lLines;
223
209
  }
224
210
  };
225
211