@jdeighan/coffee-utils 14.0.15 → 14.0.17
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +3 -3
- package/src/fs.coffee +29 -8
- package/src/fs.js +31 -6
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "14.0.
|
4
|
+
"version": "14.0.17",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -49,13 +49,13 @@
|
|
49
49
|
},
|
50
50
|
"homepage": "https://github.com/johndeighan/coffee-utils#readme",
|
51
51
|
"dependencies": {
|
52
|
-
"@jdeighan/base-utils": "^4.0.
|
52
|
+
"@jdeighan/base-utils": "^4.0.11",
|
53
53
|
"cross-env": "^7.0.3",
|
54
54
|
"n-readlines": "^1.0.1",
|
55
55
|
"readline-sync": "^1.4.10",
|
56
56
|
"svelte": "^3.55.1"
|
57
57
|
},
|
58
58
|
"devDependencies": {
|
59
|
-
"@jdeighan/unit-tester": "^3.0.
|
59
|
+
"@jdeighan/unit-tester": "^3.0.29"
|
60
60
|
}
|
61
61
|
}
|
package/src/fs.coffee
CHANGED
@@ -8,7 +8,7 @@ import NReadLines from 'n-readlines'
|
|
8
8
|
|
9
9
|
import {
|
10
10
|
undef, pass, defined, notdefined, rtrim, isEmpty, nonEmpty,
|
11
|
-
isString, isArray, isHash, isRegExp, isFunction,
|
11
|
+
isString, isArray, isHash, isRegExp, isFunction, isBoolean,
|
12
12
|
OL, toBlock, getOptions,
|
13
13
|
} from '@jdeighan/base-utils'
|
14
14
|
import {assert, croak} from '@jdeighan/base-utils/exceptions'
|
@@ -131,7 +131,7 @@ export getFullPath = (filepath) =>
|
|
131
131
|
|
132
132
|
# ---------------------------------------------------------------------------
|
133
133
|
|
134
|
-
export
|
134
|
+
export forEachLineInFile = (filepath, func) =>
|
135
135
|
|
136
136
|
reader = new NReadLines(filepath)
|
137
137
|
nLines = 0
|
@@ -141,12 +141,33 @@ export forEachLine = (filepath, func) =>
|
|
141
141
|
# --- text is split on \n chars,
|
142
142
|
# we also need to remove \r chars
|
143
143
|
line = buffer.toString().replace(/\r/g, '')
|
144
|
-
|
144
|
+
result = func(line, nLines)
|
145
|
+
assert isBoolean(result)
|
146
|
+
if result
|
145
147
|
reader.close() # allow premature termination
|
148
|
+
return
|
146
149
|
return
|
147
150
|
|
148
151
|
# ---------------------------------------------------------------------------
|
149
152
|
|
153
|
+
export mapEachLineInFile = (filepath, func) =>
|
154
|
+
|
155
|
+
reader = new NReadLines(filepath)
|
156
|
+
nLines = 0
|
157
|
+
|
158
|
+
lLines = []
|
159
|
+
while (buffer = reader.next())
|
160
|
+
nLines += 1
|
161
|
+
# --- text is split on \n chars,
|
162
|
+
# we also need to remove \r chars
|
163
|
+
line = buffer.toString().replace(/\r/g, '')
|
164
|
+
result = func(line, nLines)
|
165
|
+
if defined(result)
|
166
|
+
lLines.push result
|
167
|
+
return lLines
|
168
|
+
|
169
|
+
# ---------------------------------------------------------------------------
|
170
|
+
|
150
171
|
export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
|
151
172
|
|
152
173
|
lLines = []
|
@@ -165,9 +186,9 @@ export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
|
|
165
186
|
firstLineNum = lineNum+1
|
166
187
|
else
|
167
188
|
lLines.push line
|
168
|
-
return
|
189
|
+
return false
|
169
190
|
|
170
|
-
|
191
|
+
forEachLineInFile filepath, callback
|
171
192
|
if ! earlyExit
|
172
193
|
func(lLines.join('\n'), firstLineNum)
|
173
194
|
return
|
@@ -200,9 +221,9 @@ export forEachSetOfBlocks = (filepath, func,
|
|
200
221
|
lLines = []
|
201
222
|
else
|
202
223
|
lLines.push line
|
203
|
-
return
|
224
|
+
return false
|
204
225
|
|
205
|
-
|
226
|
+
forEachLineInFile filepath, callback
|
206
227
|
if ! earlyExit
|
207
228
|
lBlocks.push(lLines.join('\n'))
|
208
229
|
func(lBlocks, firstLineNum)
|
@@ -215,7 +236,7 @@ export slurp = (filepath, maxLines=undef) =>
|
|
215
236
|
|
216
237
|
if defined(maxLines)
|
217
238
|
lLines = []
|
218
|
-
|
239
|
+
forEachLineInFile filepath, (line, nLines) ->
|
219
240
|
lLines.push line
|
220
241
|
return (nLines >= maxLines)
|
221
242
|
contents = toBlock(lLines)
|
package/src/fs.js
CHANGED
@@ -25,6 +25,7 @@ import {
|
|
25
25
|
isHash,
|
26
26
|
isRegExp,
|
27
27
|
isFunction,
|
28
|
+
isBoolean,
|
28
29
|
OL,
|
29
30
|
toBlock,
|
30
31
|
getOptions
|
@@ -167,8 +168,8 @@ export var getFullPath = (filepath) => {
|
|
167
168
|
};
|
168
169
|
|
169
170
|
// ---------------------------------------------------------------------------
|
170
|
-
export var
|
171
|
-
var buffer, line, nLines, reader;
|
171
|
+
export var forEachLineInFile = (filepath, func) => {
|
172
|
+
var buffer, line, nLines, reader, result;
|
172
173
|
reader = new NReadLines(filepath);
|
173
174
|
nLines = 0;
|
174
175
|
while ((buffer = reader.next())) {
|
@@ -176,12 +177,34 @@ export var forEachLine = (filepath, func) => {
|
|
176
177
|
// --- text is split on \n chars,
|
177
178
|
// we also need to remove \r chars
|
178
179
|
line = buffer.toString().replace(/\r/g, '');
|
179
|
-
|
180
|
+
result = func(line, nLines);
|
181
|
+
assert(isBoolean(result));
|
182
|
+
if (result) {
|
180
183
|
reader.close(); // allow premature termination
|
184
|
+
return;
|
181
185
|
}
|
182
186
|
}
|
183
187
|
};
|
184
188
|
|
189
|
+
// ---------------------------------------------------------------------------
|
190
|
+
export var mapEachLineInFile = (filepath, func) => {
|
191
|
+
var buffer, lLines, line, nLines, reader, result;
|
192
|
+
reader = new NReadLines(filepath);
|
193
|
+
nLines = 0;
|
194
|
+
lLines = [];
|
195
|
+
while ((buffer = reader.next())) {
|
196
|
+
nLines += 1;
|
197
|
+
// --- text is split on \n chars,
|
198
|
+
// we also need to remove \r chars
|
199
|
+
line = buffer.toString().replace(/\r/g, '');
|
200
|
+
result = func(line, nLines);
|
201
|
+
if (defined(result)) {
|
202
|
+
lLines.push(result);
|
203
|
+
}
|
204
|
+
}
|
205
|
+
return lLines;
|
206
|
+
};
|
207
|
+
|
185
208
|
// ---------------------------------------------------------------------------
|
186
209
|
export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
|
187
210
|
var callback, earlyExit, firstLineNum, lLines;
|
@@ -204,8 +227,9 @@ export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
|
|
204
227
|
} else {
|
205
228
|
lLines.push(line);
|
206
229
|
}
|
230
|
+
return false;
|
207
231
|
};
|
208
|
-
|
232
|
+
forEachLineInFile(filepath, callback);
|
209
233
|
if (!earlyExit) {
|
210
234
|
func(lLines.join('\n'), firstLineNum);
|
211
235
|
}
|
@@ -239,8 +263,9 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
|
|
239
263
|
} else {
|
240
264
|
lLines.push(line);
|
241
265
|
}
|
266
|
+
return false;
|
242
267
|
};
|
243
|
-
|
268
|
+
forEachLineInFile(filepath, callback);
|
244
269
|
if (!earlyExit) {
|
245
270
|
lBlocks.push(lLines.join('\n'));
|
246
271
|
func(lBlocks, firstLineNum);
|
@@ -253,7 +278,7 @@ export var slurp = (filepath, maxLines = undef) => {
|
|
253
278
|
var contents, lLines;
|
254
279
|
if (defined(maxLines)) {
|
255
280
|
lLines = [];
|
256
|
-
|
281
|
+
forEachLineInFile(filepath, function(line, nLines) {
|
257
282
|
lLines.push(line);
|
258
283
|
return nLines >= maxLines;
|
259
284
|
});
|