diff --git a/frontend/src/js/misc/wb-id-tools.js b/frontend/src/js/misc/wb-id-tools.js new file mode 100644 index 0000000..2ac4752 --- /dev/null +++ b/frontend/src/js/misc/wb-id-tools.js @@ -0,0 +1,44 @@ +import arvadosTypeName from 'arvados-type-name'; + +const UUID_REGEX = /[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}/g; +const PDH_REGEX = /[a-f0-9]{32}\+[0-9]+/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 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;