transform protocol.js into an es6 class

This commit is contained in:
Romain Beaumont 2015-09-20 21:32:50 +02:00
parent 4e9d8e06c9
commit 78ff667c1f

View file

@ -1,27 +1,12 @@
var { getFieldInfo } = require('./utils'); var { getFieldInfo } = require('./utils');
var reduce = require('lodash.reduce'); var reduce = require('lodash.reduce');
function NMProtocols() {
this.types = {};
}
function isFieldInfo(type) { function isFieldInfo(type) {
return typeof type === "string" return typeof type === "string"
|| (Array.isArray(type) && typeof type[0] === "string") || (Array.isArray(type) && typeof type[0] === "string")
|| type.type; || 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) { function findArgs(acc, v, k) {
if (typeof v === "string" && v.charAt(0) === '$') if (typeof v === "string" && v.charAt(0) === '$')
acc.push({ "path": k, "val": v.substr(1) }); acc.push({ "path": k, "val": v.substr(1) });
@ -30,6 +15,7 @@ function findArgs(acc, v, k) {
return acc; return acc;
} }
function setField(path, val, into) { function setField(path, val, into) {
var c = path.split('.').reverse(); var c = path.split('.').reverse();
while (c.length > 1) { while (c.length > 1) {
@ -64,14 +50,31 @@ function extendType(functions, defaultTypeArgs) {
}]; }];
} }
NMProtocols.prototype.addTypes = function(types) { class NMProtocols
{
constructor() {
this.types = {};
}
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;
}
addTypes(types) {
var self = this; var self = this;
Object.keys(types).forEach(function(name) { Object.keys(types).forEach(function(name) {
self.addType(name, types[name]); self.addType(name, types[name]);
}); });
}; }
NMProtocols.prototype.read = function(buffer, cursor, _fieldInfo, rootNodes) { read(buffer, cursor, _fieldInfo, rootNodes) {
let fieldInfo = getFieldInfo(_fieldInfo); let fieldInfo = getFieldInfo(_fieldInfo);
var type = this.types[fieldInfo.type]; var type = this.types[fieldInfo.type];
if(!type) { if(!type) {
@ -85,9 +88,9 @@ NMProtocols.prototype.read = function(buffer, cursor, _fieldInfo, rootNodes) {
} }
if(readResults && readResults.error) return {error: readResults.error}; if(readResults && readResults.error) return {error: readResults.error};
return readResults; return readResults;
}; }
NMProtocols.prototype.write = function(value, buffer, offset, _fieldInfo, rootNode) { write(value, buffer, offset, _fieldInfo, rootNode) {
let fieldInfo = getFieldInfo(_fieldInfo); let fieldInfo = getFieldInfo(_fieldInfo);
var type = this.types[fieldInfo.type]; var type = this.types[fieldInfo.type];
if(!type) { if(!type) {
@ -96,9 +99,9 @@ NMProtocols.prototype.write = function(value, buffer, offset, _fieldInfo, rootNo
}; };
} }
return type[1].call(this, value, buffer, offset, fieldInfo.typeArgs, rootNode); return type[1].call(this, value, buffer, offset, fieldInfo.typeArgs, rootNode);
}; }
NMProtocols.prototype.sizeOf = function(value, _fieldInfo, rootNode) { sizeOf(value, _fieldInfo, rootNode) {
let fieldInfo = getFieldInfo(_fieldInfo); let fieldInfo = getFieldInfo(_fieldInfo);
var type = this.types[fieldInfo.type]; var type = this.types[fieldInfo.type];
if(!type) { if(!type) {
@ -109,8 +112,7 @@ NMProtocols.prototype.sizeOf = function(value, _fieldInfo, rootNode) {
} else { } else {
return type[2]; return type[2];
} }
}; }
}
module.exports = NMProtocols; module.exports = NMProtocols;