@jdeighan/coffee-utils 16.0.13 → 16.0.15

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "16.0.13",
4
+ "version": "16.0.15",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
8
+ ".": "./src/lib/browser.js",
8
9
  "./server": "./src/lib/server.js",
9
10
  "./browser": "./src/lib/browser.js",
10
11
  "./fs": "./src/lib/fs.js",
@@ -45,14 +46,14 @@
45
46
  },
46
47
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
47
48
  "dependencies": {
48
- "@jdeighan/base-utils": "^8.0.0",
49
+ "@jdeighan/base-utils": "^8.0.16",
49
50
  "cross-env": "^7.0.3",
50
51
  "n-readlines": "^1.0.1",
51
52
  "readline-sync": "^1.4.10",
52
- "svelte": "^4.0.5"
53
+ "svelte": "^4.1.1"
53
54
  },
54
55
  "devDependencies": {
55
- "@jdeighan/unit-tester": "^3.0.58",
56
- "ava": "^5.3.1"
56
+ "@jdeighan/unit-tester": "^3.0.65",
57
+ "ava": "^6.0.1"
57
58
  }
58
59
  }
@@ -10,7 +10,7 @@ import {LOG, LOGVALUE, LOGTAML} from '@jdeighan/base-utils/log'
10
10
  import {
11
11
  dbg, dbgEnter, dbgReturn, dbgYield, dbgResume,
12
12
  } from '@jdeighan/base-utils/debug'
13
- import {isTAML, fromTAML} from '@jdeighan/base-utils/taml'
13
+ import {isTAML, fromTAML} from '@jdeighan/base-utils/ll-taml'
14
14
 
15
15
  import {Section} from '@jdeighan/coffee-utils/section'
16
16
 
@@ -43,7 +43,7 @@ import {
43
43
  import {
44
44
  isTAML,
45
45
  fromTAML
46
- } from '@jdeighan/base-utils/taml';
46
+ } from '@jdeighan/base-utils/ll-taml';
47
47
 
48
48
  import {
49
49
  Section
package/src/lib/fs.coffee CHANGED
@@ -8,23 +8,26 @@ 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
+ mydir, mkpath, isFile, isDir, rmFileSync, mkdirSync,
19
+ forEachLineInFile, fixPath,
20
+ rmFile, rmDir, rmDirSync,
21
21
  } from '@jdeighan/base-utils/fs'
22
22
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
23
23
  import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
24
24
  import {dbg, dbgEnter, dbgReturn} from '@jdeighan/base-utils/debug'
25
- import {fromTAML} from '@jdeighan/base-utils/taml'
25
+ import {fromTAML} from '@jdeighan/base-utils/ll-taml'
26
26
 
27
- export {mkpath, isFile, isDir, rmFileSync, mkdirSync}
27
+ export {
28
+ mydir, mkpath, isFile, isDir, rmFileSync, mkdirSync,
29
+ forEachLineInFile, rmDir, rmDirSync, rmFile,
30
+ }
28
31
 
29
32
  fix = true
30
33
 
@@ -35,27 +38,6 @@ export doFixOutput = (flag=true) =>
35
38
  fix = flag
36
39
  return
37
40
 
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
41
  # --------------------------------------------------------------------------
60
42
 
61
43
  export fixOutput = (contents) =>
@@ -126,17 +108,6 @@ export cloneRepo = (user, repo, dir) =>
126
108
  git_repo = "https://github.com/#{user}/#{repo}.git"
127
109
  return execCmd "git clone #{git_repo} #{dir}"
128
110
 
129
- # ---------------------------------------------------------------------------
130
- # mydir() - pass argument import.meta.url and it will return
131
- # the directory your file is in
132
-
133
- export mydir = (url) =>
134
-
135
- path = urllib.fileURLToPath(url)
136
- dir = pathlib.dirname(path)
137
- final = mkpath(dir)
138
- return final
139
-
140
111
  # ---------------------------------------------------------------------------
141
112
 
142
113
  export homeDir = () =>
@@ -212,44 +183,6 @@ export getFullPath = (filepath) =>
212
183
 
213
184
  # ---------------------------------------------------------------------------
214
185
 
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
186
  export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
254
187
 
255
188
  lLines = []
@@ -277,25 +210,28 @@ export forEachBlock = (filepath, func, regexp = /^-{16,}$/) =>
277
210
 
278
211
  # ---------------------------------------------------------------------------
279
212
 
