@jdeighan/coffee-utils 16.0.13 → 16.0.14

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": "16.0.13",
4
+ "version": "16.0.14",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -45,14 +45,14 @@
45
45
  },
46
46
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
47
47
  "dependencies": {
48
- "@jdeighan/base-utils": "^8.0.0",
48
+ "@jdeighan/base-utils": "^8.0.7",
49
49
  "cross-env": "^7.0.3",
50
50
  "n-readlines": "^1.0.1",
51
51
  "readline-sync": "^1.4.10",
52
- "svelte": "^4.0.5"
52
+ "svelte": "^4.1.1"
53
53
  },
54
54
  "devDependencies": {
55
- "@jdeighan/unit-tester": "^3.0.58",
55
+ "@jdeighan/unit-tester": "^3.0.61",
56
56
  "ava": "^5.3.1"
57
57
  }
58
58
  }
package/src/lib/fs.coffee CHANGED
@@ -8,23 +8,25 @@ import {
8
8
  readFile, writeFile, rm, rmdir, # rmSync, rmdirSync,
9
9
  } from 'node:fs/promises'
10
10
  import {execSync} from 'node:child_process'
11
- import readline from 'readline'
12
- import NReadLines from 'n-readlines'
13
11
 
14
12
  import {
15
13
  undef, pass, defined, notdefined, rtrim, isEmpty, nonEmpty,
16
14
  isString, isArray, isHash, isRegExp, isFunction, isBoolean,
17
- OL, toBlock, getOptions, isArrayOfStrings,
15
+ OL, toBlock, getOptions, isArrayOfStrings, deepCopy,
18
16
  } from '@jdeighan/base-utils'
19
17
  import {
20
- mkpath, isFile, isDir, rmFileSync, mkdirSync,
18
+ mkpath, isFile, isDir, rmFileSync, mkdirSync, forEachLineInFile,
19
+ rmFile, rmDir, rmDirSync,
21
20
  } from '@jdeighan/base-utils/fs'
22
21
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
23
22
  import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
24
23
  import {dbg, dbgEnter, dbgReturn} from '@jdeighan/base-utils/debug'
25
24
  import {fromTAML} from '@jdeighan/base-utils/taml'
26
25
 
27
- export {mkpath, isFile, isDir, rmFileSync, mkdirSync}
26
+ export {
27
+ mkpath, isFile, isDir, rmFileSync, mkdirSync,
28
+ forEachLineInFile, rmDir, rmDirSync, rmFile,
29
+ }
28
30
 
29
31
  fix = true
30
32
 
@@ -35,27 +37,6 @@ export doFixOutput = (flag=true) =>
35
37
  fix = flag
36
38
  return
37
39
 
38
- # ---------------------------------------------------------------------------
39
-
40
- export rmDir = (dirpath) =>
41
-
42
- await rmdir dirpath, {recursive: true}
43
- return
44
-
45
- # ---------------------------------------------------------------------------
46
-
47
- export rmDirSync = (dirpath) =>
48
-
49
- fs.rmdirSync dirpath, {recursive: true}
50
- return
51
-
52
- # ---------------------------------------------------------------------------
53
-
54
- export rmFile = (filepath) =>
55
-
56
- await rm filepath
57
- return
58
-
59
40
  # --------------------------------------------------------------------------
60
41
 
61
42
  export fixOutput = (contents) =>
@@ -212,44 +193,6 @@ export getFullPath = (filepath) =>
212
193
 
213
194
  # ---------------------------------------------------------------------------
214
195
 
215
- export forEachLineInFile = (filepath, func) =>
216
- # --- func gets (line, lineNum, filepath) - lineNum starts at 1
217
-
218
- reader = new NReadLines(filepath)
219
- nLines = 0
220
-
221
- while (buffer = reader.next())
222
- nLines += 1
223
- # --- text is split on \n chars,
224
- # we also need to remove \r chars
225
- line = buffer.toString().replace(/\r/g, '')
226
- result = func(line, nLines, filepath)
227
- assert isBoolean(result)
228
- if result
229
- reader.close() # allow premature termination
230
- return
231
- return
232
-
233
- # ---------------------------------------------------------------------------
234
-
235
- export mapEachLineInFile = (filepath, func) =>
236
-
237
- reader = new NReadLines(filepath)
238
- nLines = 0
239
-
240
- lLines = []
241
- while (buffer = reader.next())
242
- nLines += 1
243
- # --- text is split on \n chars,
244
- # we also need to remove \r chars
245
- line = buffer.toString().replace(/\r/g, '')
246
- result = func(line, nLines)
247
- if defined(result)
248
- lLines.push result
249
- return lLines
250
-
251
- # ---------------------------------------------------------------------------
252
-
253
196
  export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
