rsformat 0.2.1 → 0.2.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
@@ -90,7 +90,7 @@ format('{:.>7}', [1,2]); // '....1,2'
90
90
 
91
91
  #### Pretty Printing
92
92
 
93
- In some instances (namely debug and hexadecimal formatting), adding a `#` before the format specifier will use an alternative 'pretty' printing style. This amounts to using non-compact `util.inspect` for debug printing (spanning multiple lines), and adding 0x to hexadecimal numbers.
93
+ In some instances (namely debug, binary, octal and hexadecimal formatting), adding a `#` before the format specifier will use an alternative 'pretty' printing style. This amounts to using non-compact `util.inspect` for debug printing (spanning multiple lines), and adding 0b/0o/0x as a prefix for the numbers formatted as powers of two.
94
94
 
95
95
  ```js
96
96
  format('{:#X}', 255); // '0xFF'
package/lib/format.js CHANGED
@@ -57,21 +57,22 @@ function fmt_raw(str, params, options = { colors: false }) {
57
57
  throw new Error(`parameter ${$param_number || param_counter - 1} either NaN or not provided`);
58
58
  }
59
59
  let param_type = typeof param;
60
+ let true_length = -1;
60
61
  // Process parameter type
61
62
  switch ($type) {
62
- case "o":
63
+ case 'o':
63
64
  param = param.toString(8);
64
65
  break;
65
- case "x":
66
+ case 'x':
66
67
  param = param.toString(16);
67
68
  break;
68
- case "X":
69
+ case 'X':
69
70
  param = param.toString(16).toUpperCase();
70
71
  break;
71
- case "b":
72
+ case 'b':
72
73
  param = param.toString(2);
73
74
  break;
74
- case "e":
75
+ case 'e':
75
76
  switch (param_type) {
76
77
  case 'number':
77
78
  param = param.toExponential();
@@ -87,7 +88,7 @@ function fmt_raw(str, params, options = { colors: false }) {
87
88
  break;
88
89
  }
89
90
  break;
90
- case "E":
91
+ case 'E':
91
92
  switch (param_type) {
92
93
  case 'number':
93
94
  param = param.toExponential().toUpperCase();
@@ -103,7 +104,15 @@ function fmt_raw(str, params, options = { colors: false }) {
103
104
  break;
104
105
  }
105
106
  break;
106
- 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;
107
116
  param = (0, node_util_1.inspect)(param, {
108
117
  depth: Infinity,
109
118
  colors: options.colors,
@@ -115,11 +124,14 @@ function fmt_raw(str, params, options = { colors: false }) {
115
124
  break;
116
125
  }
117
126
  ;
127
+ if (true_length == -1) {
128
+ true_length = param.length;
129
+ }
118
130
  // Compute radix-point precision on numbers
119
131
  if (param_type == 'number' && $precision) {
120
- let [pre, post] = param.split(".");
132
+ let [pre, post] = param.split('.');
121
133
  if (post === undefined) {
122
- post = "";
134
+ post = '';
123
135
  }
124
136
  let precision = +$precision.substring(1, $precision.length);
125
137
  if (post.length > precision) {
@@ -129,7 +141,7 @@ function fmt_raw(str, params, options = { colors: false }) {
129
141
  while (post.length < precision) {
130
142
  post = post + '0';
131
143
  }
132
- param = pre + "." + post;
144
+ param = pre + '.' + post;
133
145
  }
134
146
  let width;
135
147
  if ($width === undefined) {
@@ -153,9 +165,20 @@ function fmt_raw(str, params, options = { colors: false }) {
153
165
  else {
154
166
  maybe_sign = '';
155
167
  }
156
- //If the type is x or X and pretty printing enabled, add 0x
157
- if ((($type === 'x') || ($type === 'X')) && ($pretty === '#')) {
158
- maybe_sign += '0x';
168
+ // If pretty printing is enabled and the formating calls for a prefix, add it
169
+ if ($pretty === '#') {
170
+ switch ($type) {
171
+ case 'o':
172
+ maybe_sign += "0o";
173
+ break;
174
+ case 'x':
175
+ case 'X':
176
+ maybe_sign += "0x";
177
+ break;
178
+ case 'b':
179
+ maybe_sign += "0b";
180
+ break;
181
+ }
159
182
  }
160
183
  //pad with zeroes if specified
161
184
  if ($pad_zeroes === '0') {
@@ -166,31 +189,27 @@ function fmt_raw(str, params, options = { colors: false }) {
166
189
  }
167
190
  param = maybe_sign + param;
168
191
  }
169
- if (!filled && width) {
192
+ if (!filled && width > true_length) {
170
193
  // Compute fill/align
171
194
  $align_direction ||= '>';
172
195
  $fill_character ||= ' ';
196
+ let left = '';
197
+ let right = '';
198
+ let diff = width - true_length;
173
199
  switch ($align_direction) {
174
200
  case '>':
175
- while (width - param.length > 0) {
176
- param = $fill_character + param;
177
- }
201
+ left = $fill_character.repeat(diff);
178
202
  break;
179
203
  case '<':
180
- while (width - param.length > 0) {
181
- param = param + $fill_character;
182
- }
204
+ right = $fill_character.repeat(diff);
183
205
  break;
184
206
  case '^':
185
- while (width - param.length > 1) {
186
- param = $fill_character + param + $fill_character;
187
- }
188
- // Prioritise right-aligment on uneven alignment
189
- if (width - param.length == 1) {
190
- param = $fill_character + param;
191
- }
207
+ left = $fill_character.repeat(diff - diff / 2);
208
+ // Prioritise right-aligment on uneven length
209
+ right = $fill_character.repeat(diff / 2 + diff % 2);
192
210
  break;
193
211
  }
212
+ param = left + param + right;
194
213
  }
195
214
  return param;
196
215
  });
package/lib/print.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import { Writable } from 'node:stream';
3
- export declare function Printer(outStream?: Writable, errStream?: Writable): {
3
+ export declare function Printer(outStream?: Writable, errStream?: Writable, options?: {
4
+ debugColors: boolean;
5
+ }): {
4
6
  /**
5
7
  * Print a format string to an output stream (usually process.stdout).
6
8
  *
package/lib/print.js CHANGED
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.eprintln = exports.eprint = exports.println = exports.print = exports.Printer = void 0;
8
8
  const node_process_1 = __importDefault(require("node:process"));
9
9
  const format_1 = require("./format");
10
- function Printer(outStream = node_process_1.default.stdout, errStream = node_process_1.default.stderr) {
10
+ function Printer(outStream = node_process_1.default.stdout, errStream = node_process_1.default.stderr, options = { debugColors: true }) {
11
11
  return {
12
12
  /**
13
13
  * Print a format string to an output stream (usually process.stdout).
@@ -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.fmt_raw)(format_string, params, { colors: true }));
19
+ outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: options.debugColors }));
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.fmt_raw)(format_string, params, { colors: true }) + '\n');
29
+ outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: options.debugColors }) + '\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.fmt_raw)(format_string, params, { colors: true }));
38
+ errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: options.debugColors }));
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.fmt_raw)(format_string, params, { colors: true }) + '\n');
48
+ errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: options.debugColors }) + '\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.1",
3
+ "version": "0.2.3",
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",
@@ -36,5 +32,8 @@
36
32
  "devDependencies": {
37
33
  "@types/node": "^22.8.6"
38
34
  },
39
- "packageManager": "pnpm@9.14.2+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "test": "echo \"Error: no test specified\" && exit 1"
38
+ }
40
39
  }