Skip to content

Commit

Permalink
util: expose stripVTControlCharacters()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cjihrig authored and targos committed Oct 4, 2021
1 parent dc0c274 commit f4164fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,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({})
});

0 comments on commit f4164fa

Please sign in to comment.