@jdeighan/coffee-utils 6.0.9 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,17 +4,15 @@ import yaml from 'js-yaml'
4
4
 
5
5
  import {
6
6
  assert, undef, isNumber, isInteger, isString, isHash, isFunction,
7
- escapeStr, sep_eq, sep_dash
7
+ escapeStr, sep_eq, sep_dash, pass
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
10
  import {tabify, untabify, indentation} from '@jdeighan/coffee-utils/indent'
11
- import {arrow, hasArrow, removeArrow} from '@jdeighan/coffee-utils/arrow'
12
11
 
13
12
  # --- This logger only ever gets passed a single string argument
14
13
  putstr = undef
15
14
 
16
15
  export stringify = undef
17
- objSep = '-'.repeat(42)
18
16
 
19
17
  # ---------------------------------------------------------------------------
20
18
  # This is useful for debugging and easy to remove after debugging
@@ -23,13 +21,13 @@ export LOG = (lArgs...) ->
23
21
 
24
22
  [label, item] = lArgs
25
23
  if lArgs.length > 1
26
- console.log objSep
24
+ console.log sep_dash
27
25
  if item?
28
26
  console.log "#{label}:"
29
27
  console.log untabify(orderedStringify(item))
30
28
  else
31
29
  console.log "[#{label}]: UNDEFINED"
32
- console.log objSep
30
+ console.log sep_dash
33
31
  else
34
32
  console.log label
35
33
  return
@@ -107,61 +105,77 @@ maxOneLine = 32
107
105
 
108
106
  # ---------------------------------------------------------------------------
109
107
 
110
- export log = (item, hOptions={}) ->
108
+ fixStr = (str) ->
109
+
110
+ if !str
111
+ return ''
112
+
113
+ # --- If putstr is console.log, we'll convert TAB char to 3 spaces
114
+ if putstr == console.log
115
+ return untabify(str)
116
+ else
117
+ return str
118
+
119
+ # ---------------------------------------------------------------------------
120
+
121
+ export log = (str, hOptions={}) ->
111
122
  # --- valid options:
112
- # label
113
123
  # prefix
114
- # itemPrefix
115
- # escape
116
124
 
117
125
  assert isFunction(putstr), "putstr not properly set"
118
- if isString(hOptions)
119
- label = hOptions
120
- prefix = itemPrefix = ''
121
- else
122
- assert isHash(hOptions), "log(): 2nd arg must be a string or hash"
123
- label = hOptions.label || ''
124
- prefix = hOptions.prefix || ''
125
- itemPrefix = hOptions.itemPrefix || prefix || ''
126
+ assert isString(str), "log(): not a string"
127
+ assert isHash(hOptions), "log(): arg 2 not a hash"
126
128
 
127
- # --- If putstr is console.log, we'll convert TAB char to 3 spaces
128
- if putstr == console.log
129
- label = untabify(label)
130
- prefix = untabify(prefix)
131
- itemPrefix = untabify(itemPrefix)
129
+ prefix = fixStr(hOptions.prefix)
130
+ putstr "#{prefix}#{str}"
131
+ return
132
132
 
133
- if isString(item) && (label == '')
134
- if hOptions.escape
135
- putstr "#{prefix}#{escapeStr(item)}"
136
- else
137
- putstr "#{prefix}#{item}"
138
- return
133
+ # ---------------------------------------------------------------------------
139
134
 
140
- if (label == '')
141
- label = 'ITEM'
135
+ export logItem = (label, item, hOptions={}) ->
136
+ # --- valid options:
137
+ # prefix
138
+ # itemPrefix
139
+
140
+ assert isFunction(putstr), "putstr not properly set"
141
+ assert !label || isString(label), "label a non-string"
142
+ assert isHash(hOptions), "arg 3 not a hash"
143
+
144
+ label = fixStr(label)
145
+ prefix = fixStr(hOptions.prefix)
146
+ itemPrefix = fixStr(hOptions.itemPrefix || prefix)
147
+
148
+ labelStr = if label then "#{label} = " else ""
142
149
 
143
150
  if (item == undef)
144
- putstr "#{prefix}#{label} = undef"
151
+ putstr "#{prefix}#{labelStr}undef"
145
152
  else if isString(item)
146
153
  if (item.length <= maxOneLine)
147
- putstr "#{prefix}#{label} = '#{escapeStr(item)}'"
154
+ putstr "#{prefix}#{labelStr}'#{escapeStr(item)}'"
148
155
  else
149
- putstr "#{prefix}#{label}:"
150
- putstr "#{itemPrefix}#{sep_eq}"
151
- for line in blockToArray(item)
152
- putstr "#{itemPrefix}#{escapeStr(line)}"
153
- putstr "#{itemPrefix}#{sep_eq}"
156
+ if label
157
+ putstr "#{prefix}#{label}:"
158
+ putBlock item, itemPrefix
154
159
  else if isNumber(item)
155
- putstr "#{prefix}#{label} = #{item}"
160
+ putstr "#{prefix}#{labelStr}#{item}"
156
161
  else
157
- putstr "#{removeArrow(prefix, true)}#{objSep}"
158
- putstr "#{prefix}#{label}:"
159
- for str in blockToArray(stringify(item, true))
160
- if putstr == console.log
161
- putstr "#{itemPrefix} #{untabify(str)}"
162
- else
163
- putstr "#{itemPrefix}#{indentation(1)}#{str}"
164
- putstr "#{removeArrow(prefix, false)}#{objSep}"
162
+ putstr "#{itemPrefix}#{sep_dash}"
163
+ if label
164
+ putstr "#{prefix}#{label}:"
165
+ for str in blockToArray(stringify(item, true)) # escape special chars
166
+ putstr "#{itemPrefix}#{indentation(1)}#{fixStr(str)}"
167
+ putstr "#{itemPrefix}#{sep_dash}"
168
+
169
+ return
170
+
171
+ # ---------------------------------------------------------------------------
172
+
173
+ putBlock = (item, prefix='') ->
174
+
175
+ putstr "#{prefix}#{sep_eq}"
176
+ for line in blockToArray(item)
177
+ putstr "#{prefix}#{escapeStr(line)}"
178
+ putstr "#{prefix}#{sep_eq}"
165
179
  return
166
180
 
167
181
  # ---------------------------------------------------------------------------
package/src/log_utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // log_utils.coffee
3
- var escReplacer, loaded, maxOneLine, objSep, putstr;
3
+ var escReplacer, fixStr, loaded, maxOneLine, putBlock, putstr;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -14,7 +14,8 @@ import {
14
14
  isFunction,
15
15
  escapeStr,
16
16
  sep_eq,
17
- sep_dash
17
+ sep_dash,
18
+ pass
18
19
  } from '@jdeighan/coffee-utils';
19
20
 
20
21
  import {
@@ -27,33 +28,25 @@ import {
27
28
  indentation
28
29
  } from '@jdeighan/coffee-utils/indent';
29
30
 
30
- import {
31
- arrow,
32
- hasArrow,
33
- removeArrow
34
- } from '@jdeighan/coffee-utils/arrow';
35
-
36
31
  // --- This logger only ever gets passed a single string argument
37
32
  putstr = undef;
38
33
 
39
34
  export var stringify = undef;
40
35
 
41
- objSep = '-'.repeat(42);
42
-
43
36
  // ---------------------------------------------------------------------------
44
37
  // This is useful for debugging and easy to remove after debugging
45
38
  export var LOG = function(...lArgs) {
46
39
  var item, label;
47
40
  [label, item] = lArgs;
48
41
  if (lArgs.length > 1) {
49
- console.log(objSep);
42
+ console.log(sep_dash);
50
43
  if (item != null) {
51
44
  console.log(`${label}:`);
52
45
  console.log(untabify(orderedStringify(item)));
53
46
  } else {
54
47
  console.log(`[${label}]: UNDEFINED`);
55
48
  }
56
- console.log(objSep);
49
+ console.log(sep_dash);
57
50
  } else {
58
51
  console.log(label);
59
52
  }
@@ -132,71 +125,81 @@ export var orderedStringify = function(obj, escape = false) {
132
125
  maxOneLine = 32;
133
126
 
134
127
  // ---------------------------------------------------------------------------
135
- export var log = function(item, hOptions = {}) {
136
- var i, itemPrefix, j, label, len, len1, line, prefix, ref, ref1, str;
137
- // --- valid options:
138
- // label
139
- // prefix
140
- // itemPrefix
141
- // escape
142
- assert(isFunction(putstr), "putstr not properly set");
143
- if (isString(hOptions)) {
144
- label = hOptions;
145
- prefix = itemPrefix = '';
146
- } else {
147
- assert(isHash(hOptions), "log(): 2nd arg must be a string or hash");
148
- label = hOptions.label || '';
149
- prefix = hOptions.prefix || '';
150
- itemPrefix = hOptions.itemPrefix || prefix || '';
128
+ fixStr = function(str) {
129
+ if (!str) {
130
+ return '';
151
131
  }
152
132
  // --- If putstr is console.log, we'll convert TAB char to 3 spaces
153
133
  if (putstr === console.log) {
154
- label = untabify(label);
155
- prefix = untabify(prefix);
156
- itemPrefix = untabify(itemPrefix);
157
- }
158
- if (isString(item) && (label === '')) {
159
- if (hOptions.escape) {
160
- putstr(`${prefix}${escapeStr(item)}`);
161
- } else {
162
- putstr(`${prefix}${item}`);
163
- }
164
- return;
165
- }
166
- if (label === '') {
167
- label = 'ITEM';
134
+ return untabify(str);
135
+ } else {
136
+ return str;
168
137
  }
138
+ };
139
+
140
+ // ---------------------------------------------------------------------------
141
+ export var log = function(str, hOptions = {}) {
142
+ var prefix;
143
+ // --- valid options:
144
+ // prefix
145
+ assert(isFunction(putstr), "putstr not properly set");
146
+ assert(isString(str), "log(): not a string");
147
+ assert(isHash(hOptions), "log(): arg 2 not a hash");
148
+ prefix = fixStr(hOptions.prefix);
149
+ putstr(`${prefix}${str}`);
150
+ };
151
+
152
+ // ---------------------------------------------------------------------------
153
+ export var logItem = function(label, item, hOptions = {}) {
154
+ var i, itemPrefix, labelStr, len, prefix, ref, str;
155
+ // --- valid options:
156
+ // prefix
157
+ // itemPrefix
158
+ assert(isFunction(putstr), "putstr not properly set");
159
+ assert(!label || isString(label), "label a non-string");
160
+ assert(isHash(hOptions), "arg 3 not a hash");
161
+ label = fixStr(label);
162
+ prefix = fixStr(hOptions.prefix);
163
+ itemPrefix = fixStr(hOptions.itemPrefix || prefix);
164
+ labelStr = label ? `${label} = ` : "";
169
165
  if (item === undef) {
170
- putstr(`${prefix}${label} = undef`);
166
+ putstr(`${prefix}${labelStr}undef`);
171
167
  } else if (isString(item)) {
172
168
  if (item.length <= maxOneLine) {
173
- putstr(`${prefix}${label} = '${escapeStr(item)}'`);
169
+ putstr(`${prefix}${labelStr}'${escapeStr(item)}'`);
174
170
  } else {
175
- putstr(`${prefix}${label}:`);
176
- putstr(`${itemPrefix}${sep_eq}`);
177
- ref = blockToArray(item);
178
- for (i = 0, len = ref.length; i < len; i++) {
179
- line = ref[i];
180
- putstr(`${itemPrefix}${escapeStr(line)}`);
171
+ if (label) {
172
+ putstr(`${prefix}${label}:`);
181
173
  }
182
- putstr(`${itemPrefix}${sep_eq}`);
174
+ putBlock(item, itemPrefix);
183
175
  }
184
176
  } else if (isNumber(item)) {
185
- putstr(`${prefix}${label} = ${item}`);
177
+ putstr(`${prefix}${labelStr}${item}`);
186
178
  } else {
187
- putstr(`${removeArrow(prefix, true)}${objSep}`);
188
- putstr(`${prefix}${label}:`);
189
- ref1 = blockToArray(stringify(item, true));
190
- for (j = 0, len1 = ref1.length; j < len1; j++) {
191
- str = ref1[j];
192
- if (putstr === console.log) {
193
- putstr(`${itemPrefix} ${untabify(str)}`);
194
- } else {
195
- putstr(`${itemPrefix}${indentation(1)}${str}`);
196
- }
179
+ putstr(`${itemPrefix}${sep_dash}`);
180
+ if (label) {
181
+ putstr(`${prefix}${label}:`);
182
+ }
183
+ ref = blockToArray(stringify(item, true));
184
+ // escape special chars
185
+ for (i = 0, len = ref.length; i < len; i++) {
186
+ str = ref[i];
187
+ putstr(`${itemPrefix}${indentation(1)}${fixStr(str)}`);
197
188
  }
198
- putstr(`${removeArrow(prefix, false)}${objSep}`);
189
+ putstr(`${itemPrefix}${sep_dash}`);
190
+ }
191
+ };
192
+
193
+ // ---------------------------------------------------------------------------
194
+ putBlock = function(item, prefix = '') {
195
+ var i, len, line, ref;
196
+ putstr(`${prefix}${sep_eq}`);
197
+ ref = blockToArray(item);
198
+ for (i = 0, len = ref.length; i < len; i++) {
199
+ line = ref[i];
200
+ putstr(`${prefix}${escapeStr(line)}`);
199
201
  }
202
+ putstr(`${prefix}${sep_eq}`);
200
203
  };
201
204
 
202
205
  if (!loaded) {
package/src/taml.coffee CHANGED
@@ -6,7 +6,6 @@ import {
6
6
  assert, undef, oneline, isString,
7
7
  } from '@jdeighan/coffee-utils'
8
8
  import {untabify, tabify, splitLine} from '@jdeighan/coffee-utils/indent'
9
- import {log, tamlStringify} from '@jdeighan/coffee-utils/log'
10
9
  import {slurp} from '@jdeighan/coffee-utils/fs'
11
10
  import {debug} from '@jdeighan/coffee-utils/debug'
12
11
  import {
package/src/taml.js CHANGED
@@ -17,11 +17,6 @@ import {
17
17
  splitLine
18
18
  } from '@jdeighan/coffee-utils/indent';
19
19
 
20
- import {
21
- log,
22
- tamlStringify
23
- } from '@jdeighan/coffee-utils/log';
24
-
25
20
  import {
26
21
  slurp
27
22
  } from '@jdeighan/coffee-utils/fs';