From a8f5eb703baa32eff9e007bd2e1546dd29da107d Mon Sep 17 00:00:00 2001 From: EmersonRabelo Date: Thu, 8 Feb 2024 19:36:30 -0300 Subject: [PATCH 1/5] refactor: Refactor assertString --- src/lib/util/assertString.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/lib/util/assertString.js b/src/lib/util/assertString.js index 948bcba66..3f0f281ad 100644 --- a/src/lib/util/assertString.js +++ b/src/lib/util/assertString.js @@ -1,11 +1,6 @@ -export default function assertString(input) { - const isString = typeof input === 'string' || input instanceof String; +const assertString = (input) => { + if (!input) throw new TypeError(`Expected a string but received a ${input || 'null'}`); + if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`); +}; - 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}`); - } -} +export default assertString; From 241c4a879573323eac15242443aa6ddb4d3dd983 Mon Sep 17 00:00:00 2001 From: EmersonRabelo Date: Fri, 9 Feb 2024 00:41:35 -0300 Subject: [PATCH 2/5] refactor: Refactor assertString with unit tests --- src/lib/util/assertString.js | 8 +++----- test/util.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/lib/util/assertString.js b/src/lib/util/assertString.js index 3f0f281ad..70f4a856c 100644 --- a/src/lib/util/assertString.js +++ b/src/lib/util/assertString.js @@ -1,6 +1,4 @@ -const assertString = (input) => { - if (!input) throw new TypeError(`Expected a string but received a ${input || 'null'}`); +export default function assertString(input) { + if (input === undefined) 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}`); -}; - -export default assertString; +} diff --git a/test/util.test.js b/test/util.test.js index 449cd9ee7..83891c52b 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -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', () => { @@ -18,3 +20,25 @@ 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('testing'); }); + }); +}); From 69cb5fac5e8d8c9bc211f996a5378b4fb9c6d7fa Mon Sep 17 00:00:00 2001 From: EmersonRabelo Date: Fri, 9 Feb 2024 09:06:30 -0300 Subject: [PATCH 3/5] add: Empty string unit test --- test/util.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/util.test.js b/test/util.test.js index 83891c52b..caf19893d 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -38,6 +38,10 @@ describe('assertString', () => { 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'); }); }); From f726e2d7bb19365d8dfe74a358036d7f8cfb2303 Mon Sep 17 00:00:00 2001 From: EmersonRabelo Date: Fri, 9 Feb 2024 17:10:08 -0300 Subject: [PATCH 4/5] add: Null input value and more tests --- src/lib/util/assertString.js | 2 +- test/util.test.js | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/lib/util/assertString.js b/src/lib/util/assertString.js index 70f4a856c..3baa4452f 100644 --- a/src/lib/util/assertString.js +++ b/src/lib/util/assertString.js @@ -1,4 +1,4 @@ export default function assertString(input) { - if (input === undefined) throw new TypeError(`Expected a string but received a ${input}`); + 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}`); } diff --git a/test/util.test.js b/test/util.test.js index caf19893d..daf1fcc53 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -22,27 +22,43 @@ describe('Util', () => { }); describe('assertString', () => { - it('Should throw an error if no argument is provided', () => { + it('Should throw an error if no argument is provided, undefined', () => { 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 argument provided is a null', () => { + assert.throws(() => { assertString(null); }, TypeError); }); - it('Should throw an error if the argument is not a string, Object', () => { + 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 the argument is not a string, Array', () => { + 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 a string', () => { + 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('testing'); }); + it('Should not throw an error if the argument is a String', () => { + assert.doesNotThrow(() => { assertString('antidisestablishmentarianism'); }); }); }); From f8e62de84bc6cb52c5398bb339f98c778dd44509 Mon Sep 17 00:00:00 2001 From: EmersonRabelo Date: Fri, 9 Feb 2024 17:20:08 -0300 Subject: [PATCH 5/5] fix: Sentence correction --- test/util.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/util.test.js b/test/util.test.js index daf1fcc53..0146a4e2c 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -22,7 +22,7 @@ describe('Util', () => { }); describe('assertString', () => { - it('Should throw an error if no argument is provided, undefined', () => { + it('Should throw an error if argument provided is an undefined', () => { assert.throws(() => { assertString(); }, TypeError); });