Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const isSpeacialCharAndNotNumber = (inputString) => {
- return inputString
- .match(/^[^a-zA-Z]+$/)
- ? true : false
- }
- const enumerate = (array) => {
- return array.map((item, index) => [index, item])
- }
- const insertAfterFirsItemOfArray = (array, value) => {
- /*
- Input: insertAfterFirsItemOfArray([1, 2, 3, 4, 5], "wow")
- Output: [1, "wow", 2, 3, 4, 5]
- */
- const newArray = [
- array[0], value, ...array.slice(1)
- ]
- return newArray
- }
- const fakeTranslateReplacementForWord = (charObject) => {
- if (charObject.isSpecialOrNumber) {
- return charObject.char
- } else {
- return charObject.fakeTranslateReplacement
- }
- }
- const defineCharToAdd = (wordObject) => {
- if (wordObject.isAllUpper) return "P"
- return "v"
- }
- const fakeTranslateReplacementForStr = (wordObject) => {
- let word
- if (wordObject.wholeWordIsSpecialCharOrNumber) {
- word = wordObject.fakeTranslateReplacement
- word = word.join("")
- return word
- }
- const fakeTranslateReplacement = wordObject.fakeTranslateReplacement
- let amountCharsToAdd = 12 - wordObject.length.nonSpecial
- if (amountCharsToAdd < 0) amountCharsToAdd = 0
- const charToAdd = defineCharToAdd(wordObject)
- const charsToAdd = charToAdd.repeat(amountCharsToAdd)
- word = insertAfterFirsItemOfArray(fakeTranslateReplacement, charsToAdd)
- word = word.join("")
- return word
- }
- const convertStrToStrObject = (inputString) => {
- /*
- inputString: string
- interface charObject {
- char: string,
- fakeTranslateReplacement: string,
- isSpecialOrNumber: boolean,
- isLast: boolean,
- isUpper: boolean,
- charOrder: integer,
- }
- inteface Length {
- total: number,
- nonSpecial: number,
- special: number
- }
- interface wordObject {
- word: string,
- fakeTranslateReplacement: Array<string>,
- specialCharInWord: boolean,
- isLast: boolean,
- isAllUpper: boolean,
- wholeWordIsSpecialCharOrNumber: boolean,
- length: Length
- chars: Array<charObject>
- }
- interface StrObject {
- string: string
- fakeTranslateReplacement: string
- specialCharInStr: boolean,
- wordObjects: Array<wordObject>
- }
- Example:
- Input: convertStrToStrObject("dw m.e.")
- Output: Object: StrObject
- */
- const strObject = {}
- strObject.string = inputString
- strObject.fakeTranslateReplacement = ""
- const words = inputString.split(" ")
- const wordObjects = []
- for (let [wordOrder, word] of enumerate(words)) {
- const wordObject = {}
- wordObject.word = word
- wordObject.fakeTranslateReplacement = []
- wordObject.specialCharInWord = false
- wordObject.isLast = wordOrder == words.length-1
- wordObject.isAllUpper = false
- wordObject.wholeWordIsSpecialCharOrNumber = false
- wordObject.length = {}
- wordObject.length.total = word.length
- wordObject.length.nonSpecial = 0
- wordObject.length.special = 0
- wordObject.chars = []
- word = Array.from(word)
- for (const [charOrder, char] of enumerate(word)) {
- const speacialChar = isSpeacialCharAndNotNumber(char)
- wordObject.length.nonSpecial += ( () => !speacialChar ? 1 : 0 )()
- wordObject.length.special += ( () => speacialChar ? 1 : 0 )()
- wordObject.specialCharInWord = speacialChar
- strObject.specialCharInStr = speacialChar
- const charObject = {}
- charObject.char = char
- charObject.fakeTranslateReplacement = ""
- charObject.isSpecialOrNumber = speacialChar
- charObject.isLast = charOrder == word.length-1
- charObject.isUpper = char == char.toUpperCase()
- charObject.fakeTranslateReplacement = charObject.isUpper ? "P" : "v"
- charObject.charOrder = charOrder
- wordObject.chars.push(charObject)
- }
- wordObject.fakeTranslateReplacement = wordObject.chars.map(
- charObject => (fakeTranslateReplacementForWord(charObject))
- )
- wordObject.isAllUpper = (
- wordObject.chars
- .every(char => char.isUpper)
- )
- wordObject.wholeWordIsSpecialCharOrNumber = (
- wordObject.chars
- .every(char => char.isSpecialOrNumber)
- )
- wordObjects.push(wordObject)
- }
- strObject.wordObjects = wordObjects
- let strFakeTranslateReplacement = strObject.wordObjects.map(
- word => ( fakeTranslateReplacementForStr(word) )
- )
- strFakeTranslateReplacement = strFakeTranslateReplacement.join(" ")
- strObject.fakeTranslateReplacement = strFakeTranslateReplacement
- return strObject
- }
- let inputString = "Luck ev!eryone!"
- inputString = "BET"
- const StrObject = convertStrToStrObject(inputString) //?
- StrObject.fakeTranslateReplacement //?
- /* TESTS START */
- /*
- StrObject.fakeTranslateReplacement.split(" ")
- .map(e => Array.from(e).filter(e => !isSpeacialCharAndNotNumber(e)))
- .map(e => e.length) //?
- for (const word of StrObject.fakeTranslateReplacement.split(" ") ) {
- let amountOfLetters = 0
- const letters = []
- for (const [number, char] of enumerate(Array.from(word))) {
- if (!isSpeacialCharAndNotNumber(char)) {
- amountOfLetters++
- letters.push({order: amountOfLetters, lettr: char})
- }
- }
- console.log(letters.slice(-1))
- }
- */
- /* TESTS END */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement