Skip to content

Commit b610a88

Browse files
Refactor assertString: Faster, less nested and more consistent. (#2372)
* refactor: Refactor assertString * refactor: Refactor assertString with unit tests * add: Empty string unit test * add: Null input value and more tests * fix: Sentence correction
1 parent 055559d commit b610a88

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/lib/util/assertString.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
export default function assertString(input) {
2-
const isString = typeof input === 'string' || input instanceof String;
3-
4-
if (!isString) {
5-
let invalidType = typeof input;
6-
if (input === null) invalidType = 'null';
7-
else if (invalidType === 'object') invalidType = input.constructor.name;
8-
9-
throw new TypeError(`Expected a string but received a ${invalidType}`);
10-
}
2+
if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`);
3+
if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);
114
}

test/util.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
import assert from 'assert';
66
import typeOf from '../src/lib/util/typeOf';
7+
import assertString from '../src/lib/util/assertString';
8+
79

810
describe('Util', () => {
911
it('should validate different typeOf', () => {
@@ -18,3 +20,45 @@ describe('Util', () => {
1820
assert.notStrictEqual(typeOf([]), 'object');
1921
});
2022
});
23+
24+
describe('assertString', () => {
25+
it('Should throw an error if argument provided is an undefined', () => {
26+
assert.throws(() => { assertString(); }, TypeError);
27+
});
28+
29+
it('Should throw an error if argument provided is a null', () => {
30+
assert.throws(() => { assertString(null); }, TypeError);
31+
});
32+
33+
it('Should throw an error if argument provided is a Boolean', () => {
34+
assert.throws(() => { assertString(true); }, TypeError);
35+
});
36+
37+
it('Should throw an error if argument provided is a Date', () => {
38+
assert.throws(() => { assertString(new Date()); }, TypeError);
39+
});
40+
41+
it('Should throw an error if argument provided is a Number(NaN)', () => {
42+
assert.throws(() => { assertString(NaN); }, TypeError);
43+
});
44+
45+
it('Should throw an error if argument provided is a Number', () => {
46+
assert.throws(() => { assertString(2024); }, TypeError);
47+
});
48+
49+
it('Should throw an error if argument provided is an Object', () => {
50+
assert.throws(() => { assertString({}); }, TypeError);
51+
});
52+
53+
it('Should throw an error if argument provided is an Array', () => {
54+
assert.throws(() => { assertString([]); }, TypeError);
55+
});
56+
57+
it('Should not throw an error if the argument is an empty string', () => {
58+
assert.doesNotThrow(() => { assertString(''); });
59+
});
60+
61+
it('Should not throw an error if the argument is a String', () => {
62+
assert.doesNotThrow(() => { assertString('antidisestablishmentarianism'); });
63+
});
64+
});

0 commit comments

Comments
 (0)