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

Add case-insensitive equality check #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@
return this;
};

/**
* Checks if the string equals another case insensitively.
*/

Assertion.prototype.equalCaseInsensitive = function (obj) {
this.assert(
expect.equalCaseInsensitive(this.obj, obj)
, function(){ return 'expected ' + i(this.obj) + ' to be equal case insensitively ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to be not equal case insensitively ' + i(obj) }
, obj);
return this;
};

/**
* Assert within start to finish (inclusive).
*
Expand Down Expand Up @@ -910,6 +923,26 @@
}
};

/**
* Asserts strings equality case-insensitive
*/

expect.equalCaseInsensitive = function equalCaseInsensitive(actual, expected) {
// check if values are string
if (typeof actual != "string" && typeof expected != "string"){
throw new Error(`values are not string, actual value is a ${typeof actual} and expected value is a ${typeof expected}`);
} else if (typeof actual != "string"){
throw new Error(`actual value is not string, it's a ${typeof actual}`);
} else if (typeof expected != "string"){
throw new Error(`expected value is not string, it's a ${typeof expected}`);
}

// check values equality case-insensitive
const lowerCaseActual = actual.toLowerCase();
const lowerCaseExpected = expected.toLowerCase();
return lowerCaseActual === lowerCaseExpected;
};

function isUndefinedOrNull (value) {
return value === null || value === undefined;
}
Expand Down