node-aurora
Version:
Provides an interface to the Aurora Dreamband.
189 lines (129 loc) • 5.43 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _toArray2 = require('babel-runtime/helpers/toArray');
var _toArray3 = _interopRequireDefault(_toArray2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _camelCase = require('lodash/camelCase');
var _camelCase2 = _interopRequireDefault(_camelCase);
var _zipObject = require('lodash/zipObject');
var _zipObject2 = _interopRequireDefault(_zipObject);
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ResponseStates = {
INIT: 0,
OBJECT: 1,
TABLE: 2,
OUTPUT: 3
};
var AuroraCmdResponseParser = function () {
function AuroraCmdResponseParser() {
(0, _classCallCheck3.default)(this, AuroraCmdResponseParser);
this.reset();
}
(0, _createClass3.default)(AuroraCmdResponseParser, [{
key: 'reset',
value: function reset() {
this.response = null;
this.responseState = ResponseStates.INIT;
this.responseTableCols = [];
}
}, {
key: 'parseObject',
value: function parseObject(line) {
if (line.length > 120) throw new Error('Line exceeded max length of 120 bytes.');
line = line.trim();
if (!line.length) return;
if (this.responseState == ResponseStates.INIT) {
this.response = {};
this.responseState = ResponseStates.OBJECT;
}
if (this.responseState != ResponseStates.OBJECT) throw new Error('Invalid response state to parse an object.');
var _line$split = line.split(':'),
_line$split2 = (0, _toArray3.default)(_line$split),
objKey = _line$split2[0],
objValueArray = _line$split2.slice(1);
var objValue = objValueArray.join(':');
this.response[(0, _camelCase2.default)(objKey.trim())] = (0, _util.parseValueString)(objValue);
}
}, {
key: 'parseTable',
value: function parseTable(line) {
if (line.length > 120) throw new Error('Line exceeded max length of 120 bytes.');
line = line.trim();
if (!line.length) return;
if (this._isTableDivider(line)) return;
if (line[0] == '|' && line[line.length - 1] == '|') {
line = line.slice(1, -1);
}
//this must be the columns
if (this.responseState == ResponseStates.INIT) {
this.response = [];
this.responseTableCols = line.split('|').map(function (col) {
return (0, _camelCase2.default)(col.trim());
});
this.responseState = ResponseStates.TABLE;
return;
}
if (this.responseState != ResponseStates.TABLE) throw new Error('Invalid response state to parse a table.');
this.response.push((0, _zipObject2.default)(this.responseTableCols, line.split('|').map(_util.parseValueString)));
}
}, {
key: 'parseDetect',
value: function parseDetect(line) {
if (line.length > 120) throw new Error('Line exceeded max length of 120 bytes.');
line = line.trim();
if (!line.length) return;
//on initial state we detect the response type
if (this.responseState == ResponseStates.INIT) {
var detectedState = this._detectState(line);
if (detectedState == ResponseStates.OBJECT) {
this.parseObject(line);
} else if (detectedState == ResponseStates.TABLE) {
this.parseTable(line);
}
} else if (this.responseState == ResponseStates.OBJECT) {
this.parseObject(line);
} else if (this.responseState == ResponseStates.TABLE) {
this.parseTable(line);
}
}
}, {
key: 'getResponse',
value: function getResponse() {
return this.response;
}
}, {
key: '_getCurrentResponse',
value: function _getCurrentResponse() {
if (this.responseNestedProp) {
return this.response[this.responseNestedProp];
}
return this.response;
}
}, {
key: '_detectState',
value: function _detectState(line) {
if (line.length > 2) {
if (line[0] == '|' && line[line.length - 1] == '|') {
return ResponseStates.TABLE;
} else if (line.indexOf(':') > 0) {
return ResponseStates.OBJECT;
}
}
return ResponseStates.INIT;
}
}, {
key: '_isTableDivider',
value: function _isTableDivider(line) {
//looks for divider that starts/ends with '|' and consists exclusively of '|' and '-', or '=' characters
return line.trim().match(/^\|[-|=]{3,}\|$/) != null;
}
}]);
return AuroraCmdResponseParser;
}();
exports.default = AuroraCmdResponseParser;