conditionalExpression.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { keys, isArray, map, isObject, isString, isRegExp, isArrayLike, hasOwn, isNumber } from 'zrender/lib/core/util.js';
  41. import { throwError, makePrintable } from './log.js';
  42. import { getRawValueParser, createFilterComparator } from '../data/helper/dataValueHelper.js';
  43. ;
  44. var RELATIONAL_EXPRESSION_OP_ALIAS_MAP = {
  45. value: 'eq',
  46. // PENDING: not good for literal semantic?
  47. '<': 'lt',
  48. '<=': 'lte',
  49. '>': 'gt',
  50. '>=': 'gte',
  51. '=': 'eq',
  52. '!=': 'ne',
  53. '<>': 'ne'
  54. // Might be misleading for sake of the difference between '==' and '===',
  55. // so don't support them.
  56. // '==': 'eq',
  57. // '===': 'seq',
  58. // '!==': 'sne'
  59. // PENDING: Whether support some common alias "ge", "le", "neq"?
  60. // ge: 'gte',
  61. // le: 'lte',
  62. // neq: 'ne',
  63. };
  64. // type RelationalExpressionOpEvaluate = (tarVal: unknown, condVal: unknown) => boolean;
  65. var RegExpEvaluator = /** @class */function () {
  66. function RegExpEvaluator(rVal) {
  67. // Support condVal: RegExp | string
  68. var condValue = this._condVal = isString(rVal) ? new RegExp(rVal) : isRegExp(rVal) ? rVal : null;
  69. if (condValue == null) {
  70. var errMsg = '';
  71. if (process.env.NODE_ENV !== 'production') {
  72. errMsg = makePrintable('Illegal regexp', rVal, 'in');
  73. }
  74. throwError(errMsg);
  75. }
  76. }
  77. RegExpEvaluator.prototype.evaluate = function (lVal) {
  78. var type = typeof lVal;
  79. return isString(type) ? this._condVal.test(lVal) : isNumber(type) ? this._condVal.test(lVal + '') : false;
  80. };
  81. return RegExpEvaluator;
  82. }();
  83. var ConstConditionInternal = /** @class */function () {
  84. function ConstConditionInternal() {}
  85. ConstConditionInternal.prototype.evaluate = function () {
  86. return this.value;
  87. };
  88. return ConstConditionInternal;
  89. }();
  90. var AndConditionInternal = /** @class */function () {
  91. function AndConditionInternal() {}
  92. AndConditionInternal.prototype.evaluate = function () {
  93. var children = this.children;
  94. for (var i = 0; i < children.length; i++) {
  95. if (!children[i].evaluate()) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. };
  101. return AndConditionInternal;
  102. }();
  103. var OrConditionInternal = /** @class */function () {
  104. function OrConditionInternal() {}
  105. OrConditionInternal.prototype.evaluate = function () {
  106. var children = this.children;
  107. for (var i = 0; i < children.length; i++) {
  108. if (children[i].evaluate()) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. };
  114. return OrConditionInternal;
  115. }();
  116. var NotConditionInternal = /** @class */function () {
  117. function NotConditionInternal() {}
  118. NotConditionInternal.prototype.evaluate = function () {
  119. return !this.child.evaluate();
  120. };
  121. return NotConditionInternal;
  122. }();
  123. var RelationalConditionInternal = /** @class */function () {
  124. function RelationalConditionInternal() {}
  125. RelationalConditionInternal.prototype.evaluate = function () {
  126. var needParse = !!this.valueParser;
  127. // Call getValue with no `this`.
  128. var getValue = this.getValue;
  129. var tarValRaw = getValue(this.valueGetterParam);
  130. var tarValParsed = needParse ? this.valueParser(tarValRaw) : null;
  131. // Relational cond follow "and" logic internally.
  132. for (var i = 0; i < this.subCondList.length; i++) {
  133. if (!this.subCondList[i].evaluate(needParse ? tarValParsed : tarValRaw)) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. };
  139. return RelationalConditionInternal;
  140. }();
  141. function parseOption(exprOption, getters) {
  142. if (exprOption === true || exprOption === false) {
  143. var cond = new ConstConditionInternal();
  144. cond.value = exprOption;
  145. return cond;
  146. }
  147. var errMsg = '';
  148. if (!isObjectNotArray(exprOption)) {
  149. if (process.env.NODE_ENV !== 'production') {
  150. errMsg = makePrintable('Illegal config. Expect a plain object but actually', exprOption);
  151. }
  152. throwError(errMsg);
  153. }
  154. if (exprOption.and) {
  155. return parseAndOrOption('and', exprOption, getters);
  156. } else if (exprOption.or) {
  157. return parseAndOrOption('or', exprOption, getters);
  158. } else if (exprOption.not) {
  159. return parseNotOption(exprOption, getters);
  160. }
  161. return parseRelationalOption(exprOption, getters);
  162. }
  163. function parseAndOrOption(op, exprOption, getters) {
  164. var subOptionArr = exprOption[op];
  165. var errMsg = '';
  166. if (process.env.NODE_ENV !== 'production') {
  167. errMsg = makePrintable('"and"/"or" condition should only be `' + op + ': [...]` and must not be empty array.', 'Illegal condition:', exprOption);
  168. }
  169. if (!isArray(subOptionArr)) {
  170. throwError(errMsg);
  171. }
  172. if (!subOptionArr.length) {
  173. throwError(errMsg);
  174. }
  175. var cond = op === 'and' ? new AndConditionInternal() : new OrConditionInternal();
  176. cond.children = map(subOptionArr, function (subOption) {
  177. return parseOption(subOption, getters);
  178. });
  179. if (!cond.children.length) {
  180. throwError(errMsg);
  181. }
  182. return cond;
  183. }
  184. function parseNotOption(exprOption, getters) {
  185. var subOption = exprOption.not;
  186. var errMsg = '';
  187. if (process.env.NODE_ENV !== 'production') {
  188. errMsg = makePrintable('"not" condition should only be `not: {}`.', 'Illegal condition:', exprOption);
  189. }
  190. if (!isObjectNotArray(subOption)) {
  191. throwError(errMsg);
  192. }
  193. var cond = new NotConditionInternal();
  194. cond.child = parseOption(subOption, getters);
  195. if (!cond.child) {
  196. throwError(errMsg);
  197. }
  198. return cond;
  199. }
  200. function parseRelationalOption(exprOption, getters) {
  201. var errMsg = '';
  202. var valueGetterParam = getters.prepareGetValue(exprOption);
  203. var subCondList = [];
  204. var exprKeys = keys(exprOption);
  205. var parserName = exprOption.parser;
  206. var valueParser = parserName ? getRawValueParser(parserName) : null;
  207. for (var i = 0; i < exprKeys.length; i++) {
  208. var keyRaw = exprKeys[i];
  209. if (keyRaw === 'parser' || getters.valueGetterAttrMap.get(keyRaw)) {
  210. continue;
  211. }
  212. var op = hasOwn(RELATIONAL_EXPRESSION_OP_ALIAS_MAP, keyRaw) ? RELATIONAL_EXPRESSION_OP_ALIAS_MAP[keyRaw] : keyRaw;
  213. var condValueRaw = exprOption[keyRaw];
  214. var condValueParsed = valueParser ? valueParser(condValueRaw) : condValueRaw;
  215. var evaluator = createFilterComparator(op, condValueParsed) || op === 'reg' && new RegExpEvaluator(condValueParsed);
  216. if (!evaluator) {
  217. if (process.env.NODE_ENV !== 'production') {
  218. errMsg = makePrintable('Illegal relational operation: "' + keyRaw + '" in condition:', exprOption);
  219. }
  220. throwError(errMsg);
  221. }
  222. subCondList.push(evaluator);
  223. }
  224. if (!subCondList.length) {
  225. if (process.env.NODE_ENV !== 'production') {
  226. errMsg = makePrintable('Relational condition must have at least one operator.', 'Illegal condition:', exprOption);
  227. }
  228. // No relational operator always disabled in case of dangers result.
  229. throwError(errMsg);
  230. }
  231. var cond = new RelationalConditionInternal();
  232. cond.valueGetterParam = valueGetterParam;
  233. cond.valueParser = valueParser;
  234. cond.getValue = getters.getValue;
  235. cond.subCondList = subCondList;
  236. return cond;
  237. }
  238. function isObjectNotArray(val) {
  239. return isObject(val) && !isArrayLike(val);
  240. }
  241. var ConditionalExpressionParsed = /** @class */function () {
  242. function ConditionalExpressionParsed(exprOption, getters) {
  243. this._cond = parseOption(exprOption, getters);
  244. }
  245. ConditionalExpressionParsed.prototype.evaluate = function () {
  246. return this._cond.evaluate();
  247. };
  248. return ConditionalExpressionParsed;
  249. }();
  250. ;
  251. export function parseConditionalExpression(exprOption, getters) {
  252. return new ConditionalExpressionParsed(exprOption, getters);
  253. }