UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

86 lines 2.73 kB
var UUID_1; import { __decorate, __metadata } from "tslib"; import { SerializableMember, SerializableObject } from './decorators'; import { v4 as uuidv4 } from 'uuid'; import { BufferUtils } from '../utils/BufferUtils'; const UUID_PADDING = '-0000-1000-8000-00805f9b34fb'; /** * Unique identifier */ let UUID = UUID_1 = class UUID { constructor(buffer) { this._raw = buffer; } static generate() { const uuidString = uuidv4(); return UUID_1.fromString(uuidString); } static fromBuffer(buffer, littleEndian) { if (littleEndian) { let swappedBuffer = new Uint8Array(); for (let i = buffer.length - 1; i >= 0; i--) { swappedBuffer = BufferUtils.concatBuffer(swappedBuffer, new Uint8Array([buffer[i]])); } return new this(swappedBuffer); } else { return new this(buffer); } } static fromString(uuid) { const hexArray = uuid.replace(UUID_PADDING, '').replace(/-/g, '').split(/(..)/).filter(a => { return a !== ''; }).map(hex => { return Number(`0x${hex}`); }); if (hexArray.includes(NaN)) { return undefined; } let array = Uint8Array.from(hexArray); if (uuid.startsWith('0000')) { array = array.slice(2); } return new this(array); } toBuffer() { return this._raw; } to128bit() { return UUID_1.fromString(this.toString(8)); } toString(byteLength, padding = true) { byteLength = byteLength !== null && byteLength !== void 0 ? byteLength : this._raw.byteLength; const bytes = []; for (const [, value] of this._raw.entries()) { bytes.push(value); } const PADDING = padding ? UUID_PADDING : ''; if (byteLength === 2) { // 16 bit return '0000' + bytes.map(byte => { return byte.toString(16).padStart(2, '0'); }).join('') + PADDING; } else if (byteLength === 4) { // 32 bit return bytes.map(byte => { return byte.toString(16).padStart(2, '0'); }).join('') + PADDING; } else if (byteLength === 16) { // 128 bit const hex = bytes.map(byte => { return byte.toString(16).padStart(2, '0'); }); return hex.splice(0, 4).join('') + '-' + hex.splice(0, 2).join('') + '-' + hex.splice(0, 2).join('') + '-' + hex.splice(0, 2).join('') + '-' + hex.join(''); } else { // Strange const hex = bytes.map(byte => { return byte.toString(16).padStart(2, '0'); }); return hex.join(''); } } }; __decorate([SerializableMember({ name: 'value' }), __metadata("design:type", Uint8Array)], UUID.prototype, "_raw", void 0); UUID = UUID_1 = __decorate([SerializableObject(), __metadata("design:paramtypes", [Uint8Array])], UUID); export { UUID };