254
197
 
255
198
  lLines = []
@@ -277,25 +220,28 @@ export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
277
220
 
278
221
  # ---------------------------------------------------------------------------
279
222
 
280
- export forEachSetOfBlocks = (filepath, func,
281
- block_regexp = /^-{16,}$/,
282
- set_regexp = /^={16,}$/) =>
223
+ export forEachSetOfBlocks = (filepath, func, \
224
+ block_regexp = /^-{16,}$/, \
225
+ set_regexp = /^={16,}$/) \
226
+ =>
283
227
 
228
+ dbgEnter 'forEachSetOfBlocks', filepath
284
229
  lBlocks = []
285
230
  lLines = []
286
231
  firstLineNum = 1
287
232
  earlyExit = false
288
233
 
289
- callback = (line, lineNum) ->
234
+ callback = (line, hContext) ->
235
+ dbgEnter 'callback', line, hContext.lineNum
236
+ lineNum = hContext.lineNum
290
237
  if (line.match(set_regexp))
291
238
  lBlocks.push(lLines.join('\n'))
292
239
  lLines = []
293
- if result = func(lBlocks, firstLineNum, line)
294
- if (result == true)
295
- earlyExit = true
296
- return true
297
- else if defined(result)
298
- croak "forEachSetOfBlocks() - callback returned '#{result}'"
240
+ result = func(deepCopy(lBlocks), firstLineNum, line)
241
+ if (result == true)
242
+ earlyExit = true
243
+ dbgReturn 'callback', true
244
+ return true
299
245
  lBlocks = []
300
246
  firstLineNum = lineNum+1
301
247
  else if (line.match(block_regexp))
