rsformat 1.0.0 → 1.1.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/README.md CHANGED
@@ -31,23 +31,25 @@ npm install rsformat
31
31
  ### Basic formatting and printing to console
32
32
 
33
33
  The `rs` template tag can be used to enable rust-style formatting in a template.
34
- To reference a previous or following argument, use `rs.ref` with the parameter number.
34
+
35
+ To reference a previous or following argument, use `rs.ref` with the argument number. This is useful if you want to reuse a complicated expression without having to declare it separately.
35
36
 
36
37
  ```js
37
38
  import { rs, println } from 'rsformat'; // ESM
38
39
  const { rs, println } = require('rsformat'); // CommonJS
39
40
 
40
- let number = 15;
41
+ let number = 14;
41
42
 
42
- let info = rs`${number} is ${rs.ref(0)}:x in hex`; // info == '15 is f in hex'
43
+ let info = rs`${number+1} is ${rs.ref(0)}:x in hex`; // info == '15 is f in hex'
43
44
  ```
44
45
 
45
- > 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` with ANSI control characters escaped.
46
47
 
47
48
  The printing functions can be called with plain strings, instances of `String` or templates formatted with `rs`:
48
49
 
49
50
  ```ts
50
51
  println('Hello World');
52
+ println(`This template did ${'Not'} need fancy formatting`);
51
53
  println(rs`...`);
