rsformat 0.2.0 → 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.d.ts +11 -0
- package/lib/format.js +47 -27
- package/lib/print.js +4 -4
- package/package.json +7 -6
package/lib/format.d.ts
CHANGED
|
@@ -5,3 +5,14 @@
|
|
|
5
5
|
* @param params Parameters to be inserted into the format string
|
|
6
6
|
*/
|
|
7
7
|
export declare function format(str: string, ...params: any[]): string;
|
|
8
|
+
/**
|
|
9
|
+
* Raw formatting behaviour function called by `format` and printing functions.
|
|
10
|
+
*
|
|
11
|
+
* @param str String used for formatting
|
|
12
|
+
* @param params Parameters to be inserted into the format string
|
|
13
|
+
* @param options Options passed into formatting
|
|
14
|
+
* @param options.colors Whether to use colors in debug formatting
|
|
15
|
+
*/
|
|
16
|
+
export declare function fmt_raw(str: string, params: any[], options?: {
|
|
17
|
+
colors: boolean;
|
|
18
|
+
}): string;
|
package/lib/format.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.format = void 0;
|
|
3
|
+
exports.fmt_raw = exports.format = void 0;
|
|
4
4
|
const node_util_1 = require("node:util");
|
|
5
5
|
/**
|
|
6
6
|
* Regex to match for possible formatting insertion points.
|
|
@@ -27,6 +27,18 @@ const FORMAT_REGEX = (/\{{2}|\}{2}|\{(\d*?)(?::(?:(.?)(\^|>|<))?(\+)?(#)?(0)?(\d
|
|
|
27
27
|
* @param params Parameters to be inserted into the format string
|
|
28
28
|
*/
|
|
29
29
|
function format(str, ...params) {
|
|
30
|
+
return fmt_raw(str, params);
|
|
31
|
+
}
|
|
32
|
+
exports.format = format;
|
|
33
|
+
/**
|
|
34
|
+
* Raw formatting behaviour function called by `format` and printing functions.
|
|
35
|
+
*
|
|
36
|
+
* @param str String used for formatting
|
|
37
|
+
* @param params Parameters to be inserted into the format string
|
|
38
|
+
* @param options Options passed into formatting
|
|
39
|
+
* @param options.colors Whether to use colors in debug formatting
|
|
40
|
+
*/
|
|
41
|
+
function fmt_raw(str, params, options = { colors: false }) {
|
|
30
42
|
// Counter used for insertion of unnumbered values
|
|
31
43
|
let param_counter = 0;
|
|
32
44
|
str = str.replace(FORMAT_REGEX, ($, $param_number, $fill_character, $align_direction, $sign, $pretty, $pad_zeroes, $width, $precision, $type) => {
|
|
@@ -45,21 +57,22 @@ function format(str, ...params) {
|
|
|
45
57
|
throw new Error(`parameter ${$param_number || param_counter - 1} either NaN or not provided`);
|
|
46
58
|
}
|
|
47
59
|
let param_type = typeof param;
|
|
60
|
+
let true_length = -1;
|
|
48
61
|
// Process parameter type
|
|
49
62
|
switch ($type) {
|
|
50
|
-
case
|
|
63
|
+
case 'o':
|
|
51
64
|
param = param.toString(8);
|
|
52
65
|
break;
|
|
53
|
-
case
|
|
66
|
+
case 'x':
|
|
54
67
|
param = param.toString(16);
|
|
55
68
|
break;
|
|
56
|
-
case
|
|
69
|
+
case 'X':
|
|
57
70
|
param = param.toString(16).toUpperCase();
|
|
58
71
|
break;
|
|
59
|
-
case
|
|
72
|
+
case 'b':
|
|
60
73
|
param = param.toString(2);
|
|
61
74
|
break;
|
|
62
|
-
case
|
|
75
|
+
case 'e':
|
|
63
76
|
switch (param_type) {
|
|
64
77
|
case 'number':
|
|
65
78
|
param = param.toExponential();
|
|
@@ -75,7 +88,7 @@ function format(str, ...params) {
|
|
|
75
88
|
break;
|
|
76
89
|
}
|
|
77
90
|
break;
|
|
78
|
-
case
|
|
91
|
+
case 'E':
|
|
79
92
|
switch (param_type) {
|
|
80
93
|
case 'number':
|
|
81
94
|
param = param.toExponential().toUpperCase();
|
|
@@ -91,10 +104,18 @@ function format(str, ...params) {
|
|
|
91
104
|
break;
|
|
92
105
|
}
|
|
93
106
|
break;
|
|
94
|
-
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;
|
|
95
116
|
param = (0, node_util_1.inspect)(param, {
|
|
96
117
|
depth: Infinity,
|
|
97
|
-
colors:
|
|
118
|
+
colors: options.colors,
|
|
98
119
|
compact: $pretty !== '#'
|
|
99
120
|
});
|
|
100
121
|
break;
|
|
@@ -103,11 +124,14 @@ function format(str, ...params) {
|
|
|
103
124
|
break;
|
|
104
125
|
}
|
|
105
126
|
;
|
|
127
|
+
if (true_length == -1) {
|
|
128
|
+
true_length = param.length;
|
|
129
|
+
}
|
|
106
130
|
// Compute radix-point precision on numbers
|
|
107
131
|
if (param_type == 'number' && $precision) {
|
|
108
|
-
let [pre, post] = param.split(
|
|
132
|
+
let [pre, post] = param.split('.');
|
|
109
133
|
if (post === undefined) {
|
|
110
|
-
post =
|
|
134
|
+
post = '';
|
|
111
135
|
}
|
|
112
136
|
let precision = +$precision.substring(1, $precision.length);
|
|
113
137
|
if (post.length > precision) {
|
|
@@ -117,7 +141,7 @@ function format(str, ...params) {
|
|
|
117
141
|
while (post.length < precision) {
|
|
118
142
|
post = post + '0';
|
|
119
143
|
}
|
|
120
|
-
param = pre +
|
|
144
|
+
param = pre + '.' + post;
|
|
121
145
|
}
|
|
122
146
|
let width;
|
|
123
147
|
if ($width === undefined) {
|
|
@@ -154,34 +178,30 @@ function format(str, ...params) {
|
|
|
154
178
|
}
|
|
155
179
|
param = maybe_sign + param;
|
|
156
180
|
}
|
|
157
|
-
if (!filled && width) {
|
|
181
|
+
if (!filled && width > true_length) {
|
|
158
182
|
// Compute fill/align
|
|
159
183
|
$align_direction ||= '>';
|
|
160
184
|
$fill_character ||= ' ';
|
|
185
|
+
let left = '';
|
|
186
|
+
let right = '';
|
|
187
|
+
let diff = width - true_length;
|
|
161
188
|
switch ($align_direction) {
|
|
162
189
|
case '>':
|
|
163
|
-
|
|
164
|
-
param = $fill_character + param;
|
|
165
|
-
}
|
|
190
|
+
left = $fill_character.repeat(diff);
|
|
166
191
|
break;
|
|
167
192
|
case '<':
|
|
168
|
-
|
|
169
|
-
param = param + $fill_character;
|
|
170
|
-
}
|
|
193
|
+
right = $fill_character.repeat(diff);
|
|
171
194
|
break;
|
|
172
195
|
case '^':
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// Prioritise right-aligment on uneven alignment
|
|
177
|
-
if (width - param.length == 1) {
|
|
178
|
-
param = $fill_character + param;
|
|
179
|
-
}
|
|
196
|
+
left = $fill_character.repeat(diff - diff / 2);
|
|
197
|
+
// Prioritise right-aligment on uneven length
|
|
198
|
+
right = $fill_character.repeat(diff / 2 + diff % 2);
|
|
180
199
|
break;
|
|
181
200
|
}
|
|
201
|
+
param = left + param + right;
|
|
182
202
|
}
|
|
183
203
|
return param;
|
|
184
204
|
});
|
|
185
205
|
return str;
|
|
186
206
|
}
|
|
187
|
-
exports.
|
|
207
|
+
exports.fmt_raw = fmt_raw;
|
package/lib/print.js
CHANGED
|
@@ -16,7 +16,7 @@ function Printer(outStream = node_process_1.default.stdout, errStream = node_pro
|
|
|
16
16
|
* @param params Parameters to be inserted into the format string
|
|
17
17
|
*/
|
|
18
18
|
print: function print(format_string, ...params) {
|
|
19
|
-
outStream.write((0, format_1.
|
|
19
|
+
outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }));
|
|
20
20
|
},
|
|
21
21
|
/**
|
|
22
22
|
* Print a format string to an output stream (usually process.stdout)
|
|
@@ -26,7 +26,7 @@ function Printer(outStream = node_process_1.default.stdout, errStream = node_pro
|
|
|
26
26
|
* @param params Parameters to be inserted into the format string
|
|
27
27
|
*/
|
|
28
28
|
println: function println(format_string, ...params) {
|
|
29
|
-
outStream.write((0, format_1.
|
|
29
|
+
outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }) + '\n');
|
|
30
30
|
},
|
|
31
31
|
/**
|
|
32
32
|
* Print a format string to an error stream (usually process.stderr).
|
|
@@ -35,7 +35,7 @@ function Printer(outStream = node_process_1.default.stdout, errStream = node_pro
|
|
|
35
35
|
* @param params Parameters to be inserted into the format string
|
|
36
36
|
*/
|
|
37
37
|
eprint: function eprint(format_string, ...params) {
|
|
38
|
-
errStream.write((0, format_1.
|
|
38
|
+
errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }));
|
|
39
39
|
},
|
|
40
40
|
/**
|
|
41
41
|
* Print a format string to an error stream (usually process.stderr)
|
|
@@ -45,7 +45,7 @@ function Printer(outStream = node_process_1.default.stdout, errStream = node_pro
|
|
|
45
45
|
* @param params Parameters to be inserted into the format string
|
|
46
46
|
*/
|
|
47
47
|
eprintln: function eprintln(format_string, ...params) {
|
|
48
|
-
errStream.write((0, format_1.
|
|
48
|
+
errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }) + '\n');
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
51
|
}
|
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",
|
|
@@ -28,11 +24,16 @@
|
|
|
28
24
|
"println",
|
|
29
25
|
"printf",
|
|
30
26
|
"console",
|
|
31
|
-
"log"
|
|
27
|
+
"log",
|
|
28
|
+
"console.log"
|
|
32
29
|
],
|
|
33
30
|
"author": "Alfio (https://github.com/p2js)",
|
|
34
31
|
"license": "ISC",
|
|
35
32
|
"devDependencies": {
|
|
36
33
|
"@types/node": "^22.8.6"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
37
38
|
}
|
|
38
39
|
}
|