Skip to content

Commit 67fd886

Browse files
committed
extract function
1 parent 9fa8f49 commit 67fd886

File tree

1 file changed

+98
-42
lines changed

1 file changed

+98
-42
lines changed

src/lib/unpackPlotProps.js

Lines changed: 98 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,103 @@ import nestedProperty from 'plotly.js/src/lib/nested_property';
22
import isNumeric from 'fast-isnumeric';
33
import {MULTI_VALUED, MULTI_VALUED_PLACEHOLDER} from './constants';
44

5+
export function computeVisibility(props, fullValue, customConfig) {
6+
let isVisible = false;
7+
8+
const hasFullValue = () => fullValue !== void 0 && fullValue !== null;
9+
10+
const isValidCustomConfigObject = customConfig => {
11+
if (customConfig.visibility_rules) {
12+
const logRuleError = () => {
13+
console.error(
14+
'invalid customConfig: all customConfig visibility rules must include type and regex_match keys.'
15+
);
16+
};
17+
18+
if (
19+
(customConfig.visibility_rules.blacklist &&
20+
customConfig.visibility_rules.blacklist.some(r => !r.type || !r.regex_match)) ||
21+
(customConfig.visibility_rules.whitelist &&
22+
customConfig.visibility_rules.whitelist.some(r => !r.type || !r.regex_match))
23+
) {
24+
logRuleError();
25+
return false;
26+
}
27+
28+
if (
29+
customConfig.visibility_rules.order &&
30+
Array.isArray(customConfig.visibility_rules.order) &&
31+
customConfig.visibility_rules.order.some(o => !['blacklist', 'whitelist'].includes(o))
32+
) {
33+
console.error(
34+
"invalid customConfig: only 2 values are accepted in the visibility_rules.order array: 'blacklist' and 'whitelist'"
35+
);
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
return false;
42+
};
43+
44+
if (
45+
customConfig &&
46+
customConfig === Object(customConfig) &&
47+
Object.keys(customConfig).length &&
48+
isValidCustomConfigObject(customConfig)
49+
) {
50+
const matches = o => {
51+
if (customConfig.visibility_rules[o]) {
52+
return customConfig.visibility_rules[o].some(rule => {
53+
const regex = RegExp(rule.regex_match);
54+
return rule.type === 'attrName' && regex.test(props.attr);
55+
});
56+
}
57+
return false;
58+
};
59+
60+
if (customConfig.visibility_rules.blacklist && customConfig.visibility_rules.whitelist) {
61+
// Look to see if there was an order to blacklisting or whitelisting specified
62+
const order =
63+
customConfig.visibility_rules.order && customConfig.visibility_rules.order.length
64+
? customConfig.visibility_rules.order
65+
: ['blacklist', 'whitelist'];
66+
67+
if (order[0] === 'blacklist') {
68+
const shouldBlacklist = matches('blacklist') && !matches('whitelist');
69+
if (!shouldBlacklist && hasFullValue()) {
70+
isVisible = true;
71+
}
72+
}
73+
74+
if (order[0] === 'whitelist') {
75+
const shouldWhitelist = matches('whitelist') && !matches('blacklist');
76+
if (shouldWhitelist && hasFullValue()) {
77+
isVisible = true;
78+
}
79+
}
80+
} else {
81+
if (customConfig.visibility_rules.blacklist) {
82+
if (!matches('blacklist') && hasFullValue()) {
83+
isVisible = true;
84+
}
85+
}
86+
87+
if (customConfig.visibility_rules.whitelist) {
88+
if (matches('whitelist') && hasFullValue()) {
89+
isVisible = true;
90+
}
91+
}
92+
}
93+
} else {
94+
if (props.show || hasFullValue()) {
95+
isVisible = true;
96+
}
97+
}
98+
99+
return isVisible;
100+
}
101+
5102
export default function unpackPlotProps(props, context) {
6103
const {container, getValObject, defaultContainer, updateContainer, customConfig} = context;
7104

@@ -28,48 +125,7 @@ export default function unpackPlotProps(props, context) {
28125
multiValued = true;
29126
}
30127

31-
let isVisible = false;
32-
// Base visibility rules
33-
if (props.show || (fullValue !== void 0 && fullValue !== null)) {
34-
isVisible = true;
35-
}
36-
// We can override base visibility rules with a customConfig
37-
if (
38-
customConfig &&
39-
customConfig === Object(customConfig) &&
40-
Object.keys(customConfig).length &&
41-
customConfig.visibility_rules
42-
) {
43-
// Look to see if there was an order to blacklisting or whitelisting specified
44-
const order =
45-
customConfig.visibility_rules.order &&
46-
Array.isArray(customConfig.visibility_rules.order) &&
47-
customConfig.visibility_rules.order.length
48-
? customConfig.visibility_rules.order
49-
: ['blacklist', 'whitelist'];
50-
51-
order.forEach(o => {
52-
if (customConfig.visibility_rules[o]) {
53-
customConfig.visibility_rules[o].forEach(r => {
54-
if (r.type && r.regex_match) {
55-
const regex = RegExp(r.regex_match);
56-
if (o === 'blacklist' && r.type === 'attrName' && regex.test(props.attr)) {
57-
isVisible = false;
58-
}
59-
if (o === 'whitelist' && r.type === 'attrName' && regex.test(props.attr)) {
60-
isVisible = true;
61-
}
62-
} else {
63-
throw new Error(
64-
`sorry, you must include the type and regex_match in each of your blacklisting and whitelisting customConfig rules`
65-
);
66-
}
67-
});
68-
} else {
69-
throw new Error(`sorry, you have an invalid key of ${o} in your customConfig prop`);
70-
}
71-
});
72-
}
128+
const isVisible = computeVisibility(props, fullValue, customConfig);
73129

74130
let defaultValue = props.defaultValue;
75131
if (defaultValue === void 0 && defaultContainer) {

0 commit comments

Comments
 (0)