Guest User

Untitled

a guest
Oct 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. // {
  2. // id
  3. // created
  4. // employee
  5. // - id
  6. // - firstname
  7. // - employeeType
  8. // - id
  9. // - lastname
  10. // location
  11. // }
  12.  
  13. const addIndent = number => [...Array(number).keys()].reduce(prev => `${prev}-`, "");
  14.  
  15. const fn = str => {
  16. let indent = -1;
  17. let output = "";
  18.  
  19. str.split("").filter(char => char !== " ").map(char => {
  20. if (char === "(") indent++;
  21. if (char === ")") {
  22. indent--;
  23. return;
  24. };
  25.  
  26. output += ["(", ","].includes(char) ? `\n${addIndent(indent)}` : char;
  27. });
  28.  
  29. console.log(output);
  30. };
  31.  
  32. fn("(id,created,employee(id,firstname,employeeType(id), lastname),location)");
Add Comment
Please, Sign In to add comment