Advertisement
kopyl

Untitled

Oct 20th, 2021 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const isSpeacialCharAndNotNumber = (inputString) => {
  3.     return inputString
  4.            .match(/^[^a-zA-Z]+$/)
  5.            ? true : false
  6. }
  7.  
  8.  
  9. const enumerate = (array) => {
  10.     return array.map((item, index) => [index, item])
  11. }
  12.  
  13.  
  14. const insertAfterFirsItemOfArray = (array, value) => {
  15.     /*
  16.     Input: insertAfterFirsItemOfArray([1, 2, 3, 4, 5], "wow")
  17.     Output: [1, "wow", 2, 3, 4, 5]
  18.     */
  19.     const newArray = [
  20.         array[0], value, ...array.slice(1)
  21.     ]
  22.     return newArray
  23. }
  24.  
  25.  
  26. const fakeTranslateReplacementForWord = (charObject) => {
  27.     if (charObject.isSpecialOrNumber) {
  28.         return charObject.char
  29.     } else {
  30.         return charObject.fakeTranslateReplacement
  31.     }
  32. }
  33.  
  34. const defineCharToAdd = (wordObject) => {
  35.     if (wordObject.isAllUpper) return "P"
  36.     return "v"
  37. }
  38.  
  39.  
  40. const fakeTranslateReplacementForStr = (wordObject) => {
  41.     let word
  42.  
  43.     if (wordObject.wholeWordIsSpecialCharOrNumber) {
  44.         word = wordObject.fakeTranslateReplacement
  45.         word = word.join("")
  46.         return word
  47.     }
  48.  
  49.     const fakeTranslateReplacement = wordObject.fakeTranslateReplacement
  50.  
  51.     let amountCharsToAdd = 12 - wordObject.length.nonSpecial
  52.     if (amountCharsToAdd < 0) amountCharsToAdd = 0
  53.    
  54.     const charToAdd = defineCharToAdd(wordObject)
  55.     const charsToAdd = charToAdd.repeat(amountCharsToAdd)
  56.  
  57.     word = insertAfterFirsItemOfArray(fakeTranslateReplacement, charsToAdd)
  58.     word = word.join("")
  59.     return word
  60. }
  61.  
  62.  
  63. const convertStrToStrObject = (inputString) => {
  64.  
  65.  
  66.     /*
  67.  
  68.     inputString: string
  69.  
  70.     interface charObject {
  71.         char: string,
  72.         fakeTranslateReplacement: string,
  73.         isSpecialOrNumber: boolean,
  74.         isLast: boolean,
  75.         isUpper: boolean,
  76.         charOrder: integer,
  77.     }
  78.  
  79.     inteface Length {
  80.         total: number,
  81.         nonSpecial: number,
  82.         special: number
  83.     }
  84.    
  85.     interface wordObject {
  86.         word: string,
  87.         fakeTranslateReplacement: Array<string>,
  88.         specialCharInWord: boolean,
  89.         isLast: boolean,
  90.         isAllUpper: boolean,
  91.         wholeWordIsSpecialCharOrNumber: boolean,
  92.         length: Length
  93.         chars: Array<charObject>
  94.     }
  95.  
  96.     interface StrObject {
  97.         string: string
  98.         fakeTranslateReplacement: string
  99.         specialCharInStr: boolean,
  100.         wordObjects: Array<wordObject>
  101.     }
  102.  
  103.     Example:
  104.         Input: convertStrToStrObject("dw m.e.")
  105.         Output: Object: StrObject
  106.  
  107.     */
  108.  
  109.  
  110.     const strObject = {}
  111.     strObject.string = inputString
  112.     strObject.fakeTranslateReplacement = ""
  113.  
  114.     const words = inputString.split(" ")
  115.     const wordObjects = []
  116.  
  117.  
  118.     for (let [wordOrder, word] of enumerate(words)) {
  119.         const wordObject = {}
  120.  
  121.         wordObject.word = word
  122.         wordObject.fakeTranslateReplacement = []
  123.         wordObject.specialCharInWord = false
  124.         wordObject.isLast = wordOrder == words.length-1
  125.         wordObject.isAllUpper = false
  126.         wordObject.wholeWordIsSpecialCharOrNumber = false
  127.  
  128.         wordObject.length = {}
  129.         wordObject.length.total = word.length
  130.         wordObject.length.nonSpecial = 0
  131.         wordObject.length.special = 0
  132.  
  133.         wordObject.chars = []
  134.        
  135.         word = Array.from(word)
  136.         for (const [charOrder, char] of enumerate(word)) {
  137.             const speacialChar = isSpeacialCharAndNotNumber(char)
  138.             wordObject.length.nonSpecial += ( () => !speacialChar ? 1 : 0 )()
  139.             wordObject.length.special += ( () => speacialChar ? 1 : 0 )()
  140.             wordObject.specialCharInWord = speacialChar
  141.             strObject.specialCharInStr = speacialChar
  142.  
  143.             const charObject = {}
  144.             charObject.char = char
  145.             charObject.fakeTranslateReplacement = ""
  146.             charObject.isSpecialOrNumber = speacialChar
  147.             charObject.isLast = charOrder == word.length-1
  148.             charObject.isUpper = char == char.toUpperCase()
  149.             charObject.fakeTranslateReplacement = charObject.isUpper ? "P" : "v"
  150.             charObject.charOrder = charOrder
  151.  
  152.             wordObject.chars.push(charObject)
  153.         }
  154.  
  155.         wordObject.fakeTranslateReplacement = wordObject.chars.map(
  156.             charObject => (fakeTranslateReplacementForWord(charObject))
  157.         )
  158.  
  159.         wordObject.isAllUpper = (
  160.             wordObject.chars
  161.             .every(char => char.isUpper)
  162.         )
  163.  
  164.         wordObject.wholeWordIsSpecialCharOrNumber = (
  165.             wordObject.chars
  166.             .every(char => char.isSpecialOrNumber)
  167.         )
  168.  
  169.         wordObjects.push(wordObject)
  170.     }
  171.  
  172.     strObject.wordObjects = wordObjects
  173.  
  174.     let strFakeTranslateReplacement = strObject.wordObjects.map(
  175.         word => ( fakeTranslateReplacementForStr(word) )
  176.     )
  177.     strFakeTranslateReplacement = strFakeTranslateReplacement.join(" ")
  178.     strObject.fakeTranslateReplacement = strFakeTranslateReplacement
  179.  
  180.     return strObject
  181. }
  182.  
  183.  
  184. let inputString = "Luck ev!eryone!"
  185. inputString = "BET"
  186.  
  187.  
  188. const StrObject = convertStrToStrObject(inputString) //?
  189. StrObject.fakeTranslateReplacement  //?
  190.  
  191.  
  192.  
  193. /*  TESTS START  */
  194. /*
  195. StrObject.fakeTranslateReplacement.split(" ")
  196. .map(e => Array.from(e).filter(e => !isSpeacialCharAndNotNumber(e)))
  197. .map(e => e.length)  //?
  198.  
  199.  
  200. for (const word of StrObject.fakeTranslateReplacement.split(" ") ) {
  201.     let amountOfLetters = 0
  202.     const letters = []
  203.     for (const [number, char] of enumerate(Array.from(word))) {  
  204.         if (!isSpeacialCharAndNotNumber(char)) {
  205.             amountOfLetters++
  206.             letters.push({order: amountOfLetters, lettr: char})
  207.  
  208.         }
  209.     }
  210.     console.log(letters.slice(-1))
  211. }
  212. */
  213. /*  TESTS END  */
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement