rsformat 1.1.0 → 1.1.3
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/README.md +1 -1
- package/lib/format.d.ts +1 -2
- package/lib/format.js +24 -10
- package/lib/index.d.ts +39 -13
- package/lib/index.js +16 -26
- package/lib/print.d.ts +9 -2
- package/lib/print.js +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ let number = 14;
|
|
|
43
43
|
let info = rs`${number+1} is ${rs.ref(0)}:x in hex`; // info == '15 is f in hex'
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
> NB: templates tagged with `rs` are instances of a special class `RsString` that extends `String`, rather than a primitive value. This is to enable colors for debug formatting inside the printing functions. This difference should not affect normal usage, but `rs.raw` can be used as an alternative tag to get a primitive `string
|
|
46
|
+
> NB: templates tagged with `rs` are instances of a special class `RsString` that extends `String`, rather than a primitive value. This is to enable colors for debug formatting inside the printing functions. This difference should not affect normal usage, but `rs.raw` can be used as an alternative tag to get a primitive `string`.
|
|
47
47
|
|
|
48
48
|
The printing functions can be called with plain strings, instances of `String` or templates formatted with `rs`:
|
|
49
49
|
|
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
|
|
40
|
+
* Will include colors in the output of debug formating.
|
|
41
41
|
*
|
|
42
42
|
* @param param parameter to format
|
|
43
43
|
* @param format format specifier object
|
|
44
|
-
* @param debugColors whether to use colors in debug formatting
|
|
45
44
|
* @returns `param` as a formatted string
|
|
46
45
|
*/
|
|
47
46
|
export declare function formatParam(param: any, format: FormatSpecifier): string;
|
package/lib/format.js
CHANGED
|
@@ -37,7 +37,8 @@ exports.RsString = RsString;
|
|
|
37
37
|
* @returns An object with raw and colored versions of the formatted parameter
|
|
38
38
|
*/
|
|
39
39
|
function buildString(strings, params) {
|
|
40
|
-
let
|
|
40
|
+
let raw = strings[0];
|
|
41
|
+
let colored = strings[0];
|
|
41
42
|
for (let i = 1; i < strings.length; ++i) {
|
|
42
43
|
let string = strings[i];
|
|
43
44
|
let param = params[i - 1];
|
|
@@ -45,7 +46,9 @@ function buildString(strings, params) {
|
|
|
45
46
|
while (typeof param == 'object' && '__rs_param_ref' in param) {
|
|
46
47
|
let ref_number = param.__rs_param_ref;
|
|
47
48
|
if (typeof ref_number != 'number'
|
|
48
|
-
|| ref_number < 0
|
|
49
|
+
|| ref_number < 0
|
|
50
|
+
|| ref_number >= params.length
|
|
51
|
+
|| !Number.isInteger(ref_number)) {
|
|
49
52
|
throw new Error(`Parameter ${i - 1}: Invalid reference`);
|
|
50
53
|
}
|
|
51
54
|
if (ref_number == i - 1)
|
|
@@ -57,12 +60,16 @@ function buildString(strings, params) {
|
|
|
57
60
|
// If it has two the first : is being escaped and can be removed
|
|
58
61
|
if (string[0] == ':') {
|
|
59
62
|
if (string[1] == ':') {
|
|
60
|
-
|
|
63
|
+
let stringified = param.toString() + string.substring(1);
|
|
64
|
+
raw += stringified;
|
|
65
|
+
colored += stringified;
|
|
61
66
|
continue;
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
else {
|
|
65
|
-
|
|
70
|
+
let stringified = param.toString() + string;
|
|
71
|
+
raw += stringified;
|
|
72
|
+
colored += stringified;
|
|
66
73
|
continue;
|
|
67
74
|
}
|
|
68
75
|
;
|
|
@@ -134,8 +141,12 @@ function buildString(strings, params) {
|
|
|
134
141
|
if (string[idx] == ':') {
|
|
135
142
|
idx++;
|
|
136
143
|
}
|
|
137
|
-
else if (string[idx] != ' '
|
|
138
|
-
|
|
144
|
+
else if (string[idx] != ' '
|
|
145
|
+
&& string[idx] != '\t'
|
|
146
|
+
&& string[idx] != '\r'
|
|
147
|
+
&& string[idx] != '\n'
|
|
148
|
+
&& string[idx] !== undefined) {
|
|
149
|
+
throw error(i - 1, idx, `Expected colon (':') or space character (' '/'\\t'/'\\r'/'\\n') at end of formatting specifier (found '${string[idx]}')`);
|
|
139
150
|
}
|
|
140
151
|
// Format parameter according to specifier
|
|
141
152
|
let formatted = formatParam(param, {
|
|
@@ -148,18 +159,21 @@ function buildString(strings, params) {
|
|
|
148
159
|
precision,
|
|
149
160
|
type: format_type
|
|
150
161
|
});
|
|
151
|
-
|
|
162
|
+
let escaped_string = string.substring(idx);
|
|
163
|
+
colored += formatted + escaped_string;
|
|
164
|
+
if (format_type == '?')
|
|
165
|
+
formatted = node_util_1.default.stripVTControlCharacters(formatted);
|
|
166
|
+
raw += formatted + escaped_string;
|
|
152
167
|
}
|
|
153
|
-
return { raw
|
|
168
|
+
return { raw, colored };
|
|
154
169
|
}
|
|
155
170
|
exports.buildString = buildString;
|
|
156
171
|
/**
|
|
157
172
|
* Format a parameter as a string according to a specifier.
|
|
158
|
-
* Will include colors in the output of debug formating
|
|
173
|
+
* Will include colors in the output of debug formating.
|
|
159
174
|
*
|
|
160
175
|
* @param param parameter to format
|
|
161
176
|
* @param format format specifier object
|
|
162
|
-
* @param debugColors whether to use colors in debug formatting
|
|
163
177
|
* @returns `param` as a formatted string
|
|
164
178
|
*/
|
|
165
179
|
function formatParam(param, format) {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import { RsString } from './format';
|
|
2
|
-
export { print, println, eprint, eprintln } from './print';
|
|
2
|
+
export { print, println, eprint, eprintln, dbg } from './print';
|
|
3
|
+
type ParameterReference = {
|
|
4
|
+
__rs_param_ref: number;
|
|
5
|
+
};
|
|
6
|
+
type rs = {
|
|
7
|
+
(strings: TemplateStringsArray, ...params: any[]): RsString;
|
|
8
|
+
/**
|
|
9
|
+
* Tag to use Rust-style formatting in a template literal.
|
|
10
|
+
* Returns a `string` primitive.
|
|
11
|
+
*
|
|
12
|
+
* ```js
|
|
13
|
+
* let number = 14;
|
|
14
|
+
* let info = rs.raw`${number+1} is ${rs.ref(0)}:x in hex`;
|
|
15
|
+
* // info === '15 is f in hex'
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @returns a string primitive of the formatted string
|
|
19
|
+
*/
|
|
20
|
+
raw: (strings: TemplateStringsArray, ...params: any[]) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Reference another parameter in a `rs`-tagged template.
|
|
23
|
+
*
|
|
24
|
+
* @param n Number of parameter to reference
|
|
25
|
+
* @returns A reference to the `n`th parameter
|
|
26
|
+
*/
|
|
27
|
+
ref: (n: number) => ParameterReference;
|
|
28
|
+
};
|
|
3
29
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
30
|
+
* Tag to use Rust-style formatting in a template literal.
|
|
31
|
+
* Returns an extended `String` object.
|
|
32
|
+
*
|
|
33
|
+
* ```js
|
|
34
|
+
* let number = 14;
|
|
35
|
+
* let info = rs`${number+1} is ${rs.ref(0)}:x in hex`;
|
|
36
|
+
* // info == '15 is f in hex'
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @returns a String object with the formatted string
|
|
40
|
+
*/
|
|
41
|
+
export declare let rs: rs;
|
package/lib/index.js
CHANGED
|
@@ -1,35 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rs = exports.eprintln = exports.eprint = exports.println = exports.print = void 0;
|
|
3
|
+
exports.rs = exports.dbg = exports.eprintln = exports.eprint = exports.println = exports.print = void 0;
|
|
4
4
|
const format_1 = require("./format");
|
|
5
5
|
var print_1 = require("./print");
|
|
6
6
|
Object.defineProperty(exports, "print", { enumerable: true, get: function () { return print_1.print; } });
|
|
7
7
|
Object.defineProperty(exports, "println", { enumerable: true, get: function () { return print_1.println; } });
|
|
8
8
|
Object.defineProperty(exports, "eprint", { enumerable: true, get: function () { return print_1.eprint; } });
|
|
9
9
|
Object.defineProperty(exports, "eprintln", { enumerable: true, get: function () { return print_1.eprintln; } });
|
|
10
|
+
Object.defineProperty(exports, "dbg", { enumerable: true, get: function () { return print_1.dbg; } });
|
|
10
11
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*/
|
|
26
|
-
rs.raw = function (strings, ...params) {
|
|
27
|
-
return (0, format_1.buildString)(strings, params).raw;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Reference another parameter in a `rs`-tagged template.
|
|
31
|
-
*
|
|
32
|
-
* @param n Number of parameter to reference
|
|
33
|
-
* @returns A reference to the `n`th parameter
|
|
34
|
-
*/
|
|
35
|
-
rs.ref = (n) => ({ __rs_param_ref: n });
|
|
12
|
+
* Tag to use Rust-style formatting in a template literal.
|
|
13
|
+
* Returns an extended `String` object.
|
|
14
|
+
*
|
|
15
|
+
* ```js
|
|
16
|
+
* let number = 14;
|
|
17
|
+
* let info = rs`${number+1} is ${rs.ref(0)}:x in hex`;
|
|
18
|
+
* // info == '15 is f in hex'
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @returns a String object with the formatted string
|
|
22
|
+
*/
|
|
23
|
+
exports.rs = ((strings, ...params) => new format_1.RsString(strings, params));
|
|
24
|
+
exports.rs.raw = (strings, ...params) => (0, format_1.buildString)(strings, params).raw;
|
|
25
|
+
exports.rs.ref = (n) => ({ __rs_param_ref: n });
|
package/lib/print.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Writable } from 'node:stream';
|
|
2
|
+
import type { Writable } from 'node:stream';
|
|
3
3
|
/**
|
|
4
|
-
* Print a string (or instance of String/RsString) to a stream.
|
|
4
|
+
* Print a string (or instance of String/RsString) to a `Writable` stream.
|
|
5
5
|
*
|
|
6
6
|
* @param stream Stream to print the string to
|
|
7
7
|
* @param string String to print
|
|
@@ -36,6 +36,13 @@ export declare function eprintln(string: string | String): void;
|
|
|
36
36
|
/**
|
|
37
37
|
* Debug print a value to stderr and return it.
|
|
38
38
|
*
|
|
39
|
+
* ```js
|
|
40
|
+
* let r = Math.random();
|
|
41
|
+
* if(dbg(r < 0.3)) { // prints 'true' or 'false'
|
|
42
|
+
* println("Unlucky!");
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
39
46
|
* @param value Value to debug print
|
|
40
47
|
*/
|
|
41
48
|
export declare function dbg(value: any): any;
|
package/lib/print.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.dbg = exports.eprintln = exports.eprint = exports.println = exports.prin
|
|
|
7
7
|
const node_process_1 = __importDefault(require("node:process"));
|
|
8
8
|
const _1 = require(".");
|
|
9
9
|
/**
|
|
10
|
-
* Print a string (or instance of String/RsString) to a stream.
|
|
10
|
+
* Print a string (or instance of String/RsString) to a `Writable` stream.
|
|
11
11
|
*
|
|
12
12
|
* @param stream Stream to print the string to
|
|
13
13
|
* @param string String to print
|
|
@@ -62,6 +62,13 @@ exports.eprintln = eprintln;
|
|
|
62
62
|
/**
|
|
63
63
|
* Debug print a value to stderr and return it.
|
|
64
64
|
*
|
|
65
|
+
* ```js
|
|
66
|
+
* let r = Math.random();
|
|
67
|
+
* if(dbg(r < 0.3)) { // prints 'true' or 'false'
|
|
68
|
+
* println("Unlucky!");
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
65
72
|
* @param value Value to debug print
|
|
66
73
|
*/
|
|
67
74
|
function dbg(value) {
|