Skip to content

Commit f4164fa

Browse files
cjihrigtargos
authored andcommitted
util: expose stripVTControlCharacters()
This commit exposes the existing stripVTControlCharacters() method with docs and some additional input validation. PR-URL: #40214 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dc0c274 commit f4164fa

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

doc/api/util.md

+15
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,21 @@ doSomething[kCustomPromisifiedSymbol] = (foo) => {
11091109
};
11101110
```
11111111

1112+
## `util.stripVTControlCharacters(str)`
1113+
<!-- YAML
1114+
added: REPLACEME
1115+
-->
1116+
1117+
* `str` {string}
1118+
* Returns: {string}
1119+
1120+
Returns `str` with any ANSI escape codes removed.
1121+
1122+
```js
1123+
console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
1124+
// Prints "value"
1125+
```
1126+
11121127
## Class: `util.TextDecoder`
11131128
<!-- YAML
11141129
added: v8.3.0

lib/internal/util/inspect.js

+3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const assert = require('internal/assert');
140140
const { NativeModule } = require('internal/bootstrap/loaders');
141141
const {
142142
validateObject,
143+
validateString,
143144
} = require('internal/validators');
144145

145146
let hexSlice;
@@ -2113,6 +2114,8 @@ if (internalBinding('config').hasIntl) {
21132114
* Remove all VT control characters. Use to estimate displayed string width.
21142115
*/
21152116
function stripVTControlCharacters(str) {
2117+
validateString(str, 'str');
2118+
21162119
return str.replace(ansi, '');
21172120
}
21182121

lib/util.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const {
5757
const {
5858
format,
5959
formatWithOptions,
60-
inspect
60+
inspect,
61+
stripVTControlCharacters,
6162
} = require('internal/util/inspect');
6263
const { debuglog } = require('internal/util/debuglog');
6364
const {
@@ -369,6 +370,7 @@ module.exports = {
369370
isPrimitive,
370371
log,
371372
promisify,
373+
stripVTControlCharacters,
372374
toUSVString,
373375
TextDecoder,
374376
TextEncoder,

test/parallel/test-util.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323
// Flags: --expose-internals
24-
require('../common');
24+
const common = require('../common');
2525
const assert = require('assert');
2626
const util = require('util');
2727
const errors = require('internal/errors');
@@ -178,3 +178,11 @@ assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
178178
true
179179
);
180180
}
181+
182+
assert.throws(() => {
183+
util.stripVTControlCharacters({});
184+
}, {
185+
code: 'ERR_INVALID_ARG_TYPE',
186+
message: 'The "str" argument must be of type string.' +
187+
common.invalidArgTypeHelper({})
188+
});

0 commit comments

Comments
 (0)