rsformat 0.2.1 → 0.2.2
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/lib/format.js +32 -24
- package/package.json +5 -6
package/lib/format.js
CHANGED
|
@@ -57,21 +57,22 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
57
57
|
throw new Error(`parameter ${$param_number || param_counter - 1} either NaN or not provided`);
|
|
58
58
|
}
|
|
59
59
|
let param_type = typeof param;
|
|
60
|
+
let true_length = -1;
|
|
60
61
|
// Process parameter type
|
|
61
62
|
switch ($type) {
|
|
62
|
-
case
|
|
63
|
+
case 'o':
|
|
63
64
|
param = param.toString(8);
|
|
64
65
|
break;
|
|
65
|
-
case
|
|
66
|
+
case 'x':
|
|
66
67
|
param = param.toString(16);
|
|
67
68
|
break;
|
|
68
|
-
case
|
|
69
|
+
case 'X':
|
|
69
70
|
param = param.toString(16).toUpperCase();
|
|
70
71
|
break;
|
|
71
|
-
case
|
|
72
|
+
case 'b':
|
|
72
73
|
param = param.toString(2);
|
|
73
74
|
break;
|
|
74
|
-
case
|
|
75
|
+
case 'e':
|
|
75
76
|
switch (param_type) {
|
|
76
77
|
case 'number':
|
|
77
78
|
param = param.toExponential();
|
|
@@ -87,7 +88,7 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
87
88
|
break;
|
|
88
89
|
}
|
|
89
90
|
break;
|
|
90
|
-
case
|
|
91
|
+
case 'E':
|
|
91
92
|
switch (param_type) {
|
|
92
93
|
case 'number':
|
|
93
94
|
param = param.toExponential().toUpperCase();
|
|
@@ -103,7 +104,15 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
103
104
|
break;
|
|
104
105
|
}
|
|
105
106
|
break;
|
|
106
|
-
case
|
|
107
|
+
case '?':
|
|
108
|
+
// Do not force sign or align to precision when using inspect
|
|
109
|
+
$sign = undefined;
|
|
110
|
+
$precision = undefined;
|
|
111
|
+
true_length = (0, node_util_1.inspect)(param, {
|
|
112
|
+
depth: Infinity,
|
|
113
|
+
colors: false,
|
|
114
|
+
compact: $pretty !== '#'
|
|
115
|
+
}).length;
|
|
107
116
|
param = (0, node_util_1.inspect)(param, {
|
|
108
117
|
depth: Infinity,
|
|
109
118
|
colors: options.colors,
|
|
@@ -115,11 +124,14 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
115
124
|
break;
|
|
116
125
|
}
|
|
117
126
|
;
|
|
127
|
+
if (true_length == -1) {
|
|
128
|
+
true_length = param.length;
|
|
129
|
+
}
|
|
118
130
|
// Compute radix-point precision on numbers
|
|
119
131
|
if (param_type == 'number' && $precision) {
|
|
120
|
-
let [pre, post] = param.split(
|
|
132
|
+
let [pre, post] = param.split('.');
|
|
121
133
|
if (post === undefined) {
|
|
122
|
-
post =
|
|
134
|
+
post = '';
|
|
123
135
|
}
|
|
124
136
|
let precision = +$precision.substring(1, $precision.length);
|
|
125
137
|
if (post.length > precision) {
|
|
@@ -129,7 +141,7 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
129
141
|
while (post.length < precision) {
|
|
130
142
|
post = post + '0';
|
|
131
143
|
}
|
|
132
|
-
param = pre +
|
|
144
|
+
param = pre + '.' + post;
|
|
133
145
|
}
|
|
134
146
|
let width;
|
|
135
147
|
if ($width === undefined) {
|
|
@@ -166,31 +178,27 @@ function fmt_raw(str, params, options = { colors: false }) {
|
|
|
166
178
|
}
|
|
167
179
|
param = maybe_sign + param;
|
|
168
180
|
}
|
|
169
|
-
if (!filled && width) {
|
|
181
|
+
if (!filled && width > true_length) {
|
|
170
182
|
// Compute fill/align
|
|
171
183
|
$align_direction ||= '>';
|
|
172
184
|
$fill_character ||= ' ';
|
|
185
|
+
let left = '';
|
|
186
|
+
let right = '';
|
|
187
|
+
let diff = width - true_length;
|
|
173
188
|
switch ($align_direction) {
|
|
174
189
|
case '>':
|
|
175
|
-
|
|
176
|
-
param = $fill_character + param;
|
|
177
|
-
}
|
|
190
|
+
left = $fill_character.repeat(diff);
|
|
178
191
|
break;
|
|
179
192
|
case '<':
|
|
180
|
-
|
|
181
|
-
param = param + $fill_character;
|
|
182
|
-
}
|
|
193
|
+
right = $fill_character.repeat(diff);
|
|
183
194
|
break;
|
|
184
195
|
case '^':
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// Prioritise right-aligment on uneven alignment
|
|
189
|
-
if (width - param.length == 1) {
|
|
190
|
-
param = $fill_character + param;
|
|
191
|
-
}
|
|
196
|
+
left = $fill_character.repeat(diff - diff / 2);
|
|
197
|
+
// Prioritise right-aligment on uneven length
|
|
198
|
+
right = $fill_character.repeat(diff / 2 + diff % 2);
|
|
192
199
|
break;
|
|
193
200
|
}
|
|
201
|
+
param = left + param + right;
|
|
194
202
|
}
|
|
195
203
|
return param;
|
|
196
204
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rsformat",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib",
|
|
@@ -16,10 +16,6 @@
|
|
|
16
16
|
"default": "./lib/*.js"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"scripts": {
|
|
20
|
-
"build": "tsc",
|
|
21
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
-
},
|
|
23
19
|
"keywords": [
|
|
24
20
|
"fmt",
|
|
25
21
|
"format",
|
|
@@ -36,5 +32,8 @@
|
|
|
36
32
|
"devDependencies": {
|
|
37
33
|
"@types/node": "^22.8.6"
|
|
38
34
|
},
|
|
39
|
-
"
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
38
|
+
}
|
|
40
39
|
}
|