@tbd54566975/dap
Version:
TypeScript SDK for DAPs
59 lines (57 loc) • 1.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/dap.ts
var _Dap = class _Dap {
/**
* Creates a new DAP instance.
* @param handle - The local handle part of the DAP.
* @param domain - The domain part of the DAP.
*/
constructor(handle, domain) {
this.handle = handle;
this.domain = domain;
}
/**
* Converts the DAP instance to its string representation.
* @returns The string representation of the DAP.
*/
toString() {
return `${_Dap.PREFIX}${this.handle}${_Dap.SEPARATOR}${this.domain}`;
}
/**
* Parses a DAP string and creates a new DAP instance.
* @param dap - The DAP string to parse.
* @returns A new DAP instance.
* @throws {InvalidDap} If the provided string is not a valid DAP.
*/
static parse(dap) {
const match = dap.match(_Dap.DAP_REGEX);
if (!match) {
throw new InvalidDap();
}
const [, handle, domain] = match;
return new _Dap(handle, domain);
}
};
/** The prefix character for a DAP. */
__publicField(_Dap, "PREFIX", "@");
/** The separator character between the handle and domain in a DAP. */
__publicField(_Dap, "SEPARATOR", "/");
/** Regular expression for validating and parsing DAP strings. */
__publicField(_Dap, "DAP_REGEX", new RegExp(`^${_Dap.PREFIX}([^${_Dap.PREFIX}${_Dap.SEPARATOR}]+)${_Dap.SEPARATOR}([^${_Dap.PREFIX}${_Dap.SEPARATOR}]+)$`));
var Dap = _Dap;
var InvalidDap = class extends Error {
/**
* Creates a new InvalidDap error.
* @param message - Optional custom error message. Defaults to 'Invalid DAP'.
*/
constructor(message) {
super(message ?? "Invalid DAP");
this.name = "InvalidDap";
}
};
export {
Dap,
InvalidDap
};