@sc-voice/tools 3.16.0 → 3.17.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.17.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,72 @@ 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 'object': {
66
+ if (thing === null) {
67
+ return 'null';
68
+ }
69
+ if (thing instanceof Date) {
70
+ return this.dateFormat.format(thing);
71
+ }
72
+ if (
73
+ thing.constructor !== Object &&
74
+ typeof thing.toString === 'function'
75
+ ) {
76
+ return thing.toString();
77
+ }
78
+ return thing;
79
+ }
80
+ case 'string':
81
+ return thing;
82
+ case 'number': {
83
+ let v = thing.toFixed(precision);
84
+ if (thing === Number(v)) {
85
+ v = v.replace(/\.?0+$/, '');
86
+ }
87
+ return v;
88
+ }
89
+ default:
90
+ return JSON.stringify(thing);
91
+ }
92
+ } // valueOf
93
+
57
94
  color(textColor, ...things) {
58
- let { precision, valueColor } = this;
95
+ let { valueColor } = this;
59
96
  let label = '';
60
97
  let endColor = NO_COLOR;
61
98
  return things.reduce((a, thing) => {
62
99
  let newLabel = '';
100
+ let v = this.valueOf(thing);
63
101
  switch (typeof thing) {
64
102
  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'
103
+ if (
104
+ thing === null ||
105
+ (thing.constructor !== Object &&
106
+ typeof thing.toString === 'function')
70
107
  ) {
71
- a.push(label + valueColor + thing.toString() + endColor);
108
+ a.push(label + valueColor + v + endColor);
72
109
  } else {
73
110
  label && a.push(label + endColor);
74
- a.push(thing);
111
+ a.push(v);
75
112
  }
76
113
  break;
77
114
  }
78
115
  case 'string':
79
116
  if (thing.endsWith(':')) {
80
- newLabel = textColor + thing;
117
+ newLabel = textColor + v;
81
118
  } else if (label) {
82
- a.push(label + valueColor + thing + endColor);
119
+ a.push(label + valueColor + v + endColor);
83
120
  } 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+$/, '');
121
+ a.push(textColor + v + endColor);
91
122
  }
92
- a.push(label + valueColor + v + endColor);
93
123
  break;
94
- }
124
+ case 'number':
95
125
  default:
96
- a.push(
97
- label + valueColor + JSON.stringify(thing) + endColor,
98
- );
126
+ a.push(label + valueColor + v + endColor);
99
127
  break;
100
128
  }
101
129
  label = newLabel;
@@ -138,5 +166,4 @@ export class ColorConsole {
138
166
  bad2(...rest) {
139
167
  this.write(...this.color(this.badColor2, ...rest));
140
168
  }
141
-
142
169
  }