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

Refactor assertString: Faster, less nested and more consistent. #2372

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/lib/util/assertString.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export default function assertString(input) {
const isString = typeof input === 'string' || input instanceof String;

if (!isString) {
let invalidType = typeof input;
if (input === null) invalidType = 'null';
else if (invalidType === 'object') invalidType = input.constructor.name;

throw new TypeError(`Expected a string but received a ${invalidType}`);
}
if (input === undefined) throw new TypeError(`Expected a string but received a ${input}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably check for null as well, see #2372 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add null validation and create tests for both cases.

if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);
}
28 changes: 28 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
import assert from 'assert';
import typeOf from '../src/lib/util/typeOf';
import assertString from '../src/lib/util/assertString';


describe('Util', () => {
it('should validate different typeOf', () => {
Expand All @@ -18,3 +20,29 @@ describe('Util', () => {
assert.notStrictEqual(typeOf([]), 'object');
});
});

describe('assertString', () => {
it('Should throw an error if no argument is provided', () => {
assert.throws(() => { assertString(); }, TypeError);
});

it('Should throw an error if the argument is not a string, number', () => {
assert.throws(() => { assertString(123); }, TypeError);
});

it('Should throw an error if the argument is not a string, Object', () => {
assert.throws(() => { assertString({}); }, TypeError);
});

it('Should throw an error if the argument is not a string, Array', () => {
assert.throws(() => { assertString([]); }, TypeError);
});

it('Should not throw an error if the argument is a string', () => {
assert.doesNotThrow(() => { assertString(''); });
});

it('Should not throw an error if the argument is a string', () => {
assert.doesNotThrow(() => { assertString('testing'); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the empty string unit test? Then we will not forget that we accept that on purpose

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can. I'll create and commit it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we've this unit teste:
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we are at it, we should likely also test cases for null and undefined.

Maybe NaN too? And I guess true and false should also be tested against, for full coverage of all types :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the null input validation and also added the suggested tests. I tried to add the following test:

it('Should not throw an error if the argument is a String instance', () => { assert.doesNotThrow(() => { assertString(new String('antidisestablishmentarianism')); }); });
But some ESLint rules prevent this approach, ESLint "no-new-wrappers.

});
});