1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Filter = void 0;
|
4 | const util_1 = require("@antv/util");
|
5 | const helper_1 = require("../utils/helper");
|
6 | const helper_2 = require("./utils/helper");
|
7 | function normalizeValue(value) {
|
8 | if (typeof value === 'object')
|
9 | return [value.value, value.ordinal];
|
10 | else
|
11 | return [value, true];
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 | const Filter = (options = {}) => {
|
17 | return (I, mark) => {
|
18 | const { encode, data } = mark;
|
19 | const filters = Object.entries(options)
|
20 | .map(([key, v]) => {
|
21 | const [V] = (0, helper_2.columnOf)(encode, key);
|
22 |
|
23 | if (!V)
|
24 | return null;
|
25 | const [value, ordinal = true] = normalizeValue(v);
|
26 | if (typeof value === 'function')
|
27 | return (i) => value(V[i]);
|
28 | if (ordinal) {
|
29 | const expectedValues = Array.isArray(value) ? value : [value];
|
30 |
|
31 | if (expectedValues.length === 0)
|
32 | return null;
|
33 | return (i) => expectedValues.includes(V[i]);
|
34 | }
|
35 | else {
|
36 | const [start, end] = value;
|
37 | return (i) => V[i] >= start && V[i] <= end;
|
38 | }
|
39 | })
|
40 | .filter(helper_1.defined);
|
41 |
|
42 | if (filters.length === 0)
|
43 | return [I, mark];
|
44 |
|
45 | const totalFilter = (i) => filters.every((f) => f(i));
|
46 | const FI = I.filter(totalFilter);
|
47 | const newIndex = FI.map((_, i) => i);
|
48 | const newEncodes = Object.entries(encode).map(([key, encode]) => {
|
49 | return [
|
50 | key,
|
51 | Object.assign(Object.assign({}, encode), { value: newIndex
|
52 | .map((i) => encode.value[FI[i]])
|
53 | .filter((v) => v !== undefined) }),
|
54 | ];
|
55 | });
|
56 | return [
|
57 | newIndex,
|
58 | (0, util_1.deepMix)({}, mark, {
|
59 | encode: Object.fromEntries(newEncodes),
|
60 |
|
61 | data: FI.map((i) => data[i]),
|
62 | }),
|
63 | ];
|
64 | };
|
65 | };
|
66 | exports.Filter = Filter;
|
67 | exports.Filter.props = {};
|
68 |
|
\ | No newline at end of file |