Skip to content
Closed
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
lib: return boolean result of assert.match method
The `assert.match` method by default will throws if there is no match,
this change can add the function of instead of throwing or not, returns
the boolean result of match.

Fixes: #33869
  • Loading branch information
juanarbol committed Sep 9, 2020
commit 179801f16a177de45cafeda839a28f86ca9338fb
12 changes: 9 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ assert.ifError = function ifError(err) {
}
};

function internalMatch(string, regexp, message, fn) {
function internalMatch(string, regexp, message, fn, shouldThrows = true) {
if (!isRegExp(regexp)) {
throw new ERR_INVALID_ARG_TYPE(
'regexp', 'RegExp', regexp
Expand All @@ -896,9 +896,11 @@ function internalMatch(string, regexp, message, fn) {
if (typeof string !== 'string' ||
RegExpPrototypeTest(regexp, string) !== match) {
if (message instanceof Error) {
if (!shouldThrows) return false;
throw message;
}

if (!shouldThrows) return false;
const generatedMessage = !message;

// 'The input was expected to not match the regular expression ' +
Expand All @@ -919,10 +921,14 @@ function internalMatch(string, regexp, message, fn) {
err.generatedMessage = generatedMessage;
throw err;
}
if (!shouldThrows) return true;
}

assert.match = function match(string, regexp, message) {
internalMatch(string, regexp, message, match);
assert.match = function match(string, regexp, message, shouldThrows) {
// Undefined shouldThrows and message as boolean
// means message is shouldThrows.
if (!shouldThrows && typeof message === 'boolean') shouldThrows = message;
return internalMatch(string, regexp, message, match, shouldThrows);
};

assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,12 @@ assert.throws(
assert.match('I will pass', /pass$/);
}

// Use the assert.match boolean result.
{
assert(assert.match('meow', /meow$/, false));
assert(!assert.match('meow', /woof$/, false));
}

// Multiple assert.doesNotMatch() tests.
{
assert.throws(
Expand Down