@jdeighan/coffee-utils 6.0.6 → 6.0.9

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": "6.0.6",
4
+ "version": "6.0.9",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -26,7 +26,7 @@ export class CallStack
26
26
 
27
27
  if doDebugStack
28
28
  prefix = ' '.repeat(@lStack.length)
29
- LOG "#{prefix}[CALL #{funcName}]"
29
+ LOG "#{prefix}[--> CALL #{funcName}]"
30
30
  @lStack.push({funcName, hInfo})
31
31
  return
32
32
 
@@ -37,14 +37,14 @@ export class CallStack
37
37
  if @lStack.length == 0
38
38
  LOG "returnFrom('#{fName}') but stack is empty"
39
39
  return undef
40
- if doDebugStack
41
- prefix = ' '.repeat(@lStack.length-1)
42
- LOG "#{prefix}[RETURN FROM #{fName}]"
43
40
  {funcName, hInfo} = @lStack.pop()
44
41
  while (funcName != fName) && (@lStack.length > 0)
45
42
  LOG "[MISSING RETURN FROM #{funcName} (return from #{fName})]"
46
43
  {funcName, hInfo} = @lStack.pop()
47
44
 
45
+ if doDebugStack
46
+ prefix = ' '.repeat(@lStack.length)
47
+ LOG "#{prefix}[<-- BACK #{fName}]"
48
48
  if (funcName == fName)
49
49
  return hInfo
50
50
  else
package/src/call_stack.js CHANGED
@@ -31,7 +31,7 @@ export var CallStack = class CallStack {
31
31
  var prefix;
32
32
  if (doDebugStack) {
33
33
  prefix = ' '.repeat(this.lStack.length);
34
- LOG(`${prefix}[CALL ${funcName}]`);
34
+ LOG(`${prefix}[--> CALL ${funcName}]`);
35
35
  }
36
36
  this.lStack.push({funcName, hInfo});
37
37
  }
@@ -43,15 +43,15 @@ export var CallStack = class CallStack {
43
43
  LOG(`returnFrom('${fName}') but stack is empty`);
44
44
  return undef;
45
45
  }
46
- if (doDebugStack) {
47
- prefix = ' '.repeat(this.lStack.length - 1);
48
- LOG(`${prefix}[RETURN FROM ${fName}]`);
49
- }
50
46
  ({funcName, hInfo} = this.lStack.pop());
51
47
  while ((funcName !== fName) && (this.lStack.length > 0)) {
52
48
  LOG(`[MISSING RETURN FROM ${funcName} (return from ${fName})]`);
53
49
  ({funcName, hInfo} = this.lStack.pop());
54
50
  }
