@jdeighan/coffee-utils 10.0.17 → 11.0.1

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": "10.0.17",
4
+ "version": "11.0.1",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -5,7 +5,6 @@ import {
5
5
  undef, defined, OL, escapeStr, deepCopy,
6
6
  isArray, isBoolean,
7
7
  } from '@jdeighan/coffee-utils'
8
- import {LOG} from '@jdeighan/coffee-utils/log'
9
8
 
10
9
  doDebugStack = false
11
10
 
@@ -29,7 +28,7 @@ export class CallStack
29
28
  reset: () ->
30
29
 
31
30
  if doDebugStack
32
- LOG "RESET STACK"
31
+ console.log "RESET STACK"
33
32
  @lStack = []
34
33
  return
35
34
 
@@ -48,7 +47,7 @@ export class CallStack
48
47
  assert isBoolean(isLogged), "missing isLogged"
49
48
 
50
49
  if doDebugStack
51
- LOG @indent() + "[--> ENTER #{funcName}]"
50
+ console.log @indent() + "[--> ENTER #{funcName}]"
52
51
 
53
52
  lMatches = funcName.match(///^
54
53
  ([A-Za-z_][A-Za-z0-9_]*)
@@ -101,13 +100,13 @@ export class CallStack
101
100
  returnFrom: (fName) ->
102
101
 
103
102
  if @lStack.length == 0
104
- LOG "ERROR: returnFrom('#{fName}') but stack is empty"
103
+ console.log "ERROR: returnFrom('#{fName}') but stack is empty"
105
104
  return
106
105
  {fullName, isLogged} = @lStack.pop()
107
106
  if doDebugStack
108
- LOG @indent() + "[<-- BACK #{fName}]"
107
+ console.log @indent() + "[<-- BACK #{fName}]"
109
108
  if (fullName != fName)
110
- LOG "ERROR: returnFrom('#{fName}') but TOS is #{fullName}"
109
+ console.log "ERROR: returnFrom('#{fName}') but TOS is #{fullName}"
111
110
  return
112
111
 
113
112
  return
package/src/call_stack.js CHANGED
@@ -17,10 +17,6 @@ import {
17
17
  isBoolean
18
18
  } from '@jdeighan/coffee-utils';
19
19
 
20
- import {
21
- LOG
22
- } from '@jdeighan/coffee-utils/log';
23
-
24
20
  doDebugStack = false;
25
21
 
26
22
  // ---------------------------------------------------------------------------
@@ -37,7 +33,7 @@ export var CallStack = class CallStack {
37
33
  // ........................................................................
38
34
  reset() {
39
35
  if (doDebugStack) {
40
- LOG("RESET STACK");
36
+ console.log("RESET STACK");
41
37
  }
42
38
  this.lStack = [];
43
39
  }
@@ -54,7 +50,7 @@ export var CallStack = class CallStack {
54
50
  assert(isArray(lArgs), "missing lArgs");
55
51
  assert(isBoolean(isLogged), "missing isLogged");
56
52
  if (doDebugStack) {
57
- LOG(this.indent() + `[--> ENTER ${funcName}]`);
53
+ console.log(this.indent() + `[--> ENTER ${funcName}]`);
58
54
  }
59
55
  lMatches = funcName.match(/^([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?$/);
60
56
  assert(defined(lMatches), `Bad funcName: ${OL(funcName)}`);
@@ -106,15 +102,15 @@ export var CallStack = class CallStack {
106
102
  returnFrom(fName) {
107
103
  var fullName, isLogged;
108
104
  if (this.lStack.length === 0) {
109
- LOG(`ERROR: returnFrom('${fName}') but stack is empty`);
105
+ console.log(`ERROR: returnFrom('${fName}') but stack is empty`);
110
106
  return;
111
107
  }
112
108
  ({fullName, isLogged} = this.lStack.pop());
113
109
  if (doDebugStack) {
114
- LOG(this.indent() + `[<-- BACK ${fName}]`);
110
+ console.log(this.indent() + `[<-- BACK ${fName}]`);
115
111
  }
116
112
  if (fullName !== fName) {
117
- LOG(`ERROR: returnFrom('${fName}') but TOS is ${fullName}`);
113
+ console.log(`ERROR: returnFrom('${fName}') but TOS is ${fullName}`);
118
114
  return;
119
115
  }
120
116
  }
@@ -69,7 +69,7 @@ export isUndented = (line) ->
69
69
 
70
70
  # ---------------------------------------------------------------------------
71
71
  # indented - add indentation to each string in a block or array
72
- # - always returns a string
72
+ # - returns the same type as input, i.e. array or string
73
73
 
74
74
  export indented = (input, level=1, oneIndent="\t") ->
75
75
 
@@ -78,14 +78,20 @@ export indented = (input, level=1, oneIndent="\t") ->
78
78
  return input
79
79
 
80
80
  toAdd = indentation(level, oneIndent)
81
- lInputLines = toArray(input)
82
81
 
83
- lLines = for line in lInputLines
82
+ # --- NOTE: toArray(input) just returns input if it's an array
83
+ # else it splits the string into an array of lines
84
+ lLines = for line in toArray(input)
84
85
  if isEmpty(line)
85
86
  ""
86
87
  else
87
88
  "#{toAdd}#{line}"
88
- return arrayToBlock(lLines)
89
+ if isArray(input)
90
+ return lLines
91
+ else if isString(input)
92
+ return toBlock(lLines)
93
+ else
94
+ croak "Invalid input; #{OL(input)}"
89
95
 
90
96
  # ---------------------------------------------------------------------------
91
97
  # undented - string with 1st line indentation removed for each line
@@ -82,20 +82,22 @@ export var isUndented = function(line) {
82
82
 
83
83
  // ---------------------------------------------------------------------------
84
84
  // indented - add indentation to each string in a block or array
85
- // - always returns a string
85
+ // - returns the same type as input, i.e. array or string
86
86
  export var indented = function(input, level = 1, oneIndent = "\t") {
87
- var lInputLines, lLines, line, toAdd;
87
+ var lLines, line, toAdd;
88
88
  assert(level >= 0, "indented(): negative level");
89
89
  if (level === 0) {
90
90
  return input;
91
91
  }
92
92
  toAdd = indentation(level, oneIndent);
93
- lInputLines = toArray(input);
93
+ // --- NOTE: toArray(input) just returns input if it's an array
94
+ // else it splits the string into an array of lines
94
95
  lLines = (function() {
95
- var i, len1, results;
96
+ var i, len1, ref, results;
97
+ ref = toArray(input);
96
98
  results = [];
97
- for (i = 0, len1 = lInputLines.length; i < len1; i++) {
98
- line = lInputLines[i];
99
+ for (i = 0, len1 = ref.length; i < len1; i++) {
100
+ line = ref[i];
99
101
  if (isEmpty(line)) {
100
102
  results.push("");
101
103
  } else {
@@ -104,7 +106,13 @@ export var indented = function(input, level = 1, oneIndent = "\t") {
104
106
  }
105
107
  return results;
106
108
  })();
107
- return arrayToBlock(lLines);
109
+ if (isArray(input)) {
110
+ return lLines;
111
+ } else if (isString(input)) {
112
+ return toBlock(lLines);
113
+ } else {
114
+ return croak(`Invalid input; ${OL(input)}`);
115
+ }
108
116
  };
109
117
 
110
118
  // ---------------------------------------------------------------------------
@@ -1,7 +1,5 @@
1
1
  # log_utils.coffee
2
2
 
3
- import yaml from 'js-yaml'
4
-
5
3
  import {assert, error, croak} from '@jdeighan/unit-tester/utils'
6
4
  import {
7
5
  undef, isNumber, isInteger, isString, isHash, isFunction,
@@ -11,6 +9,7 @@ import {blockToArray} from '@jdeighan/coffee-utils/block'
11
9
  import {
12
10
  tabify, untabify, indentation, indented,
13
11
  } from '@jdeighan/coffee-utils/indent'
12
+ import {toTAML} from '@jdeighan/coffee-utils/taml'
14
13
 
15
14
  # --- This logger only ever gets passed a single string argument
16
15
  putstr = undef
@@ -96,39 +95,24 @@ export resetLogger = () ->
96
95
 
97
96
  # ---------------------------------------------------------------------------
98
97
 
99
- escReplacer = (name, value) ->
100
-
101
- if ! isString(value)
102
- return value
103
- return escapeStr(value)
104
-
105
- # ---------------------------------------------------------------------------
106
-
107
98
  export tamlStringify = (obj, escape=false) ->
108
99
 
109
- str = yaml.dump(obj, {
110
- skipInvalid: true
111
- indent: 1
100
+ return toTAML(obj, {
101
+ useTabs: false
112
102
  sortKeys: false
113
- lineWidth: -1
114
- replacer: if escape then escReplacer else (name,value) -> value
103
+ escape
115
104
  })
116
- return "---\n" + str
117
105
 
118
106
  # ---------------------------------------------------------------------------
119
107
 
120
108
  export orderedStringify = (obj, escape=false) ->
121
109
 
122
- str = yaml.dump(obj, {
123
- skipInvalid: true
124
- indent: 1
110
+ return toTAML(obj, {
111
+ useTabs: false
125
112
  sortKeys: true
126
- lineWidth: 40
127
- replacer: if escape then escReplacer else (name,value) -> value
113
+ escape
128
114
  })
129
115
 
130
- return "---\n" + str
131
-
132
116
  # ---------------------------------------------------------------------------
133
117
 
134
118
  maxOneLine = 32
package/src/log_utils.js CHANGED
@@ -1,8 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // log_utils.coffee
3
- var doDebugLog, escReplacer, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
4
-
5
- import yaml from 'js-yaml';
3
+ var doDebugLog, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
6
4
 
7
5
  import {
8
6
  assert,
@@ -35,6 +33,10 @@ import {
35
33
  indented
36
34
  } from '@jdeighan/coffee-utils/indent';
37
35
 
36
+ import {
37
+ toTAML
38
+ } from '@jdeighan/coffee-utils/taml';
39
+
38
40
  // --- This logger only ever gets passed a single string argument
39
41
  putstr = undef;
40
42
 
@@ -122,42 +124,22 @@ export var resetLogger = function() {
122
124
  return setLogger(console.log);
123
125
  };
124
126
 
125
- // ---------------------------------------------------------------------------
126
- escReplacer = function(name, value) {
127
- if (!isString(value)) {
128
- return value;
129
- }
130
- return escapeStr(value);
131
- };
132
-
133
127
  // ---------------------------------------------------------------------------
134
128
  export var tamlStringify = function(obj, escape = false) {
135
- var str;
136
- str = yaml.dump(obj, {
137
- skipInvalid: true,
138
- indent: 1,
129
+ return toTAML(obj, {
130
+ useTabs: false,
139
131
  sortKeys: false,
140
- lineWidth: -1,
141
- replacer: escape ? escReplacer : function(name, value) {
142
- return value;
143
- }
132
+ escape
144
133
  });
145
- return "---\n" + str;
146
134
  };
147
135
 
148
136
  // ---------------------------------------------------------------------------
149
137
  export var orderedStringify = function(obj, escape = false) {
150
- var str;
151
- str = yaml.dump(obj, {
152
- skipInvalid: true,
153
- indent: 1,
138
+ return toTAML(obj, {
139
+ useTabs: false,
154
140
  sortKeys: true,
155
- lineWidth: 40,
156
- replacer: escape ? escReplacer : function(name, value) {
157
- return value;
158
- }
141
+ escape
159
142
  });
160
- return "---\n" + str;
161
143
  };
162
144
 
163
145
  // ---------------------------------------------------------------------------
package/src/taml.coffee CHANGED
@@ -4,7 +4,7 @@ import yaml from 'js-yaml'
4
4
 
5
5
  import {assert, error, croak} from '@jdeighan/unit-tester/utils'
6
6
  import {
7
- undef, oneline, isString, chomp,
7
+ undef, oneline, isString, chomp, escapeStr,
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {untabify, tabify, splitLine} from '@jdeighan/coffee-utils/indent'
10
10
  import {slurp} from '@jdeighan/coffee-utils/fs'
@@ -64,9 +64,24 @@ export fromTAML = taml
64
64
 
65
65
  # ---------------------------------------------------------------------------
66
66
 
67
- export toTAML = (x, useTabs=false) ->
67
+ escReplacer = (name, value) ->
68
68
 
69
- str = yaml.dump(x, {indent:3})
69
+ if ! isString(value)
70
+ return value
71
+ return escapeStr(value)
72
+
73
+ # ---------------------------------------------------------------------------
74
+
75
+ export toTAML = (obj, hOptions={}) ->
76
+
77
+ {useTabs, sortKeys, escape} = hOptions
78
+ str = yaml.dump(obj, {
79
+ skipInvalid: true
80
+ indent: 3
81
+ sortKeys: !!sortKeys
82
+ lineWidth: -1
83
+ replacer: if !!escape then escReplacer else (name,value) -> value
84
+ })
70
85
  if useTabs
71
86
  str = str.replace(/ /g, "\t")
72
87
  return "---\n" + chomp(str)
package/src/taml.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // taml.coffee
3
- var squote;
3
+ var escReplacer, squote;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -14,7 +14,8 @@ import {
14
14
  undef,
15
15
  oneline,
16
16
  isString,
17
- chomp
17
+ chomp,
18
+ escapeStr
18
19
  } from '@jdeighan/coffee-utils';
19
20
 
20
21
  import {
@@ -89,10 +90,25 @@ export var taml = function(text) {
89
90
  export var fromTAML = taml;
90
91
 
91
92
  // ---------------------------------------------------------------------------
92
- export var toTAML = function(x, useTabs = false) {
93
- var str;
94
- str = yaml.dump(x, {
95
- indent: 3
93
+ escReplacer = function(name, value) {
94
+ if (!isString(value)) {
95
+ return value;
96
+ }
97
+ return escapeStr(value);
98
+ };
99
+
100
+ // ---------------------------------------------------------------------------
101
+ export var toTAML = function(obj, hOptions = {}) {
102
+ var escape, sortKeys, str, useTabs;
103
+ ({useTabs, sortKeys, escape} = hOptions);
104
+ str = yaml.dump(obj, {
105
+ skipInvalid: true,
106
+ indent: 3,
107
+ sortKeys: !!sortKeys,
108
+ lineWidth: -1,
109
+ replacer: !!escape ? escReplacer : function(name, value) {
110
+ return value;
111
+ }
96
112
  });
97
113
  if (useTabs) {
98
114
  str = str.replace(/ /g, "\t");