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.

80 lines
1.9KB

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