Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular js - Format String in Modal with correct spaces

I have a popup which has the following output! The output is just one complete string with spaces and newline characters. But each line is concatenated to the previous line. So, each line can be adjusted individually.

Test1                        :  Success :    200
Test2              :  Success :    200
Test3                  :  Success :    200
Test4                :  Success :    200
Test5                  :  Success  :    404
Test6           :  Success  :    401

Since I have multiple such popups and multiple tests for each popup. Is there a way I can format the strings to have a proper indents? That is I would like my output to be :

Test1               :  Success :    200
Test2               :  Success :    200
Test3               :  Success :    200
Test4               :  Success :    200
Test5               :  Success :    404
Test6               :  Success :    401
like image 332
Dreams Avatar asked May 16 '17 06:05

Dreams


1 Answers

Here's what I would do:

First, split your string with \n to get each line in array. Next, split again with : and trim to remove variable spaces.

Finally, join them again but with first element appended with extra space which would be same for each one of them.

let str = "Test1                        :  Success :    200\nTest2              :  Success :    200\nTest3                  :  Success :    200\nTest4                :  Success :    200\nTest5                  :  Success  :    404\nTest6           :  Success  :    401"


let arr = str.split("\n")

let res = arr.map(function(st) {
  let temp = st.split(":")
  return temp.map(s => s.trim())
})

let final = res.map(function(a) {
  a[0] = a[0] + "            "
  return a.join(" : ")
})

console.log(final)
like image 59
tanmay Avatar answered Oct 21 '22 04:10

tanmay