rsformat 0.1.1 → 0.2.1

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
@@ -1,6 +1,6 @@
1
1
  # RSFormat
2
2
 
3
- RSFormat is a string formatting/printing library for JavaScript. It offers an alternative to the string formatting and printing provided by `console.log`.
3
+ RSFormat is a string formatting/printing library for JavaScript. It offers a minimal, yet powerful and flexible alternative to the string formatting and printing provided by `console.log`.
4
4
 
5
5
  ## Motivation
6
6
 
@@ -123,11 +123,48 @@ If you want to use the print function to output to anything other than `process.
123
123
 
124
124
  ```ts
125
125
  // Custom output example (ts)
126
- import { Printer } from 'rsformat-local/print';
126
+ import { Printer } from 'rsformat/print';
127
127
  import { Writable } from 'stream';
128
128
 
129
129
  let someOutputStream: Writable = /* ... */;
130
130
  let someErrorStream: Writable = /* ... */;
131
131
 
132
132
  let { print, println, eprint, eprintln } = Printer(someOutputStream, someErrorStream);
133
- ```
133
+ ```
134
+
135
+ ## A Note on Performance
136
+
137
+ You might think that these utilities might have a performance impact on RSFormat's printing functions. And while they do, the functions are still consistently faster than `console.log`.
138
+
139
+ A simple benchmark setup like the one below will demonstrate that `println` is more performant, even when doing things like base conversions and text alignment, compared to `console.log` logging a simple string:
140
+
141
+ ```js
142
+ // benchmark.mjs
143
+ import { println } from 'rsformat';
144
+
145
+ const time = (fn, iter) => {
146
+ let time = Date.now();
147
+ while (iter-- > 0) {
148
+ fn();
149
+ }
150
+ return Date.now() - time;
151
+ }
152
+
153
+ let iterations = 100000;
154
+
155
+ let logTime = time(() => console.log('hello'), iterations);
156
+ let printlnTime = time(() => println('{:>+#7X}', 255), iterations);
157
+
158
+ println('console.log time for {} executions: {}ms', iterations, logTime);
159
+ println('rsformat.println time for {} executions: {}ms', iterations, printlnTime);
160
+ ```
161
+
162
+ ```
163
+ > node benchmark.mjs
164
+ ...After a lot of output...
165
+
166
+ console.log time for 100000 executions: 7217ms
167
+ rsformat.println time for 100000 executions: 5900ms
168
+ ```
169
+
170
+ _Tested on node.js using a Windows laptop on an Intel core I7-1360P, on battery power. Performance will vary, but this benchmark was just to show that RSFormat has no performance penalty._
package/lib/format.d.ts CHANGED
@@ -5,3 +5,14 @@
5
5
  * @param params Parameters to be inserted into the format string
6
6
  */
7
7
  export declare function format(str: string, ...params: any[]): string;
8
+ /**
9
+ * Raw formatting behaviour function called by `format` and printing functions.
10
+ *
11
+ * @param str String used for formatting
12
+ * @param params Parameters to be inserted into the format string
13
+ * @param options Options passed into formatting
14
+ * @param options.colors Whether to use colors in debug formatting
15
+ */
16
+ export declare function fmt_raw(str: string, params: any[], options?: {
17
+ colors: boolean;
18
+ }): string;
package/lib/format.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.format = void 0;
4
- const util_1 = require("util");
3
+ exports.fmt_raw = exports.format = void 0;
4
+ const node_util_1 = require("node:util");
5
5
  /**
6
6
  * Regex to match for possible formatting insertion points.
7
7
  * Similar to the way formatting is parsed in rust,
@@ -27,6 +27,18 @@ const FORMAT_REGEX = (/\{{2}|\}{2}|\{(\d*?)(?::(?:(.?)(\^|>|<))?(\+)?(#)?(0)?(\d
27
27
  * @param params Parameters to be inserted into the format string
28
28
  */
