All files / ua encode.js

100% Statements 57/57
93.02% Branches 40/43
100% Functions 2/2
100% Lines 57/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109  55x 55x   55x 55x 55x 55x   729x 729x 729x 729x 729x 131x   598x   4x 4x     1x 1x     7x 7x     1x 1x     5x 5x     1x 1x     142x 142x     1x 1x     1x 1x     1x 1x     6x 6x     177x 177x     38x 38x     25x 25x     60x 18x 18x   42x 42x 42x 54x       54x     42x       128x 558x 558x 558x 558x         558x         580x    
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Bucket_1 = __importDefault(require("./Bucket"));
const guards_1 = require("./guards");
exports.encode = (args) => {
    var _a, _b;
    const { instance } = args;
    const type = (_a = args.type, (_a !== null && _a !== void 0 ? _a : typeof instance));
    const subtype = (_b = args.subtype, (_b !== null && _b !== void 0 ? _b : ''));
    const bucket = new Bucket_1.default();
    if (guards_1.isEncoder(instance)) {
        return instance.encode();
    }
    switch (type) {
        case 'boolean': {
            bucket.writeBoolean(instance);
            break;
        }
        case 'int8': {
            bucket.writeInt8(instance);
            break;
        }
        case 'uint8': {
            bucket.writeUint8(instance);
            break;
        }
        case 'int16': {
            bucket.writeInt16(instance);
            break;
        }
        case 'uint16': {
            bucket.writeUint16(instance);
            break;
        }
        case 'int32': {
            bucket.writeInt32(instance);
            break;
        }
        case 'uint32': {
            bucket.writeUint32(instance);
            break;
        }
        case 'int64': {
            bucket.writeInt64(BigInt(instance));
            break;
        }
        case 'uint64': {
            bucket.writeUint64(BigInt(instance));
            break;
        }
        case 'float32': {
            bucket.writeFloat32(instance);
            break;
        }
        case 'float64': {
            bucket.writeFloat64(instance);
            break;
        }
        case 'string': {
            bucket.writeString(instance);
            break;
        }
        case 'Date': {
            bucket.writeDate(instance);
            break;
        }
        case 'ByteString': {
            bucket.writeByteString(instance);
            break;
        }
        case 'Array': {
            if (instance === null) {
                bucket.writeInt32(-1);
                return bucket.bytes;
            }
            Eif (guards_1.isTypedArray(instance) || Array.isArray(instance)) {
                bucket.writeUint32(instance.length);
                for (const item of instance) {
                    const b = exports.encode({
                        instance: item,
                        type: subtype
                    });
                    bucket.write(b);
                }
            }
            break;
        }
        // all complex objects, structs and classes
        default: {
            for (const name of Object.getOwnPropertyNames(instance)) {
                Eif (guards_1.isNotNullObject(instance) && guards_1.keyInObject(instance, name)) {
                    const type = Reflect.getMetadata('design:type', instance, name);
                    const subtype = Reflect.getMetadata('design:subtype', instance, name);
                    const b = exports.encode({
                        instance: instance[name],
                        type,
                        subtype
                    });
                    bucket.write(b);
                }
            }
        }
    }
    return bucket.bytes;
};