@@ -303,12 +249,14 @@ export forEachSetOfBlocks = (filepath, func,
303
249
  lLines = []
304
250
  else
305
251
  lLines.push line
252
+ dbgReturn 'callback', false
306
253
  return false
307
254
 
308
255
  forEachLineInFile filepath, callback
309
256
  if ! earlyExit
310
257
  lBlocks.push(lLines.join('\n'))
311
258
  func(lBlocks, firstLineNum)
259
+ dbgReturn 'forEachSetOfBlocks'
312
260
  return
313
261
 
314
262
  # ---------------------------------------------------------------------------
@@ -544,3 +492,5 @@ export parseSource = (source) =>
544
492
  hSourceInfo.purpose = lMatches[1]
545
493
  dbgReturn "parseSource", hSourceInfo
546
494
  return hSourceInfo
495
+
496
+ # ---------------------------------------------------------------------------
package/src/lib/fs.js CHANGED
@@ -21,10 +21,6 @@ import {
21
21
  execSync
22
22
  } from 'node:child_process';
23
23
 
24
- import readline from 'readline';
25
-
26
- import NReadLines from 'n-readlines';
27
-
28
24
  import {
29
25
  undef,
30
26
  pass,
@@ -42,7 +38,8 @@ import {
42
38
  OL,
43
39
  toBlock,
44
40
  getOptions,
45
- isArrayOfStrings
41
+ isArrayOfStrings,
42
+ deepCopy
46
43
  } from '@jdeighan/base-utils';
47
44
 
48
45
  import {
@@ -50,7 +47,11 @@ import {
50
47
  isFile,
51
48
  isDir,
52
49
  rmFileSync,
53
- mkdirSync
50
+ mkdirSync,
51
+ forEachLineInFile,
52
+ rmFile,
53
+ rmDir,
54
+ rmDirSync
54
55
  } from '@jdeighan/base-utils/fs';
55
56
 
56
57
  import {
@@ -78,7 +79,11 @@ export {
78
79
  isFile,
79
80
  isDir,
80
81
  rmFileSync,
81
- mkdirSync
82
+ mkdirSync,
83
+ forEachLineInFile,
84
+ rmDir,
85
+ rmDirSync,
86
+ rmFile
82
87
  };
83
88
 
84
89
  fix = true;
@@ -88,25 +93,6 @@ export var doFixOutput = (flag = true) => {
88
93
  fix = flag;
89
94
  };
90
95
 
91
- // ---------------------------------------------------------------------------
92
- export var rmDir = async(dirpath) => {
93
- await rmdir(dirpath, {
94
- recursive: true
95
- });
96
- };
97
-
98
- // ---------------------------------------------------------------------------
99
- export var rmDirSync = (dirpath) => {
100
- fs.rmdirSync(dirpath, {
101
- recursive: true
102
- });
103
- };
104
-
105
- // ---------------------------------------------------------------------------
106
- export var rmFile = async(filepath) => {
107
- await rm(filepath);
108
- };
109
-
110
96
  // --------------------------------------------------------------------------
111
97
  export var fixOutput = (contents) => {
112
98
  if (fix && isString(contents)) {
@@ -273,45 +259,6 @@ export var getFullPath = (filepath) => {
273
259
  return mkpath(pathlib.resolve(filepath));
274
260
  };
275
261
 
276
- // ---------------------------------------------------------------------------
277
- export var forEachLineInFile = (filepath, func) => {
278
- var buffer, line, nLines, reader, result;
279
- // --- func gets (line, lineNum, filepath) - lineNum starts at 1
280
- reader = new NReadLines(filepath);
281
- nLines = 0;
282
- while ((buffer = reader.next())) {
283
- nLines += 1;
284
- // --- text is split on \n chars,
285
- // we also need to remove \r chars
286
- line = buffer.toString().replace(/\r/g, '');
287
- result = func(line, nLines, filepath);
288
- assert(isBoolean(result));
289
- if (result) {
290
- reader.close(); // allow premature termination
291
- return;
292
- }
293
- }
294
- };
295
-
296
- // ---------------------------------------------------------------------------
297
- export var mapEachLineInFile = (filepath, func) => {
298
- var buffer, lLines, line, nLines, reader, result;
299
- reader = new NReadLines(filepath);
300
- nLines = 0;
301
- lLines = [];
302
- while ((buffer = reader.next())) {
303
- nLines += 1;
304
- // --- text is split on \n chars,
305
- // we also need to remove \r chars
306
- line = buffer.toString().replace(/\r/g, '');
307
- result = func(line, nLines);
308
- if (defined(result)) {
309
- lLines.push(result);
310
- }
311
- }
312
- return lLines;
313
- };
314
-
315
262
  // ---------------------------------------------------------------------------
316
263
  export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
317
264
  var callback, earlyExit, firstLineNum, lLines;
@@ -345,22 +292,23 @@ export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
345
292
  // ---------------------------------------------------------------------------
346
293
  export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_regexp = /^={16,}$/) => {
347
294
  var callback, earlyExit, firstLineNum, lBlocks, lLines;
295
+ dbgEnter('forEachSetOfBlocks', filepath);
348
296
  lBlocks = [];
349
297
  lLines = [];
350
298
  firstLineNum = 1;
351
299
  earlyExit = false;
352
- callback = function(line, lineNum) {
353
- var result;
300
+ callback = function(line, hContext) {
301
+ var lineNum, result;
302
+ dbgEnter('callback', line, hContext.lineNum);
303
+ lineNum = hContext.lineNum;
354
304
  if (line.match(set_regexp)) {
355
305
  lBlocks.push(lLines.join('\n'));
356
306
  lLines = [];
357
- if (result = func(lBlocks, firstLineNum, line)) {
358
- if (result === true) {
359
- earlyExit = true;
360
- return true;
361
- } else if (defined(result)) {
362
- croak(`forEachSetOfBlocks() - callback returned '${result}'`);
363
- }
307
+ result = func(deepCopy(lBlocks), firstLineNum, line);
308
+ if (result === true) {
309
+ earlyExit = true;
310
+ dbgReturn('callback', true);
311
+ return true;
364
312
  }
365
313
  lBlocks = [];
366
314
  firstLineNum = lineNum + 1;
@@ -370,6 +318,7 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
370
318
  } else {
371
319
  lLines.push(line);
372
320
  }
321
+ dbgReturn('callback', false);
373
322
  return false;
374
323
  };
375
324
  forEachLineInFile(filepath, callback);
@@ -377,6 +326,7 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
377
326
  lBlocks.push(lLines.join('\n'));
378
327
  func(lBlocks, firstLineNum);
379
328
  }
329
+ dbgReturn('forEachSetOfBlocks');
380
330
  };
381
331
 
382
332
  // ---------------------------------------------------------------------------
@@ -644,3 +594,5 @@ export var parseSource = (source) => {
644
594
  dbgReturn("parseSource", hSourceInfo);
645
595
  return hSourceInfo;
646
596
  };
597
+
598
+ // ---------------------------------------------------------------------------