console-toolkit 1.2.8 → 1.2.10

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.
Files changed (156) hide show
  1. package/README.md +70 -25
  2. package/package.json +26 -6
  3. package/src/alphanumeric/arrows.d.ts +48 -0
  4. package/src/alphanumeric/arrows.js +23 -0
  5. package/src/alphanumeric/fractions.d.ts +65 -0
  6. package/src/alphanumeric/fractions.js +49 -0
  7. package/src/alphanumeric/number-formatters.d.ts +91 -0
  8. package/src/alphanumeric/number-formatters.js +45 -1
  9. package/src/alphanumeric/roman.d.ts +15 -0
  10. package/src/alphanumeric/roman.js +12 -0
  11. package/src/alphanumeric/unicode-cultural-numbers.d.ts +65 -0
  12. package/src/alphanumeric/unicode-cultural-numbers.js +1 -0
  13. package/src/alphanumeric/unicode-letters.d.ts +32 -0
  14. package/src/alphanumeric/unicode-letters.js +8 -0
  15. package/src/alphanumeric/unicode-numbers.d.ts +44 -0
  16. package/src/alphanumeric/unicode-numbers.js +21 -0
  17. package/src/alphanumeric/utils.d.ts +45 -0
  18. package/src/alphanumeric/utils.js +26 -0
  19. package/src/ansi/csi.d.ts +141 -0
  20. package/src/ansi/csi.js +51 -2
  21. package/src/ansi/index.d.ts +26 -0
  22. package/src/ansi/sgr-constants.d.ts +173 -0
  23. package/src/ansi/sgr-state.d.ts +91 -0
  24. package/src/ansi/sgr-state.js +45 -0
  25. package/src/ansi/sgr.d.ts +587 -0
  26. package/src/ansi/sgr.js +426 -6
  27. package/src/box.d.ts +160 -0
  28. package/src/box.js +113 -12
  29. package/src/charts/bars/block-frac-grouped.d.ts +12 -0
  30. package/src/charts/bars/block-frac-grouped.js +6 -0
  31. package/src/charts/bars/block-frac.d.ts +34 -0
  32. package/src/charts/bars/block-frac.js +13 -0
  33. package/src/charts/bars/block-grouped.d.ts +12 -0
  34. package/src/charts/bars/block-grouped.js +6 -0
  35. package/src/charts/bars/block.d.ts +43 -0
  36. package/src/charts/bars/block.js +13 -0
  37. package/src/charts/bars/draw-grouped.d.ts +41 -0
  38. package/src/charts/bars/draw-grouped.js +4 -0
  39. package/src/charts/bars/draw-stacked.d.ts +47 -0
  40. package/src/charts/bars/draw-stacked.js +4 -0
  41. package/src/charts/bars/frac-grouped.d.ts +32 -0
  42. package/src/charts/bars/frac-grouped.js +13 -0
  43. package/src/charts/bars/plain-grouped.d.ts +12 -0
  44. package/src/charts/bars/plain-grouped.js +6 -0
  45. package/src/charts/bars/plain.d.ts +75 -0
  46. package/src/charts/bars/plain.js +27 -0
  47. package/src/charts/columns/block-frac-grouped.d.ts +12 -0
  48. package/src/charts/columns/block-frac-grouped.js +6 -0
  49. package/src/charts/columns/block-frac.d.ts +39 -0
  50. package/src/charts/columns/block-frac.js +13 -0
  51. package/src/charts/columns/block-grouped.d.ts +12 -0
  52. package/src/charts/columns/block-grouped.js +6 -0
  53. package/src/charts/columns/block.d.ts +43 -0
  54. package/src/charts/columns/block.js +13 -0
  55. package/src/charts/columns/draw-grouped.d.ts +41 -0
  56. package/src/charts/columns/draw-grouped.js +4 -0
  57. package/src/charts/columns/draw-stacked.d.ts +39 -0
  58. package/src/charts/columns/draw-stacked.js +4 -0
  59. package/src/charts/columns/frac-grouped.d.ts +37 -0
  60. package/src/charts/columns/frac-grouped.js +13 -0
  61. package/src/charts/columns/plain-grouped.d.ts +12 -0
  62. package/src/charts/columns/plain-grouped.js +6 -0
  63. package/src/charts/columns/plain.d.ts +32 -0
  64. package/src/charts/columns/plain.js +13 -0
  65. package/src/charts/themes/default.d.ts +6 -0
  66. package/src/charts/themes/default.js +1 -0
  67. package/src/charts/themes/rainbow-reversed.d.ts +6 -0
  68. package/src/charts/themes/rainbow-reversed.js +2 -1
  69. package/src/charts/themes/rainbow.d.ts +6 -0
  70. package/src/charts/themes/rainbow.js +1 -0
  71. package/src/charts/utils.d.ts +79 -0
  72. package/src/charts/utils.js +32 -4
  73. package/src/draw-block-frac.d.ts +16 -0
  74. package/src/draw-block-frac.js +14 -0
  75. package/src/draw-block.d.ts +53 -0
  76. package/src/draw-block.js +25 -1
  77. package/src/meta.d.ts +84 -0
  78. package/src/meta.js +64 -0
  79. package/src/output/show.d.ts +55 -0
  80. package/src/output/show.js +28 -0
  81. package/src/output/updater.d.ts +114 -0
  82. package/src/output/updater.js +58 -4
  83. package/src/output/writer.d.ts +87 -0
  84. package/src/output/writer.js +57 -5
  85. package/src/panel.d.ts +402 -0
  86. package/src/panel.js +219 -5
  87. package/src/plot/bitmap.d.ts +80 -0
  88. package/src/plot/bitmap.js +33 -4
  89. package/src/plot/draw-line.d.ts +13 -0
  90. package/src/plot/draw-line.js +8 -0
  91. package/src/plot/draw-rect.d.ts +13 -0
  92. package/src/plot/draw-rect.js +38 -30
  93. package/src/plot/index.d.ts +39 -0
  94. package/src/plot/index.js +22 -0
  95. package/src/plot/to-quads.d.ts +10 -0
  96. package/src/plot/to-quads.js +5 -0
  97. package/src/spinner/index.d.ts +4 -0
  98. package/src/spinner/index.js +0 -2
  99. package/src/spinner/spin.d.ts +13 -0
  100. package/src/spinner/spin.js +13 -2
  101. package/src/spinner/spinner.d.ts +69 -0
  102. package/src/spinner/spinner.js +30 -2
  103. package/src/spinner/spinners.d.ts +34 -0
  104. package/src/spinner/spinners.js +23 -9
  105. package/src/strings/clip.d.ts +21 -0
  106. package/src/strings/clip.js +10 -0
  107. package/src/strings/parse.d.ts +23 -0
  108. package/src/strings/parse.js +7 -0
  109. package/src/strings/split.d.ts +38 -0
  110. package/src/strings/split.js +15 -0
  111. package/src/strings.d.ts +44 -0
  112. package/src/strings.js +34 -4
  113. package/src/style.d.ts +462 -0
  114. package/src/style.js +58 -4
  115. package/src/symbols.d.ts +167 -0
  116. package/src/symbols.js +91 -7
  117. package/src/table/draw-borders.d.ts +38 -0
  118. package/src/table/draw-borders.js +10 -2
  119. package/src/table/index.d.ts +8 -0
  120. package/src/table/index.js +1 -0
  121. package/src/table/table.d.ts +234 -0
  122. package/src/table/table.js +59 -1
  123. package/src/themes/blocks/unicode-half.d.ts +6 -0
  124. package/src/themes/blocks/unicode-half.js +1 -0
  125. package/src/themes/blocks/unicode-thin.d.ts +6 -0
  126. package/src/themes/blocks/unicode-thin.js +1 -0
  127. package/src/themes/lines/ascii-compact.d.ts +6 -0
  128. package/src/themes/lines/ascii-compact.js +1 -0
  129. package/src/themes/lines/ascii-dots.d.ts +6 -0
  130. package/src/themes/lines/ascii-dots.js +1 -0
  131. package/src/themes/lines/ascii-girder.d.ts +6 -0
  132. package/src/themes/lines/ascii-girder.js +1 -0
  133. package/src/themes/lines/ascii-github.d.ts +6 -0
  134. package/src/themes/lines/ascii-github.js +1 -0
  135. package/src/themes/lines/ascii-reddit.d.ts +6 -0
  136. package/src/themes/lines/ascii-reddit.js +1 -0
  137. package/src/themes/lines/ascii-rounded.d.ts +6 -0
  138. package/src/themes/lines/ascii-rounded.js +1 -0
  139. package/src/themes/lines/ascii.d.ts +6 -0
  140. package/src/themes/lines/ascii.js +1 -0
  141. package/src/themes/lines/unicode-bold.d.ts +6 -0
  142. package/src/themes/lines/unicode-bold.js +1 -0
  143. package/src/themes/lines/unicode-rounded.d.ts +6 -0
  144. package/src/themes/lines/unicode-rounded.js +1 -0
  145. package/src/themes/lines/unicode.d.ts +6 -0
  146. package/src/themes/lines/unicode.js +1 -0
  147. package/src/themes/utils.d.ts +33 -0
  148. package/src/themes/utils.js +7 -0
  149. package/src/turtle/draw-line-art.d.ts +19 -0
  150. package/src/turtle/draw-line-art.js +7 -0
  151. package/src/turtle/draw-unicode.d.ts +19 -0
  152. package/src/turtle/draw-unicode.js +8 -0
  153. package/src/turtle/index.d.ts +21 -0
  154. package/src/turtle/index.js +8 -0
  155. package/src/turtle/turtle.d.ts +269 -0
  156. package/src/turtle/turtle.js +124 -4
