@jdeighan/coffee-utils 7.0.1 → 7.0.4

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": "7.0.1",
4
+ "version": "7.0.4",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -49,7 +49,7 @@
49
49
  "js-yaml": "^4.1.0",
50
50
  "n-readlines": "^1.0.1",
51
51
  "readline-sync": "^1.4.10",
52
- "svelte": "^3.46.4"
52
+ "svelte": "^3.46.6"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@jdeighan/unit-tester": "^1.0.5"
@@ -168,7 +168,10 @@ export debug = (lArgs...) ->
168
168
  # when debugging is off
169
169
 
170
170
  [mainPre, auxPre, hEnv, type] = adjustStack(label)
171
- hOptions = {prefix: mainPre, itemPrefix: auxPre}
171
+ hOptions = {
172
+ prefix: mainPre
173
+ itemPrefix: auxPre
174
+ }
172
175
  switch type
173
176
  when 'enter'
174
177
  log label, hOptions
@@ -239,21 +239,26 @@ export pathTo = (fname, dir, direction="down") ->
239
239
 
240
240
  debug "enter pathTo('#{fname}','#{dir}','#{direction}')"
241
241
  assert fs.existsSync(dir), "Directory #{dir} does not exist"
242
- if fs.existsSync("#{dir}/#{fname}")
243
- debug "return from pathTo: #{dir}/#{fname} - file exists"
244
- return mkpath("#{dir}/#{fname}")
242
+ filepath = mkpath(dir, fname)
243
+ if fs.existsSync(filepath)
244
+ debug "return from pathTo: #{filepath} - file exists"
245
+ return filepath
245
246
  else if (direction == 'down')
246
247
  # --- Search all directories in this directory
248
+ # getSubDirs() returns dirs sorted alphabetically
247
249
  for subdir in getSubDirs(dir)
248
- if fpath = pathTo(fname, "#{dir}/#{subdir}")
250
+ dirpath = mkpath(dir, subdir)
251
+ debug "check #{dirpath}"
252
+ if fpath = pathTo(fname, dirpath)
249
253
  debug "return from pathTo: #{fpath}"
250
254
  return fpath
251
255
  else if (direction == 'up')
252
- while dir = getParentDir(dir)
253
- debug "check #{dir}"
254
- if fs.existsSync("#{dir}/#{fname}")
255
- debug "return from pathTo(): #{dir}/#{fname}"
256
- return "#{dir}/#{fname}"
256
+ while dirpath = getParentDir(dir)
257
+ debug "check #{dirpath}"
258
+ filepath = mkpath(dirpath, fname)
259
+ if fs.existsSync(filepath)
260
+ debug "return from pathTo(): #{filepath}"
261
+ return filepath
257
262
  else
258
263
  error "pathTo(): Invalid direction '#{direction}'"
259
264
  debug "return undef from pathTo - file not found"
package/src/fs_utils.js CHANGED
@@ -281,28 +281,33 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
281
281
 
282
282
  // ---------------------------------------------------------------------------
