BasicEvaluatedExpression.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class BasicEvaluatedExpression {
  7. constructor() {
  8. this.range = null;
  9. }
  10. isNull() {
  11. return !!this.null;
  12. }
  13. isString() {
  14. return Object.prototype.hasOwnProperty.call(this, "string");
  15. }
  16. isNumber() {
  17. return Object.prototype.hasOwnProperty.call(this, "number");
  18. }
  19. isBoolean() {
  20. return Object.prototype.hasOwnProperty.call(this, "bool");
  21. }
  22. isRegExp() {
  23. return Object.prototype.hasOwnProperty.call(this, "regExp");
  24. }
  25. isConditional() {
  26. return Object.prototype.hasOwnProperty.call(this, "options");
  27. }
  28. isArray() {
  29. return Object.prototype.hasOwnProperty.call(this, "items");
  30. }
  31. isConstArray() {
  32. return Object.prototype.hasOwnProperty.call(this, "array");
  33. }
  34. isIdentifier() {
  35. return Object.prototype.hasOwnProperty.call(this, "identifier");
  36. }
  37. isWrapped() {
  38. return Object.prototype.hasOwnProperty.call(this, "prefix") || Object.prototype.hasOwnProperty.call(this, "postfix");
  39. }
  40. isTemplateString() {
  41. return Object.prototype.hasOwnProperty.call(this, "quasis");
  42. }
  43. asBool() {
  44. if(this.isBoolean()) return this.bool;
  45. else if(this.isNull()) return false;
  46. else if(this.isString()) return !!this.string;
  47. else if(this.isNumber()) return !!this.number;
  48. else if(this.isRegExp()) return true;
  49. else if(this.isArray()) return true;
  50. else if(this.isConstArray()) return true;
  51. else if(this.isWrapped()) return this.prefix && this.prefix.asBool() || this.postfix && this.postfix.asBool() ? true : undefined;
  52. else if(this.isTemplateString()) {
  53. if(this.quasis.length === 1) return this.quasis[0].asBool();
  54. for(let i = 0; i < this.quasis.length; i++) {
  55. if(this.quasis[i].asBool()) return true;
  56. }
  57. // can't tell if string will be empty without executing
  58. }
  59. return undefined;
  60. }
  61. setString(str) {
  62. if(str === null)
  63. delete this.string;
  64. else
  65. this.string = str;
  66. return this;
  67. }
  68. setNull() {
  69. this.null = true;
  70. return this;
  71. }
  72. setNumber(num) {
  73. if(num === null)
  74. delete this.number;
  75. else
  76. this.number = num;
  77. return this;
  78. }
  79. setBoolean(bool) {
  80. if(bool === null)
  81. delete this.bool;
  82. else
  83. this.bool = bool;
  84. return this;
  85. }
  86. setRegExp(regExp) {
  87. if(regExp === null)
  88. delete this.regExp;
  89. else
  90. this.regExp = regExp;
  91. return this;
  92. }
  93. setIdentifier(identifier) {
  94. if(identifier === null)
  95. delete this.identifier;
  96. else
  97. this.identifier = identifier;
  98. return this;
  99. }
  100. setWrapped(prefix, postfix) {
  101. this.prefix = prefix;
  102. this.postfix = postfix;
  103. return this;
  104. }
  105. unsetWrapped() {
  106. delete this.prefix;
  107. delete this.postfix;
  108. return this;
  109. }
  110. setOptions(options) {
  111. if(options === null)
  112. delete this.options;
  113. else
  114. this.options = options;
  115. return this;
  116. }
  117. setItems(items) {
  118. if(items === null)
  119. delete this.items;
  120. else
  121. this.items = items;
  122. return this;
  123. }
  124. setArray(array) {
  125. if(array === null)
  126. delete this.array;
  127. else
  128. this.array = array;
  129. return this;
  130. }
  131. setTemplateString(quasis) {
  132. if(quasis === null)
  133. delete this.quasis;
  134. else
  135. this.quasis = quasis;
  136. return this;
  137. }
  138. addOptions(options) {
  139. if(!this.options) this.options = [];
  140. options.forEach(item => {
  141. this.options.push(item);
  142. }, this);
  143. return this;
  144. }
  145. setRange(range) {
  146. this.range = range;
  147. return this;
  148. }
  149. }
  150. module.exports = BasicEvaluatedExpression;