dataValueHelper.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 { parseDate, numericToNumber } from '../../util/number.js';
  41. import { createHashMap, trim, hasOwn, isString, isNumber } from 'zrender/lib/core/util.js';
  42. import { throwError } from '../../util/log.js';
  43. /**
  44. * Convert raw the value in to inner value in List.
  45. *
  46. * [Performance sensitive]
  47. *
  48. * [Caution]: this is the key logic of user value parser.
  49. * For backward compatibility, do not modify it until you have to!
  50. */
  51. export function parseDataValue(value,
  52. // For high performance, do not omit the second param.
  53. opt) {
  54. // Performance sensitive.
  55. var dimType = opt && opt.type;
  56. if (dimType === 'ordinal') {
  57. // If given value is a category string
  58. return value;
  59. }
  60. if (dimType === 'time'
  61. // spead up when using timestamp
  62. && !isNumber(value) && value != null && value !== '-') {
  63. value = +parseDate(value);
  64. }
  65. // dimType defaults 'number'.
  66. // If dimType is not ordinal and value is null or undefined or NaN or '-',
  67. // parse to NaN.
  68. // number-like string (like ' 123 ') can be converted to a number.
  69. // where null/undefined or other string will be converted to NaN.
  70. return value == null || value === '' ? NaN
  71. // If string (like '-'), using '+' parse to NaN
  72. // If object, also parse to NaN
  73. : Number(value);
  74. }
  75. ;
  76. var valueParserMap = createHashMap({
  77. 'number': function (val) {
  78. // Do not use `numericToNumber` here. We have `numericToNumber` by default.
  79. // Here the number parser can have loose rule:
  80. // enable to cut suffix: "120px" => 120, "14%" => 14.
  81. return parseFloat(val);
  82. },
  83. 'time': function (val) {
  84. // return timestamp.
  85. return +parseDate(val);
  86. },
  87. 'trim': function (val) {
  88. return isString(val) ? trim(val) : val;
  89. }
  90. });
  91. export function getRawValueParser(type) {
  92. return valueParserMap.get(type);
  93. }
  94. var ORDER_COMPARISON_OP_MAP = {
  95. lt: function (lval, rval) {
  96. return lval < rval;
  97. },
  98. lte: function (lval, rval) {
  99. return lval <= rval;
  100. },
  101. gt: function (lval, rval) {
  102. return lval > rval;
  103. },
  104. gte: function (lval, rval) {
  105. return lval >= rval;
  106. }
  107. };
  108. var FilterOrderComparator = /** @class */function () {
  109. function FilterOrderComparator(op, rval) {
  110. if (!isNumber(rval)) {
  111. var errMsg = '';
  112. if (process.env.NODE_ENV !== 'production') {
  113. errMsg = 'rvalue of "<", ">", "<=", ">=" can only be number in filter.';
  114. }
  115. throwError(errMsg);
  116. }
  117. this._opFn = ORDER_COMPARISON_OP_MAP[op];
  118. this._rvalFloat = numericToNumber(rval);
  119. }
  120. // Performance sensitive.
  121. FilterOrderComparator.prototype.evaluate = function (lval) {
  122. // Most cases is 'number', and typeof maybe 10 times faseter than parseFloat.
  123. return isNumber(lval) ? this._opFn(lval, this._rvalFloat) : this._opFn(numericToNumber(lval), this._rvalFloat);
  124. };
  125. return FilterOrderComparator;
  126. }();
  127. var SortOrderComparator = /** @class */function () {
  128. /**
  129. * @param order by default: 'asc'
  130. * @param incomparable by default: Always on the tail.
  131. * That is, if 'asc' => 'max', if 'desc' => 'min'
  132. * See the definition of "incomparable" in [SORT_COMPARISON_RULE].
  133. */
  134. function SortOrderComparator(order, incomparable) {
  135. var isDesc = order === 'desc';
  136. this._resultLT = isDesc ? 1 : -1;
  137. if (incomparable == null) {
  138. incomparable = isDesc ? 'min' : 'max';
  139. }
  140. this._incomparable = incomparable === 'min' ? -Infinity : Infinity;
  141. }
  142. // See [SORT_COMPARISON_RULE].
  143. // Performance sensitive.
  144. SortOrderComparator.prototype.evaluate = function (lval, rval) {
  145. // Most cases is 'number', and typeof maybe 10 times faseter than parseFloat.
  146. var lvalFloat = isNumber(lval) ? lval : numericToNumber(lval);
  147. var rvalFloat = isNumber(rval) ? rval : numericToNumber(rval);
  148. var lvalNotNumeric = isNaN(lvalFloat);
  149. var rvalNotNumeric = isNaN(rvalFloat);
  150. if (lvalNotNumeric) {
  151. lvalFloat = this._incomparable;
  152. }
  153. if (rvalNotNumeric) {
  154. rvalFloat = this._incomparable;
  155. }
  156. if (lvalNotNumeric && rvalNotNumeric) {
  157. var lvalIsStr = isString(lval);
  158. var rvalIsStr = isString(rval);
  159. if (lvalIsStr) {
  160. lvalFloat = rvalIsStr ? lval : 0;
  161. }
  162. if (rvalIsStr) {
  163. rvalFloat = lvalIsStr ? rval : 0;
  164. }
  165. }
  166. return lvalFloat < rvalFloat ? this._resultLT : lvalFloat > rvalFloat ? -this._resultLT : 0;
  167. };
  168. return SortOrderComparator;
  169. }();
  170. export { SortOrderComparator };
  171. var FilterEqualityComparator = /** @class */function () {
  172. function FilterEqualityComparator(isEq, rval) {
  173. this._rval = rval;
  174. this._isEQ = isEq;
  175. this._rvalTypeof = typeof rval;
  176. this._rvalFloat = numericToNumber(rval);
  177. }
  178. // Performance sensitive.
  179. FilterEqualityComparator.prototype.evaluate = function (lval) {
  180. var eqResult = lval === this._rval;
  181. if (!eqResult) {
  182. var lvalTypeof = typeof lval;
  183. if (lvalTypeof !== this._rvalTypeof && (lvalTypeof === 'number' || this._rvalTypeof === 'number')) {
  184. eqResult = numericToNumber(lval) === this._rvalFloat;
  185. }
  186. }
  187. return this._isEQ ? eqResult : !eqResult;
  188. };
  189. return FilterEqualityComparator;
  190. }();
  191. /**
  192. * [FILTER_COMPARISON_RULE]
  193. * `lt`|`lte`|`gt`|`gte`:
  194. * + rval must be a number. And lval will be converted to number (`numericToNumber`) to compare.
  195. * `eq`:
  196. * + If same type, compare with `===`.
  197. * + If there is one number, convert to number (`numericToNumber`) to compare.
  198. * + Else return `false`.
  199. * `ne`:
  200. * + Not `eq`.
  201. *
  202. *
  203. * [SORT_COMPARISON_RULE]
  204. * All the values are grouped into three categories:
  205. * + "numeric" (number and numeric string)
  206. * + "non-numeric-string" (string that excluding numeric string)
  207. * + "others"
  208. * "numeric" vs "numeric": values are ordered by number order.
  209. * "non-numeric-string" vs "non-numeric-string": values are ordered by ES spec (#sec-abstract-relational-comparison).
  210. * "others" vs "others": do not change order (always return 0).
  211. * "numeric" vs "non-numeric-string": "non-numeric-string" is treated as "incomparable".
  212. * "number" vs "others": "others" is treated as "incomparable".
  213. * "non-numeric-string" vs "others": "others" is treated as "incomparable".
  214. * "incomparable" will be seen as -Infinity or Infinity (depends on the settings).
  215. * MEMO:
  216. * Non-numeric string sort makes sense when we need to put the items with the same tag together.
  217. * But if we support string sort, we still need to avoid the misleading like `'2' > '12'`,
  218. * So we treat "numeric-string" sorted by number order rather than string comparison.
  219. *
  220. *
  221. * [CHECK_LIST_OF_THE_RULE_DESIGN]
  222. * + Do not support string comparison until required. And also need to
  223. * avoid the misleading of "2" > "12".
  224. * + Should avoid the misleading case:
  225. * `" 22 " gte "22"` is `true` but `" 22 " eq "22"` is `false`.
  226. * + JS bad case should be avoided: null <= 0, [] <= 0, ' ' <= 0, ...
  227. * + Only "numeric" can be converted to comparable number, otherwise converted to NaN.
  228. * See `util/number.ts#numericToNumber`.
  229. *
  230. * @return If `op` is not `RelationalOperator`, return null;
  231. */
  232. export function createFilterComparator(op, rval) {
  233. return op === 'eq' || op === 'ne' ? new FilterEqualityComparator(op === 'eq', rval) : hasOwn(ORDER_COMPARISON_OP_MAP, op) ? new FilterOrderComparator(op, rval) : null;
  234. }