parse-path.ts 889 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { each, isArray, isString } from 'lodash-es';
  2. const regexTags = /[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/gi;
  3. const regexDot = /[^\s\,]+/gi;
  4. function parsePath(p: string): string[] {
  5. let path = p || ([] as string | string[]);
  6. if (isArray(path)) {
  7. return path;
  8. }
  9. if (isString(path)) {
  10. path = path.match(regexTags);
  11. each(path, (item, index) => {
  12. // @ts-ignore
  13. item = item.match(regexDot);
  14. if (item[0].length > 1) {
  15. const tag = item[0].charAt(0);
  16. // @ts-ignore
  17. item.splice(1, 0, item[0].substr(1));
  18. // @ts-ignore
  19. item[0] = tag;
  20. }
  21. // @ts-ignore
  22. each(item, function (sub, i) {
  23. // @ts-ignore
  24. if (!isNaN(sub)) {
  25. // @ts-ignore
  26. item[i] = +sub;
  27. }
  28. });
  29. // @ts-ignore
  30. path[index] = item;
  31. });
  32. return path;
  33. }
  34. }
  35. export default parsePath;