rsformat 0.1.0 → 0.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/README.md +40 -3
- package/lib/format.js +3 -3
- package/lib/print.d.ts +1 -1
- package/lib/print.js +5 -1
- package/package.json +1 -1
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
|
|
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
|
|
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.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.format = void 0;
|
|
4
|
-
const
|
|
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,
|
|
@@ -38,7 +38,7 @@ function format(str, ...params) {
|
|
|
38
38
|
return '}';
|
|
39
39
|
}
|
|
40
40
|
// Process parameter number; increment param_counter if not included
|
|
41
|
-
let param = $param_number ===
|
|
41
|
+
let param = $param_number === ''
|
|
42
42
|
? params[param_counter++]
|
|
43
43
|
: params[+$param_number];
|
|
44
44
|
if (param === undefined) {
|
|
@@ -92,7 +92,7 @@ function format(str, ...params) {
|
|
|
92
92
|
}
|
|
93
93
|
break;
|
|
94
94
|
case "?":
|
|
95
|
-
param = (0,
|
|
95
|
+
param = (0, node_util_1.inspect)(param, {
|
|
96
96
|
depth: Infinity,
|
|
97
97
|
colors: true,
|
|
98
98
|
compact: $pretty !== '#'
|
package/lib/print.d.ts
CHANGED
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 =
|
|
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).
|