29
29
  function format(str, ...params) {
30
+ return fmt_raw(str, params);
31
+ }
32
+ exports.format = format;
33
+ /**
34
+ * Raw formatting behaviour function called by `format` and printing functions.
35
+ *
36
+ * @param str String used for formatting
37
+ * @param params Parameters to be inserted into the format string
38
+ * @param options Options passed into formatting
39
+ * @param options.colors Whether to use colors in debug formatting
40
+ */
41
+ function fmt_raw(str, params, options = { colors: false }) {
30
42
  // Counter used for insertion of unnumbered values
31
43
  let param_counter = 0;
32
44
  str = str.replace(FORMAT_REGEX, ($, $param_number, $fill_character, $align_direction, $sign, $pretty, $pad_zeroes, $width, $precision, $type) => {
@@ -92,9 +104,9 @@ function format(str, ...params) {
92
104
  }
93
105
  break;
94
106
  case "?":
95
- param = (0, util_1.inspect)(param, {
107
+ param = (0, node_util_1.inspect)(param, {
96
108
  depth: Infinity,
97
- colors: true,
109
+ colors: options.colors,
98
110
  compact: $pretty !== '#'
99
111
  });
100
112
  break;
@@ -184,4 +196,4 @@ function format(str, ...params) {
184
196
  });
185
197
  return str;
186
198
  }
187
- exports.format = format;
199
+ exports.fmt_raw = fmt_raw;
package/lib/print.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Writable } from 'stream';
2
+ import { Writable } from 'node:stream';
3
3
  export declare function Printer(outStream?: Writable, errStream?: Writable): {
4
4
  /**
5
5
  * Print a format string to an output stream (usually process.stdout).
package/lib/print.js CHANGED
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  var _a;
3
6
  Object.defineProperty(exports, "__esModule", { value: true });
4
7
  exports.eprintln = exports.eprint = exports.println = exports.print = exports.Printer = void 0;
8
+ const node_process_1 = __importDefault(require("node:process"));
5
9
  const format_1 = require("./format");
6
- function Printer(outStream = process.stdout, errStream = process.stderr) {
10
+ function Printer(outStream = node_process_1.default.stdout, errStream = node_process_1.default.stderr) {
7
11
  return {
8
12
  /**
9
13
  * Print a format string to an output stream (usually process.stdout).
@@ -12,7 +16,7 @@ function Printer(outStream = process.stdout, errStream = process.stderr) {
12
16
  * @param params Parameters to be inserted into the format string
13
17
  */
14
18
  print: function print(format_string, ...params) {
15
- outStream.write((0, format_1.format)(format_string, ...params));
19
+ outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }));
16
20
  },
17
21
  /**
18
22
  * Print a format string to an output stream (usually process.stdout)
@@ -22,7 +26,7 @@ function Printer(outStream = process.stdout, errStream = process.stderr) {
22
26
  * @param params Parameters to be inserted into the format string
23
27
  */
24
28
  println: function println(format_string, ...params) {
25
- outStream.write((0, format_1.format)(format_string, ...params) + "\n");
29
+ outStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }) + '\n');
26
30
  },
27
31
  /**
28
32
  * Print a format string to an error stream (usually process.stderr).
@@ -31,7 +35,7 @@ function Printer(outStream = process.stdout, errStream = process.stderr) {
31
35
  * @param params Parameters to be inserted into the format string
32
36
  */
33
37
  eprint: function eprint(format_string, ...params) {
34
- errStream.write((0, format_1.format)(format_string, ...params));
38
+ errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }));
35
39
  },
36
40
  /**
37
41
  * Print a format string to an error stream (usually process.stderr)
@@ -41,7 +45,7 @@ function Printer(outStream = process.stdout, errStream = process.stderr) {
41
45
  * @param params Parameters to be inserted into the format string
42
46
  */
43
47
  eprintln: function eprintln(format_string, ...params) {
44
- errStream.write((0, format_1.format)(format_string, ...params) + "\n");
48
+ errStream.write((0, format_1.fmt_raw)(format_string, params, { colors: true }) + '\n');
45
49
  },
46
50
  };
47
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsformat",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Formatting/printing library for JavaScript that takes after rust's string formatting ",
5
5
  "files": [
6
6
  "lib",
@@ -28,11 +28,13 @@
28
28
  "println",
29
29
  "printf",
30
30
  "console",
31
- "log"
31
+ "log",
32
+ "console.log"
32
33
  ],
33
34
  "author": "Alfio (https://github.com/p2js)",
34
35
  "license": "ISC",
35
36
  "devDependencies": {
36
37
  "@types/node": "^22.8.6"
37
- }
38
+ },
39
+ "packageManager": "pnpm@9.14.2+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
38
40
  }