rsformat 1.1.3 → 1.2.0

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 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.formatParam = exports.buildString = exports.RsString = void 0;
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
- let stringified = param.toString() + string.substring(1);
64
- raw += stringified;
65
- colored += stringified;
66
- continue;
66
+ string = string.substring(1);
67
+ skipParsing = true;
67
68
  }
68
69
  }
69
- else {
70
- let stringified = param.toString() + string;
71
- raw += stringified;
72
- colored += stringified;
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 formatted = formatParam(param, {
154
+ let [formatted_colored, formatted_raw] = formatParam(param, {
153
155
  fill,
154
156
  align,
155
157
  force_sign,
@@ -160,94 +162,99 @@ function buildString(strings, params) {
160
162
  type: format_type
161
163
  });
162
164
  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;
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
- // Process parameter type
182
- switch (format.type) {
183
- case 'o':
184
- param = param.toString(8);
185
- break;
186
- case 'x':
187
- param = param.toString(16);
188
- break;
189
- case 'X':
190
- param = param.toString(16).toUpperCase();
191
- break;
192
- case 'b':
193
- param = param.toString(2);
194
- break;
195
- case 'e':
196
- case 'E':
197
- if (param_type != 'number' && param_type != 'bigint') {
179
+ let param_colored = "";
180
+ // embed RsStrings directly
181
+ if (param instanceof String && format.type != '?') {
182
+ console.log("rsString passed");
183
+ param_colored = param.toString(true);
184
+ param = param.toString(false);
185
+ }
186
+ else
187
+ switch (format.type) {
188
+ // Process format type
189
+ case 'o':
190
+ param = param.toString(8);
191
+ break;
192
+ case 'x':
193
+ param = param.toString(16);
194
+ break;
195
+ case 'X':
196
+ param = param.toString(16).toUpperCase();
197
+ break;
198
+ case 'b':
199
+ param = param.toString(2);
200
+ break;
201
+ case 'e':
202
+ case 'E':
203
+ if (param_type != 'number' && param_type != 'bigint') {
204
+ param = param.toString();
205
+ break;
206
+ }
207
+ param = param.toLocaleString('en-US', { notation: 'scientific', maximumFractionDigits: 20 });
208
+ if (format.type == 'e')
209
+ param = param.toLowercase();
210
+ break;
211
+ case 'n':
212
+ case 'N':
213
+ if (param_type != 'number' && param_type != 'bigint') {
214
+ param = param.toString();
215
+ break;
216
+ }
217
+ // Round and add suffix
218
+ if (param_type == 'number')
219
+ param = Math.round(param);
198
220
  param = param.toString();
221
+ let last_2_digits = param.substring(param.length - 2);
222
+ if (last_2_digits == '11' || last_2_digits == '12' || last_2_digits == '13') {
223
+ param = param + 'th';
224
+ }
225
+ else
226
+ switch (last_2_digits[last_2_digits.length - 1]) {
227
+ case '1':
228
+ param = param + 'st';
229
+ break;
230
+ case '2':
231
+ param = param + 'nd';
232
+ break;
233
+ case '3':
234
+ param = param + 'rd';
235
+ break;
236
+ default: param = param + 'th';
237
+ }
238
+ if (format.type == 'N')
239
+ param = param.toUpperCase();
240
+ // Do not pad with zeroes or align to precision when using ordinal formatting
241
+ format.pad_zeroes = false;
242
+ format.precision = -1;
199
243
  break;
200
- }
201
- param = param.toLocaleString('en-US', { notation: 'scientific', maximumFractionDigits: 20 });
202
- if (format.type == 'e')
203
- param = param.toLowercase();
204
- break;
205
- case 'n':
206
- case 'N':
207
- if (param_type != 'number' && param_type != 'bigint') {
244
+ case '?':
245
+ param_colored = node_util_1.default.inspect(param, {
246
+ depth: Infinity,
247
+ colors: true,
248
+ compact: !format.pretty
249
+ });
250
+ param = node_util_1.default.stripVTControlCharacters(param_colored);
251
+ // Do not force sign, pad with zeroes or align to precision when using debug formatting
252
+ param_type = 'string';
253
+ break;
254
+ default:
208
255
  param = param.toString();
209
256
  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
- }
257
+ }
251
258
  ;
252
259
  // Compute radix-point precision on numbers
253
260
  if (param_type == 'number' && format.precision != -1) {
@@ -300,7 +307,9 @@ function formatParam(param, format) {
300
307
  }
301
308
  param = maybe_sign + param;
302
309
  }
303
- if ( /*!filled && */format.width > param.length) {
310
+ if (param_colored == "")
311
+ param_colored = param;
312
+ if (format.width > param.length) {
304
313
  // Compute fill/align
305
314
  let left = '';
306
315
  let right = '';
@@ -319,7 +328,7 @@ function formatParam(param, format) {
319
328
  break;
320
329
  }
321
330
  param = left + param + right;
331
+ param_colored = left + param_colored + right;
322
332
  }
323
- return param;
333
+ return [param_colored, param];
324
334
  }
325
- exports.formatParam = formatParam;
package/lib/print.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { Writable } from 'node:stream';
3
2
  /**
4
3
  * Print a string (or instance of String/RsString) to a `Writable` stream.
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.dbg = exports.eprintln = exports.eprint = exports.println = exports.print = exports.printToStream = void 0;
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(true);
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",
3
+ "version": "1.2.0",
4
4
  "description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
5
5
  "files": [
6
6
  "lib",