51
+ if (doDebugStack) {
52
+ prefix = ' '.repeat(this.lStack.length);
53
+ LOG(`${prefix}[<-- BACK ${fName}]`);
54
+ }
55
55
  if (funcName === fName) {
56
56
  return hInfo;
57
57
  } else {
@@ -38,21 +38,20 @@ export setDEBUGDEBUG = (flag=true) ->
38
38
 
39
39
  # ---------------------------------------------------------------------------
40
40
 
41
- export resetDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
41
+ resetDebugging = () ->
42
42
 
43
43
  debugging = false
44
44
  debugLevel = 0
45
45
  stack.reset()
46
46
  shouldDebugFunc = (func) -> debugging
47
47
  shouldLogString = (str) -> debugging
48
- if funcDoDebug
49
- setDebugging funcDoDebug, funcDoLog
50
48
  return
51
49
 
52
50
  # ---------------------------------------------------------------------------
53
51
 
54
52
  export setDebugging = (funcDoDebug=undef, funcDoLog=undef) ->
55
53
 
54
+ resetDebugging()
56
55
  if isBoolean(funcDoDebug)
57
56
  if DEBUGDEBUG
58
57
  console.log "setDebugging #{funcDoDebug}"
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // debug_utils.coffee
3
- var DEBUGDEBUG, curEnv, debugLevel, reMethod, setEnv, shouldDebugFunc, shouldLogString, stack;
3
+ var DEBUGDEBUG, curEnv, debugLevel, reMethod, resetDebugging, setEnv, shouldDebugFunc, shouldLogString, stack;
4
4
 
5
5
  import {
6
6
  assert,
@@ -75,7 +75,7 @@ export var setDEBUGDEBUG = function(flag = true) {
75
75
  };
76
76
 
77
77
  // ---------------------------------------------------------------------------
78
- export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
78
+ resetDebugging = function() {
79
79
  debugging = false;
80
80
  debugLevel = 0;
81
81
  stack.reset();
@@ -85,14 +85,12 @@ export var resetDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
85
85
  shouldLogString = function(str) {
86
86
  return debugging;
87
87
  };
88
- if (funcDoDebug) {
89
- setDebugging(funcDoDebug, funcDoLog);
90
- }
91
88
  };
92
89
 
93
90
  // ---------------------------------------------------------------------------
94
91
  export var setDebugging = function(funcDoDebug = undef, funcDoLog = undef) {
95
92
  var lFuncNames;
93
+ resetDebugging();
96
94
  if (isBoolean(funcDoDebug)) {
97
95
  if (DEBUGDEBUG) {
98
96
  console.log(`setDebugging ${funcDoDebug}`);
@@ -34,6 +34,8 @@ export LOG = (lArgs...) ->
34
34
  console.log label
35
35
  return
36
36
 
37
+ export DEBUG = LOG # synonym
38
+
37
39
  # ---------------------------------------------------------------------------
38
40
 
39
41
  export setStringifier = (func) ->
package/src/log_utils.js CHANGED
@@ -59,6 +59,9 @@ export var LOG = function(...lArgs) {
59
59
  }
60
60
  };
61
61
 
62
+ export var DEBUG = LOG; // synonym
63
+
64
+
62
65
  // ---------------------------------------------------------------------------
63
66
  export var setStringifier = function(func) {
64
67
  var orgStringifier;
package/src/taml.coffee CHANGED
@@ -5,11 +5,13 @@ import yaml from 'js-yaml'
5
5
  import {
6
6
  assert, undef, oneline, isString,
7
7
  } from '@jdeighan/coffee-utils'
8
- import {untabify, tabify} from '@jdeighan/coffee-utils/indent'
8
+ import {untabify, tabify, splitLine} from '@jdeighan/coffee-utils/indent'
9
9
  import {log, tamlStringify} from '@jdeighan/coffee-utils/log'
10
10
  import {slurp} from '@jdeighan/coffee-utils/fs'
11
11
  import {debug} from '@jdeighan/coffee-utils/debug'
12
- import {firstLine} from '@jdeighan/coffee-utils/block'
12
+ import {
13
+ firstLine, blockToArray, arrayToBlock,
14
+ } from '@jdeighan/coffee-utils/block'
13
15
 
14
16
  # ---------------------------------------------------------------------------
15
17
  # isTAML - is the string valid TAML?
@@ -18,6 +20,12 @@ export isTAML = (text) ->
18
20
 
19
21
  return isString(text) && (firstLine(text).indexOf('---') == 0)
20
22
 
23
+ # ---------------------------------------------------------------------------
24
+
25
+ squote = (text) ->
26
+
27
+ return "'" + text.replace(/'/g, "''") + "'"
28
+
21
29
  # ---------------------------------------------------------------------------
22
30
  # taml - convert valid TAML string to a JavaScript value
23
31
 
@@ -28,8 +36,27 @@ export taml = (text) ->
28
36
  debug "return undef from taml() - text is not defined"
29
37
  return undef
30
38
  assert isTAML(text), "taml(): string #{oneline(text)} isn't TAML"
39
+
40
+ lLines = for line in blockToArray(text)
41
+ [level, str] = splitLine(line)
42
+ prefix = ' '.repeat(level)
43
+ if lMatches = line.match(///^
44
+ ([A-Za-z_][A-Za-z0-9_]*) # the key
45
+ \s*
46
+ :
47
+ \s*
48
+ (.*)
49
+ $///)
50
+ [_, key, text] = lMatches
51
+ if isEmpty(text) || text.match(/\d+(?:\.\d*)$/)
52
+ prefix + str
53
+ else
54
+ prefix + key + ':' + ' ' + squote(text)
55
+ else
56
+ prefix + str
57
+
31
58
  debug "return from taml()"
32
- return yaml.load(untabify(text, 1), {skipInvalid: true})
59
+ return yaml.load(arrayToBlock(lLines), {skipInvalid: true})
33
60
 
34
61
  # ---------------------------------------------------------------------------
35
62
  # slurpTAML - read TAML from a file
package/src/taml.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // taml.coffee
3
+ var squote;
4
+
3
5
  import yaml from 'js-yaml';
4
6
 
5
7
  import {
@@ -11,7 +13,8 @@ import {
11
13
 
12
14
  import {
13
15
  untabify,
14
- tabify
16
+ tabify,
17
+ splitLine
15
18
  } from '@jdeighan/coffee-utils/indent';
16
19
 
17
20
  import {
@@ -28,7 +31,9 @@ import {
28
31
  } from '@jdeighan/coffee-utils/debug';
29
32
 
30
33
  import {
31
- firstLine
34
+ firstLine,
35
+ blockToArray,
36
+ arrayToBlock
32
37
  } from '@jdeighan/coffee-utils/block';
33
38
 
34
39
  // ---------------------------------------------------------------------------
@@ -37,17 +42,44 @@ export var isTAML = function(text) {
37
42
  return isString(text) && (firstLine(text).indexOf('---') === 0);
38
43
  };
39
44
 
45
+ // ---------------------------------------------------------------------------
46
+ squote = function(text) {
47
+ return "'" + text.replace(/'/g, "''") + "'";
48
+ };
49
+
40
50
  // ---------------------------------------------------------------------------
41
51
  // taml - convert valid TAML string to a JavaScript value
42
52
  export var taml = function(text) {
53
+ var _, key, lLines, lMatches, level, line, prefix, str;
43
54
  debug(`enter taml(${oneline(text)})`);
44
55
  if (text == null) {
45
56
  debug("return undef from taml() - text is not defined");
46
57
  return undef;
47
58
  }
48
59
  assert(isTAML(text), `taml(): string ${oneline(text)} isn't TAML`);
60
+ lLines = (function() {
61
+ var i, len, ref, results;
62
+ ref = blockToArray(text);
63
+ results = [];
64
+ for (i = 0, len = ref.length; i < len; i++) {
65
+ line = ref[i];
66
+ [level, str] = splitLine(line);
67
+ prefix = ' '.repeat(level);
68
+ if (lMatches = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*)$/)) { // the key
69
+ [_, key, text] = lMatches;
70
+ if (isEmpty(text) || text.match(/\d+(?:\.\d*)$/)) {
71
+ results.push(prefix + str);
72
+ } else {
73
+ results.push(prefix + key + ':' + ' ' + squote(text));
74
+ }
75
+ } else {
76
+ results.push(prefix + str);
77
+ }
78
+ }
79
+ return results;
80
+ })();
49
81
  debug("return from taml()");
50
- return yaml.load(untabify(text, 1), {
82
+ return yaml.load(arrayToBlock(lLines), {
51
83
  skipInvalid: true
52
84
  });
53
85
  };