From 769a3889fff2e46dcfd4746226300a498b0a0dba Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月23日 14:31:02 +0600 Subject: [PATCH 01/14] pref: optimize the algo via reduce & replace method --- String/CheckAnagram.js | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 21ca1458d8..ceae9903e6 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -12,36 +12,10 @@ const checkAnagram = (str1, str2) => { return false } - // Use hashmap to keep count of characters in str1 - - const str1CharCount = new Map() - - for (let i = 0; i < str1.length; i++) { - let previousCount = 0 - if (str1CharCount.has(str1[i])) { - previousCount = str1CharCount.get(str1[i]) - } - str1CharCount.set(str1[i], previousCount + 1) - } - - // Now check if second string has same characters? - - for (let i = 0; i < str2.length; i++) { - let previousCount = 0 - // if str1CharCount has no key for str2[i] then not anagram. - if (!str1CharCount.has(str2[i])) return false - - previousCount = str1CharCount.get(str2[i]) - str1CharCount.set(str2[i], previousCount - 1) - } - - // Now check if all entries in hashmap has zeros. - - for (const key in str1CharCount) { - if (str1CharCount[key] !== 0) return false - } - - return true + return ![...str1].reduce( + (str2Acc, cur) => str2Acc.replace(cur, ''), // remove the similar letter from str2Acc + str2 + ) } export { checkAnagram } From d0d8bff8e9707dad1678c65c90cfaa87a4a38e62 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月23日 15:22:44 +0600 Subject: [PATCH 02/14] feat: add TypeError for invalid types --- String/CheckAnagram.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index ceae9903e6..2b1394e0eb 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -4,7 +4,7 @@ const checkAnagram = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { - return 'Not string(s)' + throw new TypeError('Arguments should be string both') } // If both strings have not same lengths then they can not be anagram. From 40ae7b6b9ed62c62751737865c195f4ac25c488c Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月23日 15:23:35 +0600 Subject: [PATCH 03/14] test: upgrade test case for invalid types --- String/test/CheckAnagram.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index ae1a3a593a..c794fe2f71 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -10,10 +10,11 @@ describe('checkAnagram', () => { ${'abcd'} | ${[1, 2, 3, 4, 5, 6]} ${'abcd'} | ${{ test: 'test' }} `( - 'expects to return "Not string(s)" given values $inputOne and $inputTwo', + 'expects to throw the type Error given values $inputOne and $inputTwo', ({ inputOne, inputTwo }) => { - const SUT = checkAnagram(inputOne, inputTwo) - expect(SUT).toBe('Not string(s)') + expect( + () => checkAnagram(inputOne, inputTwo) + ).toThrowError() } ) From 7c7f8c70feb69289328ce900656b5a1cf691d9e6 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 07:46:58 +0600 Subject: [PATCH 04/14] docs: add js doc --- String/CheckAnagram.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 2b1394e0eb..0f0e1dd689 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,6 +1,12 @@ -// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. -// Anagram check is case sensitive; i.e. Aba and aba is not a anagram. -// inputs are strings i.e. str1 and str2 +/** + * @function checkAnagram + * @param {string} str1 + * @param {string} str2 + * @returns {boolean} + * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is case sensitive; + * @example - checkAnagram('node', 'deno') => true + * @example - checkAnagram('Eleven plus two', 'Twelve plus one') => true + */ const checkAnagram = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { From db1f2615e8deef3354196e78de2b1d9ffba38c33 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 07:49:43 +0600 Subject: [PATCH 05/14] test: modify the case-sensitive test case --- String/test/CheckAnagram.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index c794fe2f71..72ac029b09 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -60,11 +60,13 @@ describe('checkAnagram', () => { expect(SUT2).toBe(false) }) - it('expects to return false if the arguments contain the same letters but have unequal case', () => { + it('expects to return true if the arguments contain the same letters but have unequal case', () => { const SUT = checkAnagram('ABDCE', 'abcde') - expect(SUT).toBe(false) + expect(SUT).toBe(true) const SUT2 = checkAnagram('AbCdE', 'aBCdE') - expect(SUT2).toBe(false) + expect(SUT2).toBe(true) + const SUT3 = checkAnagram('Eleven plus two', 'Twelve plus one') + expect(SUT3).toBe(true) }) it('expects to return true if the arguments are anagrams and contain number characters', () => { From 28836df0690eb2b92bb8d91c622c3a24c12d1acd Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 07:51:41 +0600 Subject: [PATCH 06/14] pref: Optimize algo & add case-insensitive mode --- String/CheckAnagram.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 0f0e1dd689..f48becb214 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -18,8 +18,9 @@ const checkAnagram = (str1, str2) => { return false } + // str1 converted to array and traverse the each letter of str1 by reduce method return ![...str1].reduce( - (str2Acc, cur) => str2Acc.replace(cur, ''), // remove the similar letter from str2Acc + (str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive str2 ) } From aaeed302833f874d349be0de281294fc54ea629e Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 08:17:34 +0600 Subject: [PATCH 07/14] style: format with standard style --- String/CheckAnagram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index f48becb214..2eafb7237c 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,7 +1,7 @@ /** * @function checkAnagram - * @param {string} str1 - * @param {string} str2 + * @param {string} str1 + * @param {string} str2 * @returns {boolean} * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is case sensitive; * @example - checkAnagram('node', 'deno') => true From 0fd4db1d8a7311cf202192cdd88a9aa0ba0c6399 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 19:36:33 +0600 Subject: [PATCH 08/14] docs: fix the js doc --- String/CheckAnagram.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 2eafb7237c..17e3484bd9 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -3,7 +3,7 @@ * @param {string} str1 * @param {string} str2 * @returns {boolean} - * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is case sensitive; + * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; * @example - checkAnagram('node', 'deno') => true * @example - checkAnagram('Eleven plus two', 'Twelve plus one') => true */ From 5d097a51416158388a9f91f9ed495cb0c726a2d1 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 21:02:04 +0600 Subject: [PATCH 09/14] docs: rename function name & add comments --- String/CheckAnagram.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 17e3484bd9..495afa4473 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,13 +1,13 @@ /** - * @function checkAnagram + * @function checkAnagramViaRegex * @param {string} str1 * @param {string} str2 * @returns {boolean} * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; - * @example - checkAnagram('node', 'deno') => true - * @example - checkAnagram('Eleven plus two', 'Twelve plus one') => true + * @example - checkAnagramViaRegex('node', 'deno') => true + * @example - checkAnagramViaRegex('Eleven plus two', 'Twelve plus one') => true */ -const checkAnagram = (str1, str2) => { +const checkAnagramViaRegex = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { throw new TypeError('Arguments should be string both') @@ -18,11 +18,15 @@ const checkAnagram = (str1, str2) => { return false } - // str1 converted to array and traverse the each letter of str1 by reduce method + /** + * str1 converted to an array and traverse each letter of str1 by reduce method + * reduce method return string which is empty or not. + * if it returns empty string '' -> falsy, with Logical !(NOT) Operator, it's will be converted to boolean and return true else false + */ return ![...str1].reduce( (str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive str2 ) } -export { checkAnagram } +export { checkAnagramViaRegex } From 98a5b2bb03d43585b3e059c6452339362570d64b Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 21:37:02 +0600 Subject: [PATCH 10/14] feat: add chackAnagramViaMap function --- String/CheckAnagram.js | 48 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 495afa4473..0e19d459f1 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,9 +1,10 @@ +// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; /** * @function checkAnagramViaRegex * @param {string} str1 * @param {string} str2 * @returns {boolean} - * @description An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; + * @description - check anagram with the help of Regex * @example - checkAnagramViaRegex('node', 'deno') => true * @example - checkAnagramViaRegex('Eleven plus two', 'Twelve plus one') => true */ @@ -29,4 +30,47 @@ const checkAnagramViaRegex = (str1, str2) => { ) } -export { checkAnagramViaRegex } +/** + * @function checkAnagramViaMap + * @description - check anagram via using HashMap + * @param {string} str1 + * @param {string} str2 + * @returns {boolean} +* @example - checkAnagramViaMap('node', 'deno') => true + * @example - checkAnagramViaMap('Eleven plus two', 'Twelve plus one') => true + */ +const checkAnagramViaMap = (str1, str2) => { + // check that inputs are strings. + if (typeof str1 !== 'string' || typeof str2 !== 'string') { + throw new TypeError('Arguments should be string both') + } + + // If both strings have not same lengths then they can not be anagram. + if (str1.length !== str2.length) { + return false + } + + const str1List = Array.from(str1.toUpperCase()) // str1 to array + + // get the occurrences of str1 characters by using HashMap + const str1Occurs = str1List.reduce( + (map, char) => map.set(char, map.get(char) + 1 || 1), + new Map() + ) + + for (const char of str2.toUpperCase()) { + // if char has not exist to the map it's return false + if (!str1Occurs.has(char)) { + return false + } + + let getCharCount = str1Occurs.get(char) + str1Occurs.set(char, --getCharCount) + + getCharCount === 0 && str1Occurs.delete(char) + } + + return true +} + +export { checkAnagramViaRegex, checkAnagramViaMap } From 828d2d38de02378994b07acf59a928c225ec450b Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月24日 21:37:48 +0600 Subject: [PATCH 11/14] test: add test case for checkAnagramViaMap func --- String/test/CheckAnagram.test.js | 128 ++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 19 deletions(-) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index 72ac029b09..db0137b5a1 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -1,6 +1,6 @@ -import { checkAnagram } from '../CheckAnagram' +import { checkAnagramViaMap, checkAnagramViaRegex } from '../CheckAnagram' -describe('checkAnagram', () => { +describe('Testing checkAnagramViaRegex', () => { it.each` inputOne | inputTwo ${123456} | ${'abcd'} @@ -13,79 +13,169 @@ describe('checkAnagram', () => { 'expects to throw the type Error given values $inputOne and $inputTwo', ({ inputOne, inputTwo }) => { expect( - () => checkAnagram(inputOne, inputTwo) + () => checkAnagramViaRegex(inputOne, inputTwo) ).toThrowError() } ) it('expects to return false if the arguments have different lengths', () => { - const SUT = checkAnagram('abs', 'abds') + const SUT = checkAnagramViaRegex('abs', 'abds') expect(SUT).toBe(false) }) it('expects to return false if the arguments are not anagrams', () => { - const SUT = checkAnagram('abcs', 'abds') + const SUT = checkAnagramViaRegex('abcs', 'abds') expect(SUT).toBe(false) }) it('expects to return true if the arguments are anagrams', () => { - const SUT = checkAnagram('abcd', 'bcad') + const SUT = checkAnagramViaRegex('abcd', 'bcad') expect(SUT).toBe(true) }) it('expects to return true if the arguments of length 1 and are the same letter', () => { - const SUT = checkAnagram('a', 'a') + const SUT = checkAnagramViaRegex('a', 'a') expect(SUT).toBe(true) }) it('expects to return true if the arguments of are both empty strings', () => { - const SUT = checkAnagram('', '') + const SUT = checkAnagramViaRegex('', '') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an odd length', () => { - const SUT = checkAnagram('abcde', 'edcab') + const SUT = checkAnagramViaRegex('abcde', 'edcab') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an even length', () => { - const SUT = checkAnagram('abcdef', 'fedcab') + const SUT = checkAnagramViaRegex('abcdef', 'fedcab') expect(SUT).toBe(true) }) it('expects to return false if either argument is an empty string while the other is not', () => { - const SUT = checkAnagram('', 'edcab') + const SUT = checkAnagramViaRegex('', 'edcab') expect(SUT).toBe(false) - const SUT2 = checkAnagram('edcab', '') + const SUT2 = checkAnagramViaRegex('edcab', '') expect(SUT2).toBe(false) }) it('expects to return true if the arguments contain the same letters but have unequal case', () => { - const SUT = checkAnagram('ABDCE', 'abcde') + const SUT = checkAnagramViaRegex('ABDCE', 'abcde') expect(SUT).toBe(true) - const SUT2 = checkAnagram('AbCdE', 'aBCdE') + const SUT2 = checkAnagramViaRegex('AbCdE', 'aBCdE') expect(SUT2).toBe(true) - const SUT3 = checkAnagram('Eleven plus two', 'Twelve plus one') + const SUT3 = checkAnagramViaRegex('Eleven plus two', 'Twelve plus one') expect(SUT3).toBe(true) }) it('expects to return true if the arguments are anagrams and contain number characters', () => { - const SUT = checkAnagram('a1b2', '12ba') + const SUT = checkAnagramViaRegex('a1b2', '12ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain space characters', () => { - const SUT = checkAnagram('a1 b2', '1 2ba') + const SUT = checkAnagramViaRegex('a1 b2', '1 2ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { - const SUT = checkAnagram('a!1b@2', '1@2ba!') + const SUT = checkAnagramViaRegex('a!1b@2', '1@2ba!') expect(SUT).toBe(true) }) it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { - const SUT = checkAnagram('ea cb', 'e cba') + const SUT = checkAnagramViaRegex('ea cb', 'e cba') + expect(SUT).toBe(false) + }) +}) + +describe('Testing checkAnagramViaMap', () => { + it.each` + inputOne | inputTwo + ${123456} | ${'abcd'} + ${[1, 2, 3, 4, 5, 6]} | ${'abcd'} + ${{ test: 'test' }} | ${'abcd'} + ${'abcd'} | ${123456} + ${'abcd'} | ${[1, 2, 3, 4, 5, 6]} + ${'abcd'} | ${{ test: 'test' }} + `( + 'expects to throw the type Error given values $inputOne and $inputTwo', + ({ inputOne, inputTwo }) => { + expect( + () => checkAnagramViaMap(inputOne, inputTwo) + ).toThrowError() + } + ) + + it('expects to return false if the arguments have different lengths', () => { + const SUT = checkAnagramViaMap('abs', 'abds') + expect(SUT).toBe(false) + }) + + it('expects to return false if the arguments are not anagrams', () => { + const SUT = checkAnagramViaMap('abcs', 'abds') + expect(SUT).toBe(false) + }) + + it('expects to return true if the arguments are anagrams', () => { + const SUT = checkAnagramViaMap('abcd', 'bcad') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments of length 1 and are the same letter', () => { + const SUT = checkAnagramViaMap('a', 'a') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments of are both empty strings', () => { + const SUT = checkAnagramViaMap('', '') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments are anagrams with an odd length', () => { + const SUT = checkAnagramViaMap('abcde', 'edcab') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments are anagrams with an even length', () => { + const SUT = checkAnagramViaMap('abcdef', 'fedcab') + expect(SUT).toBe(true) + }) + + it('expects to return false if either argument is an empty string while the other is not', () => { + const SUT = checkAnagramViaMap('', 'edcab') + expect(SUT).toBe(false) + const SUT2 = checkAnagramViaMap('edcab', '') + expect(SUT2).toBe(false) + }) + + it('expects to return true if the arguments contain the same letters but have unequal case', () => { + const SUT = checkAnagramViaMap('ABDCE', 'abcde') + expect(SUT).toBe(true) + const SUT2 = checkAnagramViaMap('AbCdE', 'aBCdE') + expect(SUT2).toBe(true) + const SUT3 = checkAnagramViaMap('Eleven plus two', 'Twelve plus one') + expect(SUT3).toBe(true) + }) + + it('expects to return true if the arguments are anagrams and contain number characters', () => { + const SUT = checkAnagramViaMap('a1b2', '12ba') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments are anagrams and contain space characters', () => { + const SUT = checkAnagramViaMap('a1 b2', '1 2ba') + expect(SUT).toBe(true) + }) + + it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { + const SUT = checkAnagramViaMap('a!1b@2', '1@2ba!') + expect(SUT).toBe(true) + }) + + it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { + const SUT = checkAnagramViaMap('ea cb', 'e cba') expect(SUT).toBe(false) }) }) From e24f57ec1cf5ee170fc43b638af878b191eae528 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月25日 00:02:03 +0600 Subject: [PATCH 12/14] fix: remove **Via** from functions name --- String/CheckAnagram.js | 18 ++++---- String/test/CheckAnagram.test.js | 74 ++++++++++++++++---------------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 0e19d459f1..d3c6695560 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,14 +1,14 @@ // An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; /** - * @function checkAnagramViaRegex + * @function checkAnagramRegex * @param {string} str1 * @param {string} str2 * @returns {boolean} * @description - check anagram with the help of Regex - * @example - checkAnagramViaRegex('node', 'deno') => true - * @example - checkAnagramViaRegex('Eleven plus two', 'Twelve plus one') => true + * @example - checkAnagramRegex('node', 'deno') => true + * @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true */ -const checkAnagramViaRegex = (str1, str2) => { +const checkAnagramRegex = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { throw new TypeError('Arguments should be string both') @@ -31,15 +31,15 @@ const checkAnagramViaRegex = (str1, str2) => { } /** - * @function checkAnagramViaMap + * @function checkAnagramMap * @description - check anagram via using HashMap * @param {string} str1 * @param {string} str2 * @returns {boolean} -* @example - checkAnagramViaMap('node', 'deno') => true - * @example - checkAnagramViaMap('Eleven plus two', 'Twelve plus one') => true +* @example - checkAnagramMap('node', 'deno') => true + * @example - checkAnagramMap('Eleven plus two', 'Twelve plus one') => true */ -const checkAnagramViaMap = (str1, str2) => { +const checkAnagramMap = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { throw new TypeError('Arguments should be string both') @@ -73,4 +73,4 @@ const checkAnagramViaMap = (str1, str2) => { return true } -export { checkAnagramViaRegex, checkAnagramViaMap } +export { checkAnagramRegex, checkAnagramMap } diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index db0137b5a1..44adc196bc 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -1,6 +1,6 @@ -import { checkAnagramViaMap, checkAnagramViaRegex } from '../CheckAnagram' +import { checkAnagramMap, checkAnagramRegex } from '../CheckAnagram' -describe('Testing checkAnagramViaRegex', () => { +describe('Testing checkAnagramRegex', () => { it.each` inputOne | inputTwo ${123456} | ${'abcd'} @@ -13,84 +13,84 @@ describe('Testing checkAnagramViaRegex', () => { 'expects to throw the type Error given values $inputOne and $inputTwo', ({ inputOne, inputTwo }) => { expect( - () => checkAnagramViaRegex(inputOne, inputTwo) + () => checkAnagramRegex(inputOne, inputTwo) ).toThrowError() } ) it('expects to return false if the arguments have different lengths', () => { - const SUT = checkAnagramViaRegex('abs', 'abds') + const SUT = checkAnagramRegex('abs', 'abds') expect(SUT).toBe(false) }) it('expects to return false if the arguments are not anagrams', () => { - const SUT = checkAnagramViaRegex('abcs', 'abds') + const SUT = checkAnagramRegex('abcs', 'abds') expect(SUT).toBe(false) }) it('expects to return true if the arguments are anagrams', () => { - const SUT = checkAnagramViaRegex('abcd', 'bcad') + const SUT = checkAnagramRegex('abcd', 'bcad') expect(SUT).toBe(true) }) it('expects to return true if the arguments of length 1 and are the same letter', () => { - const SUT = checkAnagramViaRegex('a', 'a') + const SUT = checkAnagramRegex('a', 'a') expect(SUT).toBe(true) }) it('expects to return true if the arguments of are both empty strings', () => { - const SUT = checkAnagramViaRegex('', '') + const SUT = checkAnagramRegex('', '') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an odd length', () => { - const SUT = checkAnagramViaRegex('abcde', 'edcab') + const SUT = checkAnagramRegex('abcde', 'edcab') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an even length', () => { - const SUT = checkAnagramViaRegex('abcdef', 'fedcab') + const SUT = checkAnagramRegex('abcdef', 'fedcab') expect(SUT).toBe(true) }) it('expects to return false if either argument is an empty string while the other is not', () => { - const SUT = checkAnagramViaRegex('', 'edcab') + const SUT = checkAnagramRegex('', 'edcab') expect(SUT).toBe(false) - const SUT2 = checkAnagramViaRegex('edcab', '') + const SUT2 = checkAnagramRegex('edcab', '') expect(SUT2).toBe(false) }) it('expects to return true if the arguments contain the same letters but have unequal case', () => { - const SUT = checkAnagramViaRegex('ABDCE', 'abcde') + const SUT = checkAnagramRegex('ABDCE', 'abcde') expect(SUT).toBe(true) - const SUT2 = checkAnagramViaRegex('AbCdE', 'aBCdE') + const SUT2 = checkAnagramRegex('AbCdE', 'aBCdE') expect(SUT2).toBe(true) - const SUT3 = checkAnagramViaRegex('Eleven plus two', 'Twelve plus one') + const SUT3 = checkAnagramRegex('Eleven plus two', 'Twelve plus one') expect(SUT3).toBe(true) }) it('expects to return true if the arguments are anagrams and contain number characters', () => { - const SUT = checkAnagramViaRegex('a1b2', '12ba') + const SUT = checkAnagramRegex('a1b2', '12ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain space characters', () => { - const SUT = checkAnagramViaRegex('a1 b2', '1 2ba') + const SUT = checkAnagramRegex('a1 b2', '1 2ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { - const SUT = checkAnagramViaRegex('a!1b@2', '1@2ba!') + const SUT = checkAnagramRegex('a!1b@2', '1@2ba!') expect(SUT).toBe(true) }) it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { - const SUT = checkAnagramViaRegex('ea cb', 'e cba') + const SUT = checkAnagramRegex('ea cb', 'e cba') expect(SUT).toBe(false) }) }) -describe('Testing checkAnagramViaMap', () => { +describe('Testing checkAnagramMap', () => { it.each` inputOne | inputTwo ${123456} | ${'abcd'} @@ -103,79 +103,79 @@ describe('Testing checkAnagramViaMap', () => { 'expects to throw the type Error given values $inputOne and $inputTwo', ({ inputOne, inputTwo }) => { expect( - () => checkAnagramViaMap(inputOne, inputTwo) + () => checkAnagramMap(inputOne, inputTwo) ).toThrowError() } ) it('expects to return false if the arguments have different lengths', () => { - const SUT = checkAnagramViaMap('abs', 'abds') + const SUT = checkAnagramMap('abs', 'abds') expect(SUT).toBe(false) }) it('expects to return false if the arguments are not anagrams', () => { - const SUT = checkAnagramViaMap('abcs', 'abds') + const SUT = checkAnagramMap('abcs', 'abds') expect(SUT).toBe(false) }) it('expects to return true if the arguments are anagrams', () => { - const SUT = checkAnagramViaMap('abcd', 'bcad') + const SUT = checkAnagramMap('abcd', 'bcad') expect(SUT).toBe(true) }) it('expects to return true if the arguments of length 1 and are the same letter', () => { - const SUT = checkAnagramViaMap('a', 'a') + const SUT = checkAnagramMap('a', 'a') expect(SUT).toBe(true) }) it('expects to return true if the arguments of are both empty strings', () => { - const SUT = checkAnagramViaMap('', '') + const SUT = checkAnagramMap('', '') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an odd length', () => { - const SUT = checkAnagramViaMap('abcde', 'edcab') + const SUT = checkAnagramMap('abcde', 'edcab') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams with an even length', () => { - const SUT = checkAnagramViaMap('abcdef', 'fedcab') + const SUT = checkAnagramMap('abcdef', 'fedcab') expect(SUT).toBe(true) }) it('expects to return false if either argument is an empty string while the other is not', () => { - const SUT = checkAnagramViaMap('', 'edcab') + const SUT = checkAnagramMap('', 'edcab') expect(SUT).toBe(false) - const SUT2 = checkAnagramViaMap('edcab', '') + const SUT2 = checkAnagramMap('edcab', '') expect(SUT2).toBe(false) }) it('expects to return true if the arguments contain the same letters but have unequal case', () => { - const SUT = checkAnagramViaMap('ABDCE', 'abcde') + const SUT = checkAnagramMap('ABDCE', 'abcde') expect(SUT).toBe(true) - const SUT2 = checkAnagramViaMap('AbCdE', 'aBCdE') + const SUT2 = checkAnagramMap('AbCdE', 'aBCdE') expect(SUT2).toBe(true) - const SUT3 = checkAnagramViaMap('Eleven plus two', 'Twelve plus one') + const SUT3 = checkAnagramMap('Eleven plus two', 'Twelve plus one') expect(SUT3).toBe(true) }) it('expects to return true if the arguments are anagrams and contain number characters', () => { - const SUT = checkAnagramViaMap('a1b2', '12ba') + const SUT = checkAnagramMap('a1b2', '12ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain space characters', () => { - const SUT = checkAnagramViaMap('a1 b2', '1 2ba') + const SUT = checkAnagramMap('a1 b2', '1 2ba') expect(SUT).toBe(true) }) it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { - const SUT = checkAnagramViaMap('a!1b@2', '1@2ba!') + const SUT = checkAnagramMap('a!1b@2', '1@2ba!') expect(SUT).toBe(true) }) it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { - const SUT = checkAnagramViaMap('ea cb', 'e cba') + const SUT = checkAnagramMap('ea cb', 'e cba') expect(SUT).toBe(false) }) }) From 423c2c1b47dd35efc1d9638e4708901c80fd368f Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: 2022年2月25日 00:22:04 +0600 Subject: [PATCH 13/14] style: fix alignment of js doc --- String/CheckAnagram.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index d3c6695560..2044de5c3e 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -36,7 +36,7 @@ const checkAnagramRegex = (str1, str2) => { * @param {string} str1 * @param {string} str2 * @returns {boolean} -* @example - checkAnagramMap('node', 'deno') => true + * @example - checkAnagramMap('node', 'deno') => true * @example - checkAnagramMap('Eleven plus two', 'Twelve plus one') => true */ const checkAnagramMap = (str1, str2) => { From 87b3a4a8129e4f3e53b11435c470e187dd30eb79 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: 2022年2月25日 21:49:48 +0530 Subject: [PATCH 14/14] chore: grammar fix --- String/CheckAnagram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 2044de5c3e..1fc5854298 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -11,7 +11,7 @@ const checkAnagramRegex = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { - throw new TypeError('Arguments should be string both') + throw new TypeError('Both arguments should be strings.') } // If both strings have not same lengths then they can not be anagram. @@ -42,7 +42,7 @@ const checkAnagramRegex = (str1, str2) => { const checkAnagramMap = (str1, str2) => { // check that inputs are strings. if (typeof str1 !== 'string' || typeof str2 !== 'string') { - throw new TypeError('Arguments should be string both') + throw new TypeError('Both arguments should be strings.') } // If both strings have not same lengths then they can not be anagram.

AltStyle によって変換されたページ (->オリジナル) /