Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: expose stripVTControlCharacters() #40214

Merged
merged 2 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
6 changes: 5 additions & 1 deletion 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 @@ -222,7 +223,8 @@ const meta = [
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
// Matches all ansi escape code sequences in a string
const ansiPattern = '[\\u001B\\u009B][[\\]()#;?]*' +
'(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' +
'(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*' +
'|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' +
'|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))';
const ansi = new RegExp(ansiPattern, 'g');

Expand Down Expand Up @@ -2113,6 +2115,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({})
});
cjihrig marked this conversation as resolved.
Show resolved Hide resolved