UNPKG

hadron-document

Version:
161 lines 6.11 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const hadron_type_checker_1 = __importStar(require("hadron-type-checker")); const element_events_1 = require("../element-events"); const standard_1 = __importDefault(require("./standard")); /** * Converts a Binary UUID (subtype 4) to a UUID string with hyphens. */ const binaryToUUIDString = (binary) => { try { return binary.toUUID().toString(); } catch { // Fallback to hex if toUUID fails return binary.toString('hex'); } }; /** * Converts a Binary Legacy Java UUID (subtype 3) to a UUID string with hyphens. * Java legacy format reverses byte order for both MSB and LSB. */ const binaryToLegacyJavaUUIDString = (binary) => { const hex = binary.toString('hex'); const reversedHex = (0, hadron_type_checker_1.reverseJavaUUIDBytes)(hex); return (0, hadron_type_checker_1.uuidHexToString)(reversedHex); }; /** * Converts a Binary Legacy C# UUID (subtype 3) to a UUID string with hyphens. * C# legacy format reverses byte order for first 3 groups only. */ const binaryToLegacyCSharpUUIDString = (binary) => { const hex = binary.toString('hex'); const reversedHex = (0, hadron_type_checker_1.reverseCSharpUUIDBytes)(hex); return (0, hadron_type_checker_1.uuidHexToString)(reversedHex); }; /** * Converts a Binary Legacy Python UUID (subtype 3) to a UUID string with hyphens. * Python legacy format uses direct byte order (no reversal). */ const binaryToLegacyPythonUUIDString = (binary) => { const hex = binary.toString('hex'); return (0, hadron_type_checker_1.uuidHexToString)(hex); }; /** * CRUD editor for UUID values (Binary subtypes 3 and 4). */ class UUIDEditor extends standard_1.default { uuidType; /** * Create the UUID editor. * * @param element - The hadron document element. */ constructor(element, displayType) { super(element, displayType); this.uuidType = (0, hadron_type_checker_1.isUUIDType)(this.displayType) ? this.displayType : 'UUID'; } /** * Get the value being edited as a UUID string. */ value() { const val = this.element.currentValue; // If already a string (during editing), return as is if (typeof val === 'string') { return val; } // If it's a Binary, convert to UUID string based on the type if ((0, hadron_type_checker_1.getBsonType)(val) === 'Binary') { const binary = val; switch (this.uuidType) { case 'LegacyJavaUUID': return binaryToLegacyJavaUUIDString(binary); case 'LegacyCSharpUUID': return binaryToLegacyCSharpUUIDString(binary); case 'LegacyPythonUUID': return binaryToLegacyPythonUUIDString(binary); case 'UUID': default: return binaryToUUIDString(binary); } } return String(val); } /** * Edit the element with the provided value. */ edit(value) { try { hadron_type_checker_1.default.cast(value, this.uuidType); this.element.currentValue = value; this.element.setValid(); this.element._bubbleUp(element_events_1.ElementEvents.Edited, this.element); } catch (e) { this.element.setInvalid(value, this.element.currentType, e.message); } } /** * Start the UUID edit - convert Binary to string for editing. */ start() { super.start(); if (this.element.isCurrentTypeValid()) { // Update the currentType to the UUID type so the UI displays correctly // This is needed because the element may have been created with currentType='Binary' // but we want to edit it as a UUID type this.element.currentType = this.uuidType; this.edit(this.value()); } } /** * Complete the UUID edit by converting the valid string back to Binary. */ complete() { super.complete(); if (this.element.isCurrentTypeValid()) { this.element.edit(hadron_type_checker_1.default.cast(this.element.currentValue, this.uuidType)); // Preserve the UUID type since element.edit() sets currentType to 'Binary' // for Binary values (TypeChecker.type() returns 'Binary' for Binary objects) this.element.currentType = this.uuidType; } } } exports.default = UUIDEditor; //# sourceMappingURL=uuid.js.map