2016-09-21 16:38:33 -04:00
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Object representing a Scratch variable.
|
|
|
|
*/
|
|
|
|
|
2017-06-15 17:29:15 -04:00
|
|
|
const uid = require('../util/uid');
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Variable {
|
|
|
|
/**
|
2017-06-15 17:29:15 -04:00
|
|
|
* @param {string} id Id of the variable.
|
|
|
|
* @param {string} name Name of the variable.
|
2017-11-09 17:19:34 -05:00
|
|
|
* @param {(string|number)} type Type of the variable, one of "" or "list"
|
2017-04-17 19:42:48 -04:00
|
|
|
* @param {boolean} isCloud Whether the variable is stored in the cloud.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-11-09 17:19:34 -05:00
|
|
|
constructor (id, name, type, isCloud) {
|
2017-06-15 17:29:15 -04:00
|
|
|
this.id = id || uid();
|
2017-04-17 19:42:48 -04:00
|
|
|
this.name = name;
|
2017-11-09 17:19:34 -05:00
|
|
|
this.type = type;
|
2017-04-17 19:42:48 -04:00
|
|
|
this.isCloud = isCloud;
|
2017-11-09 17:19:34 -05:00
|
|
|
switch (this.type) {
|
2017-11-13 16:29:38 -05:00
|
|
|
case Variable.SCALAR_TYPE:
|
2017-11-09 17:19:34 -05:00
|
|
|
this.value = 0;
|
|
|
|
break;
|
2017-11-13 16:29:38 -05:00
|
|
|
case Variable.LIST_TYPE:
|
2017-11-09 17:19:34 -05:00
|
|
|
this.value = [];
|
|
|
|
break;
|
2017-11-13 16:29:38 -05:00
|
|
|
default:
|
|
|
|
throw new Error(`Invalid variable type: ${this.type}`);
|
2017-11-09 17:19:34 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-05-25 11:44:49 -04:00
|
|
|
|
|
|
|
toXML () {
|
2017-11-09 17:19:34 -05:00
|
|
|
return `<variable type="${this.type}" id="${this.id}">${this.name}</variable>`;
|
2017-05-25 11:44:49 -04:00
|
|
|
}
|
2017-11-13 16:29:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-24 11:49:34 -04:00
|
|
|
|
|
|
|
module.exports = Variable;
|