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 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
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 || input === null) throw new TypeError(`Expected a string but received a ${input}`);
if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);
}
44 changes: 44 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,45 @@ describe('Util', () => {
assert.notStrictEqual(typeOf([]), 'object');
});
});

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

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

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

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

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

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

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

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

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

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