Skip to content

Commit

Permalink
util: simplify util.toUSVString
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Aug 26, 2021
1 parent 31772a4 commit 27be963
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/internal/util.js
Expand Up @@ -57,8 +57,7 @@ const experimentalWarnings = new SafeSet();

const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex

const unpairedSurrogateRe =
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
const unpairedSurrogateRe = /\p{Surrogate}/u;
function toUSVString(val) {
const str = `${val}`;
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-util.js
Expand Up @@ -148,7 +148,17 @@ assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);

assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
// Lead surrogates: D800..DBFF
assert.strictEqual(util.toUSVString('string\ud800'), 'string\ufffd');
assert.strictEqual(util.toUSVString('string\udabc'), 'string\ufffd');
assert.strictEqual(util.toUSVString('string\udbff'), 'string\ufffd');
// Trail surrogates: DC00..DFFF
assert.strictEqual(util.toUSVString('string\udc00'), 'string\ufffd');
assert.strictEqual(util.toUSVString('string\ude12'), 'string\ufffd');
assert.strictEqual(util.toUSVString('string\udfff'), 'string\ufffd');
// Verify surrogate pairs are unaffected.
assert.strictEqual(util.toUSVString('string\ud800\udc00'),
'string\ud800\udc00');

{
assert.strictEqual(util.types.isNativeError(new Error()), true);
Expand Down

0 comments on commit 27be963

Please sign in to comment.