urn-uuid.ts (1150B)
1 import { URISchemeHandler, URIComponents, URIOptions } from "../uri"; 2 import { URNComponents } from "./urn"; 3 import { SCHEMES } from "../uri"; 4 5 export interface UUIDComponents extends URNComponents { 6 uuid?: string; 7 } 8 9 const UUID = /^[0-9A-Fa-f]{8}(?:\-[0-9A-Fa-f]{4}){3}\-[0-9A-Fa-f]{12}$/; 10 const UUID_PARSE = /^[0-9A-Fa-f\-]{36}/; 11 12 //RFC 4122 13 const handler:URISchemeHandler<UUIDComponents, URIOptions, URNComponents> = { 14 scheme : "urn:uuid", 15 16 parse : function (urnComponents:URNComponents, options:URIOptions):UUIDComponents { 17 const uuidComponents = urnComponents as UUIDComponents; 18 uuidComponents.uuid = uuidComponents.nss; 19 uuidComponents.nss = undefined; 20 21 if (!options.tolerant && (!uuidComponents.uuid || !uuidComponents.uuid.match(UUID))) { 22 uuidComponents.error = uuidComponents.error || "UUID is not valid."; 23 } 24 25 return uuidComponents; 26 }, 27 28 serialize : function (uuidComponents:UUIDComponents, options:URIOptions):URNComponents { 29 const urnComponents = uuidComponents as URNComponents; 30 //normalize UUID 31 urnComponents.nss = (uuidComponents.uuid || "").toLowerCase(); 32 return urnComponents; 33 }, 34 }; 35 36 export default handler;