Refactoring representation of variable type.

This commit is contained in:
Karishma Chadha 2017-11-13 16:29:38 -05:00
parent 1d4acde7dc
commit 26d4a3a069
2 changed files with 22 additions and 4 deletions

View file

@ -124,7 +124,7 @@ class Target extends EventEmitter {
const list = this.lookupVariableById(id);
if (list) return list;
// No variable with this name exists - create it locally.
const newList = new Variable(id, name, 'list', false);
const newList = new Variable(id, name, Variable.LIST_TYPE, false);
this.variables[id] = newList;
return newList;
}
@ -134,7 +134,7 @@ class Target extends EventEmitter {
* dictionary of variables.
* @param {string} id Id of variable
* @param {string} name Name of variable.
* @param {string} type Type of variable, one of string, number, list
* @param {string} type Type of variable, '' or 'list'
*/
createVariable (id, name, type) {
if (!this.variables.hasOwnProperty(id)) {

View file

@ -19,18 +19,36 @@ class Variable {
this.type = type;
this.isCloud = isCloud;
switch (this.type) {
case '':
case Variable.SCALAR_TYPE:
this.value = 0;
break;
case 'list':
case Variable.LIST_TYPE:
this.value = [];
break;
default:
throw new Error(`Invalid variable type: ${this.type}`);
}
}
toXML () {
return `<variable type="${this.type}" id="${this.id}">${this.name}</variable>`;
}
/**
* Type representation for scalar variables.
* @const {string}
*/
static get SCALAR_TYPE () {
return '';
}
/**
* Type representation for list variables.
* @const {string}
*/
static get LIST_TYPE () {
return 'list';
}
}
module.exports = Variable;