conditionalExpression.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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' // Might be misleading for sake of the difference between '==' and '===',
  54. // so don't support them.
  55. // '==': 'eq',
  56. // '===': 'seq',
  57. // '!==': 'sne'
  58. // PENDING: Whether support some common alias "ge", "le", "neq"?
  59. // ge: 'gte',
  60. // le: 'lte',
  61. // neq: 'ne',
  62. }; // type RelationalExpressionOpEvaluate = (tarVal: unknown, condVal: unknown) => boolean;
  63. var RegExpEvaluator =
  64. /** @class */
  65. 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 =
  84. /** @class */
  85. function () {
  86. function ConstConditionInternal() {}
  87. ConstConditionInternal.prototype.evaluate = function () {
  88. return this.value;
  89. };
  90. return ConstConditionInternal;
  91. }();
  92. var AndConditionInternal =
  93. /** @class */
  94. function () {
  95. function AndConditionInternal() {}
  96. AndConditionInternal.prototype.evaluate = function () {
  97. var children = this.children;
  98. for (var i = 0; i < children.length; i++) {
  99. if (!children[i].evaluate()) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. };
  105. return AndConditionInternal;
  106. }();
  107. var OrConditionInternal =
  108. /** @class */
  109. function () {
  110. function OrConditionInternal() {}
  111. OrConditionInternal.prototype.evaluate = function () {
  112. var children = this.children;
  113. for (var i = 0; i < children.length; i++) {
  114. if (children[i].evaluate()) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. };
  120. return OrConditionInternal;
  121. }();
  122. var NotConditionInternal =
  123. /** @class */
  124. function () {
  125. function NotConditionInternal() {}
  126. NotConditionInternal.prototype.evaluate = function () {
  127. return !this.child.evaluate();
  128. };
  129. return NotConditionInternal;
  130. }();
  131. var RelationalConditionInternal =
  132. /** @class */
  133. function () {
  134. function RelationalConditionInternal() {}
  135. RelationalConditionInternal.prototype.evaluate = function () {
  136. var needParse = !!this.valueParser; // Call getValue with no `this`.
  137. var getValue = this.getValue;
  138. var tarValRaw = getValue(this.valueGetterParam);
  139. var tarValParsed = needParse ? this.valueParser(tarValRaw) : null; // Relational cond follow "and" logic internally.
  140. for (var i = 0; i < this.subCondList.length; i++) {
  141. if (!this.subCondList[i].evaluate(needParse ? tarValParsed : tarValRaw)) {
  142. return false;
  143. }
  144. }
  145. return true;
  146. };
  147. return RelationalConditionInternal;
  148. }();
  149. function parseOption(exprOption, getters) {
  150. if (exprOption === true || exprOption === false) {
  151. var cond = new ConstConditionInternal();
  152. cond.value = exprOption;
  153. return cond;
  154. }
  155. var errMsg = '';
  156. if (!isObjectNotArray(exprOption)) {
  157. if (process.env.NODE_ENV !== 'production') {
  158. errMsg = makePrintable('Illegal config. Expect a plain object but actually', exprOption);
  159. }
  160. throwError(errMsg);
  161. }
  162. if (exprOption.and) {
  163. return parseAndOrOption('and', exprOption, getters);
  164. } else if (exprOption.or) {
  165. return parseAndOrOption('or', exprOption, getters);
  166. } else if (exprOption.not) {
  167. return parseNotOption(exprOption, getters);
  168. }
  169. return parseRelationalOption(exprOption, getters);
  170. }
  171. function parseAndOrOption(op, exprOption, getters) {
  172. var subOptionArr = exprOption[op];
  173. var errMsg = '';
  174. if (process.env.NODE_ENV !== 'production') {
  175. errMsg = makePrintable('"and"/"or" condition should only be `' + op + ': [...]` and must not be empty array.', 'Illegal condition:', exprOption);
  176. }
  177. if (!isArray(subOptionArr)) {
  178. throwError(errMsg);
  179. }
  180. if (!subOptionArr.length) {
  181. throwError(errMsg);
  182. }
  183. var cond = op === 'and' ? new AndConditionInternal() : new OrConditionInternal();
  184. cond.children = map(subOptionArr, function (subOption) {
  185. return parseOption(subOption, getters);
  186. });
  187. if (!cond.children.length) {
  188. throwError(errMsg);
  189. }
  190. return cond;
  191. }
  192. function parseNotOption(exprOption, getters) {
  193. var subOption = exprOption.not;
  194. var errMsg = '';
  195. if (process.env.NODE_ENV !== 'production') {
  196. errMsg = makePrintable('"not" condition should only be `not: {}`.', 'Illegal condition:', exprOption);
  197. }
  198. if (!isObjectNotArray(subOption)) {
  199. throwError(errMsg);
  200. }
  201. var cond = new NotConditionInternal();
  202. cond.child = parseOption(subOption, getters);
  203. if (!cond.child) {
  204. throwError(errMsg);
  205. }
  206. return cond;
  207. }
  208. function parseRelationalOption(exprOption, getters) {
  209. var errMsg = '';
  210. var valueGetterParam = getters.prepareGetValue(exprOption);
  211. var subCondList = [];
  212. var exprKeys = keys(exprOption);
  213. var parserName = exprOption.parser;
  214. var valueParser = parserName ? getRawValueParser(parserName) : null;
  215. for (var i = 0; i < exprKeys.length; i++) {
  216. var keyRaw = exprKeys[i];
  217. if (keyRaw === 'parser' || getters.valueGetterAttrMap.get(keyRaw)) {
  218. continue;
  219. }
  220. var op = hasOwn(RELATIONAL_EXPRESSION_OP_ALIAS_MAP, keyRaw) ? RELATIONAL_EXPRESSION_OP_ALIAS_MAP[keyRaw] : keyRaw;
  221. var condValueRaw = exprOption[keyRaw];
  222. var condValueParsed = valueParser ? valueParser(condValueRaw) : condValueRaw;
  223. var evaluator = createFilterComparator(op, condValueParsed) || op === 'reg' && new RegExpEvaluator(condValueParsed);
  224. if (!evaluator) {
  225. if (process.env.NODE_ENV !== 'production') {
  226. errMsg = makePrintable('Illegal relational operation: "' + keyRaw + '" in condition:', exprOption);
  227. }
  228. throwError(errMsg);
  229. }
  230. subCondList.push(evaluator);
  231. }
  232. if (!subCondList.length) {
  233. if (process.env.NODE_ENV !== 'production') {
  234. errMsg = makePrintable('Relational condition must have at least one operator.', 'Illegal condition:', exprOption);
  235. } // No relational operator always disabled in case of dangers result.
  236. throwError(errMsg);
  237. }
  238. var cond = new RelationalConditionInternal();
  239. cond.valueGetterParam = valueGetterParam;
  240. cond.valueParser = valueParser;
  241. cond.getValue = getters.getValue;
  242. cond.subCondList = subCondList;
  243. return cond;
  244. }
  245. function isObjectNotArray(val) {
  246. return isObject(val) && !isArrayLike(val);
  247. }
  248. var ConditionalExpressionParsed =
  249. /** @class */
  250. function () {
  251. function ConditionalExpressionParsed(exprOption, getters) {
  252. this._cond = parseOption(exprOption, getters);
  253. }
  254. ConditionalExpressionParsed.prototype.evaluate = function () {
  255. return this._cond.evaluate();
  256. };
  257. return ConditionalExpressionParsed;
  258. }();
  259. ;
  260. export function parseConditionalExpression(exprOption, getters) {
  261. return new ConditionalExpressionParsed(exprOption, getters);
  262. }