280
- export forEachSetOfBlocks = (filepath, func,
281
- block_regexp = /^-{16,}$/,
282
- set_regexp = /^={16,}$/) =>
213
+ export forEachSetOfBlocks = (filepath, func, \
214
+ block_regexp = /^-{16,}$/, \
215
+ set_regexp = /^={16,}$/) \
216
+ =>
283
217
 
218
+ dbgEnter 'forEachSetOfBlocks', filepath
284
219
  lBlocks = []
285
220
  lLines = []
286
221
  firstLineNum = 1
287
222
  earlyExit = false
288
223
 
289
- callback = (line, lineNum) ->
224
+ callback = (line, hContext) ->
225
+ dbgEnter 'callback', line, hContext.lineNum
226
+ lineNum = hContext.lineNum
290
227
  if (line.match(set_regexp))
291
228
  lBlocks.push(lLines.join('\n'))
292
229
  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}'"
230
+ result = func(deepCopy(lBlocks), firstLineNum, line)
231
+ if (result == true)
232
+ earlyExit = true
233
+ dbgReturn 'callback', true
234
+ return true
299
235
  lBlocks = []
300
236
  firstLineNum = lineNum+1
301
237
  else if (line.match(block_regexp))
@@ -303,12 +239,14 @@ export forEachSetOfBlocks = (filepath, func,
303
239
  lLines = []
304
240
  else
305
241
  lLines.push line
242
+ dbgReturn 'callback', false
306
243
  return false
307
244
 
308
245
  forEachLineInFile filepath, callback
309
246
  if ! earlyExit
310
247
  lBlocks.push(lLines.join('\n'))
311
248
  func(lBlocks, firstLineNum)
249
+ dbgReturn 'forEachSetOfBlocks'
312
250
  return
313
251
 
314
252
  # ---------------------------------------------------------------------------
@@ -414,9 +352,9 @@ export pathTo = (fname, searchDir, options=undef) =>
414
352
  if relative
415
353
  return "./#{fname}"
416
354
  else if directory
417
- return searchDir
355
+ return fixPath(searchDir)
418
356
  else
419
- return filepath
357
+ return fixPath(filepath)
420
358
 
421
359
  if (direction == 'down')
422
360
  # --- Search all directories in this directory
@@ -427,9 +365,9 @@ export pathTo = (fname, searchDir, options=undef) =>
427
365
  if relative
428
366
  return fpath.replace('./', "./#{subdir}/")
429
367
  else if directory
430
- return dirPath
368
+ return fixPath(dirPath)
431
369
  else
432
- return fpath
370
+ return fixPath(fpath)
433
371
  else if (direction == 'up')
434
372
  nLevels = 0
435
373
  while defined(dirPath = getParentDir(searchDir))
@@ -439,9 +377,9 @@ export pathTo = (fname, searchDir, options=undef) =>
439
377
  if relative
440
378
  return "../".repeat(nLevels) + fname
441
379
  else if directory
442
- return dirPath
380
+ return fixPath(dirPath)
443
381
  else
444
- return fpath
382
+ return fixPath(fpath)
445
383
  searchDir = dirPath
446
384
  else
447
385
  croak "pathTo(): Invalid direction '#{direction}'"
@@ -544,3 +482,5 @@ export parseSource = (source) =>
544
482
  hSourceInfo.purpose = lMatches[1]
545
483
  dbgReturn "parseSource", hSourceInfo
546
484
  return hSourceInfo
485
+
486
+ # ---------------------------------------------------------------------------
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,15 +38,22 @@ 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 {
46
+ mydir,
49
47
  mkpath,
50
48
  isFile,
51
49
  isDir,
52
50
  rmFileSync,
53
- mkdirSync
51
+ mkdirSync,
52
+ forEachLineInFile,
53
+ fixPath,
54
+ rmFile,
55
+ rmDir,
56
+ rmDirSync
54
57
  } from '@jdeighan/base-utils/fs';
55
58
 
56
59
  import {
@@ -71,14 +74,19 @@ import {
71
74
 
72
75
  import {
73
76
  fromTAML
74
- } from '@jdeighan/base-utils/taml';
77
+ } from '@jdeighan/base-utils/ll-taml';
75
78
 
76
79
  export {
80
+ mydir,
77
81
  mkpath,
78
82
  isFile,
79
83
  isDir,
80
84
  rmFileSync,
81
- mkdirSync
85
+ mkdirSync,
86
+ forEachLineInFile,
87
+ rmDir,
88
+ rmDirSync,
89
+ rmFile
82
90
  };
83
91
 
84
92
  fix = true;
@@ -88,25 +96,6 @@ export var doFixOutput = (flag = true) => {
88
96
  fix = flag;
89
97
  };
90
98
 
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
99
  // --------------------------------------------------------------------------
111
100
  export var fixOutput = (contents) => {
112
101
  if (fix && isString(contents)) {
@@ -190,17 +179,6 @@ export var cloneRepo = (user, repo, dir) => {
190
179
  return execCmd(`git clone ${git_repo} ${dir}`);
191
180
  };
192
181
 
193
- // ---------------------------------------------------------------------------
194
- // mydir() - pass argument import.meta.url and it will return
195
- // the directory your file is in
196
- export var mydir = (url) => {
197
- var dir, final, path;
198
- path = urllib.fileURLToPath(url);
199
- dir = pathlib.dirname(path);
200
- final = mkpath(dir);
201
- return final;
202
- };
203
-
204
182
  // ---------------------------------------------------------------------------
205
183
  export var homeDir = () => {
206
184
  return mkpath(os.homedir());
@@ -273,45 +251,6 @@ export var getFullPath = (filepath) => {
273
251
  return mkpath(pathlib.resolve(filepath));
274
252
  };
275
253
 
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
254
  // ---------------------------------------------------------------------------
316
255
  export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
317
256
  var callback, earlyExit, firstLineNum, lLines;
@@ -345,22 +284,23 @@ export var forEachBlock = (filepath, func, regexp = /^-{16,}$/) => {
345
284
  // ---------------------------------------------------------------------------
346
285
  export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_regexp = /^={16,}$/) => {
347
286
  var callback, earlyExit, firstLineNum, lBlocks, lLines;
287
+ dbgEnter('forEachSetOfBlocks', filepath);
348
288
  lBlocks = [];
349
289
  lLines = [];
350
290
  firstLineNum = 1;
351
291
  earlyExit = false;
352
- callback = function(line, lineNum) {
353
- var result;
292
+ callback = function(line, hContext) {
293
+ var lineNum, result;
294
+ dbgEnter('callback', line, hContext.lineNum);
295
+ lineNum = hContext.lineNum;
354
296
  if (line.match(set_regexp)) {
355
297
  lBlocks.push(lLines.join('\n'));
356
298
  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
- }
299
+ result = func(deepCopy(lBlocks), firstLineNum, line);
300
+ if (result === true) {
301
+ earlyExit = true;
302
+ dbgReturn('callback', true);
303
+ return true;
364
304
  }
365
305
  lBlocks = [];
366
306
  firstLineNum = lineNum + 1;
@@ -370,6 +310,7 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
370
310
  } else {
371
311
  lLines.push(line);
372
312
  }
313
+ dbgReturn('callback', false);
373
314
  return false;
374
315
  };
375
316
  forEachLineInFile(filepath, callback);
@@ -377,6 +318,7 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
377
318
  lBlocks.push(lLines.join('\n'));
378
319
  func(lBlocks, firstLineNum);
379
320
  }
321
+ dbgReturn('forEachSetOfBlocks');
380
322
  };
381
323
 
382
324
  // ---------------------------------------------------------------------------
@@ -498,9 +440,9 @@ export var pathTo = (fname, searchDir, options = undef) => {
498
440
  if (relative) {
499
441
  return `./${fname}`;
500
442
  } else if (directory) {
501
- return searchDir;
443
+ return fixPath(searchDir);
502
444
  } else {
503
- return filepath;
445
+ return fixPath(filepath);
504
446
  }
505
447
  }
506
448
  if (direction === 'down') {
@@ -514,9 +456,9 @@ export var pathTo = (fname, searchDir, options = undef) => {
514
456
  if (relative) {
515
457
  return fpath.replace('./', `./${subdir}/`);
516
458
  } else if (directory) {
517
- return dirPath;
459
+ return fixPath(dirPath);
518
460
  } else {
519
- return fpath;
461
+ return fixPath(fpath);
520
462
  }
521
463
  }
522
464
  }
@@ -529,9 +471,9 @@ export var pathTo = (fname, searchDir, options = undef) => {
529
471
  if (relative) {
530
472
  return "../".repeat(nLevels) + fname;
531
473
  } else if (directory) {
532
- return dirPath;
474
+ return fixPath(dirPath);
533
475
  } else {
534
- return fpath;
476
+ return fixPath(fpath);
535
477
  }
536
478
  }
537
479
  searchDir = dirPath;
@@ -644,3 +586,5 @@ export var parseSource = (source) => {
644
586
  dbgReturn("parseSource", hSourceInfo);
645
587
  return hSourceInfo;
646
588
  };
589
+
590
+ // ---------------------------------------------------------------------------