package/README.md CHANGED
@@ -1,19 +1,19 @@
1
1
  # console-toolkit [![NPM version][npm-img]][npm-url]
2
2
 
3
- [npm-img]: https://img.shields.io/npm/v/console-toolkit.svg
4
- [npm-url]: https://npmjs.org/package/console-toolkit
3
+ [npm-img]: https://img.shields.io/npm/v/console-toolkit.svg
4
+ [npm-url]: https://npmjs.org/package/console-toolkit
5
5
 
6
6
  `console-toolkit` is a set of tools to create rich CLI-based applications. It provides:
7
7
 
8
- * Styles based on [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code):
9
- * [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR): colors and text styles
10
- * [CSI](https://en.wikipedia.org/wiki/ANSI_escape_code#CSIsection): cursor and screen control
11
- * Bitmap graphics
12
- * Vector graphics based on [Turtle graphics](https://en.wikipedia.org/wiki/Turtle_graphics)
13
- * Curated sets of Unicode symbols
14
- * Tables with themes
15
- * Bar and column charts with themes
16
- * Various helpers and examples
8
+ - Styles based on [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code):
9
+ - [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR): colors and text styles
10
+ - [CSI](https://en.wikipedia.org/wiki/ANSI_escape_code#CSIsection): cursor and screen control
11
+ - Bitmap graphics
12
+ - Vector graphics based on [Turtle graphics](https://en.wikipedia.org/wiki/Turtle_graphics)
13
+ - Curated sets of Unicode symbols
14
+ - Tables with themes
15
+ - Bar and column charts with themes
16
+ - Various helpers and examples
17
17
 
18
18
  ## Visual examples
19
19
 
@@ -54,7 +54,15 @@ console.log(c`{{bold}}Hello, {{bright.cyan}}world!`);
54
54
 
55
55
  // chart
56
56
 
57
- const chart = drawChart([[2, 1, 2], [5, 1, 4], [1, 1], [3, 1, 3]], 50);
57
+ const chart = drawChart(
58
+ [
59
+ [2, 1, 2],
60
+ [5, 1, 4],
61
+ [1, 1],
62
+ [3, 1, 3]
63
+ ],
64
+ 50
65
+ );
58
66
  for (const line of chart) console.log(line);
59
67
 
60
68
  // table
@@ -79,9 +87,44 @@ The output of the code is:
79
87
  npm install --save console-toolkit
80
88
  ```
81
89
 
90
+ ## Modules
91
+
92
+ ### Text containers
93
+
94
+ | Module | Import | Description |
95
+ | ----------- | ------------------------- | -------------------------------------------------------------- |
96
+ | **strings** | `console-toolkit/strings` | String array utilities: `getLength`, `clip`, `toStrings` |
97
+ | **Box** | `console-toolkit/box` | Rectangular text container — all lines equal width. Immutable. |
98
+ | **Panel** | `console-toolkit/panel` | 2D cell grid with per-cell SGR state. Mutable. |
99
+
100
+ ### Styling and drawing
101
+
102
+ | Module | Import | Description |
103
+ | ------------------- | ------------------------------------ | --------------------------------------------------------- |
104
+ | **Style** | `console-toolkit/style` | Fluent SGR styling API + `s`/`c` tagged template literals |
105
+ | **draw-block** | `console-toolkit/draw-block.js` | Draw filled blocks and frames with block themes |
106
+ | **draw-block-frac** | `console-toolkit/draw-block-frac.js` | Fractional-width/height blocks (1/8th Unicode steps) |
107
+ | **symbols** | `console-toolkit/symbols.js` | Curated Unicode constants (blocks, shades, math, marks) |
108
+
109
+ ### Packages
110
+
111
+ | Package | Import | Description |
112
+ | ---------------- | ---------------------------------- | ------------------------------------------------------------ |
113
+ | **ansi** | `console-toolkit/ansi` | Low-level ANSI CSI/SGR escape sequence handling |
114
+ | **table** | `console-toolkit/table` | Table renderer with line themes |
115
+ | **charts** | `console-toolkit/charts/...` | Bar and column charts (plain, block, frac, stacked, grouped) |
116
+ | **themes** | `console-toolkit/themes/...` | Line and block themes (unicode, ascii variants) |
117
+ | **plot** | `console-toolkit/plot` | Bitmap plotting (quadrant and braille characters) |
118
+ | **turtle** | `console-toolkit/turtle` | Turtle graphics for vector line drawing |
119
+ | **spinner** | `console-toolkit/spinner` | Spinner animations and updatable output |
120
+ | **output** | `console-toolkit/output/...` | Output helpers: Writer (streaming), Updater (in-place) |
121
+ | **alphanumeric** | `console-toolkit/alphanumeric/...` | Decorative Unicode number and letter sets |
122
+
82
123
  ## Documentation
83
124
 
84
- See [wiki](https://github.com/uhop/console-toolkit/wiki) for more details.
125
+ See [wiki](https://github.com/uhop/console-toolkit/wiki) for detailed usage docs.
126
+
127
+ For project internals see [ARCHITECTURE.md](./ARCHITECTURE.md). For development setup see [CONTRIBUTING.md](./CONTRIBUTING.md). For AI agent rules see [AGENTS.md](./AGENTS.md).
85
128
 
86
129
  ## License
87
130
 
@@ -89,15 +132,17 @@ BSD 3-Clause License
89
132
 
90
133
  ## Release history
91
134
 
92
- * 1.2.8 *Updated dev deps.*
93
- * 1.2.7 *Updated dev deps.*
94
- * 1.2.6 *Updated dev deps.*
95
- * 1.2.5 *Updated dev deps.*
96
- * 1.2.4 *Updated deps.*
97
- * 1.2.3 *Updated deps + more tests.*
98
- * 1.2.2 *Updated deps.*
99
- * 1.2.1 *Added support for `Bun.stringWidth()`.*
100
- * 1.2.0 *Refactored `strings`.*
101
- * 1.1.1 *Minor bugfixes in `Table`, some improvements, updated deps.*
102
- * 1.1.0 *Minor improvements, enhanced `Writer` and `Updater`.*
103
- * 1.0.0 *Initial release.*
135
+ - 1.2.10 _Added TypeScript typings, JSDoc, minor bug fixes, updated dev deps._
136
+ - 1.2.9 _Updated dev deps._
137
+ - 1.2.8 _Updated dev deps._
138
+ - 1.2.7 _Updated dev deps._
139
+ - 1.2.6 _Updated dev deps._
140
+ - 1.2.5 _Updated dev deps._
141
+ - 1.2.4 _Updated deps._
142
+ - 1.2.3 _Updated deps + more tests._
143
+ - 1.2.2 _Updated deps._
144
+ - 1.2.1 _Added support for `Bun.stringWidth()`._
145
+ - 1.2.0 _Refactored `strings`._
146
+ - 1.1.1 _Minor bugfixes in `Table`, some improvements, updated deps._
147
+ - 1.1.0 _Minor improvements, enhanced `Writer` and `Updater`._
148
+ - 1.0.0 _Initial release._
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "console-toolkit",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "description": "Toolkit to produce a fancy console output (boxes, tables, charts, colors).",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "module": "src/index.js",
8
+ "types": "src/index.d.ts",
8
9
  "exports": {
9
10
  "./*": "./src/*",
10
11
  "./ansi": "./src/ansi/index.js",
@@ -22,8 +23,14 @@
22
23
  "test:bun": "tape6-bun --flags FO",
23
24
  "test:deno": "tape6-deno --flags FO",
24
25
  "test:proc": "tape6-proc --flags FO",
25
- "test:proc:bun": "bun run `npx tape6-proc --self` --flags FO",
26
- "test:proc:deno": "deno run -A `npx tape6-proc --self` --flags FO -r -A"
26
+ "test:proc:bun": "bun run `tape6-proc --self` --flags FO",
27
+ "test:proc:deno": "deno run -A `tape6-proc --self` --flags FO -r -A",
28
+ "test:seq": "tape6-seq --flags FO",
29
+ "test:seq:bun": "bun run `tape6-seq --self` --flags FO",
30
+ "test:seq:deno": "deno run -A `tape6-seq --self` --flags FO",
31
+ "ts-check": "tsc --noEmit",
32
+ "lint": "prettier --check .",
33
+ "lint:fix": "prettier --write ."
27
34
  },
28
35
  "github": "http://github.com/uhop/console-toolkit",
29
36
  "repository": {
@@ -32,15 +39,25 @@
32
39
  },
33
40
  "keywords": [
34
41
  "console",
42
+ "terminal",
43
+ "ansi",
44
+ "SGR",
45
+ "escape-sequences",
35
46
  "color",
36
47
  "colors",
48
+ "styled-text",
49
+ "text-formatting",
50
+ "unicode",
37
51
  "box",
38
52
  "boxes",
53
+ "panel",
39
54
  "table",
40
55
  "tables",
41
56
  "chart",
42
57
  "charts",
58
+ "plot",
43
59
  "turtle",
60
+ "spinner",
44
61
  "CLI",
45
62
  "TUI"
46
63
  ],
@@ -63,9 +80,12 @@
63
80
  ]
64
81
  },
65
82
  "devDependencies": {
83
+ "@types/node": "^25.3.0",
66
84
  "emoji-regex": "^10.6.0",
67
- "get-east-asian-width": "^1.4.0",
68
- "tape-six": "^1.5.1",
69
- "tape-six-proc": "^1.2.1"
85
+ "get-east-asian-width": "^1.5.0",
86
+ "prettier": "^3.8.1",
87
+ "tape-six": "^1.7.2",
88
+ "tape-six-proc": "^1.2.3",
89
+ "typescript": "^5.9.3"
70
90
  }
71
91
  }
@@ -0,0 +1,48 @@
1
+ /** Direction index for left-pointing arrows. */
2
+ export const LEFT: 0;
3
+ /** Direction index for up-pointing arrows. */
4
+ export const UP: 1;
5
+ /** Direction index for right-pointing arrows. */
6
+ export const RIGHT: 2;
7
+ /** Direction index for down-pointing arrows. */
8
+ export const DOWN: 3;
9
+
10
+ /** Simple arrows [left, up, right, down]. */
11
+ export const simple: string[];
12
+ /** Arrows with stroke [left, up, right, down]. */
13
+ export const withStroke: string[];
14
+ /** Wave arrows [left, up, right, down]. */
15
+ export const wave: string[];
16
+ /** Two-headed arrows [left, up, right, down]. */
17
+ export const twoHeaded: string[];
18
+ /** Arrows with tail [left, up, right, down]. */
19
+ export const withTail: string[];
20
+ /** Arrows from bar [left, up, right, down]. */
21
+ export const fromBar: string[];
22
+ /** Arrows with loop [left, up, right, down]. */
23
+ export const withLoop: string[];
24
+ /** Double arrows [left, up, right, down]. */
25
+ export const double: string[];
26
+ /** Triple arrows [left, up, right, down]. */
27
+ export const triple: string[];
28
+ /** Squiggle arrows [left, up, right, down]. */
29
+ export const squiggle: string[];
30
+ /** Dashed arrows [left, up, right, down]. */
31
+ export const dashed: string[];
32
+ /** Arrows to bar [left, up, right, down]. */
33
+ export const toBar: string[];
34
+ /** White (outline) arrows [left, up, right, down]. */
35
+ export const white: string[];
36
+ /** Arrows with vertical stroke [left, up, right, down]. */
37
+ export const withVStroke: string[];
38
+ /** Arrows with double vertical stroke [left, up, right, down]. */
39
+ export const withDoubleVStroke: string[];
40
+ /** Open-headed arrows [left, up, right, down]. */
41
+ export const openHeaded: string[];
42
+
43
+ /** Arrows with barb up [left, up, right, down]. */
44
+ export const withBarbUp: string[];
45
+ /** Arrows with barb down [left, up, right, down]. */
46
+ export const withBarbDown: string[];
47
+ /** Double arrows with stroke [left, up, right, down]. */
48
+ export const doubleWithStroke: string[];
@@ -1,6 +1,10 @@
1
+ /** Direction index for left-pointing arrows. */
1
2
  export const LEFT = 0,
3
+ /** Direction index for up-pointing arrows. */
2
4
  UP = 1,
5
+ /** Direction index for right-pointing arrows. */
3
6
  RIGHT = 2,
7
+ /** Direction index for down-pointing arrows. */
4
8
  DOWN = 3;
5
9
 
6
10
  const toSymbol = (...args) => args.map(x => (typeof x == 'number' ? String.fromCodePoint(x) : x));
@@ -9,23 +13,42 @@ const makeArrowsH = codePoint => toSymbol(codePoint, ' ', codePoint + 1, ' ');
9
13
 
10
14
  // Arrows
11
15
 
16
+ /** Simple arrows [left, up, right, down]. */
12
17
  export const simple = makeArrows(0x2190);
18
+ /** Arrows with stroke [left, up, right, down]. */
13
19
  export const withStroke = makeArrowsH(0x219a);
20
+ /** Wave arrows [left, up, right, down]. */
14
21
  export const wave = makeArrowsH(0x219c);
22
+ /** Two-headed arrows [left, up, right, down]. */
15
23
  export const twoHeaded = makeArrows(0x219e);
24
+ /** Arrows with tail [left, up, right, down]. */
16
25
  export const withTail = makeArrowsH(0x21a2);
26
+ /** Arrows from bar [left, up, right, down]. */
17
27
  export const fromBar = makeArrows(0x21a4);
28
+ /** Arrows with loop [left, up, right, down]. */
18
29
  export const withLoop = makeArrowsH(0x21ab);
30
+ /** Double arrows [left, up, right, down]. */
19
31
  export const double = makeArrows(0x21d0);
32
+ /** Triple arrows [left, up, right, down]. */
20
33
  export const triple = makeArrowsH(0x21da);
34
+ /** Squiggle arrows [left, up, right, down]. */
21
35
  export const squiggle = makeArrowsH(0x21dc);
36
+ /** Dashed arrows [left, up, right, down]. */
22
37
  export const dashed = makeArrows(0x21e0);
38
+ /** Arrows to bar [left, up, right, down]. */
23
39
  export const toBar = makeArrowsH(0x21e4);
40
+ /** White (outline) arrows [left, up, right, down]. */
24
41
  export const white = makeArrows(0x21e6);
42
+ /** Arrows with vertical stroke [left, up, right, down]. */
25
43
  export const withVStroke = makeArrowsH(0x21f7);
44
+ /** Arrows with double vertical stroke [left, up, right, down]. */
26
45
  export const withDoubleVStroke = makeArrowsH(0x21fa);
46
+ /** Open-headed arrows [left, up, right, down]. */
27
47
  export const openHeaded = makeArrowsH(0x21fd);
28
48
 
49
+ /** Arrows with barb up [left, up, right, down]. */
29
50
  export const withBarbUp = toSymbol(0x21bc, 0x21be, 0x21c1, 0x21c3);
51
+ /** Arrows with barb down [left, up, right, down]. */
30
52
  export const withBarbDown = toSymbol(0x21bd, 0x21bf, 0x21c0, 0x21c2);
53
+ /** Double arrows with stroke [left, up, right, down]. */
31
54
  export const doubleWithStroke = toSymbol(0x21cd, ' ', 0x21cf, ' ');
@@ -0,0 +1,65 @@
1
+ /** A Unicode fraction character descriptor. */
2
+ export interface Fraction {
3
+ /** The numerator of the fraction. */
4
+ numerator: number;
5
+ /** The denominator of the fraction. */
6
+ denominator: number;
7
+ /** The Unicode fraction symbol. */
8
+ symbol: string;
9
+ /** The numeric value of the fraction. */
10
+ value: number;
11
+ }
12
+
13
+ /** All available Unicode fraction characters, sorted by value. */
14
+ export const fractions: Fraction[];
15
+ /** Unicode fraction characters with denominator 3. */
16
+ export const thirds: Fraction[];
17
+ /** Unicode fraction characters with denominator 4. */
18
+ export const quarters: Fraction[];
19
+ /** Unicode fraction characters with denominator 5. */
20
+ export const fifths: Fraction[];
21
+ /** Unicode fraction characters with denominator 6. */
22
+ export const sixths: Fraction[];
23
+ /** Unicode fraction characters with denominator 8. */
24
+ export const eighths: Fraction[];
25
+
26
+ export {quarters as fourths};
27
+
28
+ /** Finds the closest Unicode fraction symbol for a given value from all fractions.
29
+ * @param value - The value to approximate.
30
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
31
+ * @returns The integer part plus the closest fraction symbol.
32
+ */
33
+ export function getFraction(value: number, useFractionForZero?: boolean): string;
34
+ /** Finds the closest Unicode fraction symbol from thirds.
35
+ * @param value - The value to approximate.
36
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
37
+ * @returns The integer part plus the closest fraction symbol.
38
+ */
39
+ export function getThirds(value: number, useFractionForZero?: boolean): string;
40
+ /** Finds the closest Unicode fraction symbol from quarters.
41
+ * @param value - The value to approximate.
42
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
43
+ * @returns The integer part plus the closest fraction symbol.
44
+ */
45
+ export function getQuarters(value: number, useFractionForZero?: boolean): string;
46
+ /** Finds the closest Unicode fraction symbol from fifths.
47
+ * @param value - The value to approximate.
48
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
49
+ * @returns The integer part plus the closest fraction symbol.
50
+ */
51
+ export function getFifths(value: number, useFractionForZero?: boolean): string;
52
+ /** Finds the closest Unicode fraction symbol from sixths.
53
+ * @param value - The value to approximate.
54
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
55
+ * @returns The integer part plus the closest fraction symbol.
56
+ */
57
+ export function getSixths(value: number, useFractionForZero?: boolean): string;
58
+ /** Finds the closest Unicode fraction symbol from eighths.
59
+ * @param value - The value to approximate.
60
+ * @param useFractionForZero - If true, use a fraction symbol even for zero.
61
+ * @returns The integer part plus the closest fraction symbol.
62
+ */
63
+ export function getEighths(value: number, useFractionForZero?: boolean): string;
64
+
65
+ export {getQuarters as getFourths};
@@ -1,3 +1,7 @@
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
+ */
1
5
  export const fractions = [
2
6
  [1, 7, '\u2150'],
3
7
  [1, 9, '\u2151'],
@@ -24,10 +28,25 @@ export const fractions = [
24
28
 
25
29
  const pick = denominator => fractions.filter(x => !(denominator % x.denominator));
26
30
 
31
+ /** Unicode fraction characters with denominator 3.
32
+ * @type {import('./fractions.js').Fraction[]}
33
+ */
27
34
  export const thirds = pick(3);
35
+ /** Unicode fraction characters with denominator 4.
36
+ * @type {import('./fractions.js').Fraction[]}
37
+ */
28
38
  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
+ */
29
42
  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
+ */
30
46
  export const sixths = pick(6);
47
+ /** Unicode fraction characters with denominator 8.
48
+ * @type {import('./fractions.js').Fraction[]}
49
+ */
31
50
  export const eighths = [{numerator: 0, denominator: 8, symbol: '0', value: 0}, ...pick(8)];
32
51
 
33
52
  export {quarters as fourths};
@@ -72,11 +91,41 @@ const findSymbol = (fractions, value, useFractionForZero) => {
72
91
  return (int || '') + chosen.symbol;
73
92
  };
74
93
 
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
+ */
75
99
  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
+ */
76
105
  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
+ */
77
111
  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
+ */
78
117
  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
+ */
79
123
  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
+ */
80
129
  export const getEighths = (value, useFractionForZero) => findSymbol(eighths, value, useFractionForZero);
81
130
 
82
131
  export {getQuarters as getFourths};
@@ -0,0 +1,91 @@
1
+ /** Configuration for formatting time values. */
2
+ export interface TimeFormat {
3
+ /** The scale factor to apply to values. */
4
+ scale: number;
5
+ /** Number of decimal places. */
6
+ precision: number;
7
+ /** The time unit suffix (e.g., 'ms', 's', 'μs'). */
8
+ unit: string;
9
+ }
10
+
11
+ /** Prepares a time format configuration for a set of values.
12
+ * @param values - The time values to analyze.
13
+ * @param scale - Initial scale factor (default: 1).
14
+ * @param useUnicode - If true, use Unicode unit symbols (e.g., μs).
15
+ * @returns A TimeFormat configuration.
16
+ */
17
+ export function prepareTimeFormat(values: number[], scale?: number, useUnicode?: boolean): TimeFormat;
18
+
19
+ /** Options for `formatTime()`. */
20
+ export interface FormatTimeOptions extends TimeFormat {
21
+ /** If true, do not round the fractional part. */
22
+ keepFractionAsIs?: boolean;
23
+ }
24
+
25
+ /** Formats a time value using a prepared format configuration.
26
+ * @param value - The time value to format.
27
+ * @param format - The format configuration from `prepareTimeFormat()`.
28
+ * @returns The formatted time string.
29
+ */
30
+ export function formatTime(value: number, format: FormatTimeOptions): string;
31
+
32
+ /** Options for number formatting functions. */
33
+ export interface FormatNumberOptions {
34
+ /** Thousands separator character (default: ','). */
35
+ comma?: string;
36
+ /** If true, always show the sign. */
37
+ keepSign?: boolean;
38
+ /** Number of decimal places. */
39
+ decimals?: number;
40
+ /** If true, do not round the fractional part. */
41
+ keepFractionAsIs?: boolean;
42
+ /** Decimal point character (default: '.'). */
43
+ dot?: string;
44
+ }
45
+
46
+ /** Formats an integer with optional comma separators and sign.
47
+ * @param value - The integer to format.
48
+ * @param options - Formatting options.
49
+ * @returns The formatted string.
50
+ */
51
+ export function formatInteger(value: number, options?: FormatNumberOptions): string;
52
+ /** Formats a number with optional decimals, comma separators, and sign.
53
+ * @param value - The number to format.
54
+ * @param options - Formatting options.
55
+ * @returns The formatted string.
56
+ */
57
+ export function formatNumber(value: number, options?: FormatNumberOptions): string;
58
+ /** Formats a number with SI abbreviations (k, M, G, T).
59
+ * @param value - The number to format.
60
+ * @param options - Formatting options.
61
+ * @returns The abbreviated string.
62
+ */
63
+ export function abbrNumber(value: number, options?: FormatNumberOptions): string;
64
+
65
+ /** Simplifies the exponent notation of a number string.
66
+ * @param s - The number or string to simplify.
67
+ * @param options - Options.
68
+ * @returns The simplified string.
69
+ */
70
+ export function simplifyExponent(s: string | number, options?: {keepExpPlus?: boolean}): string;
71
+
72
+ /** Result of comparing two numbers. */
73
+ export interface CompareDifferenceResult {
74
+ /** True if `a < b`. */
75
+ less: boolean;
76
+ /** True if the values are effectively equal. */
77
+ equality?: boolean;
78
+ /** True if the difference is infinite. */
79
+ infinity?: boolean;
80
+ /** Formatted percentage difference, if applicable. */
81
+ percentage?: string;
82
+ /** Formatted ratio string, if applicable. */
83
+ ratio?: string;
84
+ }
85
+
86
+ /** Compares two positive numbers and returns a description of their difference.
87
+ * @param a - First number.
88
+ * @param b - Second number.
89
+ * @returns An object describing the difference.
90
+ */
91
+ export function compareDifference(a: number, b: number): CompareDifferenceResult;
@@ -3,6 +3,12 @@
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
+ */
6
12
  export const prepareTimeFormat = (values, scale = 1, useUnicode) => {
7
13
  let mx = -1000,
8
14
  mn = 1000;
@@ -22,6 +28,11 @@ export const prepareTimeFormat = (values, scale = 1, useUnicode) => {
22
28
  return {scale, precision: digits - mx, unit: (useUnicode ? unicodeUnits : units)[i]};
23
29
  };
24
30
 
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
+ */
25
36
  export const formatTime = (value, format) => {
26
37
  let result = (value * format.scale).toFixed(format.precision);
27
38
  if (format.precision > 0 && !format.keepFractionAsIs) result = result.replace(/\.0+$/, '');
@@ -41,11 +52,28 @@ const putCommasIn = (s, options) => {
41
52
  );
42
53
  };
43
54
 
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
+ */
44
62
  export const formatInteger = (value, options) =>
45
63
  isNaN(value)
46
64
  ? ''
47
65
  : (value < 0 ? '-' : options?.keepSign ? '+' : '') + putCommasIn(Math.abs(value).toFixed(0), options);
48
66
 
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
+ */
49
77
  export const formatNumber = (value, options) => {
50
78
  if (isNaN(value)) return '';
51
79
  const decimals = options?.decimals ?? 0;
@@ -65,6 +93,11 @@ export const formatNumber = (value, options) => {
65
93
  const exp = [0, 0, 0, 0, 3, 3, 6, 6, 6, 9, 9, 9, 12];
66
94
  const abbr = '***k**M**G**T';
67
95
 
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
+ */
68
101
  export const abbrNumber = (value, options) => {
69
102
  if (isNaN(value)) return '';
70
103
  const decimals = options?.decimals ?? 0;
@@ -93,9 +126,20 @@ export const abbrNumber = (value, options) => {
93
126
  );
94
127
  };
95
128
 
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
+ */
96
135
  export const simplifyExponent = (s, {keepExpPlus} = {}) =>
97
136
  String(s).replace(new RegExp('\\.?0*e' + (keepExpPlus ? '' : '\\+?'), 'i'), 'e');
98
137
 
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
+ */
99
143
  export const compareDifference = (a, b) => {
100
144
  // works only on positive numbers
101
145
  a = Math.abs(a);
@@ -111,7 +155,7 @@ export const compareDifference = (a, b) => {
111
155
  if (diff === Infinity) return {less, infinity: true};
112
156
 
113
157
  if (diff < 2) {
114
- const percentage = absDiff / (less ? a : b) * 100;
158
+ const percentage = (absDiff / (less ? a : b)) * 100;
115
159
  if (percentage < 0.001) return {less, equality: true};
116
160
  if (percentage < 1) return {less, percentage: formatNumber(percentage, {decimals: 3})};
117
161
  if (percentage < 10) return {less, percentage: formatNumber(percentage, {decimals: 2})};
@@ -0,0 +1,15 @@
1
+ /** Converts a positive integer (1-3999) to a Roman numeral string using ASCII characters.
2
+ * @param value - The integer to convert.
3
+ * @returns The Roman numeral string.
4
+ */
5
+ export function toRoman(value: number): string;
6
+ /** Converts a positive integer (1-3999) to a Roman numeral string using Unicode characters.
7
+ * @param value - The integer to convert.
8
+ * @returns The Unicode Roman numeral string.
9
+ */
10
+ export function toRomanUnicode(value: number): string;
11
+ /** Converts a positive integer (1-3999) to a lowercase Unicode Roman numeral string.
12
+ * @param value - The integer to convert.
13
+ * @returns The lowercase Unicode Roman numeral string.
14
+ */
15
+ export function toRomanLowerUnicode(value: number): string;
@@ -24,6 +24,10 @@ 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
+ */
27
31
  export const toRoman = value => {
28
32
  value = Math.round(value);
29
33
  if (value < 1 || value > 3999)
@@ -82,5 +86,13 @@ const toRomanUnicodeFn = (roman, L, C, D, M) => value => {
82
86
  return result.reverse().join('');
83
87
  };
84
88
 
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
+ */
85
93
  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
+ */
86
98
  export const toRomanLowerUnicode = toRomanUnicodeFn(romanLower, lowerL, lowerC, lowerD, lowerM);