console-toolkit 1.2.15 → 1.3.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 +3 -0
- package/llms-full.txt +64 -4
- package/package.json +6 -6
- package/src/alphanumeric/arrows.js +0 -23
- package/src/alphanumeric/fractions.js +0 -49
- package/src/alphanumeric/number-formatters.js +0 -44
- package/src/alphanumeric/roman.js +0 -12
- package/src/alphanumeric/unicode-cultural-numbers.js +0 -1
- package/src/alphanumeric/unicode-letters.js +0 -8
- package/src/alphanumeric/unicode-numbers.js +0 -21
- package/src/alphanumeric/utils.js +0 -26
- package/src/ansi/csi.js +0 -49
- package/src/ansi/sgr-state.js +7 -50
- package/src/ansi/sgr.js +0 -420
- package/src/box.d.ts +4 -1
- package/src/box.js +15 -115
- package/src/charts/bars/block-frac-grouped.js +0 -6
- package/src/charts/bars/block-frac.js +1 -14
- package/src/charts/bars/block-grouped.js +0 -6
- package/src/charts/bars/block.js +1 -14
- package/src/charts/bars/draw-grouped.js +0 -4
- package/src/charts/bars/draw-stacked.js +0 -4
- package/src/charts/bars/frac-grouped.js +2 -14
- package/src/charts/bars/plain-grouped.js +0 -6
- package/src/charts/bars/plain.js +1 -28
- package/src/charts/columns/block-frac-grouped.js +0 -6
- package/src/charts/columns/block-frac.js +0 -13
- package/src/charts/columns/block-grouped.js +0 -6
- package/src/charts/columns/block.js +0 -13
- package/src/charts/columns/draw-grouped.js +1 -5
- package/src/charts/columns/draw-stacked.js +0 -4
- package/src/charts/columns/frac-grouped.js +1 -13
- package/src/charts/columns/plain-grouped.js +0 -6
- package/src/charts/columns/plain.js +0 -13
- package/src/charts/themes/default.js +0 -1
- package/src/charts/themes/rainbow-reversed.js +0 -1
- package/src/charts/themes/rainbow.js +0 -1
- package/src/charts/utils.js +2 -28
- package/src/draw-block-frac.js +0 -14
- package/src/draw-block.js +0 -24
- package/src/meta.js +0 -64
- package/src/output/show.d.ts +8 -3
- package/src/output/show.js +7 -38
- package/src/output/updater.d.ts +8 -3
- package/src/output/updater.js +4 -51
- package/src/output/writer.js +1 -53
- package/src/panel.d.ts +16 -10
- package/src/panel.js +21 -276
- package/src/plot/bitmap.js +5 -33
- package/src/plot/draw-line.js +0 -8
- package/src/plot/draw-rect.js +25 -103
- package/src/plot/index.js +0 -22
- package/src/plot/to-quads.js +3 -6
- package/src/spinner/spin.js +23 -20
- package/src/spinner/spinner.d.ts +16 -2
- package/src/spinner/spinner.js +22 -34
- package/src/spinner/spinners.js +0 -16
- package/src/strings/clip.js +0 -10
- package/src/strings/parse.js +0 -7
- package/src/strings/split.js +0 -15
- package/src/strings.d.ts +2 -0
- package/src/strings.js +2 -32
- package/src/style.js +2 -58
- package/src/symbols.js +0 -84
- package/src/table/draw-borders.js +0 -9
- package/src/table/index.d.ts +1 -1
- package/src/table/index.js +0 -1
- package/src/table/table.js +3 -58
- package/src/themes/blocks/unicode-half.js +0 -1
- package/src/themes/blocks/unicode-thin.js +0 -1
- package/src/themes/lines/ascii-compact.js +5 -9
- package/src/themes/lines/ascii-dots.js +2 -7
- package/src/themes/lines/ascii-girder.js +4 -7
- package/src/themes/lines/ascii-github.js +5 -9
- package/src/themes/lines/ascii-reddit.js +5 -9
- package/src/themes/lines/ascii-rounded.js +5 -9
- package/src/themes/lines/ascii.js +5 -9
- package/src/themes/lines/unicode-bold.js +8 -14
- package/src/themes/lines/unicode-rounded.js +8 -14
- package/src/themes/lines/unicode.js +8 -14
- package/src/themes/utils.d.ts +6 -0
- package/src/themes/utils.js +6 -7
- package/src/turtle/draw-line-art.js +26 -18
- package/src/turtle/draw-unicode.js +0 -8
- package/src/turtle/index.d.ts +1 -1
- package/src/turtle/index.js +0 -8
- package/src/turtle/turtle.js +0 -120
package/README.md
CHANGED
|
@@ -132,6 +132,7 @@ BSD 3-Clause License
|
|
|
132
132
|
|
|
133
133
|
## Release history
|
|
134
134
|
|
|
135
|
+
- 1.3.0 _Spinner API split (`tick`/`getFrame`/`nextFrame`), JSDoc removed from JS files, minor fixes._
|
|
135
136
|
- 1.2.15 _TypeScript 6.0 compatibility, updated dev deps, CI, license year._
|
|
136
137
|
- 1.2.14 _Updated dev deps._
|
|
137
138
|
- 1.2.13 _Fixed spinner .d.ts export, added TS type tests, stricter type checking, updated dev deps._
|
|
@@ -151,3 +152,5 @@ BSD 3-Clause License
|
|
|
151
152
|
- 1.1.1 _Minor bugfixes in `Table`, some improvements, updated deps._
|
|
152
153
|
- 1.1.0 _Minor improvements, enhanced `Writer` and `Updater`._
|
|
153
154
|
- 1.0.0 _Initial release._
|
|
155
|
+
|
|
156
|
+
The full release notes are in the wiki: [Release notes](https://github.com/uhop/console-toolkit/wiki/Release-notes).
|
package/llms-full.txt
CHANGED
|
@@ -165,8 +165,13 @@ class Panel {
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
function toPanel(s: any, options?: object): Panel; // Alias for Panel.make()
|
|
168
|
+
|
|
169
|
+
// Sentinel character ('\x07' BELL) used by Panel.put() to mark empty cells.
|
|
170
|
+
const EMPTY_CELL_SENTINEL: string;
|
|
168
171
|
```
|
|
169
172
|
|
|
173
|
+
When placing text via `panel.put()`, characters matching `emptySymbol` (default `EMPTY_CELL_SENTINEL`) become empty cells. The rendering character for empty cells is set later via `toStrings({emptySymbol})`.
|
|
174
|
+
|
|
170
175
|
---
|
|
171
176
|
|
|
172
177
|
## Module: Style (`console-toolkit/style`)
|
|
@@ -399,14 +404,69 @@ import theme from 'console-toolkit/themes/blocks/unicode-thin.js';
|
|
|
399
404
|
|
|
400
405
|
## Package: spinner (`console-toolkit/spinner`)
|
|
401
406
|
|
|
407
|
+
Spinners and updatable text components. Two layers:
|
|
408
|
+
|
|
409
|
+
- `SpinnerBase` / `Spinner` — frame-driven animations. Split API (since 1.3.0): `tick()` advances state, `getFrame()` reads current frame, `nextFrame()` is the convenience combo (`tick()` + `getFrame()`).
|
|
410
|
+
- `spin` — tagged template literal that composes a `SpinnerBase`-shaped object from a template containing nested spinners, arrays, and state-aware functions.
|
|
411
|
+
|
|
402
412
|
```ts
|
|
403
|
-
import spin from 'console-toolkit/spinner';
|
|
413
|
+
import spin, {Spinner, SpinnerBase} from 'console-toolkit/spinner';
|
|
414
|
+
|
|
415
|
+
class SpinnerBase {
|
|
416
|
+
active: boolean;
|
|
417
|
+
paused: boolean;
|
|
418
|
+
finished: boolean;
|
|
419
|
+
frameIndex: number;
|
|
404
420
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
421
|
+
readonly isStarted: boolean;
|
|
422
|
+
readonly isActive: boolean;
|
|
423
|
+
readonly isFinished: boolean;
|
|
424
|
+
state: '' | 'active' | 'paused' | 'finished';
|
|
425
|
+
|
|
426
|
+
reset(): void;
|
|
427
|
+
nextFrameIndex(length: number): number;
|
|
428
|
+
|
|
429
|
+
tick(): this; // advance internal state by one step (state-aware)
|
|
430
|
+
getFrame(): string; // read current frame, no mutation
|
|
431
|
+
nextFrame(): string; // tick() + getFrame()
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
interface SpinnerDefinition {
|
|
435
|
+
frames?: string[]; // active-state frames (default: braille spinner)
|
|
436
|
+
notStarted?: string[]; // not-started frames (default: [' '])
|
|
437
|
+
finished?: string[]; // finished frames (default: ['✓'])
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
class Spinner extends SpinnerBase {
|
|
441
|
+
spinner: Required<SpinnerDefinition>;
|
|
442
|
+
constructor(spinnerDefinition?: SpinnerDefinition, isStarted?: boolean);
|
|
443
|
+
tick(): this;
|
|
444
|
+
getFrame(): string;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Tagged template — composes nested Spinners, arrays, and (state) => string functions.
|
|
448
|
+
function spin(strings: TemplateStringsArray, ...args: unknown[]): SpinnerBase;
|
|
408
449
|
```
|
|
409
450
|
|
|
451
|
+
Usage with `Updater` (auto-refreshing terminal output):
|
|
452
|
+
|
|
453
|
+
```js
|
|
454
|
+
import spin, {Spinner} from 'console-toolkit/spinner';
|
|
455
|
+
import {bouncingBall} from 'console-toolkit/spinner/spinners.js';
|
|
456
|
+
import Updater from 'console-toolkit/output/updater.js';
|
|
457
|
+
|
|
458
|
+
const spinner = spin`Loading: [${new Spinner()}], ${new Spinner(bouncingBall)}, state: ${state => state}`;
|
|
459
|
+
const updater = new Updater(spinner);
|
|
460
|
+
if (updater.writer.isTTY) {
|
|
461
|
+
updater.update();
|
|
462
|
+
updater.startRefreshing();
|
|
463
|
+
}
|
|
464
|
+
// ... async work ...
|
|
465
|
+
await updater.final();
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
`Updater` calls `nextFrame()` on its target if available, falling back to `getFrame()`. Use `nextFrame()` directly when driving a spinner from your own render loop; use `getFrame()` to peek without advancing.
|
|
469
|
+
|
|
410
470
|
---
|
|
411
471
|
|
|
412
472
|
## Package: ansi (`console-toolkit/ansi`)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "console-toolkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Toolkit to produce a fancy console output (boxes, tables, charts, colors).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -85,12 +85,12 @@
|
|
|
85
85
|
]
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
|
-
"@types/node": "^25.
|
|
88
|
+
"@types/node": "^25.6.0",
|
|
89
89
|
"emoji-regex": "^10.6.0",
|
|
90
90
|
"get-east-asian-width": "^1.5.0",
|
|
91
|
-
"prettier": "^3.8.
|
|
92
|
-
"tape-six": "^1.
|
|
93
|
-
"tape-six-proc": "^1.2.
|
|
94
|
-
"typescript": "^6.0.
|
|
91
|
+
"prettier": "^3.8.3",
|
|
92
|
+
"tape-six": "^1.9.0",
|
|
93
|
+
"tape-six-proc": "^1.2.9",
|
|
94
|
+
"typescript": "^6.0.3"
|
|
95
95
|
}
|
|
96
96
|
}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
/** Direction index for left-pointing arrows. */
|
|
2
1
|
export const LEFT = 0,
|
|
3
|
-
/** Direction index for up-pointing arrows. */
|
|
4
2
|
UP = 1,
|
|
5
|
-
/** Direction index for right-pointing arrows. */
|
|
6
3
|
RIGHT = 2,
|
|
7
|
-
/** Direction index for down-pointing arrows. */
|
|
8
4
|
DOWN = 3;
|
|
9
5
|
|
|
10
6
|
const toSymbol = (...args) => args.map(x => (typeof x == 'number' ? String.fromCodePoint(x) : x));
|
|
@@ -13,42 +9,23 @@ const makeArrowsH = codePoint => toSymbol(codePoint, ' ', codePoint + 1, ' ');
|
|
|
13
9
|
|
|
14
10
|
// Arrows
|
|
15
11
|
|
|
16
|
-
/** Simple arrows [left, up, right, down]. */
|
|
17
12
|
export const simple = makeArrows(0x2190);
|
|
18
|
-
/** Arrows with stroke [left, up, right, down]. */
|
|
19
13
|
export const withStroke = makeArrowsH(0x219a);
|
|
20
|
-
/** Wave arrows [left, up, right, down]. */
|
|
21
14
|
export const wave = makeArrowsH(0x219c);
|
|
22
|
-
/** Two-headed arrows [left, up, right, down]. */
|
|
23
15
|
export const twoHeaded = makeArrows(0x219e);
|
|
24
|
-
/** Arrows with tail [left, up, right, down]. */
|
|
25
16
|
export const withTail = makeArrowsH(0x21a2);
|
|
26
|
-
/** Arrows from bar [left, up, right, down]. */
|
|
27
17
|
export const fromBar = makeArrows(0x21a4);
|
|
28
|
-
/** Arrows with loop [left, up, right, down]. */
|
|
29
18
|
export const withLoop = makeArrowsH(0x21ab);
|
|
30
|
-
/** Double arrows [left, up, right, down]. */
|
|
31
19
|
export const double = makeArrows(0x21d0);
|
|
32
|
-
/** Triple arrows [left, up, right, down]. */
|
|
33
20
|
export const triple = makeArrowsH(0x21da);
|
|
34
|
-
/** Squiggle arrows [left, up, right, down]. */
|
|
35
21
|
export const squiggle = makeArrowsH(0x21dc);
|
|
36
|
-
/** Dashed arrows [left, up, right, down]. */
|
|
37
22
|
export const dashed = makeArrows(0x21e0);
|
|
38
|
-
/** Arrows to bar [left, up, right, down]. */
|
|
39
23
|
export const toBar = makeArrowsH(0x21e4);
|
|
40
|
-
/** White (outline) arrows [left, up, right, down]. */
|
|
41
24
|
export const white = makeArrows(0x21e6);
|
|
42
|
-
/** Arrows with vertical stroke [left, up, right, down]. */
|
|
43
25
|
export const withVStroke = makeArrowsH(0x21f7);
|
|
44
|
-
/** Arrows with double vertical stroke [left, up, right, down]. */
|
|
45
26
|
export const withDoubleVStroke = makeArrowsH(0x21fa);
|
|
46
|
-
/** Open-headed arrows [left, up, right, down]. */
|
|
47
27
|
export const openHeaded = makeArrowsH(0x21fd);
|
|
48
28
|
|
|
49
|
-
/** Arrows with barb up [left, up, right, down]. */
|
|
50
29
|
export const withBarbUp = toSymbol(0x21bc, 0x21be, 0x21c1, 0x21c3);
|
|
51
|
-
/** Arrows with barb down [left, up, right, down]. */
|
|
52
30
|
export const withBarbDown = toSymbol(0x21bd, 0x21bf, 0x21c0, 0x21c2);
|
|
53
|
-
/** Double arrows with stroke [left, up, right, down]. */
|
|
54
31
|
export const doubleWithStroke = toSymbol(0x21cd, ' ', 0x21cf, ' ');
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/** All available Unicode fraction characters, sorted by value.
|
|
2
|
-
* Each entry has `numerator`, `denominator`, `symbol`, and `value` properties.
|
|
3
|
-
* @type {{numerator: number, denominator: number, symbol: string, value: number}[]}
|
|
4
|
-
*/
|
|
5
1
|
export const fractions = [
|
|
6
2
|
[1, 7, '\u2150'],
|
|
7
3
|
[1, 9, '\u2151'],
|
|
@@ -28,25 +24,10 @@ export const fractions = [
|
|
|
28
24
|
|
|
29
25
|
const pick = denominator => fractions.filter(x => !(denominator % x.denominator));
|
|
30
26
|
|
|
31
|
-
/** Unicode fraction characters with denominator 3.
|
|
32
|
-
* @type {import('./fractions.js').Fraction[]}
|
|
33
|
-
*/
|
|
34
27
|
export const thirds = pick(3);
|
|
35
|
-
/** Unicode fraction characters with denominator 4.
|
|
36
|
-
* @type {import('./fractions.js').Fraction[]}
|
|
37
|
-
*/
|
|
38
28
|
export const quarters = [{numerator: 0, denominator: 4, symbol: '0', value: 0}, ...pick(4)];
|
|
39
|
-
/** Unicode fraction characters with denominator 5.
|
|
40
|
-
* @type {import('./fractions.js').Fraction[]}
|
|
41
|
-
*/
|
|
42
29
|
export const fifths = [{numerator: 0, denominator: 5, symbol: '0', value: 0}, ...pick(5)];
|
|
43
|
-
/** Unicode fraction characters with denominator 6.
|
|
44
|
-
* @type {import('./fractions.js').Fraction[]}
|
|
45
|
-
*/
|
|
46
30
|
export const sixths = pick(6);
|
|
47
|
-
/** Unicode fraction characters with denominator 8.
|
|
48
|
-
* @type {import('./fractions.js').Fraction[]}
|
|
49
|
-
*/
|
|
50
31
|
export const eighths = [{numerator: 0, denominator: 8, symbol: '0', value: 0}, ...pick(8)];
|
|
51
32
|
|
|
52
33
|
export {quarters as fourths};
|
|
@@ -91,41 +72,11 @@ const findSymbol = (fractions, value, useFractionForZero) => {
|
|
|
91
72
|
return (int || '') + chosen.symbol;
|
|
92
73
|
};
|
|
93
74
|
|
|
94
|
-
/** Finds the closest Unicode fraction symbol for a given value.
|
|
95
|
-
* @param {number} value - The value to approximate.
|
|
96
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
97
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
98
|
-
*/
|
|
99
75
|
export const getFraction = (value, useFractionForZero) => findSymbol(fractions, value, useFractionForZero);
|
|
100
|
-
/** Finds the closest Unicode fraction symbol from thirds.
|
|
101
|
-
* @param {number} value - The value to approximate.
|
|
102
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
103
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
104
|
-
*/
|
|
105
76
|
export const getThirds = (value, useFractionForZero) => findSymbol(thirds, value, useFractionForZero);
|
|
106
|
-
/** Finds the closest Unicode fraction symbol from quarters.
|
|
107
|
-
* @param {number} value - The value to approximate.
|
|
108
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
109
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
110
|
-
*/
|
|
111
77
|
export const getQuarters = (value, useFractionForZero) => findSymbol(quarters, value, useFractionForZero);
|
|
112
|
-
/** Finds the closest Unicode fraction symbol from fifths.
|
|
113
|
-
* @param {number} value - The value to approximate.
|
|
114
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
115
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
116
|
-
*/
|
|
117
78
|
export const getFifths = (value, useFractionForZero) => findSymbol(fifths, value, useFractionForZero);
|
|
118
|
-
/** Finds the closest Unicode fraction symbol from sixths.
|
|
119
|
-
* @param {number} value - The value to approximate.
|
|
120
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
121
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
122
|
-
*/
|
|
123
79
|
export const getSixths = (value, useFractionForZero) => findSymbol(sixths, value, useFractionForZero);
|
|
124
|
-
/** Finds the closest Unicode fraction symbol from eighths.
|
|
125
|
-
* @param {number} value - The value to approximate.
|
|
126
|
-
* @param {boolean} [useFractionForZero] - If true, use a fraction symbol even for zero.
|
|
127
|
-
* @returns {string} The integer part plus the closest fraction symbol.
|
|
128
|
-
*/
|
|
129
80
|
export const getEighths = (value, useFractionForZero) => findSymbol(eighths, value, useFractionForZero);
|
|
130
81
|
|
|
131
82
|
export {getQuarters as getFourths};
|
|
@@ -3,12 +3,6 @@
|
|
|
3
3
|
const units = ['s', 'ms', 'μs', 'ns', 'ps'],
|
|
4
4
|
unicodeUnits = ['s', '㎳', '㎲', '㎱', '㎰'];
|
|
5
5
|
|
|
6
|
-
/** Prepares a time format configuration for a set of values.
|
|
7
|
-
* @param {number[]} values - Array of time values.
|
|
8
|
-
* @param {number} [scale=1] - Scale factor.
|
|
9
|
-
* @param {boolean} [useUnicode] - If true, use Unicode unit symbols.
|
|
10
|
-
* @returns {{scale: number, precision: number, unit: string}} Format configuration.
|
|
11
|
-
*/
|
|
12
6
|
export const prepareTimeFormat = (values, scale = 1, useUnicode) => {
|
|
13
7
|
let mx = -1000,
|
|
14
8
|
mn = 1000;
|
|
@@ -28,11 +22,6 @@ export const prepareTimeFormat = (values, scale = 1, useUnicode) => {
|
|
|
28
22
|
return {scale, precision: digits - mx, unit: (useUnicode ? unicodeUnits : units)[i]};
|
|
29
23
|
};
|
|
30
24
|
|
|
31
|
-
/** Formats a time value using a prepared format configuration.
|
|
32
|
-
* @param {number} value - The time value.
|
|
33
|
-
* @param {{scale: number, precision: number, unit: string}} format - Format from `prepareTimeFormat`.
|
|
34
|
-
* @returns {string} Formatted time string.
|
|
35
|
-
*/
|
|
36
25
|
export const formatTime = (value, format) => {
|
|
37
26
|
let result = (value * format.scale).toFixed(format.precision);
|
|
38
27
|
if (format.precision > 0 && !format.keepFractionAsIs) result = result.replace(/\.0+$/, '');
|
|
@@ -52,28 +41,11 @@ const putCommasIn = (s, options) => {
|
|
|
52
41
|
);
|
|
53
42
|
};
|
|
54
43
|
|
|
55
|
-
/** Formats an integer with optional comma separators and sign.
|
|
56
|
-
* @param {number} value - The integer value.
|
|
57
|
-
* @param {object} [options] - Options.
|
|
58
|
-
* @param {string} [options.comma=','] - Thousands separator.
|
|
59
|
-
* @param {boolean} [options.keepSign] - If true, prefix positive numbers with '+'.
|
|
60
|
-
* @returns {string} Formatted string.
|
|
61
|
-
*/
|
|
62
44
|
export const formatInteger = (value, options) =>
|
|
63
45
|
isNaN(value)
|
|
64
46
|
? ''
|
|
65
47
|
: (value < 0 ? '-' : options?.keepSign ? '+' : '') + putCommasIn(Math.abs(value).toFixed(0), options);
|
|
66
48
|
|
|
67
|
-
/** Formats a number with optional decimals, comma separators, and sign.
|
|
68
|
-
* @param {number} value - The number.
|
|
69
|
-
* @param {object} [options] - Options.
|
|
70
|
-
* @param {number} [options.decimals=0] - Number of decimal places.
|
|
71
|
-
* @param {string} [options.comma=','] - Thousands separator.
|
|
72
|
-
* @param {string} [options.dot='.'] - Decimal separator.
|
|
73
|
-
* @param {boolean} [options.keepSign] - If true, prefix positive numbers with '+'.
|
|
74
|
-
* @param {boolean} [options.keepFractionAsIs] - If true, don't strip trailing zeros.
|
|
75
|
-
* @returns {string} Formatted string.
|
|
76
|
-
*/
|
|
77
49
|
export const formatNumber = (value, options) => {
|
|
78
50
|
if (isNaN(value)) return '';
|
|
79
51
|
const decimals = options?.decimals ?? 0;
|
|
@@ -93,11 +65,6 @@ export const formatNumber = (value, options) => {
|
|
|
93
65
|
const exp = [0, 0, 0, 0, 3, 3, 6, 6, 6, 9, 9, 9, 12];
|
|
94
66
|
const abbr = '***k**M**G**T';
|
|
95
67
|
|
|
96
|
-
/** Formats a number with SI abbreviations (k, M, G, T).
|
|
97
|
-
* @param {number} value - The number.
|
|
98
|
-
* @param {object} [options] - Options (same as `formatNumber`).
|
|
99
|
-
* @returns {string} Abbreviated formatted string.
|
|
100
|
-
*/
|
|
101
68
|
export const abbrNumber = (value, options) => {
|
|
102
69
|
if (isNaN(value)) return '';
|
|
103
70
|
const decimals = options?.decimals ?? 0;
|
|
@@ -126,20 +93,9 @@ export const abbrNumber = (value, options) => {
|
|
|
126
93
|
);
|
|
127
94
|
};
|
|
128
95
|
|
|
129
|
-
/** Simplifies the exponent notation of a number string.
|
|
130
|
-
* @param {string|number} s - The number or string.
|
|
131
|
-
* @param {object} [options] - Options.
|
|
132
|
-
* @param {boolean} [options.keepExpPlus] - If true, keep the '+' in the exponent.
|
|
133
|
-
* @returns {string} Simplified string.
|
|
134
|
-
*/
|
|
135
96
|
export const simplifyExponent = (s, {keepExpPlus} = {}) =>
|
|
136
97
|
String(s).replace(new RegExp('\\.?0*e' + (keepExpPlus ? '' : '\\+?'), 'i'), 'e');
|
|
137
98
|
|
|
138
|
-
/** Compares two positive numbers and returns a description of their difference.
|
|
139
|
-
* @param {number} a - First value.
|
|
140
|
-
* @param {number} b - Second value.
|
|
141
|
-
* @returns {{less: boolean, equality?: boolean, infinity?: boolean, percentage?: string, ratio?: string}}
|
|
142
|
-
*/
|
|
143
99
|
export const compareDifference = (a, b) => {
|
|
144
100
|
// works only on positive numbers
|
|
145
101
|
a = Math.abs(a);
|
|
@@ -24,10 +24,6 @@ const romanGroup = (value, one, five, ten) => {
|
|
|
24
24
|
return one + ten;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
/** Converts a positive integer (1-3999) to a Roman numeral string using ASCII characters.
|
|
28
|
-
* @param {number} value - Integer between 1 and 3999.
|
|
29
|
-
* @returns {string} Roman numeral string.
|
|
30
|
-
*/
|
|
31
27
|
export const toRoman = value => {
|
|
32
28
|
value = Math.round(value);
|
|
33
29
|
if (value < 1 || value > 3999)
|
|
@@ -86,13 +82,5 @@ const toRomanUnicodeFn = (roman, L, C, D, M) => value => {
|
|
|
86
82
|
return result.reverse().join('');
|
|
87
83
|
};
|
|
88
84
|
|
|
89
|
-
/** Converts a positive integer (1-3999) to a Roman numeral string using Unicode Roman numeral characters.
|
|
90
|
-
* @param {number} value - Integer between 1 and 3999.
|
|
91
|
-
* @returns {string} Unicode Roman numeral string.
|
|
92
|
-
*/
|
|
93
85
|
export const toRomanUnicode = toRomanUnicodeFn(roman, upperL, upperC, upperD, upperM);
|
|
94
|
-
/** Converts a positive integer (1-3999) to a lowercase Unicode Roman numeral string.
|
|
95
|
-
* @param {number} value - Integer between 1 and 3999.
|
|
96
|
-
* @returns {string} Lowercase Unicode Roman numeral string.
|
|
97
|
-
*/
|
|
98
86
|
export const toRomanLowerUnicode = toRomanUnicodeFn(romanLower, lowerL, lowerC, lowerD, lowerM);
|
|
@@ -5,7 +5,6 @@ const range = (fromCapital, fromSmall) =>
|
|
|
5
5
|
x => x
|
|
6
6
|
);
|
|
7
7
|
|
|
8
|
-
/** Transcode tables for mathematical and decorative letter styles. */
|
|
9
8
|
export const transcodeTables = {
|
|
10
9
|
bold: range('\u{1D400}', '\u{1D41A}'),
|
|
11
10
|
italic: range('\u{1D434}', '\u{1D44E}'),
|
|
@@ -57,13 +56,6 @@ transcodeTables.script[1].overlay = {e: '\u{212F}', g: '\u{210A}', o: '\u{2134}'
|
|
|
57
56
|
|
|
58
57
|
// API
|
|
59
58
|
|
|
60
|
-
/** Transcodes a string to a Unicode letter style.
|
|
61
|
-
* @param {string} s - The string to transcode.
|
|
62
|
-
* @param {string|import('./utils.js').SymbolRange[]} name - Style name (key of `transcodeTables`) or an array of SymbolRanges.
|
|
63
|
-
* @param {object} [options] - Options.
|
|
64
|
-
* @param {string} [options.missing] - Replacement for unmapped characters.
|
|
65
|
-
* @returns {string} The transcoded string.
|
|
66
|
-
*/
|
|
67
59
|
export const transcode = (s, name, options) => {
|
|
68
60
|
let tables = typeof name == 'string' ? transcodeTables[name] : name;
|
|
69
61
|
if (!tables) throw new Error(`There is no transcode table "${name}"`);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {SymbolRange, transcode as internalTranscode} from './utils.js';
|
|
2
2
|
import {minus, multiplication, superscriptPlus, superscriptMinus} from '../symbols.js';
|
|
3
3
|
|
|
4
|
-
/** Transcode tables for Unicode number styles and embellishments. */
|
|
5
4
|
export const transcodeTables = {
|
|
6
5
|
// Enclosed Alphanumeric
|
|
7
6
|
circled: new SymbolRange('①', 1, 20),
|
|
@@ -51,38 +50,18 @@ transcodeTables.superscript.overlay = {1: '¹', 2: '²', 3: '³'};
|
|
|
51
50
|
|
|
52
51
|
// API
|
|
53
52
|
|
|
54
|
-
/** Transcodes a string of digits to a Unicode number style.
|
|
55
|
-
* @param {string} s - The digit string to transcode.
|
|
56
|
-
* @param {string|import('./utils.js').SymbolRange} name - Style name (key of `transcodeTables`) or a SymbolRange.
|
|
57
|
-
* @param {object} [options] - Options.
|
|
58
|
-
* @param {string} [options.missing] - Replacement for unmapped characters.
|
|
59
|
-
* @returns {string} The transcoded string.
|
|
60
|
-
*/
|
|
61
53
|
export const transcode = (s, name, options) => {
|
|
62
54
|
const table = typeof name == 'string' ? transcodeTables[name] : name;
|
|
63
55
|
if (!table) throw new Error(`There is no transcode table "${name}"`);
|
|
64
56
|
return internalTranscode(s, table, options);
|
|
65
57
|
};
|
|
66
58
|
|
|
67
|
-
/** Replaces digit-punctuation pairs with Unicode enclosed digits.
|
|
68
|
-
* @param {string} s - The string containing digits with commas/dots.
|
|
69
|
-
* @param {object} [options] - Options for surrounding text.
|
|
70
|
-
* @param {string} [options.addBefore=''] - String to add before a digit.
|
|
71
|
-
* @param {string} [options.addAfter=' '] - String to add after a digit.
|
|
72
|
-
* @returns {string} The string with Unicode punctuation replacements.
|
|
73
|
-
*/
|
|
74
59
|
export const numberPunctuation = (s, {addBefore = '', addAfter = ' '} = {}) =>
|
|
75
60
|
s.replace(
|
|
76
61
|
/(\d)([\,\.])/g,
|
|
77
62
|
(_, d, p) => addBefore + transcodeTables[p === '.' ? 'dots' : 'commas'].transcode(d) + addAfter
|
|
78
63
|
);
|
|
79
64
|
|
|
80
|
-
/** Converts scientific notation to Unicode superscript exponent form.
|
|
81
|
-
* @param {string} s - The number string in scientific notation (e.g., '1.5e10').
|
|
82
|
-
* @param {object} [options] - Options.
|
|
83
|
-
* @param {boolean} [options.useSpecialMinus] - If true, use a special math minus symbol.
|
|
84
|
-
* @returns {string} The formatted string with Unicode superscripts.
|
|
85
|
-
*/
|
|
86
65
|
export const numberExponent = (s, {useSpecialMinus} = {}) => {
|
|
87
66
|
const r = /^([+-]?)([^e]+)e([+-]?)(.+)$/i.exec(s);
|
|
88
67
|
return r
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
/** Maps a range of numeric values to Unicode code points for transcoding digits/letters.
|
|
2
|
-
* Supports overlays for custom symbol replacements.
|
|
3
|
-
*/
|
|
4
1
|
export class SymbolRange {
|
|
5
|
-
/**
|
|
6
|
-
* @param {string} fromSymbol - The Unicode character corresponding to the `from` value.
|
|
7
|
-
* @param {number} [from=0] - Start of the range.
|
|
8
|
-
* @param {number} [to=9] - End of the range (inclusive).
|
|
9
|
-
* @param {string} [inputBase='0'] - The input character corresponding to 0.
|
|
10
|
-
*/
|
|
11
2
|
constructor(fromSymbol, from = 0, to = 9, inputBase = '0') {
|
|
12
3
|
this.from = from;
|
|
13
4
|
this.to = to;
|
|
@@ -15,10 +6,6 @@ export class SymbolRange {
|
|
|
15
6
|
this.inputBase = inputBase.codePointAt(0);
|
|
16
7
|
this.overlay = null;
|
|
17
8
|
}
|
|
18
|
-
/** Gets the transcoded symbol for a value or character.
|
|
19
|
-
* @param {number|string} i - Numeric index or input character.
|
|
20
|
-
* @returns {string|false} The transcoded symbol, or false if out of range.
|
|
21
|
-
*/
|
|
22
9
|
get(i) {
|
|
23
10
|
if (this.overlay) {
|
|
24
11
|
const result =
|
|
@@ -32,24 +19,11 @@ export class SymbolRange {
|
|
|
32
19
|
}
|
|
33
20
|
return this.from <= i && i <= this.to && String.fromCodePoint(this.base + i);
|
|
34
21
|
}
|
|
35
|
-
/** Transcodes a string by replacing each character with its mapped symbol.
|
|
36
|
-
* @param {string} s - The string to transcode.
|
|
37
|
-
* @param {object} [options] - Options.
|
|
38
|
-
* @param {string} [options.missing] - Replacement for unmapped characters.
|
|
39
|
-
* @returns {string} The transcoded string.
|
|
40
|
-
*/
|
|
41
22
|
transcode(s, {missing} = {}) {
|
|
42
23
|
return s.replace(/./g, missing ? m => this.get(m) || missing : m => this.get(m) || m);
|
|
43
24
|
}
|
|
44
25
|
}
|
|
45
26
|
|
|
46
|
-
/** Transcodes a string using one or more lookup tables.
|
|
47
|
-
* @param {string} s - The string to transcode.
|
|
48
|
-
* @param {Function|object|Array} tables - A function, object, SymbolRange, or array of them.
|
|
49
|
-
* @param {object} [options] - Options.
|
|
50
|
-
* @param {string} [options.missing] - Replacement for unmapped characters.
|
|
51
|
-
* @returns {string} The transcoded string.
|
|
52
|
-
*/
|
|
53
27
|
export const transcode = (s, tables, {missing} = {}) => {
|
|
54
28
|
let fn;
|
|
55
29
|
if (typeof tables == 'function') {
|
package/src/ansi/csi.js
CHANGED
|
@@ -7,9 +7,6 @@
|
|
|
7
7
|
export * from './sgr.js';
|
|
8
8
|
|
|
9
9
|
// matcher
|
|
10
|
-
/** RegExp matching CSI (Control Sequence Introducer) escape sequences with parameter, intermediate, and final byte groups.
|
|
11
|
-
* @type {RegExp}
|
|
12
|
-
*/
|
|
13
10
|
export const matchCsi = /\x1B\[([\x30-\x3F]*)([\x20-\x2F]*)([\x40-\x7E])/g;
|
|
14
11
|
|
|
15
12
|
const CSI = '\x1B[';
|
|
@@ -29,52 +26,14 @@ export const CURSOR_RESTORE_POS = CSI + 'u';
|
|
|
29
26
|
export const CURSOR_NORMAL = CSI + '?25h';
|
|
30
27
|
export const CURSOR_INVISIBLE = CSI + '?25l';
|
|
31
28
|
|
|
32
|
-
/** Moves the cursor up by `n` rows.
|
|
33
|
-
* @param {number} [n=1]
|
|
34
|
-
* @returns {string} CSI sequence.
|
|
35
|
-
*/
|
|
36
29
|
export const cursorUp = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'A';
|
|
37
|
-
/** Moves the cursor down by `n` rows.
|
|
38
|
-
* @param {number} [n=1]
|
|
39
|
-
* @returns {string} CSI sequence.
|
|
40
|
-
*/
|
|
41
30
|
export const cursorDown = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'B';
|
|
42
|
-
/** Moves the cursor forward by `n` columns.
|
|
43
|
-
* @param {number} [n=1]
|
|
44
|
-
* @returns {string} CSI sequence.
|
|
45
|
-
*/
|
|
46
31
|
export const cursorForward = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'C';
|
|
47
|
-
/** Moves the cursor back by `n` columns.
|
|
48
|
-
* @param {number} [n=1]
|
|
49
|
-
* @returns {string} CSI sequence.
|
|
50
|
-
*/
|
|
51
32
|
export const cursorBack = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'D';
|
|
52
|
-
/** Moves cursor to beginning of line `n` lines down.
|
|
53
|
-
* @param {number} [n=1]
|
|
54
|
-
* @returns {string} CSI sequence.
|
|
55
|
-
*/
|
|
56
33
|
export const cursorNextLine = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'E';
|
|
57
|
-
/** Moves cursor to beginning of line `n` lines up.
|
|
58
|
-
* @param {number} [n=1]
|
|
59
|
-
* @returns {string} CSI sequence.
|
|
60
|
-
*/
|
|
61
34
|
export const cursorPrevLine = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'F';
|
|
62
|
-
/** Moves cursor to column `n`.
|
|
63
|
-
* @param {number} [n=1]
|
|
64
|
-
* @returns {string} CSI sequence.
|
|
65
|
-
*/
|
|
66
35
|
export const cursorColumn = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'G';
|
|
67
|
-
/** Sets the cursor to row `n`, column `m` (1-based).
|
|
68
|
-
* @param {number} n - Row.
|
|
69
|
-
* @param {number} m - Column.
|
|
70
|
-
* @returns {string} CSI sequence.
|
|
71
|
-
*/
|
|
72
36
|
export const cursorSetPos = (n, m) => CSI + (n > 1 ? n.toFixed() : '') + ';' + (m > 1 ? m.toFixed() : '') + 'H';
|
|
73
|
-
/** Sets the cursor position (alternative HVP sequence).
|
|
74
|
-
* @param {number} n - Row.
|
|
75
|
-
* @param {number} m - Column.
|
|
76
|
-
* @returns {string} CSI sequence.
|
|
77
|
-
*/
|
|
78
37
|
export const cursorSetPosAlt = (n, m) => CSI + (n > 1 ? n.toFixed() : '') + ';' + (m > 1 ? m.toFixed() : '') + 'f'; // HVP
|
|
79
38
|
|
|
80
39
|
export const CURSOR_RIGHT1 = CURSOR_FORWARD1;
|
|
@@ -99,15 +58,7 @@ export const SCREEN_SCROLL_DOWN1 = CSI + 'T';
|
|
|
99
58
|
export const SCREEN_REPORT_FOCUS_ON = CSI + '?1004h';
|
|
100
59
|
export const SCREEN_REPORT_FOCUS_OFF = CSI + '?1004l';
|
|
101
60
|
|
|
102
|
-
/** Scrolls the screen up by `n` lines.
|
|
103
|
-
* @param {number} [n=1]
|
|
104
|
-
* @returns {string} CSI sequence.
|
|
105
|
-
*/
|
|
106
61
|
export const screenScrollUp = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'S';
|
|
107
|
-
/** Scrolls the screen down by `n` lines.
|
|
108
|
-
* @param {number} [n=1]
|
|
109
|
-
* @returns {string} CSI sequence.
|
|
110
|
-
*/
|
|
111
62
|
export const screenScrollDown = (n = 1) => CSI + (n > 1 ? n.toFixed() : '') + 'T';
|
|
112
63
|
|
|
113
64
|
export const WRAPPING_ON = CSI + '=7h';
|