Code coverage report for src/in_description.js

Statements: 100% (22 / 22)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (22 / 22)      Ignored: none     

All files » src/ » in_description.js
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 511 1             1 83 83     1   66       50       47       50       12 12 12 5 7 4 3 2   1       11   11       1
var Types = require('./types.js');
var WasabiError = require('./wasabi_error.js');
 
/**
 * A class which packs an object when passed to its .serialize function
 * @class InDescription
 * @constructor
 */
var InDescription = function () {
    this._target = null;
    this._bitStream = null;
};
 
InDescription.prototype = {
    uint: function (name, bits) {
        this._bitStream.writeUInt(this._target[name], bits);
    },
 
    sint: function (name, bits) {
        this._bitStream.writeSInt(this._target[name], bits);
    },
 
    float: function (name, bits) {
        this._bitStream.writeFloat(this._target[name], bits);
    },
 
    string: function (name) {
        this._bitStream.writeString(this._target[name]);
    },
 
    any: function (name, bits) {
        var type;
        var val = this._target[name];
        if (typeof val === 'string') {
            type = 'string';
        } else if (val | 0 === val) {
            type = 'sint';
        } else if (+val === val) {
            type = 'float';
        } else {
            throw new WasabiError('Can not serialize unsupported value ' + val.toString());
        }
 
        // Write the type specifier to the bitstream
        this._bitStream.writeUInt(Types.fromString[type], Types.bitsNeeded);
        // Invoke the appropriate function for the detected type
        this[type](name, bits);
    }
};
 
module.exports = InDescription;