@jdeighan/coffee-utils 6.0.5 → 6.0.8

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": "6.0.5",
4
+ "version": "6.0.8",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -25,27 +25,31 @@ export class CallStack
25
25
  call: (funcName, hInfo) ->
26
26
 
27
27
  if doDebugStack
28
- LOG "[CALL #{funcName}]"
28
+ prefix = ' '.repeat(@lStack.length)
29
+ LOG "#{prefix}[--> CALL #{funcName}]"
29
30
  @lStack.push({funcName, hInfo})
30
31
  return
31
32
 
32
33
  # ........................................................................
33
34
 
34
- returnFrom: (funcName) ->
35
+ returnFrom: (fName) ->
35
36
 
36
- if doDebugStack
37
- LOG "[RETURN FROM #{funcName}]"
38
37
  if @lStack.length == 0
39
- LOG "returnFrom('#{funcName}') but stack is empty"
38
+ LOG "returnFrom('#{fName}') but stack is empty"
40
39
  return undef
41
- TOSfName = @lStack[@lStack.length-1].funcName
42
- if funcName == TOSfName
40
+ {funcName, hInfo} = @lStack.pop()
41
+ while (funcName != fName) && (@lStack.length > 0)
42
+ LOG "[MISSING RETURN FROM #{funcName} (return from #{fName})]"
43
43
  {funcName, hInfo} = @lStack.pop()
44
- assert funcName==TOSfName, "Bad func name on stack"
44
+
45
+ if doDebugStack
46
+ prefix = ' '.repeat(@lStack.length)
47
+ LOG "#{prefix}[<-- BACK #{fName}]"
48
+ if (funcName == fName)
45
49
  return hInfo
46
50
  else
47
51
  @dump()
48
- LOG "returnFrom('#{funcName}') but TOS is '#{TOSfName}'"
52
+ LOG "BAD returnFrom('#{fName}')"
49
53
  return undef
50
54
 
51
55
  # ........................................................................
package/src/call_stack.js CHANGED
@@ -28,30 +28,35 @@ export var CallStack = class CallStack {
28
28
 
29
29
  // ........................................................................
30
30
  call(funcName, hInfo) {
31
+ var prefix;
31
32
  if (doDebugStack) {
32
- LOG(`[CALL ${funcName}]`);
33
+ prefix = ' '.repeat(this.lStack.length);
34
+ LOG(`${prefix}[--> CALL ${funcName}]`);
33
35
  }
34
36
  this.lStack.push({funcName, hInfo});
35
37
  }
36
38
 
37
39
  // ........................................................................
38
- returnFrom(funcName) {
39
- var TOSfName, hInfo;
40
- if (doDebugStack) {
41
- LOG(`[RETURN FROM ${funcName}]`);
42
- }
40
+ returnFrom(fName) {
41
+ var funcName, hInfo, prefix;
43
42
  if (this.lStack.length === 0) {
44
- LOG(`returnFrom('${funcName}') but stack is empty`);
43
+ LOG(`returnFrom('${fName}') but stack is empty`);
45
44
  return undef;
46
45
  }
47
- TOSfName = this.lStack[this.lStack.length - 1].funcName;
48
- if (funcName === TOSfName) {
46
+ ({funcName, hInfo} = this.lStack.pop());
47
+ while ((funcName !== fName) && (this.lStack.length > 0)) {
48
+ LOG(`[MISSING RETURN FROM ${funcName} (return from ${fName})]`);
49
49
  ({funcName, hInfo} = this.lStack.pop());
50
- assert(funcName === TOSfName, "Bad func name on stack");
50
+ }
51
+ if (doDebugStack) {
52
+ prefix = ' '.repeat(this.lStack.length);
53
+ LOG(`${prefix}[<-- BACK ${fName}]`);
54
+ }
55
+ if (funcName === fName) {
51
56
  return hInfo;
52
57
  } else {
53
58
  this.dump();
54
- LOG(`returnFrom('${funcName}') but TOS is '${TOSfName}'`);
59
+ LOG(`BAD returnFrom('${fName}')`);
55
60
  return undef;
56
61
  }
57
62
  }
@@ -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
  };