Created
September 5, 2019 15:45
-
-
Save debonx/b298560573b6e7a2d7093d69718c64ad to your computer and use it in GitHub Desktop.
A fancy Javascript object to manipulate strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FancyString = {}; | |
FancyString.countCharacter = function(inputString, inputCharacter) { | |
let count = 0; | |
let string = inputString.toLowerCase(); | |
let character = inputCharacter.toLowerCase(); | |
for (let i = 0; i < string.length; i++) { | |
if (string[i] === character) { | |
count++; | |
} | |
} | |
return count; | |
}; | |
FancyString.capitalizeFirstCharacterOfWords = function(string) { | |
let arr = string.split(" "); | |
for (let i = 0; i < arr.length; i++) { | |
let word = arr[i]; | |
arr[i] = word[0].toUpperCase() + word.substring(1); | |
} | |
return arr.join(" "); | |
}; | |
FancyString.reverseWord = function(word) { | |
return word.split("").reverse().join(""); | |
}; | |
FancyString.reverseAllWords = function(sentence) { | |
let words = sentence.split(" "); | |
for (let i = 0; i < words.length; i++) { | |
words[i] = FancyString.reverseWord(words[i]); | |
} | |
return words.join(" "); | |
}; | |
FancyString.replaceFirstOccurence = function(string, toBeReplaced, replaceWith) { | |
return string.replace(toBeReplaced, replaceWith); | |
}; | |
FancyString.replaceAllOccurrences = function(string, toBeReplaced, replaceWith) { | |
return string.split(toBeReplaced).join(replaceWith); | |
}; | |
FancyString.encode = function(string) { | |
let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" }; | |
for (let key in replacementObject) { | |
string = FancyString.replaceAllOccurrences(string, key, replacementObject[key]); | |
} | |
return string; | |
}; | |
FancyString.palindrome = function(str) { | |
return `${str} ${MessageMixer.reverseWord(str)}`; | |
} | |
FancyString.pigLatin = function(sentence, character) { | |
return sentence.split(' ').join(character + ' '); | |
}; | |
export default FancyString; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment