@jdeighan/coffee-utils 11.0.48 → 12.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +4 -4
- package/src/block.coffee +0 -88
- package/src/block.js +0 -90
- package/src/fs.coffee +72 -27
- package/src/fs.js +75 -33
- package/temp.txt +1 -0
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "
|
4
|
+
"version": "12.0.1",
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
6
6
|
"main": "coffee_utils.js",
|
7
7
|
"exports": {
|
@@ -48,14 +48,14 @@
|
|
48
48
|
},
|
49
49
|
"homepage": "https://github.com/johndeighan/coffee-utils#readme",
|
50
50
|
"dependencies": {
|
51
|
-
"@jdeighan/base-utils": "^1.0.
|
51
|
+
"@jdeighan/base-utils": "^1.0.10",
|
52
52
|
"cross-env": "^7.0.3",
|
53
53
|
"js-yaml": "^4.1.0",
|
54
54
|
"n-readlines": "^1.0.1",
|
55
55
|
"readline-sync": "^1.4.10",
|
56
|
-
"svelte": "^3.53.
|
56
|
+
"svelte": "^3.53.1"
|
57
57
|
},
|
58
58
|
"devDependencies": {
|
59
|
-
"@jdeighan/unit-tester": "^2.0.
|
59
|
+
"@jdeighan/unit-tester": "^2.0.69"
|
60
60
|
}
|
61
61
|
}
|
package/src/block.coffee
CHANGED
@@ -138,91 +138,3 @@ export joinBlocks = (lBlocks...) ->
|
|
138
138
|
if nonEmpty(block)
|
139
139
|
lNonEmptyBlocks.push block
|
140
140
|
return lNonEmptyBlocks.join('\n')
|
141
|
-
|
142
|
-
# ---------------------------------------------------------------------------
|
143
|
-
|
144
|
-
```
|
145
|
-
export async function forEachLine(filepath, func) {
|
146
|
-
|
147
|
-
const fileStream = fs.createReadStream(filepath);
|
148
|
-
const rl = readline.createInterface({
|
149
|
-
input: fileStream,
|
150
|
-
crlfDelay: Infinity
|
151
|
-
});
|
152
|
-
|
153
|
-
// Note: we use the crlfDelay option to recognize all instances of CR LF
|
154
|
-
// ('\r\n') in input.txt as a single line break.
|
155
|
-
|
156
|
-
var lineNum = 0
|
157
|
-
for await (const line of rl) {
|
158
|
-
lineNum += 1
|
159
|
-
// Each line will be successively available here as 'line'
|
160
|
-
if (func(line, lineNum)) {
|
161
|
-
rl.close(); // close if true return value
|
162
|
-
return;
|
163
|
-
}
|
164
|
-
}
|
165
|
-
} // forEachLine()
|
166
|
-
```
|
167
|
-
# ---------------------------------------------------------------------------
|
168
|
-
|
169
|
-
export forEachBlock = (filepath, func, regexp = /^-{16,}$/) ->
|
170
|
-
|
171
|
-
lLines = []
|
172
|
-
firstLineNum = 1
|
173
|
-
earlyExit = false
|
174
|
-
|
175
|
-
callback = (line, lineNum) ->
|
176
|
-
if (line.match(regexp))
|
177
|
-
if result = func(lLines.join('\n'), firstLineNum, line)
|
178
|
-
if (result == true)
|
179
|
-
earlyExit = true
|
180
|
-
return true
|
181
|
-
else if result?
|
182
|
-
croak "forEachBlock() - callback returned '#{result}'"
|
183
|
-
lLines = []
|
184
|
-
firstLineNum = lineNum+1
|
185
|
-
else
|
186
|
-
lLines.push line
|
187
|
-
return
|
188
|
-
|
189
|
-
await forEachLine filepath, callback
|
190
|
-
if ! earlyExit
|
191
|
-
func(lLines.join('\n'), firstLineNum)
|
192
|
-
return
|
193
|
-
|
194
|
-
# ---------------------------------------------------------------------------
|
195
|
-
|
196
|
-
export forEachSetOfBlocks = (filepath, func,
|
197
|
-
block_regexp = /^-{16,}$/,
|
198
|
-
set_regexp = /^={16,}$/) ->
|
199
|
-
|
200
|
-
lBlocks = []
|
201
|
-
lLines = []
|
202
|
-
firstLineNum = 1
|
203
|
-
earlyExit = false
|
204
|
-
|
205
|
-
callback = (line, lineNum) ->
|
206
|
-
if (line.match(set_regexp))
|
207
|
-
lBlocks.push(lLines.join('\n'))
|
208
|
-
lLines = []
|
209
|
-
if result = func(lBlocks, firstLineNum, line)
|
210
|
-
if (result == true)
|
211
|
-
earlyExit = true
|
212
|
-
return true
|
213
|
-
else if result?
|
214
|
-
croak "forEachSetOfBlocks() - callback returned '#{result}'"
|
215
|
-
lBlocks = []
|
216
|
-
firstLineNum = lineNum+1
|
217
|
-
else if (line.match(block_regexp))
|
218
|
-
lBlocks.push(lLines.join('\n'))
|
219
|
-
lLines = []
|
220
|
-
else
|
221
|
-
lLines.push line
|
222
|
-
return
|
223
|
-
|
224
|
-
await forEachLine filepath, callback
|
225
|
-
if ! earlyExit
|
226
|
-
lBlocks.push(lLines.join('\n'))
|
227
|
-
func(lBlocks, firstLineNum)
|
228
|
-
return
|
package/src/block.js
CHANGED
@@ -185,93 +185,3 @@ export var joinBlocks = function(...lBlocks) {
|
|
185
185
|
}
|
186
186
|
return lNonEmptyBlocks.join('\n');
|
187
187
|
};
|
188
|
-
|
189
|
-
|
190
|
-
export async function forEachLine(filepath, func) {
|
191
|
-
|
192
|
-
const fileStream = fs.createReadStream(filepath);
|
193
|
-
const rl = readline.createInterface({
|
194
|
-
input: fileStream,
|
195
|
-
crlfDelay: Infinity
|
196
|
-
});
|
197
|
-
|
198
|
-
// Note: we use the crlfDelay option to recognize all instances of CR LF
|
199
|
-
// ('\r\n') in input.txt as a single line break.
|
200
|
-
|
201
|
-
var lineNum = 0
|
202
|
-
for await (const line of rl) {
|
203
|
-
lineNum += 1
|
204
|
-
// Each line will be successively available here as 'line'
|
205
|
-
if (func(line, lineNum)) {
|
206
|
-
rl.close(); // close if true return value
|
207
|
-
return;
|
208
|
-
}
|
209
|
-
}
|
210
|
-
} // forEachLine()
|
211
|
-
// ---------------------------------------------------------------------------
|
212
|
-
;
|
213
|
-
|
214
|
-
// ---------------------------------------------------------------------------
|
215
|
-
export var forEachBlock = async function(filepath, func, regexp = /^-{16,}$/) {
|
216
|
-
var callback, earlyExit, firstLineNum, lLines;
|
217
|
-
lLines = [];
|
218
|
-
firstLineNum = 1;
|
219
|
-
earlyExit = false;
|
220
|
-
callback = function(line, lineNum) {
|
221
|
-
var result;
|
222
|
-
if (line.match(regexp)) {
|
223
|
-
if (result = func(lLines.join('\n'), firstLineNum, line)) {
|
224
|
-
if (result === true) {
|
225
|
-
earlyExit = true;
|
226
|
-
return true;
|
227
|
-
} else if (result != null) {
|
228
|
-
croak(`forEachBlock() - callback returned '${result}'`);
|
229
|
-
}
|
230
|
-
}
|
231
|
-
lLines = [];
|
232
|
-
firstLineNum = lineNum + 1;
|
233
|
-
} else {
|
234
|
-
lLines.push(line);
|
235
|
-
}
|
236
|
-
};
|
237
|
-
await forEachLine(filepath, callback);
|
238
|
-
if (!earlyExit) {
|
239
|
-
func(lLines.join('\n'), firstLineNum);
|
240
|
-
}
|
241
|
-
};
|
242
|
-
|
243
|
-
// ---------------------------------------------------------------------------
|
244
|
-
export var forEachSetOfBlocks = async function(filepath, func, block_regexp = /^-{16,}$/, set_regexp = /^={16,}$/) {
|
245
|
-
var callback, earlyExit, firstLineNum, lBlocks, lLines;
|
246
|
-
lBlocks = [];
|
247
|
-
lLines = [];
|
248
|
-
firstLineNum = 1;
|
249
|
-
earlyExit = false;
|
250
|
-
callback = function(line, lineNum) {
|
251
|
-
var result;
|
252
|
-
if (line.match(set_regexp)) {
|
253
|
-
lBlocks.push(lLines.join('\n'));
|
254
|
-
lLines = [];
|
255
|
-
if (result = func(lBlocks, firstLineNum, line)) {
|
256
|
-
if (result === true) {
|
257
|
-
earlyExit = true;
|
258
|
-
return true;
|
259
|
-
} else if (result != null) {
|
260
|
-
croak(`forEachSetOfBlocks() - callback returned '${result}'`);
|
261
|
-
}
|
262
|
-
}
|
263
|
-
lBlocks = [];
|
264
|
-
firstLineNum = lineNum + 1;
|
265
|
-
} else if (line.match(block_regexp)) {
|
266
|
-
lBlocks.push(lLines.join('\n'));
|
267
|
-
lLines = [];
|
268
|
-
} else {
|
269
|
-
lLines.push(line);
|
270
|
-
}
|
271
|
-
};
|
272
|
-
await forEachLine(filepath, callback);
|
273
|
-
if (!earlyExit) {
|
274
|
-
lBlocks.push(lLines.join('\n'));
|
275
|
-
func(lBlocks, firstLineNum);
|
276
|
-
}
|
277
|
-
};
|
package/src/fs.coffee
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
import pathlib from 'path'
|
4
4
|
import urllib from 'url'
|
5
5
|
import fs from 'fs'
|
6
|
+
import readline from 'readline'
|
6
7
|
import NReadLines from 'n-readlines'
|
7
8
|
|
8
9
|
import {assert, croak, LOG, fromTAML} from '@jdeighan/base-utils'
|
@@ -128,30 +129,94 @@ export getFullPath = (filepath) ->
|
|
128
129
|
|
129
130
|
# ---------------------------------------------------------------------------
|
130
131
|
|
131
|
-
export
|
132
|
+
export forEachLine = (filepath, func) ->
|
132
133
|
|
133
134
|
reader = new NReadLines(filepath)
|
134
135
|
nLines = 0
|
135
136
|
|
136
137
|
while (buffer = reader.next())
|
137
138
|
nLines += 1
|
138
|
-
# --- text is split on \n chars,
|
139
|
+
# --- text is split on \n chars,
|
140
|
+
# we also need to remove \r chars
|
139
141
|
line = buffer.toString().replace(/\r/g, '')
|
140
|
-
if func(line, nLines)
|
142
|
+
if func(line, nLines)
|
141
143
|
reader.close() # allow premature termination
|
142
144
|
return
|
143
145
|
|
146
|
+
# ---------------------------------------------------------------------------
|
147
|
+
|
148
|
+
export forEachBlock = (filepath, func, regexp = /^-{16,}$/) ->
|
149
|
+
|
150
|
+
lLines = []
|
151
|
+
firstLineNum = 1
|
152
|
+
earlyExit = false
|
153
|
+
|
154
|
+
callback = (line, lineNum) ->
|
155
|
+
if (line.match(regexp))
|
156
|
+
if result = func(lLines.join('\n'), firstLineNum, line)
|
157
|
+
if (result == true)
|
158
|
+
earlyExit = true
|
159
|
+
return true
|
160
|
+
else if defined(result)
|
161
|
+
croak "forEachBlock() - callback returned '#{result}'"
|
162
|
+
lLines = []
|
163
|
+
firstLineNum = lineNum+1
|
164
|
+
else
|
165
|
+
lLines.push line
|
166
|
+
return
|
167
|
+
|
168
|
+
forEachLine filepath, callback
|
169
|
+
if ! earlyExit
|
170
|
+
func(lLines.join('\n'), firstLineNum)
|
171
|
+
return
|
172
|
+
|
173
|
+
# ---------------------------------------------------------------------------
|
174
|
+
|
175
|
+
export forEachSetOfBlocks = (filepath, func,
|
176
|
+
block_regexp = /^-{16,}$/,
|
177
|
+
set_regexp = /^={16,}$/) ->
|
178
|
+
|
179
|
+
lBlocks = []
|
180
|
+
lLines = []
|
181
|
+
firstLineNum = 1
|
182
|
+
earlyExit = false
|
183
|
+
|
184
|
+
callback = (line, lineNum) ->
|
185
|
+
if (line.match(set_regexp))
|
186
|
+
lBlocks.push(lLines.join('\n'))
|
187
|
+
lLines = []
|
188
|
+
if result = func(lBlocks, firstLineNum, line)
|
189
|
+
if (result == true)
|
190
|
+
earlyExit = true
|
191
|
+
return true
|
192
|
+
else if result?
|
193
|
+
croak "forEachSetOfBlocks() - callback returned '#{result}'"
|
194
|
+
lBlocks = []
|
195
|
+
firstLineNum = lineNum+1
|
196
|
+
else if (line.match(block_regexp))
|
197
|
+
lBlocks.push(lLines.join('\n'))
|
198
|
+
lLines = []
|
199
|
+
else
|
200
|
+
lLines.push line
|
201
|
+
return
|
202
|
+
|
203
|
+
forEachLine filepath, callback
|
204
|
+
if ! earlyExit
|
205
|
+
lBlocks.push(lLines.join('\n'))
|
206
|
+
func(lBlocks, firstLineNum)
|
207
|
+
return
|
208
|
+
|
144
209
|
# ---------------------------------------------------------------------------
|
145
210
|
# slurp - read an entire file into a string
|
146
211
|
|
147
212
|
export slurp = (filepath, maxLines=undef) ->
|
148
213
|
|
149
|
-
if maxLines
|
214
|
+
if defined(maxLines)
|
150
215
|
lLines = []
|
151
|
-
|
216
|
+
forEachLine filepath, (line, nLines) ->
|
152
217
|
lLines.push line
|
153
|
-
return
|
154
|
-
contents = lLines
|
218
|
+
return (nLines >= maxLines)
|
219
|
+
contents = arrayToBlock(lLines)
|
155
220
|
else
|
156
221
|
filepath = filepath.replace(/\//g, "\\")
|
157
222
|
contents = fs.readFileSync(filepath, 'utf8').toString()
|
@@ -418,26 +483,6 @@ export parseSource = (source) ->
|
|
418
483
|
dbgReturn "parseSource", hSourceInfo
|
419
484
|
return hSourceInfo
|
420
485
|
|
421
|
-
# ---------------------------------------------------------------------------
|
422
|
-
# backup - back up a file
|
423
|
-
|
424
|
-
# --- If report is true, missing source files are not an error
|
425
|
-
# but both missing source files and successful copies
|
426
|
-
# are reported via LOG
|
427
|
-
|
428
|
-
export backup = (file, from, to, report=false) ->
|
429
|
-
src = mkpath(from, file)
|
430
|
-
dest = mkpath(to, file)
|
431
|
-
|
432
|
-
if report
|
433
|
-
if fs.existsSync(src)
|
434
|
-
LOG "OK #{file}"
|
435
|
-
fs.copyFileSync(src, dest)
|
436
|
-
else
|
437
|
-
LOG "MISSING #{src}"
|
438
|
-
else
|
439
|
-
fs.copyFileSync(src, dest)
|
440
|
-
|
441
486
|
# ---------------------------------------------------------------------------
|
442
487
|
# slurpTAML - read TAML from a file
|
443
488
|
|
package/src/fs.js
CHANGED
@@ -8,6 +8,8 @@ import urllib from 'url';
|
|
8
8
|
|
9
9
|
import fs from 'fs';
|
10
10
|
|
11
|
+
import readline from 'readline';
|
12
|
+
|
11
13
|
import NReadLines from 'n-readlines';
|
12
14
|
|
13
15
|
import {
|
@@ -160,35 +162,97 @@ export var getFullPath = function(filepath) {
|
|
160
162
|
};
|
161
163
|
|
162
164
|
// ---------------------------------------------------------------------------
|
163
|
-
export var
|
165
|
+
export var forEachLine = function(filepath, func) {
|
164
166
|
var buffer, line, nLines, reader;
|
165
167
|
reader = new NReadLines(filepath);
|
166
168
|
nLines = 0;
|
167
169
|
while ((buffer = reader.next())) {
|
168
170
|
nLines += 1;
|
169
|
-
// --- text is split on \n chars,
|
171
|
+
// --- text is split on \n chars,
|
172
|
+
// we also need to remove \r chars
|
170
173
|
line = buffer.toString().replace(/\r/g, '');
|
171
|
-
if (func(line, nLines)
|
174
|
+
if (func(line, nLines)) {
|
172
175
|
reader.close(); // allow premature termination
|
173
176
|
}
|
174
177
|
}
|
175
178
|
};
|
176
179
|
|
180
|
+
// ---------------------------------------------------------------------------
|
181
|
+
export var forEachBlock = function(filepath, func, regexp = /^-{16,}$/) {
|
182
|
+
var callback, earlyExit, firstLineNum, lLines;
|
183
|
+
lLines = [];
|
184
|
+
firstLineNum = 1;
|
185
|
+
earlyExit = false;
|
186
|
+
callback = function(line, lineNum) {
|
187
|
+
var result;
|
188
|
+
if (line.match(regexp)) {
|
189
|
+
if (result = func(lLines.join('\n'), firstLineNum, line)) {
|
190
|
+
if (result === true) {
|
191
|
+
earlyExit = true;
|
192
|
+
return true;
|
193
|
+
} else if (defined(result)) {
|
194
|
+
croak(`forEachBlock() - callback returned '${result}'`);
|
195
|
+
}
|
196
|
+
}
|
197
|
+
lLines = [];
|
198
|
+
firstLineNum = lineNum + 1;
|
199
|
+
} else {
|
200
|
+
lLines.push(line);
|
201
|
+
}
|
202
|
+
};
|
203
|
+
forEachLine(filepath, callback);
|
204
|
+
if (!earlyExit) {
|
205
|
+
func(lLines.join('\n'), firstLineNum);
|
206
|
+
}
|
207
|
+
};
|
208
|
+
|
209
|
+
// ---------------------------------------------------------------------------
|
210
|
+
export var forEachSetOfBlocks = function(filepath, func, block_regexp = /^-{16,}$/, set_regexp = /^={16,}$/) {
|
211
|
+
var callback, earlyExit, firstLineNum, lBlocks, lLines;
|
212
|
+
lBlocks = [];
|
213
|
+
lLines = [];
|
214
|
+
firstLineNum = 1;
|
215
|
+
earlyExit = false;
|
216
|
+
callback = function(line, lineNum) {
|
217
|
+
var result;
|
218
|
+
if (line.match(set_regexp)) {
|
219
|
+
lBlocks.push(lLines.join('\n'));
|
220
|
+
lLines = [];
|
221
|
+
if (result = func(lBlocks, firstLineNum, line)) {
|
222
|
+
if (result === true) {
|
223
|
+
earlyExit = true;
|
224
|
+
return true;
|
225
|
+
} else if (result != null) {
|
226
|
+
croak(`forEachSetOfBlocks() - callback returned '${result}'`);
|
227
|
+
}
|
228
|
+
}
|
229
|
+
lBlocks = [];
|
230
|
+
firstLineNum = lineNum + 1;
|
231
|
+
} else if (line.match(block_regexp)) {
|
232
|
+
lBlocks.push(lLines.join('\n'));
|
233
|
+
lLines = [];
|
234
|
+
} else {
|
235
|
+
lLines.push(line);
|
236
|
+
}
|
237
|
+
};
|
238
|
+
forEachLine(filepath, callback);
|
239
|
+
if (!earlyExit) {
|
240
|
+
lBlocks.push(lLines.join('\n'));
|
241
|
+
func(lBlocks, firstLineNum);
|
242
|
+
}
|
243
|
+
};
|
244
|
+
|
177
245
|
// ---------------------------------------------------------------------------
|
178
246
|
// slurp - read an entire file into a string
|
179
247
|
export var slurp = function(filepath, maxLines = undef) {
|
180
248
|
var contents, lLines;
|
181
|
-
if (maxLines
|
249
|
+
if (defined(maxLines)) {
|
182
250
|
lLines = [];
|
183
|
-
|
251
|
+
forEachLine(filepath, function(line, nLines) {
|
184
252
|
lLines.push(line);
|
185
|
-
|
186
|
-
return 'EOF';
|
187
|
-
} else {
|
188
|
-
return undef;
|
189
|
-
}
|
253
|
+
return nLines >= maxLines;
|
190
254
|
});
|
191
|
-
contents = lLines
|
255
|
+
contents = arrayToBlock(lLines);
|
192
256
|
} else {
|
193
257
|
filepath = filepath.replace(/\//g, "\\");
|
194
258
|
contents = fs.readFileSync(filepath, 'utf8').toString();
|
@@ -492,28 +556,6 @@ export var parseSource = function(source) {
|
|
492
556
|
return hSourceInfo;
|
493
557
|
};
|
494
558
|
|
495
|
-
// ---------------------------------------------------------------------------
|
496
|
-
// backup - back up a file
|
497
|
-
|
498
|
-
// --- If report is true, missing source files are not an error
|
499
|
-
// but both missing source files and successful copies
|
500
|
-
// are reported via LOG
|
501
|
-
export var backup = function(file, from, to, report = false) {
|
502
|
-
var dest, src;
|
503
|
-
src = mkpath(from, file);
|
504
|
-
dest = mkpath(to, file);
|
505
|
-
if (report) {
|
506
|
-
if (fs.existsSync(src)) {
|
507
|
-
LOG(`OK ${file}`);
|
508
|
-
return fs.copyFileSync(src, dest);
|
509
|
-
} else {
|
510
|
-
return LOG(`MISSING ${src}`);
|
511
|
-
}
|
512
|
-
} else {
|
513
|
-
return fs.copyFileSync(src, dest);
|
514
|
-
}
|
515
|
-
};
|
516
|
-
|
517
559
|
// ---------------------------------------------------------------------------
|
518
560
|
// slurpTAML - read TAML from a file
|
519
561
|
export var slurpTAML = function(filepath) {
|
package/temp.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
this is text
|