diff --git a/src/protocol.js b/src/protocol.js index 0dd1a35..b30ae1c 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -1,27 +1,12 @@ var { getFieldInfo } = require('./utils'); var reduce = require('lodash.reduce'); -function NMProtocols() { - this.types = {}; -} - function isFieldInfo(type) { return typeof type === "string" || (Array.isArray(type) && typeof type[0] === "string") || type.type; } -NMProtocols.prototype.addType = function(name, functions) { - if (functions === "native") - return; - else if (isFieldInfo(functions)) { - var fieldInfo = getFieldInfo(functions); - this.types[name] = extendType(this.types[fieldInfo.type], fieldInfo.typeArgs); - } - else - this.types[name] = functions; -}; - function findArgs(acc, v, k) { if (typeof v === "string" && v.charAt(0) === '$') acc.push({ "path": k, "val": v.substr(1) }); @@ -30,6 +15,7 @@ function findArgs(acc, v, k) { return acc; } + function setField(path, val, into) { var c = path.split('.').reverse(); while (c.length > 1) { @@ -64,53 +50,69 @@ function extendType(functions, defaultTypeArgs) { }]; } -NMProtocols.prototype.addTypes = function(types) { - var self = this; - Object.keys(types).forEach(function(name) { - self.addType(name, types[name]); - }); -}; - -NMProtocols.prototype.read = function(buffer, cursor, _fieldInfo, rootNodes) { - let fieldInfo = getFieldInfo(_fieldInfo); - var type = this.types[fieldInfo.type]; - if(!type) { - return { - error: new Error("missing data type: " + fieldInfo.type) - }; +class NMProtocols +{ + constructor() { + this.types = {}; } - var readResults = type[0].call(this, buffer, cursor, fieldInfo.typeArgs, rootNodes); - if(readResults == null) { - throw new Error("Reader returned null : " + JSON.stringify(fieldInfo)); - } - if(readResults && readResults.error) return {error: readResults.error}; - return readResults; -}; -NMProtocols.prototype.write = function(value, buffer, offset, _fieldInfo, rootNode) { - let fieldInfo = getFieldInfo(_fieldInfo); - var type = this.types[fieldInfo.type]; - if(!type) { - return { - error: new Error("missing data type: " + fieldInfo.type) - }; + addType(name, functions) { + if (functions === "native") + return; + else if (isFieldInfo(functions)) { + var fieldInfo = getFieldInfo(functions); + this.types[name] = extendType(this.types[fieldInfo.type], fieldInfo.typeArgs); + } + else + this.types[name] = functions; } - return type[1].call(this, value, buffer, offset, fieldInfo.typeArgs, rootNode); -}; -NMProtocols.prototype.sizeOf = function(value, _fieldInfo, rootNode) { - let fieldInfo = getFieldInfo(_fieldInfo); - var type = this.types[fieldInfo.type]; - if(!type) { - throw new Error("missing data type: " + fieldInfo.type); + addTypes(types) { + var self = this; + Object.keys(types).forEach(function(name) { + self.addType(name, types[name]); + }); } - if(typeof type[2] === 'function') { - return type[2].call(this, value, fieldInfo.typeArgs, rootNode); - } else { - return type[2]; + + read(buffer, cursor, _fieldInfo, rootNodes) { + let fieldInfo = getFieldInfo(_fieldInfo); + var type = this.types[fieldInfo.type]; + if(!type) { + return { + error: new Error("missing data type: " + fieldInfo.type) + }; + } + var readResults = type[0].call(this, buffer, cursor, fieldInfo.typeArgs, rootNodes); + if(readResults == null) { + throw new Error("Reader returned null : " + JSON.stringify(fieldInfo)); + } + if(readResults && readResults.error) return {error: readResults.error}; + return readResults; } -}; + write(value, buffer, offset, _fieldInfo, rootNode) { + let fieldInfo = getFieldInfo(_fieldInfo); + var type = this.types[fieldInfo.type]; + if(!type) { + return { + error: new Error("missing data type: " + fieldInfo.type) + }; + } + return type[1].call(this, value, buffer, offset, fieldInfo.typeArgs, rootNode); + } + sizeOf(value, _fieldInfo, rootNode) { + let fieldInfo = getFieldInfo(_fieldInfo); + var type = this.types[fieldInfo.type]; + if(!type) { + throw new Error("missing data type: " + fieldInfo.type); + } + if(typeof type[2] === 'function') { + return type[2].call(this, value, fieldInfo.typeArgs, rootNode); + } else { + return type[2]; + } + } +} module.exports = NMProtocols;