beautifyCode.ts 502 B

12345678910111213141516
  1. /**
  2. * Adds some spaces between some language constructions
  3. *
  4. * @param {string} code
  5. * @param {" " | " "} character
  6. * @returns {string}
  7. */
  8. export function beautifyCode (code: string, character: 'space' | 'tab'): string {
  9. const spaceCharacter: string = character === 'space' ? '\x20' : '\x09';
  10. return code
  11. .replace(/function\(\){/g, 'function () {')
  12. .replace(/(!?=+)/g, ' $1 ')
  13. .replace(/,/g, `,${spaceCharacter}`)
  14. .replace(/;/g, `;\n${spaceCharacter}`);
  15. }