IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
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.

73 lines
1.7KB

  1. import arvadosTypeName from 'arvados-type-name';
  2. const UUID_PATTERN = '[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}';
  3. const PDH_PATTERN = '[a-f0-9]{32}\\+[0-9]+';
  4. const UUID_REGEX = new RegExp(UUID_PATTERN);
  5. const PDH_REGEX = new RegExp(PDH_PATTERN);
  6. const UUID_REGEX_G = new RegExp(UUID_PATTERN, 'g');
  7. const PDH_REGEX_G = new RegExp(PDH_PATTERN, 'g');
  8. class WBIdTools {
  9. static isIdentifier(value) {
  10. return ( this.isUuid(value) || this.isPDH(value) );
  11. }
  12. static isUuid(value) {
  13. const m = UUID_REGEX.exec(value);
  14. return (m && m[0] === value);
  15. }
  16. static isPDH(value) {
  17. const m = PDH_REGEX.exec(value);
  18. return (m && m[0] === value);
  19. }
  20. static startsWithIdentifier(value) {
  21. return ( this.startsWithUuid(value) || this.startsWithPDH(value) );
  22. }
  23. static startsWithUuid(value) {
  24. const m = UUID_REGEX.exec(value);
  25. return ( m && m.index === 0 );
  26. }
  27. static startsWithPDH(value) {
  28. const m = PDH_REGEX.exec(value);
  29. return ( m && m.index === 0 );
  30. }
  31. static detectIdentifiers(value) {
  32. return this.detectUuids(value).concat(this.detectPDHs(value));
  33. }
  34. static detectUuids(value) {
  35. let m;
  36. const res = [];
  37. while (m = UUID_REGEX_G.exec(value)) {
  38. res.push(m);
  39. }
  40. return res;
  41. }
  42. static detectPDHs(value) {
  43. let m;
  44. const res = [];
  45. while (m = PDH_REGEX_G.exec(value)) {
  46. res.push(m);
  47. }
  48. return res;
  49. }
  50. static typeName(value) {
  51. if (this.isPDH(value))
  52. return 'collection';
  53. if (this.isUuid(value))
  54. return arvadosTypeName(value);
  55. throw Error('Given value is neither an UUID nor a PDH: ' + value);
  56. }
  57. }
  58. export default WBIdTools;