rsformat 1.1.2 → 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 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` with ANSI control characters escaped.
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 out = strings[0];
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 || ref_number >= params.length) {
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
- out += param.toString() + string.substring(1);
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
- out += param.toString() + string;
70
+ let stringified = param.toString() + string;
71
+ raw += stringified;
72
+ colored += stringified;
66
73
  continue;
67
74
  }
68
75
  ;
@@ -152,18 +159,21 @@ function buildString(strings, params) {
152
159
  precision,
153
160
  type: format_type
154
161
  });
155
- out += formatted + string.substring(idx);
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;
156
167
  }
157
- return { raw: node_util_1.default.stripVTControlCharacters(out), colored: out };
168
+ return { raw, colored };
158
169
  }
159
170
  exports.buildString = buildString;
160
171
  /**
161
172
  * Format a parameter as a string according to a specifier.
162
- * Will include colors in the output of debug formating
173
+ * Will include colors in the output of debug formating.
163
174
  *
164
175
  * @param param parameter to format
165
176
  * @param format format specifier object
166
- * @param debugColors whether to use colors in debug formatting
167
177
  * @returns `param` as a formatted string
168
178
  */
169
179
  function formatParam(param, format) {
package/lib/index.d.ts CHANGED
@@ -1,15 +1,41 @@
1
1
  import { RsString } from './format';
2
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
- * Tag to use Rust-style formatting in a template literal.
5
- * Returns an extended `String` object.
6
- *
7
- * @returns a String object with the formatted string
8
- */
9
- export declare function rs(strings: TemplateStringsArray, ...params: any[]): RsString;
10
- export declare namespace rs {
11
- var raw: (strings: TemplateStringsArray, ...params: any[]) => string;
12
- var ref: (n: number) => {
13
- __rs_param_ref: number;
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
@@ -9,28 +9,17 @@ Object.defineProperty(exports, "eprint", { enumerable: true, get: function () {
9
9
  Object.defineProperty(exports, "eprintln", { enumerable: true, get: function () { return print_1.eprintln; } });
10
10
  Object.defineProperty(exports, "dbg", { enumerable: true, get: function () { return print_1.dbg; } });
11
11
  /**
12
- * Tag to use Rust-style formatting in a template literal.
13
- * Returns an extended `String` object.
14
- *
15
- * @returns a String object with the formatted string
16
- */
17
- function rs(strings, ...params) {
18
- return new format_1.RsString(strings, params);
19
- }
20
- exports.rs = rs;
21
- /**
22
- * Tag to use Rust-style formatting in a template literal.
23
- * Returns a `string` primitive.
24
- *
25
- * @returns a string primitive of the formatted string
26
- */
27
- rs.raw = function (strings, ...params) {
28
- return (0, format_1.buildString)(strings, params).raw;
29
- };
30
- /**
31
- * Reference another parameter in a `rs`-tagged template.
32
- *
33
- * @param n Number of parameter to reference
34
- * @returns A reference to the `n`th parameter
35
- */
36
- 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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsformat",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
5
5
  "files": [
6
6
  "lib",