refactor(ES6): upgrade BasicEvaluatedExpression to ES6 (#3676)

* refactor(ES6): upgrade BasicEvaluatedExpression to ES6
This commit is contained in:
Will Mendes 2017-01-03 08:32:23 +11:00 committed by Sean Larkin
parent d9ab99792c
commit b1c7d5c8cf
1 changed files with 174 additions and 150 deletions

View File

@ -2,45 +2,60 @@
MIT License http://www.opensource.org/licenses/mit-license.php MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra Author Tobias Koppers @sokra
*/ */
function BasicEvaluatedExpression() {
this.range = null;
}
module.exports = BasicEvaluatedExpression;
BasicEvaluatedExpression.prototype.isNull = function() { "use strict";
class BasicEvaluatedExpression {
constructor() {
this.range = null;
}
isNull() {
return !!this.null; return !!this.null;
}; }
BasicEvaluatedExpression.prototype.isString = function() {
isString() {
return Object.prototype.hasOwnProperty.call(this, "string"); return Object.prototype.hasOwnProperty.call(this, "string");
}; }
BasicEvaluatedExpression.prototype.isNumber = function() {
isNumber() {
return Object.prototype.hasOwnProperty.call(this, "number"); return Object.prototype.hasOwnProperty.call(this, "number");
}; }
BasicEvaluatedExpression.prototype.isBoolean = function() {
isBoolean() {
return Object.prototype.hasOwnProperty.call(this, "bool"); return Object.prototype.hasOwnProperty.call(this, "bool");
}; }
BasicEvaluatedExpression.prototype.isRegExp = function() {
isRegExp() {
return Object.prototype.hasOwnProperty.call(this, "regExp"); return Object.prototype.hasOwnProperty.call(this, "regExp");
}; }
BasicEvaluatedExpression.prototype.isConditional = function() {
isConditional() {
return Object.prototype.hasOwnProperty.call(this, "options"); return Object.prototype.hasOwnProperty.call(this, "options");
}; }
BasicEvaluatedExpression.prototype.isArray = function() {
isArray() {
return Object.prototype.hasOwnProperty.call(this, "items"); return Object.prototype.hasOwnProperty.call(this, "items");
}; }
BasicEvaluatedExpression.prototype.isConstArray = function() {
isConstArray() {
return Object.prototype.hasOwnProperty.call(this, "array"); return Object.prototype.hasOwnProperty.call(this, "array");
}; }
BasicEvaluatedExpression.prototype.isIdentifier = function() {
isIdentifier() {
return Object.prototype.hasOwnProperty.call(this, "identifier"); return Object.prototype.hasOwnProperty.call(this, "identifier");
}; }
BasicEvaluatedExpression.prototype.isWrapped = function() {
isWrapped() {
return Object.prototype.hasOwnProperty.call(this, "prefix") || Object.prototype.hasOwnProperty.call(this, "postfix"); return Object.prototype.hasOwnProperty.call(this, "prefix") || Object.prototype.hasOwnProperty.call(this, "postfix");
}; }
BasicEvaluatedExpression.prototype.isTemplateString = function() {
isTemplateString() {
return Object.prototype.hasOwnProperty.call(this, "quasis"); return Object.prototype.hasOwnProperty.call(this, "quasis");
} }
BasicEvaluatedExpression.prototype.asBool = function() {
asBool() {
if(this.isBoolean()) return this.bool; if(this.isBoolean()) return this.bool;
else if(this.isNull()) return false; else if(this.isNull()) return false;
else if(this.isString()) return !!this.string; else if(this.isString()) return !!this.string;
@ -57,101 +72,110 @@ BasicEvaluatedExpression.prototype.asBool = function() {
// can't tell if string will be empty without executing // can't tell if string will be empty without executing
} }
return undefined; return undefined;
}; }
BasicEvaluatedExpression.prototype.set = function(value) {
if(typeof value === "string") return this.setString(value); setString(str) {
if(typeof value === "number") return this.setNumber(value);
if(typeof value === "boolean") return this.setBoolean(value);
if(value === null) return this.setNull();
if(value instanceof RegExp) return this.setRegExp(value);
if(Array.isArray(value)) return this.setArray(value);
return this;
};
BasicEvaluatedExpression.prototype.setString = function(str) {
if(str === null) if(str === null)
delete this.string; delete this.string;
else else
this.string = str; this.string = str;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setNull = function() {
setNull() {
this.null = true; this.null = true;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setNumber = function(num) {
setNumber(num) {
if(num === null) if(num === null)
delete this.number; delete this.number;
else else
this.number = num; this.number = num;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setBoolean = function(bool) {
setBoolean(bool) {
if(bool === null) if(bool === null)
delete this.bool; delete this.bool;
else else
this.bool = bool; this.bool = bool;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setRegExp = function(regExp) {
setRegExp(regExp) {
if(regExp === null) if(regExp === null)
delete this.regExp; delete this.regExp;
else else
this.regExp = regExp; this.regExp = regExp;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setIdentifier = function(identifier) {
setIdentifier(identifier) {
if(identifier === null) if(identifier === null)
delete this.identifier; delete this.identifier;
else else
this.identifier = identifier; this.identifier = identifier;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setWrapped = function(prefix, postfix) {
setWrapped(prefix, postfix) {
this.prefix = prefix; this.prefix = prefix;
this.postfix = postfix; this.postfix = postfix;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.unsetWrapped = function() {
unsetWrapped() {
delete this.prefix; delete this.prefix;
delete this.postfix; delete this.postfix;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setOptions = function(options) {
setOptions(options) {
if(options === null) if(options === null)
delete this.options; delete this.options;
else else
this.options = options; this.options = options;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setItems = function(items) {
setItems(items) {
if(items === null) if(items === null)
delete this.items; delete this.items;
else else
this.items = items; this.items = items;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setArray = function(array) {
setArray(array) {
if(array === null) if(array === null)
delete this.array; delete this.array;
else else
this.array = array; this.array = array;
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setTemplateString = function(quasis) {
setTemplateString(quasis) {
if(quasis === null) if(quasis === null)
delete this.quasis; delete this.quasis;
else else
this.quasis = quasis; this.quasis = quasis;
return this; return this;
} }
BasicEvaluatedExpression.prototype.addOptions = function(options) {
addOptions(options) {
if(!this.options) this.options = []; if(!this.options) this.options = [];
options.forEach(function(item) { options.forEach(item => {
this.options.push(item); this.options.push(item);
}, this); }, this);
return this; return this;
}; }
BasicEvaluatedExpression.prototype.setRange = function(range) {
setRange(range) {
this.range = range; this.range = range;
return this; return this;
}; }
}
module.exports = BasicEvaluatedExpression;