mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-14 18:03:53 +08:00

* 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
24 lines
557 B
JavaScript
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 }
|