dataValueHelper.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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';
  41. import { createHashMap, trim, hasOwn } from 'zrender/lib/core/util';
  42. import { throwError } from '../../util/log';
  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 compatibiliy, do not modify it until 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. && typeof value !== 'number' && 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 by defualt `numericToNumber`.
  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 typeof val === 'string' ? 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 (typeof rval !== 'number') {
  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 typeof lval === 'number' ? 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 defualt: 'asc'
  129. * @param incomparable by defualt: 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 lvalTypeof = typeof lval;
  145. var rvalTypeof = typeof rval;
  146. var lvalFloat = lvalTypeof === 'number' ? lval : numericToNumber(lval);
  147. var rvalFloat = rvalTypeof === 'number' ? 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 = lvalTypeof === 'string';
  158. var rvalIsStr = rvalTypeof === 'string';
  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 =
  172. /** @class */
  173. function () {
  174. function FilterEqualityComparator(isEq, rval) {
  175. this._rval = rval;
  176. this._isEQ = isEq;
  177. this._rvalTypeof = typeof rval;
  178. this._rvalFloat = numericToNumber(rval);
  179. } // Performance sensitive.
  180. FilterEqualityComparator.prototype.evaluate = function (lval) {
  181. var eqResult = lval === this._rval;
  182. if (!eqResult) {
  183. var lvalTypeof = typeof lval;
  184. if (lvalTypeof !== this._rvalTypeof && (lvalTypeof === 'number' || this._rvalTypeof === 'number')) {
  185. eqResult = numericToNumber(lval) === this._rvalFloat;
  186. }
  187. }
  188. return this._isEQ ? eqResult : !eqResult;
  189. };
  190. return FilterEqualityComparator;
  191. }();
  192. /**
  193. * [FILTER_COMPARISON_RULE]
  194. * `lt`|`lte`|`gt`|`gte`:
  195. * + rval must be a number. And lval will be converted to number (`numericToNumber`) to compare.
  196. * `eq`:
  197. * + If same type, compare with `===`.
  198. * + If there is one number, convert to number (`numericToNumber`) to compare.
  199. * + Else return `false`.
  200. * `ne`:
  201. * + Not `eq`.
  202. *
  203. *
  204. * [SORT_COMPARISON_RULE]
  205. * All the values are grouped into three categories:
  206. * + "numeric" (number and numeric string)
  207. * + "non-numeric-string" (string that excluding numeric string)
  208. * + "others"
  209. * "numeric" vs "numeric": values are ordered by number order.
  210. * "non-numeric-string" vs "non-numeric-string": values are ordered by ES spec (#sec-abstract-relational-comparison).
  211. * "others" vs "others": do not change order (always return 0).
  212. * "numeric" vs "non-numeric-string": "non-numeric-string" is treated as "incomparable".
  213. * "number" vs "others": "others" is treated as "incomparable".
  214. * "non-numeric-string" vs "others": "others" is treated as "incomparable".
  215. * "incomparable" will be seen as -Infinity or Infinity (depends on the settings).
  216. * MEMO:
  217. * non-numeric string sort make sence when need to put the items with the same tag together.
  218. * But if we support string sort, we still need to avoid the misleading like `'2' > '12'`,
  219. * So we treat "numeric-string" sorted by number order rather than string comparison.
  220. *
  221. *
  222. * [CHECK_LIST_OF_THE_RULE_DESIGN]
  223. * + Do not support string comparison until required. And also need to
  224. * void the misleading of "2" > "12".
  225. * + Should avoid the misleading case:
  226. * `" 22 " gte "22"` is `true` but `" 22 " eq "22"` is `false`.
  227. * + JS bad case should be avoided: null <= 0, [] <= 0, ' ' <= 0, ...
  228. * + Only "numeric" can be converted to comparable number, otherwise converted to NaN.
  229. * See `util/number.ts#numericToNumber`.
  230. *
  231. * @return If `op` is not `RelationalOperator`, return null;
  232. */
  233. export function createFilterComparator(op, rval) {
  234. return op === 'eq' || op === 'ne' ? new FilterEqualityComparator(op === 'eq', rval) : hasOwn(ORDER_COMPARISON_OP_MAP, op) ? new FilterOrderComparator(op, rval) : null;
  235. }