-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (61 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const React = require("react");
const Cookies = require("js-cookie");
const FEATURE_PREFIX = "f_";
const setFeatureCookie = (param) => {
if (!param.includes('=')) {
Cookies.set(param, true);
}
const featureName = param.split('=')[0];
const featureValue = param.split('=')[1];
if (featureValue === '_') {
Cookies.remove(featureName);
} else {
Cookies.set(featureName, featureValue);
}
}
const getFeaturesFromUrl = () => {
const urlParams = window.location.search.substr(1);
if (urlParams.includes(FEATURE_PREFIX)) {
if (urlParams.includes("&")) {
urlParams
.split("&")
.filter(param => param.includes(FEATURE_PREFIX))
.map(param => featureService.setFeatureCookie(param));
} else {
featureService.setFeatureCookie(urlParams);
}
}
return false;
}
const getFeaturesFromCookie = () => {
return document.cookie
.split(";")
.map(cookie => cookie.trim())
.map(cookie => cookie.split("="))
.reduce(function (acc, val) {
if (val[0] !== '' && val[0].slice(0, FEATURE_PREFIX.length) === FEATURE_PREFIX) {
const currKey = val[0].substr(FEATURE_PREFIX.length);
acc[currKey] = val[1] === "false" ? false : val[1] === "true" ? true : val[1];
}
return acc;
}, {});
}
const getFeatures = () => {
getFeaturesFromUrl();
return getFeaturesFromCookie();
}
const featureService = {
setFeatureCookie,
getFeaturesFromUrl,
getFeaturesFromCookie,
getFeatures
};
const FeatureContext = React.createContext(null);
const FeatureProvider = ({children}) => React.createElement(
FeatureContext.Provider,
{value: getFeatures()},
children
);
module.exports = FeatureContext;
module.exports.FeatureProvider = FeatureProvider;
module.exports.featureService = featureService;