|
- //
- // Copyright (C) Stanislaw Adaszewski, 2020
- // Contact: s.adaszewski@gmail.com
- // Website: https://adared.ch/wba
- // License: GNU Affero General Public License, Version 3
- //
-
- import arvadosTypeName from 'arvados-type-name';
-
- const UUID_PATTERN = '[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}';
- const PDH_PATTERN = '[a-f0-9]{32}\\+[0-9]+';
-
- const UUID_REGEX = new RegExp(UUID_PATTERN);
- const PDH_REGEX = new RegExp(PDH_PATTERN);
-
- const UUID_REGEX_G = new RegExp(UUID_PATTERN, 'g');
- const PDH_REGEX_G = new RegExp(PDH_PATTERN, 'g');
-
- class WBIdTools {
- static isIdentifier(value) {
- return ( this.isUuid(value) || this.isPDH(value) );
- }
-
- static isUuid(value) {
- const m = UUID_REGEX.exec(value);
- return (m && m[0] === value);
- }
-
- static isPDH(value) {
- const m = PDH_REGEX.exec(value);
- return (m && m[0] === value);
- }
-
- static startsWithIdentifier(value) {
- return ( this.startsWithUuid(value) || this.startsWithPDH(value) );
- }
-
- static startsWithUuid(value) {
- const m = UUID_REGEX.exec(value);
- return ( m && m.index === 0 );
- }
-
- static startsWithPDH(value) {
- const m = PDH_REGEX.exec(value);
- return ( m && m.index === 0 );
- }
-
- static detectIdentifiers(value) {
- return this.detectUuids(value).concat(this.detectPDHs(value));
- }
-
- static detectUuids(value) {
- let m;
- const res = [];
- while (m = UUID_REGEX_G.exec(value)) {
- res.push(m);
- }
- return res;
- }
-
- static detectPDHs(value) {
- let m;
- const res = [];
- while (m = PDH_REGEX_G.exec(value)) {
- res.push(m);
- }
- return res;
- }
-
- static typeName(value) {
- if (this.isPDH(value))
- return 'collection';
- if (this.isUuid(value))
- return arvadosTypeName(value);
- throw Error('Given value is neither an UUID nor a PDH: ' + value);
- }
- }
-
- export default WBIdTools;
|