@jdeighan/coffee-utils 7.0.65 → 7.0.66

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.65",
4
+ "version": "7.0.66",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -52,6 +52,6 @@
52
52
  "svelte": "^3.48.0"
53
53
  },
54
54
  "devDependencies": {
55
- "@jdeighan/unit-tester": "^2.0.7"
55
+ "@jdeighan/unit-tester": "^2.0.8"
56
56
  }
57
57
  }
@@ -27,8 +27,12 @@ strFuncList = undef # original string
27
27
 
28
28
  export interp = (label) ->
29
29
 
30
- return label.replace(/// \$ ([A-Za-z_][A-Za-z0-9_]*) ///g,
31
- (match, varName) -> return "\#{OL(#{varName})\}"
30
+ return label.replace(/// \$ (\@)? ([A-Za-z_][A-Za-z0-9_]*) ///g,
31
+ (_, atSign, varName) ->
32
+ if atSign
33
+ return "\#{OL(@#{varName})\}"
34
+ else
35
+ return "\#{OL(#{varName})\}"
32
36
  )
33
37
 
34
38
  # ---------------------------------------------------------------------------
@@ -67,8 +67,12 @@ strFuncList = undef; // original string
67
67
 
68
68
  // ---------------------------------------------------------------------------
69
69
  export var interp = function(label) {
70
- return label.replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, function(match, varName) {
71
- return `\#{OL(${varName})\}`;
70
+ return label.replace(/\$(\@)?([A-Za-z_][A-Za-z0-9_]*)/g, function(_, atSign, varName) {
71
+ if (atSign) {
72
+ return `\#{OL(@${varName})\}`;
73
+ } else {
74
+ return `\#{OL(${varName})\}`;
75
+ }
72
76
  });
73
77
  };
74
78
 
@@ -6,7 +6,7 @@ import fs from 'fs'
6
6
  import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
- assert, undef, pass, rtrim, error, isEmpty, nonEmpty,
9
+ assert, undef, pass, defined, rtrim, error, isEmpty, nonEmpty,
10
10
  isString, isArray, isRegExp, isFunction, croak, OL,
11
11
  } from '@jdeighan/coffee-utils'
12
12
  import {log, LOG} from '@jdeighan/coffee-utils/log'
@@ -270,30 +270,33 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
270
270
 
271
271
  export pathTo = (fname, searchDir, direction="down") ->
272
272
 
273
- debug "enter pathTo('#{fname}','#{searchDir}','#{direction}')"
273
+ debug "enter pathTo()", fname, searchDir, direction
274
274
  if ! searchDir
275
275
  searchDir = process.cwd()
276
- assert fs.existsSync(searchDir), "Directory #{searchDir} does not exist"
276
+ assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
277
277
  filepath = mkpath(searchDir, fname)
278
278
  if fs.existsSync(filepath)
279
- debug "return from pathTo: #{filepath} - file exists"
279
+ debug "return from pathTo() - file exists", filepath
280
280
  return filepath
281
- else if (direction == 'down')
281
+
282
+ if (direction == 'down')
282
283
  # --- Search all directories in this directory
283
284
  # getSubDirs() returns dirs sorted alphabetically
284
285
  for subdir in getSubDirs(searchDir)
285
286
  dirpath = mkpath(searchDir, subdir)
286
- debug "check #{dirpath}"
287
- if fpath = pathTo(fname, dirpath)
288
- debug "return from pathTo: #{fpath}"
287
+ debug "check #{subdir}"
288
+ if defined(fpath = pathTo(fname, dirpath))
289
+ debug "return from pathTo()", fpath
289
290
  return fpath
290
291
  else if (direction == 'up')
291
- while dirpath = getParentDir(searchDir)
292
- debug "check #{dirpath}"
293
- filepath = mkpath(dirpath, fname)
292
+ while defined(dirPath = getParentDir(searchDir))
293
+ debug "check #{dirPath}"
294
+ filepath = mkpath(dirPath, fname)
295
+ debug "check for #{filepath}"
294
296
  if fs.existsSync(filepath)
295
- debug "return from pathTo(): #{filepath}"
297
+ debug "return from pathTo()", filepath
296
298
  return filepath
299
+ searchDir = dirPath
297
300
  else
298
301
  error "pathTo(): Invalid direction '#{direction}'"
299
302
  debug "return undef from pathTo - file not found"
package/src/fs_utils.js CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  assert,
15
15
  undef,
16
16
  pass,
17
+ defined,
17
18
  rtrim,
18
19
  error,
19
20
  isEmpty,
@@ -326,37 +327,40 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
326
327
 
327
328
  // ---------------------------------------------------------------------------
328
329
  export var pathTo = function(fname, searchDir, direction = "down") {
329
- var dirpath, filepath, fpath, i, len, ref, subdir;
330
- debug(`enter pathTo('${fname}','${searchDir}','${direction}')`);
330
+ var dirPath, dirpath, filepath, fpath, i, len, ref, subdir;
331
+ debug("enter pathTo()", fname, searchDir, direction);
331
332
  if (!searchDir) {
332
333
  searchDir = process.cwd();
333
334
  }
334
- assert(fs.existsSync(searchDir), `Directory ${searchDir} does not exist`);
335
+ assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
335
336
  filepath = mkpath(searchDir, fname);
336
337
  if (fs.existsSync(filepath)) {
337
- debug(`return from pathTo: ${filepath} - file exists`);
338
+ debug("return from pathTo() - file exists", filepath);
338
339
  return filepath;
339
- } else if (direction === 'down') {
340
+ }
341
+ if (direction === 'down') {
340
342
  ref = getSubDirs(searchDir);
341
343
  // --- Search all directories in this directory
342
344
  // getSubDirs() returns dirs sorted alphabetically
343
345
  for (i = 0, len = ref.length; i < len; i++) {
344
346
  subdir = ref[i];
345
347
  dirpath = mkpath(searchDir, subdir);
346
- debug(`check ${dirpath}`);
347
- if (fpath = pathTo(fname, dirpath)) {
348
- debug(`return from pathTo: ${fpath}`);
348
+ debug(`check ${subdir}`);
349
+ if (defined(fpath = pathTo(fname, dirpath))) {
350
+ debug("return from pathTo()", fpath);
349
351
  return fpath;
350
352
  }
351
353
  }
352
354
  } else if (direction === 'up') {
353
- while (dirpath = getParentDir(searchDir)) {
354
- debug(`check ${dirpath}`);
355
- filepath = mkpath(dirpath, fname);
355
+ while (defined(dirPath = getParentDir(searchDir))) {
356
+ debug(`check ${dirPath}`);
357
+ filepath = mkpath(dirPath, fname);
358
+ debug(`check for ${filepath}`);
356
359
  if (fs.existsSync(filepath)) {
357
- debug(`return from pathTo(): ${filepath}`);
360
+ debug("return from pathTo()", filepath);
358
361
  return filepath;
359
362
  }
363
+ searchDir = dirPath;
360
364
  }
361
365
  } else {
362
366
  error(`pathTo(): Invalid direction '${direction}'`);
@@ -131,13 +131,14 @@ export tabify = (str, numSpaces=undef) ->
131
131
  if prefixLen == 0
132
132
  lLines.push theRest
133
133
  else
134
- if (prefix.indexOf('\t') != -1)
135
- error "tabify(): leading TAB characters not allowed"
134
+ assert (prefix.indexOf('\t') == -1), "found TAB"
136
135
  if numSpaces == undef
137
136
  numSpaces = prefixLen
138
137
  assert (prefixLen % numSpaces == 0), "Bad prefix"
139
- lLines.push '\t'.repeat(prefixLen) + theRest
140
- return arrayToBlock(lLines)
138
+ level = prefixLen / numSpaces
139
+ lLines.push '\t'.repeat(level) + theRest
140
+ result = arrayToBlock(lLines)
141
+ return result
141
142
 
142
143
  # ---------------------------------------------------------------------------
143
144
  # untabify - convert ALL TABs to spaces
@@ -147,7 +147,7 @@ export var undented = function(text, level = undef) {
147
147
  // if numSpaces is not defined, then the first line
148
148
  // that contains at least one space sets it
149
149
  export var tabify = function(str, numSpaces = undef) {
150
- var _, i, lLines, len, prefix, prefixLen, ref, theRest;
150
+ var _, i, lLines, len, level, prefix, prefixLen, ref, result, theRest;
151
151
  lLines = [];
152
152
  ref = blockToArray(str);
153
153
  for (i = 0, len = ref.length; i < len; i++) {
@@ -157,17 +157,17 @@ export var tabify = function(str, numSpaces = undef) {
157
157
  if (prefixLen === 0) {
158
158
  lLines.push(theRest);
159
159
  } else {
160
- if (prefix.indexOf('\t') !== -1) {
161
- error("tabify(): leading TAB characters not allowed");
162
- }
160
+ assert(prefix.indexOf('\t') === -1, "found TAB");
163
161
  if (numSpaces === undef) {
164
162
  numSpaces = prefixLen;
165
163
  }
166
164
  assert(prefixLen % numSpaces === 0, "Bad prefix");
167
- lLines.push('\t'.repeat(prefixLen) + theRest);
165
+ level = prefixLen / numSpaces;
166
+ lLines.push('\t'.repeat(level) + theRest);
168
167
  }
169
168
  }
170
- return arrayToBlock(lLines);
169
+ result = arrayToBlock(lLines);
170
+ return result;
171
171
  };
172
172
 
173
173
  // ---------------------------------------------------------------------------