52
54
  ```
53
55
 
package/lib/format.d.ts CHANGED
@@ -3,23 +3,25 @@
3
3
  * An extension of `String`.
4
4
  */
5
5
  export declare class RsString extends String {
6
- __debugColors: boolean;
7
- private cachedColor;
8
- private cachedPlain;
9
- private strings;
10
- private params;
6
+ /**
7
+ * A version of the string that includes ANSI escape codes for debug formatting.
8
+ */
9
+ colored: string;
11
10
  constructor(strings: TemplateStringsArray, params: any[]);
12
- toString(): string;
13
- valueOf(): string;
11
+ toString(debugColors?: boolean): string;
14
12
  }
15
13
  /**
16
- * Format a template literal with rust-style formatting and return it as a string.
14
+ * Format a template literal with rust-style formatting and return it as a raw and colored string.
17
15
  *
18
16
  * @param strings String parts of the template
19
17
  * @param params Template parameters
20
- * @returns a string primitive of the formatted string
18
+ *
19
+ * @returns An object with raw and colored versions of the formatted parameter
21
20
  */
22
- export declare function buildString(strings: TemplateStringsArray, params: any[], debugColors?: boolean): string;
21
+ export declare function buildString(strings: TemplateStringsArray, params: any[]): {
22
+ raw: string;
23
+ colored: string;
24
+ };
23
25
  type AlignDirection = '<' | '^' | '>';
24
26
  type Sign = '-' | '+' | '';
25
27
  type FormatType = '?' | 'o' | 'x' | 'X' | 'b' | 'e' | 'E' | 'n' | 'N' | '';
@@ -35,11 +37,12 @@ type FormatSpecifier = {
35
37
  };
36
38
  /**
37
39
  * Format a parameter as a string according to a specifier.
40
+ * Will include colors in the output of debug formating
38
41
  *
39
42
  * @param param parameter to format
40
43
  * @param format format specifier object
41
44
  * @param debugColors whether to use colors in debug formatting
42
45
  * @returns `param` as a formatted string
43
46
  */
44
- export declare function formatParam(param: any, format: FormatSpecifier, debugColors: boolean): string;
47
+ export declare function formatParam(param: any, format: FormatSpecifier): string;
45
48
  export {};
package/lib/format.js CHANGED
@@ -12,48 +12,32 @@ const error = (param, char, reason) => new Error(`rs[param ${param}, char ${char
12
12
  * An extension of `String`.
13
13
  */
14
14
  class RsString extends String {
15
- __debugColors = false;
16
- cachedColor = null;
17
- cachedPlain = null;
18
- strings;
19
- params;
15
+ /**
16
+ * A version of the string that includes ANSI escape codes for debug formatting.
17
+ */
18
+ colored;
20
19
  constructor(strings, params) {
21
- super('[use rs.raw to get string primitive]');
22
- Object.defineProperties(this, {
23
- strings: { value: strings, enumerable: false },
24
- params: { value: params, enumerable: false },
25
- cachedPlain: { value: null, writable: true, enumerable: false },
26
- cachedColor: { value: null, writable: true, enumerable: false },
27
- __debugColors: { value: false, writable: true, enumerable: false },
28
- });
29
- }
30
- toString() {
31
- if (this.__debugColors) {
32
- if (this.cachedColor === null)
33
- this.cachedColor = buildString(this.strings, this.params, true);
34
- return this.cachedColor;
35
- }
36
- else {
37
- if (this.cachedPlain === null)
38
- this.cachedPlain = buildString(this.strings, this.params, false);
39
- return this.cachedPlain;
40
- }
41
- ;
20
+ let { raw, colored } = buildString(strings, params);
21
+ super(raw);
22
+ this.colored = colored;
42
23
  }
43
- valueOf() {
44
- return this.toString();
24
+ toString(debugColors = false) {
25
+ if (debugColors)
26
+ return this.colored;
27
+ return this.valueOf();
45
28
  }
46
29
  }
47
30
  exports.RsString = RsString;
48
31
  /**
49
- * Format a template literal with rust-style formatting and return it as a string.
32
+ * Format a template literal with rust-style formatting and return it as a raw and colored string.
50
33
  *
51
34
  * @param strings String parts of the template
52
35
  * @param params Template parameters
53
- * @returns a string primitive of the formatted string
36
+ *
37
+ * @returns An object with raw and colored versions of the formatted parameter
54
38
  */
55
- function buildString(strings, params, debugColors = false) {
56
- let out = [strings[0]];
39
+ function buildString(strings, params) {
40
+ let out = strings[0];
57
41
  for (let i = 1; i < strings.length; ++i) {
58
42
  let string = strings[i];
59
43
  let param = params[i - 1];
@@ -73,12 +57,12 @@ function buildString(strings, params, debugColors = false) {
73
57
  // If it has two the first : is being escaped and can be removed
74
58
  if (string[0] == ':') {
75
59
  if (string[1] == ':') {
76
- out.push(param.toString() + string.substring(1));
60
+ out += param.toString() + string.substring(1);
77
61
  continue;
78
62
  }
79
63
  }
80
64
  else {
81
- out.push(param.toString() + string);
65
+ out += param.toString() + string;
82
66
  continue;
83
67
  }
84
68
  ;
@@ -150,8 +134,12 @@ function buildString(strings, params, debugColors = false) {
150
134
  if (string[idx] == ':') {
151
135
  idx++;
152
136
  }
153
- else if (string[idx] != ' ' && string[idx] !== undefined) {
154
- throw error(i - 1, idx, `Expected colon (':') or space (' ') at end of formatting specifier (found '${string[idx]}')`);
137
+ else if (string[idx] != ' '
138
+ && string[idx] != '\t'
139
+ && string[idx] != '\r'
140
+ && string[idx] != '\n'
141
+ && string[idx] !== undefined) {
142
+ throw error(i - 1, idx, `Expected colon (':') or space character (' '/'\\t'/'\\r'/'\\n') at end of formatting specifier (found '${string[idx]}')`);
155
143
  }
156
144
  // Format parameter according to specifier
157
145
  let formatted = formatParam(param, {
@@ -163,23 +151,23 @@ function buildString(strings, params, debugColors = false) {
163
151
  width,
164
152
  precision,
165
153
  type: format_type
166
- }, debugColors);
167
- out.push(formatted + string.substring(idx));
154
+ });
155
+ out += formatted + string.substring(idx);
168
156
  }
169
- return out.join('');
157
+ return { raw: node_util_1.default.stripVTControlCharacters(out), colored: out };
170
158
  }
171
159
  exports.buildString = buildString;
172
160
  /**
173
161
  * Format a parameter as a string according to a specifier.
162
+ * Will include colors in the output of debug formating
174
163
  *
175
164
  * @param param parameter to format
176
165
  * @param format format specifier object
177
166
  * @param debugColors whether to use colors in debug formatting
178
167
  * @returns `param` as a formatted string
179
168
  */
180
- function formatParam(param, format, debugColors) {
169
+ function formatParam(param, format) {
181
170
  let param_type = typeof param;
182
- let true_length = -1;
183
171
  // Process parameter type
184
172
  switch (format.type) {
185
173
  case 'o':
@@ -238,14 +226,9 @@ function formatParam(param, format, debugColors) {
238
226
  format.precision = -1;
239
227
  break;
240
228
  case '?':
241
- true_length = node_util_1.default.inspect(param, {
242
- depth: Infinity,
243
- colors: false,
244
- compact: !format.pretty
245
- }).length;
246
229
  param = node_util_1.default.inspect(param, {
247
230
  depth: Infinity,
248
- colors: debugColors,
231
+ colors: true,
249
232
  compact: !format.pretty
250
233
  });
251
234
  // Do not force sign, pad with zeroes or align to precision when using debug formatting
@@ -256,9 +239,6 @@ function formatParam(param, format, debugColors) {
256
239
  break;
257
240
  }
258
241
  ;
259
- if (true_length == -1) {
260
- true_length = param.length;
261
- }
262
242
  // Compute radix-point precision on numbers
263
243
  if (param_type == 'number' && format.precision != -1) {
264
244
  let [pre, post] = param.split('.');
@@ -269,10 +249,8 @@ function formatParam(param, format, debugColors) {
269
249
  post = ((post || '') + '0'.repeat(format.precision)).slice(0, format.precision);
270
250
  param = pre + '.' + post;
271
251
  }
272
- // Update true length for fill/align
273
- true_length = param.length;
274
252
  }
275
- let filled = false;
253
+ // let filled = false;
276
254
  if ((param_type == 'number') || (param_type == 'bigint')) {
277
255
  // Compute parameter sign
278
256
  let maybe_sign = param.substring(0, 1);
@@ -305,20 +283,18 @@ function formatParam(param, format, debugColors) {
305
283
  }
306
284
  //pad with zeroes if specified
307
285
  if (format.pad_zeroes) {
308
- filled = true;
286
+ // filled = true;
309
287
  while (param.length < format.width - maybe_sign.length) {
310
288
  param = '0' + param;
311
- true_length++;
312
289
  }
313
290
  }
314
- true_length += maybe_sign.length;
315
291
  param = maybe_sign + param;
316
292
  }
317
- if (!filled && format.width > true_length) {
293
+ if ( /*!filled && */format.width > param.length) {
318
294
  // Compute fill/align
319
295
  let left = '';
320
296
  let right = '';
321
- let diff = format.width - true_length;
297
+ let diff = format.width - param.length;
322
298
  switch (format.align) {
323
299
  case '>':
324
300
  left = format.fill.repeat(diff);
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RsString } from './format';
2
- export { print, println, eprint, eprintln } from './print';
2
+ export { print, println, eprint, eprintln, dbg } from './print';
3
3
  /**
4
4
  * Tag to use Rust-style formatting in a template literal.
5
5
  * Returns an extended `String` object.
package/lib/index.js CHANGED
@@ -1,12 +1,13 @@
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
  * Tag to use Rust-style formatting in a template literal.
12
13
  * Returns an extended `String` object.
@@ -24,7 +25,7 @@ exports.rs = rs;
24
25
  * @returns a string primitive of the formatted string
25
26
  */
26
27
  rs.raw = function (strings, ...params) {
27
- return (0, format_1.buildString)(strings, params);
28
+ return (0, format_1.buildString)(strings, params).raw;
28
29
  };
29
30
  /**
30
31
  * Reference another parameter in a `rs`-tagged template.
package/lib/print.js CHANGED
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.dbg = exports.eprintln = exports.eprint = exports.println = exports.print = exports.printToStream = void 0;
7
- const format_1 = require("./format");
8
7
  const node_process_1 = __importDefault(require("node:process"));
9
8
  const _1 = require(".");
10
9
  /**
@@ -16,13 +15,8 @@ const _1 = require(".");
16
15
  * @param colored Whether to use colors for `rs` debug formatting
17
16
  */
18
17
  function printToStream(stream, string, newline = false, colored = false) {
19
- if (string instanceof format_1.RsString) {
20
- let previousColors = string.__debugColors;
21
- if (colored)
22
- string.__debugColors = true;
23
- let stringified = string.toString();
24
- string.__debugColors = previousColors;
25
- string = stringified;
18
+ if (string instanceof String) {
19
+ string = string.toString(true);
26
20
  }
27
21
  if (newline)
28
22
  string = string + '\n';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsformat",
3
- "version": "1.0.0",
3
+ "version": "1.1.2",
4
4
  "description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
5
5
  "files": [
6
6
  "lib",