GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.0 KiB

2 months ago
  1. export function parseCommand(cmd) {
  2. const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
  3. const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
  4. const bregex = /\.*[\][<>]/g;
  5. const firstCommand = splitCommand.shift();
  6. if (!firstCommand)
  7. throw new Error(`No command found in: ${cmd}`);
  8. const parsedCommand = {
  9. cmd: firstCommand.replace(bregex, ''),
  10. demanded: [],
  11. optional: [],
  12. };
  13. splitCommand.forEach((cmd, i) => {
  14. let variadic = false;
  15. cmd = cmd.replace(/\s/g, '');
  16. if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1)
  17. variadic = true;
  18. if (/^\[/.test(cmd)) {
  19. parsedCommand.optional.push({
  20. cmd: cmd.replace(bregex, '').split('|'),
  21. variadic,
  22. });
  23. }
  24. else {
  25. parsedCommand.demanded.push({
  26. cmd: cmd.replace(bregex, '').split('|'),
  27. variadic,
  28. });
  29. }
  30. });
  31. return parsedCommand;
  32. }