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); const list = this.lookupVariableById(id);
if (list) return list; if (list) return list;
// No variable with this name exists - create it locally. // 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; this.variables[id] = newList;
return newList; return newList;
} }
@ -134,7 +134,7 @@ class Target extends EventEmitter {
* dictionary of variables. * dictionary of variables.
* @param {string} id Id of variable * @param {string} id Id of variable
* @param {string} name Name 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) { createVariable (id, name, type) {
if (!this.variables.hasOwnProperty(id)) { if (!this.variables.hasOwnProperty(id)) {

View file

@ -19,18 +19,36 @@ class Variable {
this.type = type; this.type = type;
this.isCloud = isCloud; this.isCloud = isCloud;
switch (this.type) { switch (this.type) {
case '': case Variable.SCALAR_TYPE:
this.value = 0; this.value = 0;
break; break;
case 'list': case Variable.LIST_TYPE:
this.value = []; this.value = [];
break; break;
default:
throw new Error(`Invalid variable type: ${this.type}`);
} }
} }
toXML () { toXML () {
return `<variable type="${this.type}" id="${this.id}">${this.name}</variable>`; 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; module.exports = Variable;