@jdeighan/coffee-utils 15.0.1 → 16.0.0
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 +1 -1
- package/src/indent.coffee +20 -29
- package/src/indent.js +20 -34
package/package.json
CHANGED
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
|
151
|
+
export undented = (input) =>
|
154
152
|
|
155
|
-
|
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
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
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(
|
186
|
+
return toBlock(lLines)
|
196
187
|
else
|
197
|
-
return
|
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
|
173
|
-
var
|
174
|
-
|
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
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
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
|
-
|
217
|
-
}
|
203
|
+
});
|
218
204
|
}
|
219
205
|
if (isString(input)) {
|
220
|
-
return toBlock(
|
206
|
+
return toBlock(lLines);
|
221
207
|
} else {
|
222
|
-
return
|
208
|
+
return lLines;
|
223
209
|
}
|
224
210
|
};
|
225
211
|
|