merge: Upgrade checkAnagram function & Fixes (#902)

* pref: optimize the algo via reduce & replace method

* feat: add TypeError for invalid types

* test: upgrade test case for invalid types

* docs: add js doc

* test: modify the case-sensitive test case

* pref: Optimize algo & add case-insensitive mode

* style: format with standard style

* docs: fix the js doc

* docs: rename function name & add comments

* feat: add chackAnagramViaMap function

* test: add test case for checkAnagramViaMap func

* fix: remove **Via** from functions name

* style: fix alignment of js doc

* chore: grammar fix

Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>
This commit is contained in:
Fahim Faisaal
2022-02-25 22:26:30 +06:00
committed by GitHub
parent 0178efd8df
commit d466be977e
2 changed files with 173 additions and 51 deletions

View File

@ -1,6 +1,6 @@
import { checkAnagram } from '../CheckAnagram'
import { checkAnagramMap, checkAnagramRegex } from '../CheckAnagram'
describe('checkAnagram', () => {
describe('Testing checkAnagramRegex', () => {
it.each`
inputOne | inputTwo
${123456} | ${'abcd'}
@ -10,79 +10,172 @@ 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(
() => checkAnagramRegex(inputOne, inputTwo)
).toThrowError()
}
)
it('expects to return false if the arguments have different lengths', () => {
const SUT = checkAnagram('abs', 'abds')
const SUT = checkAnagramRegex('abs', 'abds')
expect(SUT).toBe(false)
})
it('expects to return false if the arguments are not anagrams', () => {
const SUT = checkAnagram('abcs', 'abds')
const SUT = checkAnagramRegex('abcs', 'abds')
expect(SUT).toBe(false)
})
it('expects to return true if the arguments are anagrams', () => {
const SUT = checkAnagram('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 = checkAnagram('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 = checkAnagram('', '')
const SUT = checkAnagramRegex('', '')
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 = checkAnagramRegex('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 = 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 = checkAnagram('', 'edcab')
const SUT = checkAnagramRegex('', 'edcab')
expect(SUT).toBe(false)
const SUT2 = checkAnagram('edcab', '')
const SUT2 = checkAnagramRegex('edcab', '')
expect(SUT2).toBe(false)
})
it('expects to return false if the arguments contain the same letters but have unequal case', () => {
const SUT = checkAnagram('ABDCE', 'abcde')
expect(SUT).toBe(false)
const SUT2 = checkAnagram('AbCdE', 'aBCdE')
expect(SUT2).toBe(false)
it('expects to return true if the arguments contain the same letters but have unequal case', () => {
const SUT = checkAnagramRegex('ABDCE', 'abcde')
expect(SUT).toBe(true)
const SUT2 = checkAnagramRegex('AbCdE', 'aBCdE')
expect(SUT2).toBe(true)
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 = checkAnagram('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 = checkAnagram('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 = checkAnagram('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 = checkAnagram('ea cb', 'e cba')
const SUT = checkAnagramRegex('ea cb', 'e cba')
expect(SUT).toBe(false)
})
})
describe('Testing checkAnagramMap', () => {
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(
() => checkAnagramMap(inputOne, inputTwo)
).toThrowError()
}
)
it('expects to return false if the arguments have different lengths', () => {
const SUT = checkAnagramMap('abs', 'abds')
expect(SUT).toBe(false)
})
it('expects to return false if the arguments are not anagrams', () => {
const SUT = checkAnagramMap('abcs', 'abds')
expect(SUT).toBe(false)
})
it('expects to return true if the arguments are anagrams', () => {
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 = checkAnagramMap('a', 'a')
expect(SUT).toBe(true)
})
it('expects to return true if the arguments of are both empty strings', () => {
const SUT = checkAnagramMap('', '')
expect(SUT).toBe(true)
})
it('expects to return true if the arguments are anagrams with an odd length', () => {
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 = 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 = checkAnagramMap('', 'edcab')
expect(SUT).toBe(false)
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 = checkAnagramMap('ABDCE', 'abcde')
expect(SUT).toBe(true)
const SUT2 = checkAnagramMap('AbCdE', 'aBCdE')
expect(SUT2).toBe(true)
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 = checkAnagramMap('a1b2', '12ba')
expect(SUT).toBe(true)
})
it('expects to return true if the arguments are anagrams and contain space characters', () => {
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 = 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 = checkAnagramMap('ea cb', 'e cba')
expect(SUT).toBe(false)
})
})