merge: Added replace method to Upper (#916)

* feat: added replace method

* resolve: fix algo
This commit is contained in:
Fahim Faisaal
2022-03-05 15:10:39 +06:00
committed by GitHub
parent 00f593662b
commit 35ba618355

View File

@ -1,28 +1,24 @@
/** /**
* @function upper * @function upper
* @description Will convert the entire string to uppercase letters. * @description Will convert the entire string to uppercase letters.
* @param {String} url - The input URL string * @param {String} str - The input string
* @return {String} Uppercase string * @return {String} Uppercase string
* @example upper("hello") => HELLO * @example upper("hello") => HELLO
* @example upper("He_llo") => HE_LLO * @example upper("He_llo") => HE_LLO
*/ */
const upper = (str) => { const upper = (str) => {
if (typeof str !== 'string') { if (typeof str !== 'string') {
throw new TypeError('Invalid Input Type') throw new TypeError('Argument should be string')
} }
let upperString = '' return str.replace(
/[a-z]/g,
(_, indexOfLowerChar) => {
const asciiCode = str.charCodeAt(indexOfLowerChar)
for (const char of str) { return String.fromCharCode(asciiCode - 32)
let asciiCode = char.charCodeAt(0)
if (asciiCode >= 97 && asciiCode <= 122) {
asciiCode -= 32
} }
upperString += String.fromCharCode(asciiCode) )
}
return upperString
} }
export { upper } export { upper }