dataValueHelper.js 8.7 KB

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