@jdeighan/coffee-utils 4.1.35 → 5.0.0

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.
@@ -3,82 +3,81 @@
3
3
  import yaml from 'js-yaml'
4
4
 
5
5
  import {
6
- assert, undef, isNumber, isString, isHash, isFunction,
6
+ assert, undef, isNumber, isInteger, isString, isHash, isFunction,
7
7
  escapeStr, sep_eq,
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {blockToArray} from '@jdeighan/coffee-utils/block'
10
- import {tabify} from '@jdeighan/coffee-utils/indent'
10
+ import {tabify, untabify} from '@jdeighan/coffee-utils/indent'
11
11
 
12
12
  # --- This logger only ever gets passed a single string argument
13
- logger = console.log # for strings
13
+ logger = undef
14
+ export stringify = undef
15
+ export id = 42
16
+
17
+ # ---------------------------------------------------------------------------
18
+
19
+ export setStringifier = (func) ->
20
+
21
+ orgStringifier = stringify
22
+ assert isFunction(func), "setStringifier() arg is not a function"
23
+ stringify = func
24
+ return orgStringifier
25
+
26
+ # ---------------------------------------------------------------------------
27
+
28
+ export resetStringifier = () ->
29
+
30
+ setStringifier orderedStringify
14
31
 
15
32
  # ---------------------------------------------------------------------------
16
33
 
17
34
  export setLogger = (func) ->
18
35
 
19
36
  orgLogger = logger
20
- if func?
21
- assert isFunction(func), "setLogger() not a function"
22
- logger = func
23
- else
24
- logger = console.log
37
+ assert isFunction(func), "setLogger() arg is not a function"
38
+ logger = func
25
39
  return orgLogger
26
40
 
27
41
  # ---------------------------------------------------------------------------
28
42
 
29
- export tamlStringify = (obj) ->
43
+ export resetLogger = () ->
44
+
45
+ setLogger console.log
46
+
47
+ # ---------------------------------------------------------------------------
48
+
49
+ escReplacer = (name, value) ->
50
+
51
+ if ! isString(value)
52
+ return value
53
+ return escapeStr(value)
54
+
55
+ # ---------------------------------------------------------------------------
56
+
57
+ export tamlStringify = (obj, escape=false) ->
30
58
 
31
59
  str = yaml.dump(obj, {
32
60
  skipInvalid: true
33
61
  indent: 1
34
62
  sortKeys: false
35
63
  lineWidth: -1
64
+ replacer: if escape then escReplacer else (name,value) -> value
36
65
  })
37
- str = "---\n" + tabify(str)
38
- str = str.replace(/\t/g, ' ') # fr***ing Windows Terminal
39
- return str
66
+ return "---\n" + tabify(str, 1)
40
67
 
41
68
  # ---------------------------------------------------------------------------
42
- # the default stringifier
43
69
 
44
- export orderedStringify = (obj) ->
70
+ export orderedStringify = (obj, escape=false) ->
45
71
 
46
72
  str = yaml.dump(obj, {
47
73
  skipInvalid: true
48
74
  indent: 1
49
75
  sortKeys: true
50
76
  lineWidth: -1
77
+ replacer: if escape then escReplacer else (name,value) -> value
51
78
  })
52
- str = "---\n" + tabify(str)
53
- str = str.replace(/\t/g, ' ') # fr***ing Windows Terminal
54
- return str
55
-
56
- # ---------------------------------------------------------------------------
57
-
58
- export stringify = orderedStringify # for non-strings
59
-
60
- # ---------------------------------------------------------------------------
61
79
 
62
- export setStringifier = (func) ->
63
-
64
- if func?
65
- assert isFunction(func), "setStringifier() not a function"
66
- stringify = func
67
- else
68
- stringify = orderedStringify
69
- return
70
-
71
- # ---------------------------------------------------------------------------
72
-
73
- export currentLogger = () ->
74
-
75
- return logger
76
-
77
- # ---------------------------------------------------------------------------
78
-
79
- export currentStringifier = () ->
80
-
81
- return stringify
80
+ return "---\n" + tabify(str, 1)
82
81
 
83
82
  # ---------------------------------------------------------------------------
84
83
 
@@ -86,67 +85,60 @@ maxOneLine = 32
86
85
 
87
86
  # ---------------------------------------------------------------------------
88
87
 
89
- export log = (lArgs...) ->
90
- # --- (str, item, hOptions)
91
- # valid options:
92
- # logItem
88
+ export log = (item, hOptions=undef) ->
89
+ # --- valid options:
90
+ # label
93
91
  # prefix
94
- # itemPrefix
95
-
96
- if (lArgs.length==0)
97
- return
98
- str = lArgs[0]
99
- switch lArgs.length
100
- when 1
101
- logItem = false
102
- when 2
103
- item = lArgs[1]
104
- logItem = true
105
- else
106
- item = lArgs[1] # might not be logged, though
107
- hOptions = lArgs[2]
108
- assert isHash(hOptions), "log(): 3rd arg must be a hash"
109
- if hOptions.logItem?
110
- logItem = hOptions.logItem
92
+ # escape
111
93
 
94
+ assert isFunction(logger), "logger not properly set"
95
+ prefix = itemPrefix = label = ''
112
96
  if hOptions?
113
- if hOptions.prefix?
114
- prefix = hOptions.prefix
97
+ if isString(hOptions)
98
+ label = hOptions
115
99
  else
116
- prefix = ''
117
-
118
- if hOptions.itemPrefix?
119
- itemPrefix = hOptions.itemPrefix
100
+ assert isHash(hOptions), "log(): 2nd arg must be a string or hash"
101
+ if hOptions.prefix?
102
+ prefix = hOptions.prefix
103
+ if hOptions.itemPrefix?
104
+ itemPrefix = hOptions.itemPrefix
105
+ else
106
+ itemPrefix = prefix
107
+ if hOptions.label?
108
+ label = hOptions.label
109
+
110
+ if isString(item) && (label == '')
111
+ if hOptions? && hOptions.escape
112
+ logger "#{prefix}#{escapeStr(item)}"
120
113
  else
121
- itemPrefix = ''
122
- else
123
- prefix = itemPrefix = ''
114
+ logger "#{prefix}#{item}"
115
+ return
124
116
 
125
- if (! logItem)
126
- logger "#{prefix}#{str}"
127
- else if ! item?
128
- logger "#{prefix}#{str} = undef"
129
- else if isNumber(item)
130
- logger "#{prefix}#{str} = #{item}"
117
+ if (label == '')
118
+ label = 'ITEM'
119
+
120
+ if (item == undef)
121
+ logger "#{prefix}#{label} = undef"
131
122
  else if isString(item)
132
- esc = escapeStr(item)
133
- if (esc.length <= maxOneLine)
134
- logger "#{prefix}#{str} = '#{esc}'"
123
+ if (item.length <= maxOneLine)
124
+ logger "#{prefix}#{label} = '#{escapeStr(item)}'"
135
125
  else
136
- logger "#{prefix}#{str}:"
126
+ logger "#{prefix}#{label}:"
137
127
  logger "#{itemPrefix}#{sep_eq}"
138
128
  for line in blockToArray(item)
139
129
  logger "#{itemPrefix}#{escapeStr(line)}"
140
130
  logger "#{itemPrefix}#{sep_eq}"
131
+ else if isNumber(item)
132
+ logger "#{prefix}#{label} = #{item}"
141
133
  else
142
- # --- It's some type of object
143
- json = JSON.stringify(item)
144
- if (json.length <= maxOneLine)
145
- logger "#{prefix}#{str} = #{json}"
146
- else
147
- logger "#{prefix}#{str}:"
148
- for str in blockToArray(stringify(item))
149
- logger "#{itemPrefix} #{str}"
134
+ logger "#{prefix}#{label}:"
135
+ for str in blockToArray(stringify(item, true))
136
+ logger "#{itemPrefix}\t#{str}"
150
137
  return
151
138
 
152
139
  # ---------------------------------------------------------------------------
140
+
141
+ if ! loaded
142
+ setStringifier orderedStringify
143
+ resetLogger()
144
+ loaded = true
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 logger, maxOneLine;
3
+ var escReplacer, loaded, logger, maxOneLine;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -8,6 +8,7 @@ import {
8
8
  assert,
9
9
  undef,
10
10
  isNumber,
11
+ isInteger,
11
12
  isString,
12
13
  isHash,
13
14
  isFunction,
@@ -20,136 +21,131 @@ import {
20
21
  } from '@jdeighan/coffee-utils/block';
21
22
 
22
23
  import {
23
- tabify
24
+ tabify,
25
+ untabify
24
26
  } from '@jdeighan/coffee-utils/indent';
25
27
 
26
28
  // --- This logger only ever gets passed a single string argument
27
- logger = console.log; // for strings
29
+ logger = undef;
28
30
 
31
+ export var stringify = undef;
32
+
33
+ export var id = 42;
34
+
35
+ // ---------------------------------------------------------------------------
36
+ export var setStringifier = function(func) {
37
+ var orgStringifier;
38
+ orgStringifier = stringify;
39
+ assert(isFunction(func), "setStringifier() arg is not a function");
40
+ stringify = func;
41
+ return orgStringifier;
42
+ };
43
+
44
+ // ---------------------------------------------------------------------------
45
+ export var resetStringifier = function() {
46
+ return setStringifier(orderedStringify);
47
+ };
29
48
 
30
49
  // ---------------------------------------------------------------------------
31
50
  export var setLogger = function(func) {
32
51
  var orgLogger;
33
52
  orgLogger = logger;
34
- if (func != null) {
35
- assert(isFunction(func), "setLogger() not a function");
36
- logger = func;
37
- } else {
38
- logger = console.log;
39
- }
53
+ assert(isFunction(func), "setLogger() arg is not a function");
54
+ logger = func;
40
55
  return orgLogger;
41
56
  };
42
57
 
43
58
  // ---------------------------------------------------------------------------
44
- export var tamlStringify = function(obj) {
59
+ export var resetLogger = function() {
60
+ return setLogger(console.log);
61
+ };
62
+
63
+ // ---------------------------------------------------------------------------
64
+ escReplacer = function(name, value) {
65
+ if (!isString(value)) {
66
+ return value;
67
+ }
68
+ return escapeStr(value);
69
+ };
70
+
71
+ // ---------------------------------------------------------------------------
72
+ export var tamlStringify = function(obj, escape = false) {
45
73
  var str;
46
74
  str = yaml.dump(obj, {
47
75
  skipInvalid: true,
48
76
  indent: 1,
49
77
  sortKeys: false,
50
- lineWidth: -1
78
+ lineWidth: -1,
79
+ replacer: escape ? escReplacer : function(name, value) {
80
+ return value;
81
+ }
51
82
  });
52
- str = "---\n" + tabify(str);
53
- str = str.replace(/\t/g, ' '); // fr***ing Windows Terminal
54
- return str;
83
+ return "---\n" + tabify(str, 1);
55
84
  };
56
85
 
57
86
  // ---------------------------------------------------------------------------
58
- // the default stringifier
59
- export var orderedStringify = function(obj) {
87
+ export var orderedStringify = function(obj, escape = false) {
60
88
  var str;
61
89
  str = yaml.dump(obj, {
62
90
  skipInvalid: true,
63
91
  indent: 1,
64
92
  sortKeys: true,
65
- lineWidth: -1
93
+ lineWidth: -1,
94
+ replacer: escape ? escReplacer : function(name, value) {
95
+ return value;
96
+ }
66
97
  });
67
- str = "---\n" + tabify(str);
68
- str = str.replace(/\t/g, ' '); // fr***ing Windows Terminal
69
- return str;
70
- };
71
-
72
- // ---------------------------------------------------------------------------
73
- export var stringify = orderedStringify; // for non-strings
74
-
75
-
76
- // ---------------------------------------------------------------------------
77
- export var setStringifier = function(func) {
78
- if (func != null) {
79
- assert(isFunction(func), "setStringifier() not a function");
80
- stringify = func;
81
- } else {
82
- stringify = orderedStringify;
83
- }
84
- };
85
-
86
- // ---------------------------------------------------------------------------
87
- export var currentLogger = function() {
88
- return logger;
89
- };
90
-
91
- // ---------------------------------------------------------------------------
92
- export var currentStringifier = function() {
93
- return stringify;
98
+ return "---\n" + tabify(str, 1);
94
99
  };
95
100
 
96
101
  // ---------------------------------------------------------------------------
97
102
  maxOneLine = 32;
98
103
 
99
104
  // ---------------------------------------------------------------------------
100
- export var log = function(...lArgs) {
101
- var esc, hOptions, i, item, itemPrefix, j, json, len, len1, line, logItem, prefix, ref, ref1, str;
102
- // --- (str, item, hOptions)
103
- // valid options:
104
- // logItem
105
+ export var log = function(item, hOptions = undef) {
106
+ var i, itemPrefix, j, label, len, len1, line, prefix, ref, ref1, str;
107
+ // --- valid options:
108
+ // label
105
109
  // prefix
106
- // itemPrefix
107
- if (lArgs.length === 0) {
108
- return;
109
- }
110
- str = lArgs[0];
111
- switch (lArgs.length) {
112
- case 1:
113
- logItem = false;
114
- break;
115
- case 2:
116
- item = lArgs[1];
117
- logItem = true;
118
- break;
119
- default:
120
- item = lArgs[1];
121
- hOptions = lArgs[2];
122
- assert(isHash(hOptions), "log(): 3rd arg must be a hash");
123
- if (hOptions.logItem != null) {
124
- logItem = hOptions.logItem;
125
- }
126
- }
110
+ // escape
111
+ assert(isFunction(logger), "logger not properly set");
112
+ prefix = itemPrefix = label = '';
127
113
  if (hOptions != null) {
128
- if (hOptions.prefix != null) {
129
- prefix = hOptions.prefix;
114
+ if (isString(hOptions)) {
115
+ label = hOptions;
130
116
  } else {
131
- prefix = '';
117
+ assert(isHash(hOptions), "log(): 2nd arg must be a string or hash");
118
+ if (hOptions.prefix != null) {
119
+ prefix = hOptions.prefix;
120
+ }
121
+ if (hOptions.itemPrefix != null) {
122
+ itemPrefix = hOptions.itemPrefix;
123
+ } else {
124
+ itemPrefix = prefix;
125
+ }
126
+ if (hOptions.label != null) {
127
+ label = hOptions.label;
128
+ }
132
129
  }
133
- if (hOptions.itemPrefix != null) {
134
- itemPrefix = hOptions.itemPrefix;
130
+ }
131
+ if (isString(item) && (label === '')) {
132
+ if ((hOptions != null) && hOptions.escape) {
133
+ logger(`${prefix}${escapeStr(item)}`);
135
134
  } else {
136
- itemPrefix = '';
135
+ logger(`${prefix}${item}`);
137
136
  }
138
- } else {
139
- prefix = itemPrefix = '';
137
+ return;
140
138
  }
141
- if (!logItem) {
142
- logger(`${prefix}${str}`);
143
- } else if (item == null) {
144
- logger(`${prefix}${str} = undef`);
145
- } else if (isNumber(item)) {
146
- logger(`${prefix}${str} = ${item}`);
139
+ if (label === '') {
140
+ label = 'ITEM';
141
+ }
142
+ if (item === undef) {
143
+ logger(`${prefix}${label} = undef`);
147
144
  } else if (isString(item)) {
148
- esc = escapeStr(item);
149
- if (esc.length <= maxOneLine) {
150
- logger(`${prefix}${str} = '${esc}'`);
145
+ if (item.length <= maxOneLine) {
146
+ logger(`${prefix}${label} = '${escapeStr(item)}'`);
151
147
  } else {
152
- logger(`${prefix}${str}:`);
148
+ logger(`${prefix}${label}:`);
153
149
  logger(`${itemPrefix}${sep_eq}`);
154
150
  ref = blockToArray(item);
155
151
  for (i = 0, len = ref.length; i < len; i++) {
@@ -158,20 +154,21 @@ export var log = function(...lArgs) {
158
154
  }
159
155
  logger(`${itemPrefix}${sep_eq}`);
160
156
  }
157
+ } else if (isNumber(item)) {
158
+ logger(`${prefix}${label} = ${item}`);
161
159
  } else {
162
- // --- It's some type of object
163
- json = JSON.stringify(item);
164
- if (json.length <= maxOneLine) {
165
- logger(`${prefix}${str} = ${json}`);
166
- } else {
167
- logger(`${prefix}${str}:`);
168
- ref1 = blockToArray(stringify(item));
169
- for (j = 0, len1 = ref1.length; j < len1; j++) {
170
- str = ref1[j];
171
- logger(`${itemPrefix} ${str}`);
172
- }
160
+ logger(`${prefix}${label}:`);
161
+ ref1 = blockToArray(stringify(item, true));
162
+ for (j = 0, len1 = ref1.length; j < len1; j++) {
163
+ str = ref1[j];
164
+ logger(`${itemPrefix}\t${str}`);
173
165
  }
174
166
  }
175
167
  };
176
168
 
177
- // ---------------------------------------------------------------------------
169
+ if (!loaded) {
170
+ setStringifier(orderedStringify);
171
+ resetLogger();
172
+ }
173
+
174
+ loaded = true;