Files
JavaScript/String/Lower.js
Fahim Faisaal 041918d7b7 merge: Upgrade Lower function (#894)
* docs: update the js doc

* pref: Optimize algo via regex

ignore the useless traverse in best case via regex and String.prototype.replace

* test: add some new test cases

* fix: styled with standard

* refactor: remove useless variable
2022-02-19 17:08:55 +05:30

24 lines
557 B
JavaScript

/**
* @function lower
* @description Will convert the entire string to lowercase letters.
* @param {String} str - The input string
* @returns {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')
}
return str
.replace(/[A-Z]/g, (_, indexOfUpperChar) => {
const asciiCode = str.charCodeAt(indexOfUpperChar)
return String.fromCharCode(asciiCode + 32)
})
}
export { lower }