@jdeighan/coffee-utils 6.0.7 → 6.0.8

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.7",
4
+ "version": "6.0.8",
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
 
@@ -44,7 +44,7 @@ export class CallStack
44
44
 
45
45
  if doDebugStack
46
46
  prefix = ' '.repeat(@lStack.length)
47
- LOG "#{prefix}[< BACK #{fName}]"
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
  }
@@ -50,7 +50,7 @@ export var CallStack = class CallStack {
50
50
  }
51
51
  if (doDebugStack) {
52
52
  prefix = ' '.repeat(this.lStack.length);
53
- LOG(`${prefix}[< BACK ${fName}]`);
53
+ LOG(`${prefix}[<-- BACK ${fName}]`);
54
54
  }
55
55
  if (funcName === fName) {
56
56
  return hInfo;
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
  };