dataValueHelper.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. // --------- START: Parsers --------
  44. /**
  45. * Convert raw the value in to inner value in List.
  46. *
  47. * [Performance sensitive]
  48. *
  49. * [Caution]: this is the key logic of user value parser.
  50. * For backward compatibility, do not modify it until you have to!
  51. */
  52. export function parseDataValue(value,
  53. // For high performance, do not omit the second param.
  54. opt) {
  55. // Performance sensitive.
  56. var dimType = opt && opt.type;
  57. if (dimType === 'ordinal') {
  58. // If given value is a category string
  59. return value;
  60. }
  61. if (dimType === 'time'
  62. // spead up when using timestamp
  63. && !isNumber(value) && value != null && value !== '-') {
  64. value = +parseDate(value);
  65. }
  66. // dimType defaults 'number'.
  67. // If dimType is not ordinal and value is null or undefined or NaN or '-',
  68. // parse to NaN.
  69. // number-like string (like ' 123 ') can be converted to a number.
  70. // where null/undefined or other string will be converted to NaN.
  71. return value == null || value === '' ? NaN
  72. // If string (like '-'), using '+' parse to NaN
  73. // If object, also parse to NaN
  74. : Number(value);
  75. }
  76. ;
  77. var valueParserMap = createHashMap({
  78. 'number': function (val) {
  79. // Do not use `numericToNumber` here. We have `numericToNumber` by default.
  80. // Here the number parser can have loose rule:
  81. // enable to cut suffix: "120px" => 120, "14%" => 14.
  82. return parseFloat(val);
  83. },
  84. 'time': function (val) {
  85. // return timestamp.
  86. return +parseDate(val);
  87. },
  88. 'trim': function (val) {
  89. return isString(val) ? trim(val) : val;
  90. }
  91. });
  92. export function getRawValueParser(type) {
  93. return valueParserMap.get(type);
  94. }
  95. var ORDER_COMPARISON_OP_MAP = {
  96. lt: function (lval, rval) {
  97. return lval < rval;
  98. },
  99. lte: function (lval, rval) {
  100. return lval <= rval;
  101. },
  102. gt: function (lval, rval) {
  103. return lval > rval;
  104. },
  105. gte: function (lval, rval) {
  106. return lval >= rval;
  107. }
  108. };
  109. var FilterOrderComparator = /** @class */function () {
  110. function FilterOrderComparator(op, rval) {
  111. if (!isNumber(rval)) {
  112. var errMsg = '';
  113. if (process.env.NODE_ENV !== 'production') {
  114. errMsg = 'rvalue of "<", ">", "<=", ">=" can only be number in filter.';
  115. }
  116. throwError(errMsg);
  117. }
  118. this._opFn = ORDER_COMPARISON_OP_MAP[op];
  119. this._rvalFloat = numericToNumber(rval);
  120. }
  121. // Performance sensitive.
  122. FilterOrderComparator.prototype.evaluate = function (lval) {
  123. // Most cases is 'number', and typeof maybe 10 times faseter than parseFloat.
  124. return isNumber(lval) ? this._opFn(lval, this._rvalFloat) : this._opFn(numericToNumber(lval), this._rvalFloat);
  125. };
  126. return FilterOrderComparator;
  127. }();
  128. var SortOrderComparator = /** @class */function () {
  129. /**
  130. * @param order by default: 'asc'
  131. * @param incomparable by default: Always on the tail.
  132. * That is, if 'asc' => 'max', if 'desc' => 'min'
  133. * See the definition of "incomparable" in [SORT_COMPARISON_RULE].
  134. */
  135. function SortOrderComparator(order, incomparable) {
  136. var isDesc = order === 'desc';
  137. this._resultLT = isDesc ? 1 : -1;
  138. if (incomparable == null) {
  139. incomparable = isDesc ? 'min' : 'max';
  140. }
  141. this._incomparable = incomparable === 'min' ? -Infinity : Infinity;
  142. }
  143. // See [SORT_COMPARISON_RULE].
  144. // Performance sensitive.
  145. SortOrderComparator.prototype.evaluate = function (lval, rval) {
  146. // Most cases is 'number', and typeof maybe 10 times faseter than parseFloat.
  147. var lvalFloat = isNumber(lval) ? lval : numericToNumber(lval);
  148. var rvalFloat = isNumber(rval) ? rval : numericToNumber(rval);
  149. var lvalNotNumeric = isNaN(lvalFloat);
  150. var rvalNotNumeric = isNaN(rvalFloat);
  151. if (lvalNotNumeric) {
  152. lvalFloat = this._incomparable;
  153. }
  154. if (rvalNotNumeric) {
  155. rvalFloat = this._incomparable;
  156. }
  157. if (lvalNotNumeric && rvalNotNumeric) {
  158. var lvalIsStr = isString(lval);
  159. var rvalIsStr = isString(rval);
  160. if (lvalIsStr) {
  161. lvalFloat = rvalIsStr ? lval : 0;
  162. }
  163. if (rvalIsStr) {
  164. rvalFloat = lvalIsStr ? rval : 0;
  165. }
  166. }
  167. return lvalFloat < rvalFloat ? this._resultLT : lvalFloat > rvalFloat ? -this._resultLT : 0;
  168. };
  169. return SortOrderComparator;
  170. }();
  171. export { SortOrderComparator };
  172. var FilterEqualityComparator = /** @class */function () {
  173. function FilterEqualityComparator(isEq, rval) {
  174. this._rval = rval;
  175. this._isEQ = isEq;
  176. this._rvalTypeof = typeof rval;
  177. this._rvalFloat = numericToNumber(rval);
  178. }
  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 makes sense when we 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. * avoid 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. }
  236. /**
  237. * @usage
  238. * const filterParsed = parseSanitizationFilter(filter);
  239. * for( ... ) {
  240. * const val = ...;
  241. * if (!filter || passesFilter(filterParsed, val)) {
  242. * // normal handling
  243. * }
  244. * }
  245. */
  246. export function parseSanitizationFilter(filter) {
  247. var filterKey = '';
  248. var filterG = -Infinity;
  249. var filterGE = -Infinity;
  250. var filterL = Infinity;
  251. var filterLE = Infinity;
  252. if (filter) {
  253. if (filter.g != null) {
  254. filterKey += 'G' + filter.g;
  255. filterG = filter.g;
  256. }
  257. if (filter.ge != null) {
  258. filterKey += 'GE' + filter.ge;
  259. filterGE = filter.ge;
  260. }
  261. if (filter.l != null) {
  262. filterKey += 'L' + filter.l;
  263. filterL = filter.l;
  264. }
  265. if (filter.le != null) {
  266. filterKey += 'LE' + filter.le;
  267. filterLE = filter.le;
  268. }
  269. }
  270. return {
  271. key: filterKey,
  272. g: filterG,
  273. ge: filterGE,
  274. l: filterL,
  275. le: filterLE
  276. };
  277. }
  278. export function passesSanitizationFilter(filterParsed, value) {
  279. return value > filterParsed.g && value >= filterParsed.ge && value < filterParsed.l && value <= filterParsed.le;
  280. }
  281. // --------- END: Data store sanitization filters ---------