lib/encoders.js
function encodeEnum(enumDefinition) {
    /**
     * Takes in a string value and using the provided enum definition encodes it as a `sInt` stored in a [ByteBuffer]{@link https://www.npmjs.com/package/bytebuffer} object for use with the protobufjs library.
     * @param value - The value to encode.
     * @throws {Error} If the provided value was not found in the enum definition
     * @return {ByteBuffer|void}
     */
    return function encodeValueBasedOnEnum(value) {
        let buffer = new ByteBuffer();
        if (value === null) {
            return buffer;
        }
        let foundEnumVal;
        Object.keys(enumDefinition).some(function(key) {
            let enumValue = enumDefinition[key].toLowerCase();
            if (enumValue === value.toLowerCase()) {
                foundEnumVal = Number(key);
                return true;
            }
            return false;
        });
        if (!Number.isInteger(foundEnumVal)) {
            throw Error(
                `Invalid enum value ${value}, should have been one of ${JSON.stringify(
                    enumDefinition
                )}`
            );
        }
        return encodeSInt32(foundEnumVal);
    };
}
function encodeEnum(enumDefinition) {
    /**
     * Takes in a string value and using the provided enum definition encodes it as a `sInt` stored in a [ByteBuffer]{@link https://www.npmjs.com/package/bytebuffer} object for use with the protobufjs library.
     * @param value - The value to encode.
     * @throws {Error} If the provided value was not found in the enum definition
     * @return {ByteBuffer|void}
     */
    return function encodeValueBasedOnEnum(value) {
        let buffer = new ByteBuffer();
        if (value === null) {
            return buffer;
        }
        let foundEnumVal;
        Object.keys(enumDefinition).some(function(key) {
            let enumValue = enumDefinition[key].toLowerCase();
            if (enumValue === value.toLowerCase()) {
                foundEnumVal = Number(key);
                return true;
            }
            return false;
        });
        if (!Number.isInteger(foundEnumVal)) {
            throw Error(
                `Invalid enum value ${value}, should have been one of ${JSON.stringify(
                    enumDefinition
                )}`
            );
        }
        return encodeSInt32(foundEnumVal);
    };
}
function encodeEnum ( enumDefinition ) { /**
     * Takes in a string value and using the provided enum definition encodes it as a `sInt` stored in a [ByteBuffer]{@link https://www.npmjs.com/package/bytebuffer} object for use with the protobufjs library.
     * @param value - The value to encode.
     * @throws {Error} If the provided value was not found in the enum definition
     * @return {ByteBuffer|void}
     */ return function encodeValueBasedOnEnum ( value ) { let buffer = new ByteBuffer ( ) ; if ( value === null ) { return buffer ; } let foundEnumVal ; Object . keys ( enumDefinition ) . some ( function ( key ) { let enumValue = enumDefinition [ key ] . toLowerCase ( ) ; if ( enumValue === value . toLowerCase ( ) ) { foundEnumVal = Number ( key ) ; return true ; } return false ; } ) ; if ( ! Number . isInteger ( foundEnumVal ) ) { throw Error ( ` ${ value } ${ JSON . stringify ( enumDefinition ) } ` ) ; } return encodeSInt32 ( foundEnumVal ) ; } ; }
Returns a function that can be used to encode a string as the specific enum value.
@param {object} enumDefinition - The key-value enum object. Keys should be numbers, values should be strings.
@return {Function} The [function](#encodeValueBasedOnEnum) that will do the encoding.
Returns a function that can be used to encode a string as the specific enum value .