From d8e66eb1c77c797633deae261aecf805f8984e69 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Tue, 7 Apr 2020 00:24:57 +0200 Subject: [PATCH] Started implementing WBIdTools. --- frontend/src/js/misc/wb-id-tools.js | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 frontend/src/js/misc/wb-id-tools.js 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;