Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

parse-command.js 1.0KB

1234567891011121314151617181920212223242526272829303132
  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. }