@jdeighan/coffee-utils 7.0.64 → 7.0.67

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.64",
4
+ "version": "7.0.67",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -18,6 +18,7 @@
18
18
  "./svelte": "./src/svelte_utils.js",
19
19
  "./store": "./src/DataStores.js",
20
20
  "./taml": "./src/taml.js",
21
+ "./placeholders": "./src/placeholders.js",
21
22
  "./package.json": "./package.json"
22
23
  },
23
24
  "engines": {
@@ -52,6 +53,6 @@
52
53
  "svelte": "^3.48.0"
53
54
  },
54
55
  "devDependencies": {
55
- "@jdeighan/unit-tester": "^2.0.7"
56
+ "@jdeighan/unit-tester": "^2.0.8"
56
57
  }
57
58
  }
@@ -25,12 +25,26 @@ strFuncList = undef # original string
25
25
 
26
26
  # ---------------------------------------------------------------------------
27
27
 
28
+ export interp = (label) ->
29
+
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})\}"
36
+ )
37
+
38
+ # ---------------------------------------------------------------------------
39
+
28
40
  export debug = (orgLabel, lObjects...) ->
29
41
 
30
42
  assert isString(orgLabel), "1st arg #{OL(orgLabel)} should be a string"
31
43
 
32
44
  [type, funcName] = getType(orgLabel, lObjects)
33
45
  label = shouldLog(orgLabel, type, funcName, callStack)
46
+ if defined(label)
47
+ label = interp(label)
34
48
 
35
49
  switch type
36
50
 
@@ -243,22 +257,25 @@ export getType = (str, lObjects) ->
243
257
 
244
258
  if lMatches = str.match(///^
245
259
  \s*
246
- enter
260
+ ( enter | (?: return .+ from ) )
247
261
  \s+
248
- ([A-Za-z_][A-Za-z0-9_\.]*)
262
+ ([A-Za-z_][A-Za-z0-9_]*)
263
+ (?:
264
+ \.
265
+ ([A-Za-z_][A-Za-z0-9_]*)
266
+ )?
249
267
  ///)
268
+ [_, type, ident1, ident2] = lMatches
250
269
 
251
- # --- We are entering function curFunc
252
- return ['enter', lMatches[1]]
253
- else if lMatches = str.match(///^
254
- \s*
255
- return
256
- .+
257
- from
258
- \s+
259
- ([A-Za-z_][A-Za-z0-9_\.]*)
260
- ///)
261
- return ['return', lMatches[1]]
270
+ if ident2
271
+ funcName = ident2
272
+ else
273
+ funcName = ident1
274
+
275
+ if (type == 'enter')
276
+ return ['enter', funcName]
277
+ else
278
+ return ['return', funcName]
262
279
  else
263
280
  return ['string', undef]
264
281
 
@@ -65,12 +65,26 @@ lFuncList = []; // names of functions being debugged
65
65
  strFuncList = undef; // original string
66
66
 
67
67
 
68
+ // ---------------------------------------------------------------------------
69
+ export var interp = function(label) {
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
+ }
76
+ });
77
+ };
78
+
68
79
  // ---------------------------------------------------------------------------
69
80
  export var debug = function(orgLabel, ...lObjects) {
70
81
  var funcName, label, type;
71
82
  assert(isString(orgLabel), `1st arg ${OL(orgLabel)} should be a string`);
72
83
  [type, funcName] = getType(orgLabel, lObjects);
73
84
  label = shouldLog(orgLabel, type, funcName, callStack);
85
+ if (defined(label)) {
86
+ label = interp(label);
87
+ }
74
88
  switch (type) {
75
89
  case 'enter':
76
90
  if (defined(label)) {
@@ -292,12 +306,19 @@ export var funcMatch = function(funcName) {
292
306
  // ---------------------------------------------------------------------------
293
307
  // --- type is one of: 'enter', 'return', 'string'
294
308
  export var getType = function(str, lObjects) {
295
- var lMatches;
296
- if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
297
- // --- We are entering function curFunc
298
- return ['enter', lMatches[1]];
299
- } else if (lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
300
- return ['return', lMatches[1]];
309
+ var _, funcName, ident1, ident2, lMatches, type;
310
+ if (lMatches = str.match(/^\s*(enter|(?:return.+from))\s+([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?/)) {
311
+ [_, type, ident1, ident2] = lMatches;
312
+ if (ident2) {
313
+ funcName = ident2;
314
+ } else {
315
+ funcName = ident1;
316
+ }
317
+ if (type === 'enter') {
318
+ return ['enter', funcName];
319
+ } else {
320
+ return ['return', funcName];
321
+ }
301
322
  } else {
302
323
  return ['string', undef];
303
324
  }
@@ -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
  // ---------------------------------------------------------------------------
@@ -0,0 +1,25 @@
1
+ # placeholders.coffee
2
+
3
+ import {assert, undef, defined, croak} from '@jdeighan/coffee-utils'
4
+
5
+ hDefOptions = {
6
+ pre: '__'
7
+ post: '__'
8
+ }
9
+
10
+ # ---------------------------------------------------------------------------
11
+
12
+ export phStr = (name, hOptions=hDefOptions) ->
13
+
14
+ {pre, post} = hOptions
15
+ return "#{pre}#{name}#{post}"
16
+
17
+ # ---------------------------------------------------------------------------
18
+
19
+ export phReplace = (str, hValues, hOptions=hDefOptions) ->
20
+
21
+ {pre, post} = hOptions
22
+ return str.replace(
23
+ /// #{pre} ([A-Za-z_][A-Za-z0-9_]*) #{post} ///g,
24
+ (_, name) -> hValues[name]
25
+ )
@@ -0,0 +1,31 @@
1
+ // Generated by CoffeeScript 2.7.0
2
+ // placeholders.coffee
3
+ var hDefOptions;
4
+
5
+ import {
6
+ assert,
7
+ undef,
8
+ defined,
9
+ croak
10
+ } from '@jdeighan/coffee-utils';
11
+
12
+ hDefOptions = {
13
+ pre: '__',
14
+ post: '__'
15
+ };
16
+
17
+ // ---------------------------------------------------------------------------
18
+ export var phStr = function(name, hOptions = hDefOptions) {
19
+ var post, pre;
20
+ ({pre, post} = hOptions);
21
+ return `${pre}${name}${post}`;
22
+ };
23
+
24
+ // ---------------------------------------------------------------------------
25
+ export var phReplace = function(str, hValues, hOptions = hDefOptions) {
26
+ var post, pre;
27
+ ({pre, post} = hOptions);
28
+ return str.replace(RegExp(`${pre}([A-Za-z_][A-Za-z0-9_]*)${post}`, "g"), function(_, name) {
29
+ return hValues[name];
30
+ });
31
+ };