Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
util: expose stripVTControlCharacters()
This commit exposes the existing stripVTControlCharacters()
method with docs and some additional input validation.

PR-URL: #40214
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig committed Oct 2, 2021
commit 396b14b6c642affd46b0533f5af7c31e073ad29a
15 changes: 15 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,21 @@ doSomething[kCustomPromisifiedSymbol] = (foo) => {
};
```

## `util.stripVTControlCharacters(str)`
<!-- YAML
added: REPLACEME
-->

* `str` {string}
* Returns: {string}

Returns `str` with any ANSI escape codes removed.

```js
console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
// Prints "value"
```

## Class: `util.TextDecoder`
<!-- YAML
added: v8.3.0
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const assert = require('internal/assert');
const { NativeModule } = require('internal/bootstrap/loaders');
const {
validateObject,
validateString,
} = require('internal/validators');

let hexSlice;
Expand Down Expand Up @@ -2113,6 +2114,8 @@ if (internalBinding('config').hasIntl) {
* Remove all VT control characters. Use to estimate displayed string width.
*/
function stripVTControlCharacters(str) {
validateString(str, 'str');

return str.replace(ansi, '');
}

Expand Down
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const {
const {
format,
formatWithOptions,
inspect
inspect,
stripVTControlCharacters,
} = require('internal/util/inspect');
const { debuglog } = require('internal/util/debuglog');
const {
Expand Down Expand Up @@ -369,6 +370,7 @@ module.exports = {
isPrimitive,
log,
promisify,
stripVTControlCharacters,
toUSVString,
TextDecoder,
TextEncoder,
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';
// Flags: --expose-internals
require('../common');
const common = require('../common');
const assert = require('assert');
const util = require('util');
const errors = require('internal/errors');
Expand Down Expand Up @@ -178,3 +178,11 @@ assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
true
);
}

assert.throws(() => {
util.stripVTControlCharacters({});
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "str" argument must be of type string.' +
common.invalidArgTypeHelper({})
});
Comment thread
cjihrig marked this conversation as resolved.