Skip to content

Commit 0a8abd8

Browse files
committed
docs: print control characters in usage example
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 66f3611 commit 0a8abd8

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

README.md

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,50 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
9393
const { groups, index, indices } = match
9494

9595
ok(groups, 'expected `groups`')
96-
console.dir({ groups, index, indices: [...indices] }, { sorted: true })
96+
97+
console.dir({
98+
group: Object.entries(groups).reduce((acc, [key, value]) => {
99+
if (typeof value === 'string') value = hrc(value)
100+
return acc[key] = value, acc
101+
}, {}),
102+
index,
103+
indices: [...indices]
104+
}, { sorted: true })
105+
}
106+
107+
/**
108+
* Make control characters in `string` human-readable.
109+
*
110+
* @this {void}
111+
*
112+
* @param {string} string
113+
* The string containing control characters
114+
* @return {string}
115+
* `string` with human-readable control characters
116+
*/
117+
function hrc(string) {
118+
/**
119+
* Regular expression matching control characters.
120+
*
121+
* @const {RegExp} controls
122+
*/
123+
const controls = /[\u0000-\u001F\u007F-\u009F]/g
124+
125+
return string.replace(controls, hr)
126+
127+
/**
128+
* Convert a control `character` to a human-readable string.
129+
*
130+
* @this {void}
131+
*
132+
* @param {string} character
133+
* The control character
134+
* @return {string}
135+
* The control `character` as a human-readable string
136+
*/
137+
function hr(character) {
138+
return `\\u${character.codePointAt(0).toString(16).padStart(4, '0')}`
139+
}
97140
}
98141
```
99142

@@ -103,12 +146,12 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
103146
"🦄🦾🚀": false
104147
"\u001b[44m\u001b[1mhello world 🌎\u001b[22m\u001b[49m": true
105148
{
106-
groups: {
107-
ansi: '\x1B[44m',
108-
csi: '\x1B[44m',
149+
group: {
150+
ansi: '\\u001b[44m',
151+
csi: '\\u001b[44m',
109152
csi_final: 'm',
110153
csi_intermediate: '',
111-
csi_introducer: '\x1B',
154+
csi_introducer: '\\u001b',
112155
csi_params: '44',
113156
esc: undefined,
114157
esc_final: undefined,
@@ -139,12 +182,12 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
139182
]
140183
}
141184
{
142-
groups: {
143-
ansi: '\x1B[1m',
144-
csi: '\x1B[1m',
185+
group: {
186+
ansi: '\\u001b[1m',
187+
csi: '\\u001b[1m',
145188
csi_final: 'm',
146189
csi_intermediate: '',
147-
csi_introducer: '\x1B',
190+
csi_introducer: '\\u001b',
148191
csi_params: '1',
149192
esc: undefined,
150193
esc_final: undefined,
@@ -175,12 +218,12 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
175218
]
176219
}
177220
{
178-
groups: {
179-
ansi: '\x1B[22m',
180-
csi: '\x1B[22m',
221+
group: {
222+
ansi: '\\u001b[22m',
223+
csi: '\\u001b[22m',
181224
csi_final: 'm',
182225
csi_intermediate: '',
183-
csi_introducer: '\x1B',
226+
csi_introducer: '\\u001b',
184227
csi_params: '22',
185228
esc: undefined,
186229
esc_final: undefined,
@@ -211,12 +254,12 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
211254
]
212255
}
213256
{
214-
groups: {
215-
ansi: '\x1B[49m',
216-
csi: '\x1B[49m',
257+
group: {
258+
ansi: '\\u001b[49m',
259+
csi: '\\u001b[49m',
217260
csi_final: 'm',
218261
csi_intermediate: '',
219-
csi_introducer: '\x1B',
262+
csi_introducer: '\\u001b',
220263
csi_params: '49',
221264
esc: undefined,
222265
esc_final: undefined,

eslint.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import fldv from '@flex-development/eslint-config'
1414
*/
1515
const config = [
1616
...fldv.configs.node,
17+
{
18+
files: ['example.mjs'],
19+
rules: {
20+
'no-control-regex': 0
21+
}
22+
},
1723
{
1824
files: ['src/__tests__/ar.spec.mts'],
1925
rules: {

example.mjs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,48 @@ for (const match of hello.matchAll(ansiRegex({ d: true }))) {
1717
const { groups, index, indices } = match
1818

1919
ok(groups, 'expected `groups`')
20-
console.dir({ groups, index, indices: [...indices] }, { sorted: true })
20+
21+
console.dir({
22+
group: Object.entries(groups).reduce((acc, [key, value]) => {
23+
if (typeof value === 'string') value = hrc(value)
24+
return acc[key] = value, acc
25+
}, {}),
26+
index,
27+
indices: [...indices]
28+
}, { sorted: true })
29+
}
30+
31+
/**
32+
* Make control characters in `string` human-readable.
33+
*
34+
* @this {void}
35+
*
36+
* @param {string} string
37+
* The string containing control characters
38+
* @return {string}
39+
* `string` with human-readable control characters
40+
*/
41+
function hrc(string) {
42+
/**
43+
* Regular expression matching control characters.
44+
*
45+
* @const {RegExp} controls
46+
*/
47+
const controls = /[\u0000-\u001F\u007F-\u009F]/g
48+
49+
return string.replace(controls, hr)
50+
51+
/**
52+
* Convert a control `character` to a human-readable string.
53+
*
54+
* @this {void}
55+
*
56+
* @param {string} character
57+
* The control character
58+
* @return {string}
59+
* The control `character` as a human-readable string
60+
*/
61+
function hr(character) {
62+
return `\\u${character.codePointAt(0).toString(16).padStart(4, '0')}`
63+
}
2164
}

src/__tests__/ar.spec.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('unit:ar', () => {
5454
* @param {string} string
5555
* The string containing control characters
5656
* @return {string}
57-
* `value` with human-readable control characters
57+
* `string` with human-readable control characters
5858
*/
5959
function hrc(this: void, string: string): string {
6060
/**

0 commit comments

Comments
 (0)