From 1d32c02dc02a93274c06fdb0e2a1179beab75d55 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Fri, 18 Feb 2022 21:08:12 +0600 Subject: [PATCH 1/5] docs: update the js doc --- String/Lower.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/Lower.js b/String/Lower.js index 3805889948..bac14348c8 100644 --- a/String/Lower.js +++ b/String/Lower.js @@ -1,8 +1,8 @@ /** * @function lower * @description Will convert the entire string to lowercase letters. - * @param {String} url - The input URL string - * @return {String} Lowercase string + * @param {String} str - The input string + * @returns {String} Lowercase string * @example lower("HELLO") => hello * @example lower("He_llo") => he_llo */ From b8d0164966e7dbf207edf80fdfb34cc9c32ef81c Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Fri, 18 Feb 2022 21:14:46 +0600 Subject: [PATCH 2/5] pref: Optimize algo via regex ignore the useless traverse in best case via regex and String.prototype.replace --- String/Lower.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/String/Lower.js b/String/Lower.js index bac14348c8..8b2fb83234 100644 --- a/String/Lower.js +++ b/String/Lower.js @@ -12,15 +12,12 @@ const lower = (str) => { throw new TypeError('Invalid Input Type') } - let lowerString = '' + const lowerString = str + .replace(/[A-Z]/g, (_, indexOfUpperChar) => { + const asciiCode = str.charCodeAt(indexOfUpperChar) - for (const char of str) { - let asciiCode = char.charCodeAt(0) - if (asciiCode >= 65 && asciiCode <= 90) { - asciiCode += 32 - } - lowerString += String.fromCharCode(asciiCode) - } + return String.fromCharCode(asciiCode + 32) + }) return lowerString } From 2d70c7b64acd131ea70b33511bb484476292aa27 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Fri, 18 Feb 2022 21:15:25 +0600 Subject: [PATCH 3/5] test: add some new test cases --- String/test/Lower.test.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/String/test/Lower.test.js b/String/test/Lower.test.js index 0fbaaa5d62..98bbaafd9f 100644 --- a/String/test/Lower.test.js +++ b/String/test/Lower.test.js @@ -1,9 +1,19 @@ import { lower } from '../Lower' -describe('Lower', () => { - it('return uppercase strings', () => { - expect(lower('hello')).toBe('hello') +describe('Testing the Lower function', () => { + it('Test 1: Check by invalid type', () => { + expect(() => lower(345)).toThrowError() + expect(() => lower(true)).toThrowError() + expect(() => lower(null)).toThrowError() + }) + + it('Test 2: Check by uppercase string', () => { expect(lower('WORLD')).toBe('world') - expect(lower('hello_WORLD')).toBe('hello_world') + expect(lower('Hello_WORLD')).toBe('hello_world') + }) + + it('Test 3: Check by lowercase string', () => { + expect(lower('hello')).toBe('hello') + expect(lower('hello_world')).toBe('hello_world') }) -}) +}) \ No newline at end of file From a46116d4e8b1c338f9b0c7f2f993ae4de60793f5 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Fri, 18 Feb 2022 21:17:03 +0600 Subject: [PATCH 4/5] fix: styled with standard --- String/test/Lower.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/test/Lower.test.js b/String/test/Lower.test.js index 98bbaafd9f..4a211c38aa 100644 --- a/String/test/Lower.test.js +++ b/String/test/Lower.test.js @@ -16,4 +16,4 @@ describe('Testing the Lower function', () => { expect(lower('hello')).toBe('hello') expect(lower('hello_world')).toBe('hello_world') }) -}) \ No newline at end of file +}) From 131556bc8c23bfa5e0d671d4f7b29e5370736bfa Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sat, 19 Feb 2022 17:31:08 +0600 Subject: [PATCH 5/5] refactor: remove useless variable --- String/Lower.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/String/Lower.js b/String/Lower.js index 8b2fb83234..73d61878c0 100644 --- a/String/Lower.js +++ b/String/Lower.js @@ -12,14 +12,12 @@ const lower = (str) => { throw new TypeError('Invalid Input Type') } - const lowerString = str + return str .replace(/[A-Z]/g, (_, indexOfUpperChar) => { const asciiCode = str.charCodeAt(indexOfUpperChar) return String.fromCharCode(asciiCode + 32) }) - - return lowerString } export { lower }