UNPKG

diffusion

Version:

Diffusion JavaScript client

106 lines (105 loc) 3.54 kB
"use strict"; /** * @module diffusion.datatypes */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BytesImpl = void 0; var buffer_slice_1 = require("./../data/buffer-slice"); /** * Implementation of the {@link Bytes} interface */ var BytesImpl = /** @class */ (function (_super) { __extends(BytesImpl, _super); /** * Create a new BytesImpl instance * * @param buffer the internal buffer * @param offset the offset of the slice * @param length the number of bytes in the slice */ function BytesImpl(buffer, offset, length) { if (offset === void 0) { offset = 0; } if (length === void 0) { length = buffer.length; } return _super.call(this, buffer, offset, length) || this; } BytesImpl.toString = function () { return 'BytesImpl'; }; /** * @inheritdoc */ BytesImpl.prototype.length = function () { return this.$length; }; /** * @inheritdoc * * @deprecated since version 6.11 */ BytesImpl.prototype.asBuffer = function () { return Buffer.from(this.asArray()); }; /** * @inheritdoc */ BytesImpl.prototype.asArray = function () { return this.$buffer.subarray(this.$offset, this.$offset + this.$length); }; /** * @inheritdoc */ BytesImpl.prototype.copyTo = function (target, offset) { if (offset === void 0) { offset = 0; } target.set(this.$buffer.subarray(this.$offset, this.$offset + this.$length), offset); }; /** * Check if the Bytes are equal to a buffer slices * * @param bytes the buffer slice * @return `true` if the two bytes are equal */ BytesImpl.prototype.equals = function (bytes) { return this.equalBytes(bytes.$buffer, bytes.$offset, bytes.$length); }; /** * Check if the Bytes are equal to a buffer slices * * @param buffer the buffer * @param offset the offset of the slice * @param length the number of bytes in the slice * @return `true` if the two bytes are equal */ BytesImpl.prototype.equalBytes = function (buffer, offset, length) { if (offset === void 0) { offset = 0; } if (length === void 0) { length = buffer.length; } if (this.$length !== length) { return false; } if (this.$buffer === buffer && this.$offset === offset) { return true; } for (var i = 0; i < length; ++i) { if (this.$buffer[this.$offset + i] !== buffer[offset + i]) { return false; } } return true; }; return BytesImpl; }(buffer_slice_1.BufferSlice)); exports.BytesImpl = BytesImpl;