@sc-voice/tools 3.16.0 → 3.18.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sc-voice/tools",
3
- "version": "3.16.0",
3
+ "version": "3.18.0",
4
4
  "description": "Utilities for SC-Voice",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -32,6 +32,9 @@ export class ColorConsole {
32
32
  okColor1 = BRIGHT_GREEN,
33
33
  okColor2 = GREEN,
34
34
  valueColor = CYAN,
35
+ dateFormat = new Intl.DateTimeFormat(undefined, {
36
+ dateStyle: 'short',
37
+ }),
35
38
  precision = 3,
36
39
  write = (...args) => console.log.call(null, ...args),
37
40
  } = opts;
@@ -39,12 +42,13 @@ export class ColorConsole {
39
42
  Object.assign(this, {
40
43
  badColor1,
41
44
  badColor2,
45
+ dateFormat,
42
46
  fyiColor1,
43
47
  fyiColor2,
44
48
  okColor1,
45
49
  okColor2,
46
- valueColor,
47
50
  precision,
51
+ valueColor,
48
52
  write,
49
53
  });
50
54
  }
@@ -54,48 +58,74 @@ export class ColorConsole {
54
58
  return CC;
55
59
  }
56
60
 
61
+ valueOf(thing) {
62
+ const msg = 'c10e.valueOf';
63
+ let { precision } = this;
64
+ switch (typeof thing) {
65
+ case 'undefined':
66
+ return 'undefined';
67
+ case 'object': {
68
+ if (thing == null) {
69
+ return '' + thing;
70
+ }
71
+ if (thing instanceof Date) {
72
+ return this.dateFormat.format(thing);
73
+ }
74
+ if (
75
+ thing.constructor !== Object &&
76
+ typeof thing.toString === 'function'
77
+ ) {
78
+ return thing.toString();
79
+ }
80
+ return thing;
81
+ }
82
+ case 'string':
83
+ return thing;
84
+ case 'number': {
85
+ let v = thing.toFixed(precision);
86
+ if (thing === Number(v)) {
87
+ v = v.replace(/\.?0+$/, '');
88
+ }
89
+ return v;
90
+ }
91
+ default:
92
+ return JSON.stringify(thing);
93
+ }
94
+ } // valueOf
95
+
57
96
  color(textColor, ...things) {
58
- let { precision, valueColor } = this;
97
+ let { valueColor } = this;
59
98
  let label = '';
60
99
  let endColor = NO_COLOR;
61
100
  return things.reduce((a, thing) => {
62
101
  let newLabel = '';
102
+ let v = this.valueOf(thing);
63
103
  switch (typeof thing) {
64
104
  case 'object': {
65
- if (thing === null) {
66
- a.push(label + valueColor + 'null' + endColor);
67
- } else if (
68
- thing.constructor !== Object &&
69
- typeof thing.toString === 'function'
105
+ if (
106
+ thing == null ||
107
+ (thing.constructor !== Object &&
108
+ typeof thing.toString === 'function')
70
109
  ) {
71
- a.push(label + valueColor + thing.toString() + endColor);
110
+ a.push(label + valueColor + v + endColor);
72
111
  } else {
73
112
  label && a.push(label + endColor);
74
- a.push(thing);
113
+ a.push(v);
75
114
  }
76
115
  break;
77
116
  }
78
117
  case 'string':
79
118
  if (thing.endsWith(':')) {
80
- newLabel = textColor + thing;
119
+ newLabel = textColor + v;
81
120
  } else if (label) {
82
- a.push(label + valueColor + thing + endColor);
121
+ a.push(label + valueColor + v + endColor);
83
122
  } else {
84
- a.push(textColor + thing + endColor);
85
- }
86
- break;
87
- case 'number': {
88
- let v = thing.toFixed(precision);
89
- if (thing === Number(v)) {
90
- v = v.replace(/\.?0+$/, '');
123
+ a.push(textColor + v + endColor);
91
124
  }
92
- a.push(label + valueColor + v + endColor);
93
125
  break;
94
- }
126
+ case 'number':
95
127
  default:
96
- a.push(
97
- label + valueColor + JSON.stringify(thing) + endColor,
98
- );
128
+ a.push(label + valueColor + v + endColor);
99
129
  break;
100
130
  }
101
131
  label = newLabel;
@@ -138,5 +168,4 @@ export class ColorConsole {
138
168
  bad2(...rest) {
139
169
  this.write(...this.color(this.badColor2, ...rest));
140
170
  }
141
-
142
171
  }