rsformat 1.1.3 → 1.2.1
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 +2 -3
- package/lib/format.js +94 -86
- package/lib/print.d.ts +0 -1
- package/lib/print.js +7 -8
- package/package.json +2 -1
package/lib/format.d.ts
CHANGED
|
@@ -37,11 +37,10 @@ type FormatSpecifier = {
|
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
39
|
* Format a parameter as a string according to a specifier.
|
|
40
|
-
* Will include colors in the output of debug formating.
|
|
41
40
|
*
|
|
42
41
|
* @param param parameter to format
|
|
43
42
|
* @param format format specifier object
|
|
44
|
-
* @returns `param` as a formatted string
|
|
43
|
+
* @returns `param` as a debug-colored and raw formatted string
|
|
45
44
|
*/
|
|
46
|
-
export declare function formatParam(param: any, format: FormatSpecifier): string;
|
|
45
|
+
export declare function formatParam(param: any, format: FormatSpecifier): [string, string];
|
|
47
46
|
export {};
|
package/lib/format.js
CHANGED
|
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.RsString = void 0;
|
|
7
|
+
exports.buildString = buildString;
|
|
8
|
+
exports.formatParam = formatParam;
|
|
7
9
|
const node_util_1 = __importDefault(require("node:util"));
|
|
8
10
|
const is_digit = (c) => c >= '0' && c <= '9';
|
|
9
11
|
const error = (param, char, reason) => new Error(`rs[param ${param}, char ${char}] ${reason}`);
|
|
@@ -58,18 +60,18 @@ function buildString(strings, params) {
|
|
|
58
60
|
// Parse format specifier
|
|
59
61
|
// If the string starts with a single : it has a format specifier,
|
|
60
62
|
// If it has two the first : is being escaped and can be removed
|
|
63
|
+
let skipParsing = false;
|
|
61
64
|
if (string[0] == ':') {
|
|
62
65
|
if (string[1] == ':') {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
colored += stringified;
|
|
66
|
-
continue;
|
|
66
|
+
string = string.substring(1);
|
|
67
|
+
skipParsing = true;
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
|
-
else
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
else
|
|
71
|
+
skipParsing = true;
|
|
72
|
+
if (skipParsing) {
|
|
73
|
+
raw += param.toString(param instanceof String ? false : undefined) + string;
|
|
74
|
+
colored += param.toString(param instanceof String ? true : undefined) + string;
|
|
73
75
|
continue;
|
|
74
76
|
}
|
|
75
77
|
;
|
|
@@ -149,7 +151,7 @@ function buildString(strings, params) {
|
|
|
149
151
|
throw error(i - 1, idx, `Expected colon (':') or space character (' '/'\\t'/'\\r'/'\\n') at end of formatting specifier (found '${string[idx]}')`);
|
|
150
152
|
}
|
|
151
153
|
// Format parameter according to specifier
|
|
152
|
-
let
|
|
154
|
+
let [formatted_colored, formatted_raw] = formatParam(param, {
|
|
153
155
|
fill,
|
|
154
156
|
align,
|
|
155
157
|
force_sign,
|
|
@@ -160,94 +162,98 @@ function buildString(strings, params) {
|
|
|
160
162
|
type: format_type
|
|
161
163
|
});
|
|
162
164
|
let escaped_string = string.substring(idx);
|
|
163
|
-
colored +=
|
|
164
|
-
|
|
165
|
-
formatted = node_util_1.default.stripVTControlCharacters(formatted);
|
|
166
|
-
raw += formatted + escaped_string;
|
|
165
|
+
colored += formatted_colored + escaped_string;
|
|
166
|
+
raw += formatted_raw + escaped_string;
|
|
167
167
|
}
|
|
168
168
|
return { raw, colored };
|
|
169
169
|
}
|
|
170
|
-
exports.buildString = buildString;
|
|
171
170
|
/**
|
|
172
171
|
* Format a parameter as a string according to a specifier.
|
|
173
|
-
* Will include colors in the output of debug formating.
|
|
174
172
|
*
|
|
175
173
|
* @param param parameter to format
|
|
176
174
|
* @param format format specifier object
|
|
177
|
-
* @returns `param` as a formatted string
|
|
175
|
+
* @returns `param` as a debug-colored and raw formatted string
|
|
178
176
|
*/
|
|
179
177
|
function formatParam(param, format) {
|
|
180
178
|
let param_type = typeof param;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
179
|
+
let param_colored = "";
|
|
180
|
+
// embed RsStrings directly
|
|
181
|
+
if (param instanceof String && format.type != '?') {
|
|
182
|
+
param_colored = param.toString(true);
|
|
183
|
+
param = param.toString(false);
|
|
184
|
+
}
|
|
185
|
+
else
|
|
186
|
+
switch (format.type) {
|
|
187
|
+
// Process format type
|
|
188
|
+
case 'o':
|
|
189
|
+
param = param.toString(8);
|
|
190
|
+
break;
|
|
191
|
+
case 'x':
|
|
192
|
+
param = param.toString(16);
|
|
193
|
+
break;
|
|
194
|
+
case 'X':
|
|
195
|
+
param = param.toString(16).toUpperCase();
|
|
196
|
+
break;
|
|
197
|
+
case 'b':
|
|
198
|
+
param = param.toString(2);
|
|
199
|
+
break;
|
|
200
|
+
case 'e':
|
|
201
|
+
case 'E':
|
|
202
|
+
if (param_type != 'number' && param_type != 'bigint') {
|
|
203
|
+
param = param.toString();
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
param = param.toLocaleString('en-US', { notation: 'scientific', maximumFractionDigits: 20 });
|
|
207
|
+
if (format.type == 'e')
|
|
208
|
+
param = param.toLowercase();
|
|
209
|
+
break;
|
|
210
|
+
case 'n':
|
|
211
|
+
case 'N':
|
|
212
|
+
if (param_type != 'number' && param_type != 'bigint') {
|
|
213
|
+
param = param.toString();
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
// Round and add suffix
|
|
217
|
+
if (param_type == 'number')
|
|
218
|
+
param = Math.round(param);
|
|
198
219
|
param = param.toString();
|
|
220
|
+
let last_2_digits = param.substring(param.length - 2);
|
|
221
|
+
if (last_2_digits == '11' || last_2_digits == '12' || last_2_digits == '13') {
|
|
222
|
+
param = param + 'th';
|
|
223
|
+
}
|
|
224
|
+
else
|
|
225
|
+
switch (last_2_digits[last_2_digits.length - 1]) {
|
|
226
|
+
case '1':
|
|
227
|
+
param = param + 'st';
|
|
228
|
+
break;
|
|
229
|
+
case '2':
|
|
230
|
+
param = param + 'nd';
|
|
231
|
+
break;
|
|
232
|
+
case '3':
|
|
233
|
+
param = param + 'rd';
|
|
234
|
+
break;
|
|
235
|
+
default: param = param + 'th';
|
|
236
|
+
}
|
|
237
|
+
if (format.type == 'N')
|
|
238
|
+
param = param.toUpperCase();
|
|
239
|
+
// Do not pad with zeroes or align to precision when using ordinal formatting
|
|
240
|
+
format.pad_zeroes = false;
|
|
241
|
+
format.precision = -1;
|
|
199
242
|
break;
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
243
|
+
case '?':
|
|
244
|
+
param_colored = node_util_1.default.inspect(param, {
|
|
245
|
+
depth: Infinity,
|
|
246
|
+
colors: true,
|
|
247
|
+
compact: !format.pretty
|
|
248
|
+
});
|
|
249
|
+
param = node_util_1.default.stripVTControlCharacters(param_colored);
|
|
250
|
+
// Do not force sign, pad with zeroes or align to precision when using debug formatting
|
|
251
|
+
param_type = 'string';
|
|
252
|
+
break;
|
|
253
|
+
default:
|
|
208
254
|
param = param.toString();
|
|
209
255
|
break;
|
|
210
|
-
|
|
211
|
-
// Round and add suffix
|
|
212
|
-
if (param_type == 'number')
|
|
213
|
-
param = Math.round(param);
|
|
214
|
-
param = param.toString();
|
|
215
|
-
let last_2_digits = param.substring(param.length - 2);
|
|
216
|
-
if (last_2_digits == '11' || last_2_digits == '12' || last_2_digits == '13') {
|
|
217
|
-
param = param + 'th';
|
|
218
|
-
}
|
|
219
|
-
else
|
|
220
|
-
switch (last_2_digits[last_2_digits.length - 1]) {
|
|
221
|
-
case '1':
|
|
222
|
-
param = param + 'st';
|
|
223
|
-
break;
|
|
224
|
-
case '2':
|
|
225
|
-
param = param + 'nd';
|
|
226
|
-
break;
|
|
227
|
-
case '3':
|
|
228
|
-
param = param + 'rd';
|
|
229
|
-
break;
|
|
230
|
-
default: param = param + 'th';
|
|
231
|
-
}
|
|
232
|
-
if (format.type == 'N')
|
|
233
|
-
param = param.toUpperCase();
|
|
234
|
-
// Do not pad with zeroes or align to precision when using ordinal formatting
|
|
235
|
-
format.pad_zeroes = false;
|
|
236
|
-
format.precision = -1;
|
|
237
|
-
break;
|
|
238
|
-
case '?':
|
|
239
|
-
param = node_util_1.default.inspect(param, {
|
|
240
|
-
depth: Infinity,
|
|
241
|
-
colors: true,
|
|
242
|
-
compact: !format.pretty
|
|
243
|
-
});
|
|
244
|
-
// Do not force sign, pad with zeroes or align to precision when using debug formatting
|
|
245
|
-
param_type = 'string';
|
|
246
|
-
break;
|
|
247
|
-
default:
|
|
248
|
-
param = param.toString();
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
256
|
+
}
|
|
251
257
|
;
|
|
252
258
|
// Compute radix-point precision on numbers
|
|
253
259
|
if (param_type == 'number' && format.precision != -1) {
|
|
@@ -300,7 +306,9 @@ function formatParam(param, format) {
|
|
|
300
306
|
}
|
|
301
307
|
param = maybe_sign + param;
|
|
302
308
|
}
|
|
303
|
-
if (
|
|
309
|
+
if (param_colored == "")
|
|
310
|
+
param_colored = param;
|
|
311
|
+
if (format.width > param.length) {
|
|
304
312
|
// Compute fill/align
|
|
305
313
|
let left = '';
|
|
306
314
|
let right = '';
|
|
@@ -319,7 +327,7 @@ function formatParam(param, format) {
|
|
|
319
327
|
break;
|
|
320
328
|
}
|
|
321
329
|
param = left + param + right;
|
|
330
|
+
param_colored = left + param_colored + right;
|
|
322
331
|
}
|
|
323
|
-
return param;
|
|
332
|
+
return [param_colored, param];
|
|
324
333
|
}
|
|
325
|
-
exports.formatParam = formatParam;
|
package/lib/print.d.ts
CHANGED
package/lib/print.js
CHANGED
|
@@ -3,7 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.printToStream = printToStream;
|
|
7
|
+
exports.print = print;
|
|
8
|
+
exports.println = println;
|
|
9
|
+
exports.eprint = eprint;
|
|
10
|
+
exports.eprintln = eprintln;
|
|
11
|
+
exports.dbg = dbg;
|
|
7
12
|
const node_process_1 = __importDefault(require("node:process"));
|
|
8
13
|
const _1 = require(".");
|
|
9
14
|
/**
|
|
@@ -16,13 +21,12 @@ const _1 = require(".");
|
|
|
16
21
|
*/
|
|
17
22
|
function printToStream(stream, string, newline = false, colored = false) {
|
|
18
23
|
if (string instanceof String) {
|
|
19
|
-
string = string.toString(
|
|
24
|
+
string = string.toString(colored);
|
|
20
25
|
}
|
|
21
26
|
if (newline)
|
|
22
27
|
string = string + '\n';
|
|
23
28
|
stream.write(string);
|
|
24
29
|
}
|
|
25
|
-
exports.printToStream = printToStream;
|
|
26
30
|
/**
|
|
27
31
|
* Print a string to stdout.
|
|
28
32
|
*
|
|
@@ -31,7 +35,6 @@ exports.printToStream = printToStream;
|
|
|
31
35
|
function print(string) {
|
|
32
36
|
printToStream(node_process_1.default.stdout, string, false, true);
|
|
33
37
|
}
|
|
34
|
-
exports.print = print;
|
|
35
38
|
/**
|
|
36
39
|
* Print a string to stdout and append a newline.
|
|
37
40
|
*
|
|
@@ -40,7 +43,6 @@ exports.print = print;
|
|
|
40
43
|
function println(string) {
|
|
41
44
|
printToStream(node_process_1.default.stdout, string, true, true);
|
|
42
45
|
}
|
|
43
|
-
exports.println = println;
|
|
44
46
|
/**
|
|
45
47
|
* Print a string to stderr.
|
|
46
48
|
*
|
|
@@ -49,7 +51,6 @@ exports.println = println;
|
|
|
49
51
|
function eprint(string) {
|
|
50
52
|
printToStream(node_process_1.default.stderr, string, false, true);
|
|
51
53
|
}
|
|
52
|
-
exports.eprint = eprint;
|
|
53
54
|
/**
|
|
54
55
|
* Print a string to stderr and append a newline.
|
|
55
56
|
*
|
|
@@ -58,7 +59,6 @@ exports.eprint = eprint;
|
|
|
58
59
|
function eprintln(string) {
|
|
59
60
|
printToStream(node_process_1.default.stderr, string, true, true);
|
|
60
61
|
}
|
|
61
|
-
exports.eprintln = eprintln;
|
|
62
62
|
/**
|
|
63
63
|
* Debug print a value to stderr and return it.
|
|
64
64
|
*
|
|
@@ -75,4 +75,3 @@ function dbg(value) {
|
|
|
75
75
|
eprintln((0, _1.rs) `${value}:?`);
|
|
76
76
|
return value;
|
|
77
77
|
}
|
|
78
|
-
exports.dbg = dbg;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rsformat",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "tsc",
|
|
37
|
+
"prepublish": "pnpm build",
|
|
37
38
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
38
39
|
}
|
|
39
40
|
}
|