BasicEvaluatedExpression.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. isTruthy() {
  44. return this.truthy;
  45. }
  46. isFalsy() {
  47. return this.falsy;
  48. }
  49. asBool() {
  50. if(this.truthy) return true;
  51. else if(this.falsy) return false;
  52. else if(this.isBoolean()) return this.bool;
  53. else if(this.isNull()) return false;
  54. else if(this.isString()) return !!this.string;
  55. else if(this.isNumber()) return !!this.number;
  56. else if(this.isRegExp()) return true;
  57. else if(this.isArray()) return true;
  58. else if(this.isConstArray()) return true;
  59. else if(this.isWrapped()) return this.prefix && this.prefix.asBool() || this.postfix && this.postfix.asBool() ? true : undefined;
  60. else if(this.isTemplateString()) {
  61. if(this.quasis.length === 1) return this.quasis[0].asBool();
  62. for(let i = 0; i < this.quasis.length; i++) {
  63. if(this.quasis[i].asBool()) return true;
  64. }
  65. // can't tell if string will be empty without executing
  66. }
  67. return undefined;
  68. }
  69. setString(str) {
  70. if(str === null)
  71. delete this.string;
  72. else
  73. this.string = str;
  74. return this;
  75. }
  76. setNull() {
  77. this.null = true;
  78. return this;
  79. }
  80. setNumber(num) {
  81. if(num === null)
  82. delete this.number;
  83. else
  84. this.number = num;
  85. return this;
  86. }
  87. setBoolean(bool) {
  88. if(bool === null)
  89. delete this.bool;
  90. else
  91. this.bool = bool;
  92. return this;
  93. }
  94. setRegExp(regExp) {
  95. if(regExp === null)
  96. delete this.regExp;
  97. else
  98. this.regExp = regExp;
  99. return this;
  100. }
  101. setIdentifier(identifier) {
  102. if(identifier === null)
  103. delete this.identifier;
  104. else
  105. this.identifier = identifier;
  106. return this;
  107. }
  108. setWrapped(prefix, postfix) {
  109. this.prefix = prefix;
  110. this.postfix = postfix;
  111. return this;
  112. }
  113. unsetWrapped() {
  114. delete this.prefix;
  115. delete this.postfix;
  116. return this;
  117. }
  118. setOptions(options) {
  119. if(options === null)
  120. delete this.options;
  121. else
  122. this.options = options;
  123. return this;
  124. }
  125. setItems(items) {
  126. if(items === null)
  127. delete this.items;
  128. else
  129. this.items = items;
  130. return this;
  131. }
  132. setArray(array) {
  133. if(array === null)
  134. delete this.array;
  135. else
  136. this.array = array;
  137. return this;
  138. }
  139. setTemplateString(quasis) {
  140. if(quasis === null)
  141. delete this.quasis;
  142. else
  143. this.quasis = quasis;
  144. return this;
  145. }
  146. setTruthy() {
  147. this.falsy = false;
  148. this.truthy = true;
  149. return this;
  150. }
  151. setFalsy() {
  152. this.falsy = true;
  153. this.truthy = false;
  154. return this;
  155. }
  156. addOptions(options) {
  157. if(!this.options) this.options = [];
  158. options.forEach(item => {
  159. this.options.push(item);
  160. }, this);
  161. return this;
  162. }
  163. setRange(range) {
  164. this.range = range;
  165. return this;
  166. }
  167. }
  168. module.exports = BasicEvaluatedExpression;