merge: Add lower (#863)

This commit is contained in:
YATIN KATHURIA
2021-12-04 11:01:58 +05:30
committed by GitHub
parent 62b151eb69
commit 4c27e1534e
2 changed files with 37 additions and 0 deletions

28
String/Lower.js Normal file
View File

@ -0,0 +1,28 @@
/**
* @function lower
* @description Will convert the entire string to lowercase letters.
* @param {String} url - The input URL string
* @return {String} Lowercase string
* @example lower("HELLO") => hello
* @example lower("He_llo") => he_llo
*/
const lower = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Invalid Input Type')
}
let lowerString = ''
for (const char of str) {
let asciiCode = char.charCodeAt(0)
if (asciiCode >= 65 && asciiCode <= 90) {
asciiCode += 32
}
lowerString += String.fromCharCode(asciiCode)
}
return lowerString
}
export { lower }

View File

@ -0,0 +1,9 @@
import { lower } from '../Lower'
describe('Lower', () => {
it('return uppercase strings', () => {
expect(lower('hello')).toBe('hello')
expect(lower('WORLD')).toBe('world')
expect(lower('hello_WORLD')).toBe('hello_world')
})
})