@openhps/core
Version:
Open Hybrid Positioning System - Core component
119 lines • 3.64 kB
JavaScript
;
var UUID_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UUID = void 0;
const tslib_1 = require("tslib");
const decorators_1 = require("./decorators");
const uuid_1 = require("uuid");
const BufferUtils_1 = require("../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 = (0, uuid_1.v4)();
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_1.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('');
}
}
};
exports.UUID = UUID;
tslib_1.__decorate([
(0, decorators_1.SerializableMember)({
name: 'value',
}),
tslib_1.__metadata("design:type", Uint8Array)
], UUID.prototype, "_raw", void 0);
exports.UUID = UUID = UUID_1 = tslib_1.__decorate([
(0, decorators_1.SerializableObject)(),
tslib_1.__metadata("design:paramtypes", [Uint8Array])
], UUID);
//# sourceMappingURL=UUID.js.map