283
283
  export var pathTo = function(fname, dir, direction = "down") {
284
- var fpath, i, len, ref, subdir;
284
+ var dirpath, filepath, fpath, i, len, ref, subdir;
285
285
  debug(`enter pathTo('${fname}','${dir}','${direction}')`);
286
286
  assert(fs.existsSync(dir), `Directory ${dir} does not exist`);
287
- if (fs.existsSync(`${dir}/${fname}`)) {
288
- debug(`return from pathTo: ${dir}/${fname} - file exists`);
289
- return mkpath(`${dir}/${fname}`);
287
+ filepath = mkpath(dir, fname);
288
+ if (fs.existsSync(filepath)) {
289
+ debug(`return from pathTo: ${filepath} - file exists`);
290
+ return filepath;
290
291
  } else if (direction === 'down') {
291
292
  ref = getSubDirs(dir);
292
293
  // --- Search all directories in this directory
294
+ // getSubDirs() returns dirs sorted alphabetically
293
295
  for (i = 0, len = ref.length; i < len; i++) {
294
296
  subdir = ref[i];
295
- if (fpath = pathTo(fname, `${dir}/${subdir}`)) {
297
+ dirpath = mkpath(dir, subdir);
298
+ debug(`check ${dirpath}`);
299
+ if (fpath = pathTo(fname, dirpath)) {
296
300
  debug(`return from pathTo: ${fpath}`);
297
301
  return fpath;
298
302
  }
299
303
  }
300
304
  } else if (direction === 'up') {
301
- while (dir = getParentDir(dir)) {
302
- debug(`check ${dir}`);
303
- if (fs.existsSync(`${dir}/${fname}`)) {
304
- debug(`return from pathTo(): ${dir}/${fname}`);
305
- return `${dir}/${fname}`;
305
+ while (dirpath = getParentDir(dir)) {
306
+ debug(`check ${dirpath}`);
307
+ filepath = mkpath(dirpath, fname);
308
+ if (fs.existsSync(filepath)) {
309
+ debug(`return from pathTo(): ${filepath}`);
310
+ return filepath;
306
311
  }
307
312
  }
308
313
  } else {
@@ -41,7 +41,7 @@ export indentLevel = (str) ->
41
41
  # ---------------------------------------------------------------------------
42
42
  # indented - add indentation to each string in a block
43
43
 
44
- export indented = (input, level=0) ->
44
+ export indented = (input, level=1) ->
45
45
 
46
46
  assert (level >= 0), "indented(): negative level"
47
47
  if level == 0
@@ -130,22 +130,6 @@ export tabify = (str, numSpaces=undef) ->
130
130
  lLines.push '\t'.repeat(prefixLen) + theRest
131
131
  return arrayToBlock(lLines)
132
132
 
133
- # ---------------------------------------------------------------------------
134
- # untabify - convert leading TABs to spaces
135
-
136
- untabify_old = (str, numSpaces=3) ->
137
-
138
- oneIndent = ' '.repeat(numSpaces)
139
- lLines = []
140
- for str in blockToArray(str)
141
- lMatches = str.match(/^(\t*)(.*)$/)
142
- [_, prefix, theRest] = lMatches
143
- if prefix == ''
144
- lLines.push theRest
145
- else
146
- lLines.push oneIndent.repeat(prefix.length) + theRest
147
- return arrayToBlock(lLines)
148
-
149
133
  # ---------------------------------------------------------------------------
150
134
  # untabify - convert ALL TABs to spaces
151
135
 
@@ -1,7 +1,5 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
- // indent_utils.coffee
3
- var untabify_old;
4
-
2
+ // indent_utils.coffee
5
3
  import {
6
4
  assert,
7
5
  undef,
@@ -53,7 +51,7 @@ export var indentLevel = function(str) {
53
51
 
54
52
  // ---------------------------------------------------------------------------
55
53
  // indented - add indentation to each string in a block
56
- export var indented = function(input, level = 0) {
54
+ export var indented = function(input, level = 1) {
57
55
  var lInputLines, lLines, line, toAdd;
58
56
  assert(level >= 0, "indented(): negative level");
59
57
  if (level === 0) {
@@ -162,26 +160,6 @@ export var tabify = function(str, numSpaces = undef) {
162
160
  return arrayToBlock(lLines);
163
161
  };
164
162
 
165
- // ---------------------------------------------------------------------------
166
- // untabify - convert leading TABs to spaces
167
- untabify_old = function(str, numSpaces = 3) {
168
- var _, i, lLines, lMatches, len, oneIndent, prefix, ref, theRest;
169
- oneIndent = ' '.repeat(numSpaces);
170
- lLines = [];
171
- ref = blockToArray(str);
172
- for (i = 0, len = ref.length; i < len; i++) {
173
- str = ref[i];
174
- lMatches = str.match(/^(\t*)(.*)$/);
175
- [_, prefix, theRest] = lMatches;
176
- if (prefix === '') {
177
- lLines.push(theRest);
178
- } else {
179
- lLines.push(oneIndent.repeat(prefix.length) + theRest);
180
- }
181
- }
182
- return arrayToBlock(lLines);
183
- };
184
-
185
163
  // ---------------------------------------------------------------------------
186
164
  // untabify - convert ALL TABs to spaces
187
165
  export var untabify = function(str, numSpaces = 3) {
@@ -134,17 +134,15 @@ export log = (str, hOptions={}) ->
134
134
 
135
135
  export logItem = (label, item, hOptions={}) ->
136
136
  # --- valid options:
137
- # prefix
138
- # itemPrefix
137
+ # prefix - not used
138
+ # itemPrefix - always used
139
139
 
140
140
  assert isFunction(putstr), "putstr not properly set"
141
141
  assert !label || isString(label), "label a non-string"
142
142
  assert isHash(hOptions), "arg 3 not a hash"
143
143
 
144
144
  label = fixStr(label)
145
- prefix = fixStr(hOptions.prefix)
146
- itemPrefix = fixStr(hOptions.itemPrefix || prefix)
147
-
145
+ prefix = fixStr(hOptions.itemPrefix || hOptions.prefix)
148
146
  labelStr = if label then "#{label} = " else ""
149
147
 
150
148
  if (item == undef)
@@ -155,16 +153,16 @@ export logItem = (label, item, hOptions={}) ->
155
153
  else
156
154
  if label
157
155
  putstr "#{prefix}#{label}:"
158
- putBlock item, itemPrefix
156
+ putBlock item, prefix
159
157
  else if isNumber(item)
160
158
  putstr "#{prefix}#{labelStr}#{item}"
161
159
  else
162
- putstr "#{itemPrefix}#{sep_dash}"
160
+ putstr "#{prefix}#{sep_dash}"
163
161
  if label
164
162
  putstr "#{prefix}#{label}:"
165
163
  for str in blockToArray(stringify(item, true)) # escape special chars
166
- putstr "#{itemPrefix}#{indentation(1)}#{fixStr(str)}"
167
- putstr "#{itemPrefix}#{sep_dash}"
164
+ putstr "#{prefix}#{indentation(1)}#{fixStr(str)}"
165
+ putstr "#{prefix}#{sep_dash}"
168
166
 
169
167
  return
170
168
 
package/src/log_utils.js CHANGED
@@ -151,16 +151,15 @@ export var log = function(str, hOptions = {}) {
151
151
 
152
152
  // ---------------------------------------------------------------------------
153
153
  export var logItem = function(label, item, hOptions = {}) {
154
- var i, itemPrefix, labelStr, len, prefix, ref, str;
154
+ var i, labelStr, len, prefix, ref, str;
155
155
  // --- valid options:
156
- // prefix
157
- // itemPrefix
156
+ // prefix - not used
157
+ // itemPrefix - always used
158
158
  assert(isFunction(putstr), "putstr not properly set");
159
159
  assert(!label || isString(label), "label a non-string");
160
160
  assert(isHash(hOptions), "arg 3 not a hash");
161
161
  label = fixStr(label);
162
- prefix = fixStr(hOptions.prefix);
163
- itemPrefix = fixStr(hOptions.itemPrefix || prefix);
162
+ prefix = fixStr(hOptions.itemPrefix || hOptions.prefix);
164
163
  labelStr = label ? `${label} = ` : "";
165
164
  if (item === undef) {
166
165
  putstr(`${prefix}${labelStr}undef`);
@@ -171,12 +170,12 @@ export var logItem = function(label, item, hOptions = {}) {
171
170
  if (label) {
172
171
  putstr(`${prefix}${label}:`);
173
172
  }
174
- putBlock(item, itemPrefix);
173
+ putBlock(item, prefix);
175
174
  }
176
175
  } else if (isNumber(item)) {
177
176
  putstr(`${prefix}${labelStr}${item}`);
178
177
  } else {
179
- putstr(`${itemPrefix}${sep_dash}`);
178
+ putstr(`${prefix}${sep_dash}`);
180
179
  if (label) {
181
180
  putstr(`${prefix}${label}:`);
182
181
  }
@@ -184,9 +183,9 @@ export var logItem = function(label, item, hOptions = {}) {
184
183
  // escape special chars
185
184
  for (i = 0, len = ref.length; i < len; i++) {
186
185
  str = ref[i];
187
- putstr(`${itemPrefix}${indentation(1)}${fixStr(str)}`);
186
+ putstr(`${prefix}${indentation(1)}${fixStr(str)}`);
188
187
  }
189
- putstr(`${itemPrefix}${sep_dash}`);
188
+ putstr(`${prefix}${sep_dash}`);
190
189
  }
191
190
  };
192
191