UNPKG

@geckos.io/typed-array-buffer-schema

Version:

A Schema based Object to Buffer converter

65 lines 2.32 kB
"use strict"; //var property Object.defineProperty(exports, "__esModule", { value: true }); exports.Schema = void 0; class Schema { constructor(_id, _name, _struct) { this._id = _id; this._name = _name; this._struct = _struct; this._bytes = 0; Schema.Validation(_struct); this.calcBytes(); } static Validation(struct) { // do all the validation here (as static me) } get id() { return this._id; } get name() { return this._name; } isSpecialType(prop) { let propKeys = Object.keys(prop).filter(k => k != 'type' && k != 'digits' && k != 'length'); return !propKeys.length; } calcBytes() { const iterate = (obj) => { var _a, _b; for (let property in obj) { const type = (obj === null || obj === void 0 ? void 0 : obj._type) || (this.isSpecialType(obj) ? (_a = obj === null || obj === void 0 ? void 0 : obj.type) === null || _a === void 0 ? void 0 : _a._type : false); const bytes = (obj === null || obj === void 0 ? void 0 : obj._bytes) || (this.isSpecialType(obj) ? (_b = obj === null || obj === void 0 ? void 0 : obj.type) === null || _b === void 0 ? void 0 : _b._bytes : false); if (!type && obj.hasOwnProperty(property)) { if (typeof obj[property] === 'object') { iterate(obj[property]); } } //--- else { if (property !== '_type' && property !== 'type') continue; if (!bytes) continue; // we multiply the bytes by the String8 / String16 length. if (type === 'String8' || type === 'String16') { const length = obj.length || 12; this._bytes += bytes * length; } else { this._bytes += bytes; } } } }; iterate(this._struct); } get struct() { return this._struct; } get bytes() { return this._bytes; } } exports.Schema = Schema; //# sourceMappingURL=schema.js.map