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,53 +50,69 @@ function extendType(functions, defaultTypeArgs) {
}]; }];
} }
NMProtocols.prototype.addTypes = function(types) { class NMProtocols
var self = this; {
Object.keys(types).forEach(function(name) { constructor() {
self.addType(name, types[name]); this.types = {};
});
};
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)
};
} }
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) { addType(name, functions) {
let fieldInfo = getFieldInfo(_fieldInfo); if (functions === "native")
var type = this.types[fieldInfo.type]; return;
if(!type) { else if (isFieldInfo(functions)) {
return { var fieldInfo = getFieldInfo(functions);
error: new Error("missing data type: " + fieldInfo.type) 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) { addTypes(types) {
let fieldInfo = getFieldInfo(_fieldInfo); var self = this;
var type = this.types[fieldInfo.type]; Object.keys(types).forEach(function(name) {
if(!type) { self.addType(name, types[name]);
throw new Error("missing data type: " + fieldInfo.type); });
} }
if(typeof type[2] === 'function') {
return type[2].call(this, value, fieldInfo.typeArgs, rootNode); read(buffer, cursor, _fieldInfo, rootNodes) {
} else { let fieldInfo = getFieldInfo(_fieldInfo);
return type[2]; 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; module.exports = NMProtocols;