feature-toggle-api 4.1.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +14 -16
  2. package/dist/feature-toggle.js +227 -385
  3. package/dist/feature-toggle.js.map +1 -0
  4. package/dist/featureToggle.d.ts +3 -0
  5. package/dist/html-plugin.js +42 -0
  6. package/dist/html-plugin.js.map +1 -0
  7. package/dist/index.d.ts +5 -0
  8. package/dist/plugins/htmlplugin/plugin-html.d.ts +15 -0
  9. package/dist/plugins/urlplugin/plugin-url.d.ts +14 -0
  10. package/{src/types.ts → dist/types.d.ts} +44 -64
  11. package/dist/url-plugin.js +40 -0
  12. package/dist/url-plugin.js.map +1 -0
  13. package/dist/vite.config.d.ts +2 -0
  14. package/package.json +26 -11
  15. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -35
  16. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
  17. package/dist/feature-toggle.d.ts +0 -130
  18. package/dist/feature-toggle.min.cjs +0 -2
  19. package/dist/feature-toggle.min.cjs.map +0 -1
  20. package/dist/feature-toggle.min.js +0 -2
  21. package/dist/feature-toggle.min.js.map +0 -1
  22. package/dist/feature-toggle.umd.min.js +0 -2
  23. package/dist/feature-toggle.umd.min.js.map +0 -1
  24. package/dist/html-plugin.umd.min.js +0 -2
  25. package/dist/html-plugin.umd.min.js.map +0 -1
  26. package/dist/url-plugin.umd.min.js +0 -2
  27. package/dist/url-plugin.umd.min.js.map +0 -1
  28. package/docs/_config.yml +0 -1
  29. package/docs/example-intro.html +0 -75
  30. package/docs/example-setup.html +0 -55
  31. package/docs/example-visibility-basic.html +0 -144
  32. package/docs/readme.md +0 -628
  33. package/docs/vue-feature-toggle.min.js +0 -902
  34. package/examples/01_basic_esmodule.js +0 -10
  35. package/examples/02_basic_cjsmodule.cjs +0 -6
  36. package/examples/03_basic_scripttag.html +0 -24
  37. package/examples/04_example-htmlplugin.html +0 -35
  38. package/examples/05_example-urlplugin.html +0 -25
  39. package/examples/06_withListener.html +0 -21
  40. package/rollup.config.js +0 -107
  41. package/src/featureToggle.ts +0 -352
  42. package/src/index.ts +0 -13
  43. package/src/plugins/htmlplugin/plugin-html.ts +0 -83
  44. package/src/plugins/htmlplugin/readme.md +0 -221
  45. package/src/plugins/urlplugin/plugin-url.ts +0 -90
  46. package/src/plugins/urlplugin/readme.md +0 -92
  47. package/tests/featuretoggle.test.ts +0 -450
  48. package/tests/polyfills.ts +0 -20
  49. package/tsconfig.cjs.json +0 -12
  50. package/tsconfig.json +0 -12
@@ -1,397 +1,239 @@
1
- function useWindowMock() {
2
- return {
3
- isMocked: true,
4
- decodeURIComponent: function (param1) {
5
- return param1;
6
- }
7
- };
1
+ //#region src/plugins/urlplugin/plugin-url.ts
2
+ function e() {
3
+ return {
4
+ isMocked: !0,
5
+ decodeURIComponent: function(e) {
6
+ return e;
7
+ }
8
+ };
8
9
  }
9
- function parseValue(value) {
10
- if (value === 'true')
11
- return true;
12
- return false;
10
+ function t(e) {
11
+ return e === "true";
13
12
  }
14
- function getParams(url, window) {
15
- var params = {};
16
- if (!url)
17
- return [];
18
- var urlparts = url.split("?");
19
- //no params available for url
20
- if (urlparts.length < 2)
21
- return [];
22
- const parts = urlparts[1].split('&');
23
- parts.forEach(function (part) {
24
- var pair = part.split('=');
25
- pair[0] = window.decodeURIComponent(pair[0]);
26
- pair[1] = parseValue(window.decodeURIComponent(pair[1]));
27
- params[pair[0]] = (pair[1] !== 'undefined') ?
28
- pair[1] : true;
29
- });
30
- return params;
13
+ function n(e, n) {
14
+ var r = {};
15
+ if (!e) return [];
16
+ var i = e.split("?");
17
+ return i.length < 2 ? [] : (i[1].split("&").forEach(function(e) {
18
+ var i = e.split("=");
19
+ i[0] = n.decodeURIComponent(i[0]), i[1] = t(n.decodeURIComponent(i[1])), r[i[0]] = i[1] === "undefined" || i[1];
20
+ }), r);
31
21
  }
32
- function urlPlugin(config = {}) {
33
- let _window;
34
- if (config.useMockedWindow)
35
- _window = useWindowMock();
36
- else
37
- _window = window;
38
- config = Object.assign({}, {
39
- url: _window.isMocked ? "" : _window.location.href,
40
- prefix: ""
41
- }, config);
42
- return function (api) {
43
- api.url = config.url;
44
- const urlparams = getParams(config.url, _window);
45
- const prefix = config.prefix;
46
- Object.keys(urlparams).forEach(key => {
47
- if (!key.startsWith(prefix))
48
- return;
49
- const keyWithoutPrefix = key.replace(prefix, "");
50
- api.visibility(keyWithoutPrefix, urlparams[key]);
51
- });
52
- return { name: 'urlplugin' };
53
- };
22
+ function r(t = {}) {
23
+ let r;
24
+ return r = t.useMockedWindow ? e() : window, t = Object.assign({}, {
25
+ url: r.isMocked ? "" : r.location.href,
26
+ prefix: ""
27
+ }, t), function(e) {
28
+ e.url = t.url;
29
+ let i = n(t.url, r), a = t.prefix;
30
+ return Object.keys(i).forEach((t) => {
31
+ if (!t.startsWith(a)) return;
32
+ let n = t.replace(a, "");
33
+ e.visibility(n, i[t]);
34
+ }), { name: "urlplugin" };
35
+ };
54
36
  }
55
-
56
- /*
57
- creates a tag that is shown / hidden, depending on the visibility rules.
58
- if not configured dynamically, it looks like this:
59
- <feature name="featurename" variant="variantname" data="data">content </feature>
60
-
61
- Parameter: (config)
62
- - config.featureTagName: name of the tag. Default: "feature"
63
- - nameAttributeName: Name of the Name-Attribute: default: "name"
64
- - variantAttributeName: Name of the Variant-Attribute: default: "variant"
65
- */
66
- const defaultparams = {
67
- renderedTag: 'div',
68
- featureTagName: 'feature',
69
- tagAttributeName: 'tag',
70
- nameAttributeName: 'name',
71
- variantAttributeName: 'variant',
72
- dataAttributeName: 'data',
73
- displayAttributeName: 'display',
74
- defaultDisplay: 'block',
37
+ //#endregion
38
+ //#region src/plugins/htmlplugin/plugin-html.ts
39
+ var i = {
40
+ renderedTag: "div",
41
+ featureTagName: "feature",
42
+ tagAttributeName: "tag",
43
+ nameAttributeName: "name",
44
+ variantAttributeName: "variant",
45
+ dataAttributeName: "data",
46
+ displayAttributeName: "display",
47
+ defaultDisplay: "block"
75
48
  };
76
- function parseDataAttribute(attrAsString) {
77
- try {
78
- return JSON.parse(attrAsString);
79
- }
80
- catch (e) {
81
- if (!isNaN(parseFloat(attrAsString)))
82
- return parseFloat(attrAsString);
83
- return attrAsString;
84
- }
49
+ function a(e) {
50
+ try {
51
+ return JSON.parse(e);
52
+ } catch {
53
+ return isNaN(parseFloat(e)) ? e : parseFloat(e);
54
+ }
85
55
  }
86
- function htmlPlugin(config = {}) {
87
- config = Object.assign({}, defaultparams, config);
88
- function renderFeatureTag(elem, isVisible) {
89
- const tagname = elem.getAttribute(config.tagAttributeName) || config.renderedTag;
90
- const attributes = Array.from(elem.attributes);
91
- let attributesAsString = "";
92
- attributes.forEach(attr => {
93
- attributesAsString += ` ${attr.nodeName}="${attr.nodeValue.replace(/"/g, "&quot;")}"`;
94
- });
95
- const display = isVisible ? (elem.getAttribute(config.displayAttributeName) || config.defaultDisplay) : 'none';
96
- elem.outerHTML = `<${tagname} style="display:${display}" _feature="true" ${attributesAsString}>${elem.innerHTML}</${tagname}>`;
97
- }
98
- return function (api) {
99
- var renderedTags = window.document.querySelectorAll(config.featureTagName);
100
- renderedTags.forEach(tag => { renderFeatureTag(tag, false); });
101
- api.on('visibilityrule', function (event) {
102
- var selector = `[_feature][${config.nameAttributeName}="${event.name}"]`;
103
- if (event.variant)
104
- selector += `[${config.variantAttributeName}="${event.variant}"]`;
105
- var elements = document.querySelectorAll(selector);
106
- elements.forEach(elem => {
107
- const dataAsString = elem.getAttribute(config.dataAttributeName);
108
- const data = parseDataAttribute(dataAsString);
109
- const isVisible = api.isVisible(event.name, event.variant, data);
110
- renderFeatureTag(elem, isVisible);
111
- });
112
- });
113
- return { name: 'htmlplugin' };
114
- };
56
+ function o(e = {}) {
57
+ e = Object.assign({}, i, e);
58
+ function t(t, n) {
59
+ let r = t.getAttribute(e.tagAttributeName) || e.renderedTag, i = Array.from(t.attributes), a = "";
60
+ i.forEach((e) => {
61
+ a += ` ${e.nodeName}="${e.nodeValue.replace(/"/g, "&quot;")}"`;
62
+ }), t.outerHTML = `<${r} style="display:${n ? t.getAttribute(e.displayAttributeName) || e.defaultDisplay : "none"}" _feature="true" ${a}>${t.innerHTML}</${r}>`;
63
+ }
64
+ return function(n) {
65
+ return window.document.querySelectorAll(e.featureTagName).forEach((e) => {
66
+ t(e, !1);
67
+ }), n.on("visibilityrule", function(r) {
68
+ var i = `[_feature][${e.nameAttributeName}="${r.name}"]`;
69
+ r.variant && (i += `[${e.variantAttributeName}="${r.variant}"]`), document.querySelectorAll(i).forEach((i) => {
70
+ let o = a(i.getAttribute(e.dataAttributeName));
71
+ t(i, n.isVisible(r.name, r.variant, o));
72
+ });
73
+ }), { name: "htmlplugin" };
74
+ };
115
75
  }
116
-
117
- function parseToFn(fnOrBool) {
118
- if (typeof fnOrBool == 'boolean')
119
- return function () { return fnOrBool; };
120
- return fnOrBool;
76
+ //#endregion
77
+ //#region src/featureToggle.ts
78
+ function s(e) {
79
+ return typeof e == "boolean" ? function() {
80
+ return e;
81
+ } : e;
121
82
  }
122
- function getKey(name, variant) {
123
- var _name = name.toLowerCase();
124
- if (variant && typeof variant == 'string') {
125
- _name += "#" + variant.toLowerCase();
126
- }
127
- return _name;
83
+ function c(e, t) {
84
+ let n = e.toLowerCase();
85
+ return t && typeof t == "string" && (n += `#${t.toLowerCase()}`), n;
128
86
  }
129
- function initVisibilities(visibilities = {}) {
130
- const returnVisibilities = {};
131
- Object.keys(visibilities).forEach(key => {
132
- if (key.startsWith('_') || key.startsWith('$'))
133
- return;
134
- returnVisibilities[getKey(key)] = parseToFn(visibilities[key]);
135
- });
136
- return returnVisibilities;
87
+ function l(e = {}) {
88
+ let t = {};
89
+ return Object.entries(e).forEach(([e, n]) => {
90
+ e.startsWith("_") || e.startsWith("$") || (t[c(e)] = s(n));
91
+ }), t;
137
92
  }
138
- function useFeatureToggle(config = {}) {
139
- const globals = {
140
- datas: {},
141
- listeners: {},
142
- visibilities: initVisibilities(config),
143
- showLogs: false,
144
- usedPlugins: [],
145
- };
146
- function init(api) {
147
- if (config.$default) {
148
- api.setDefaultFlag(config.$default);
149
- }
150
- if (config.$required) {
151
- api.setRequiredFlag(config.$required);
152
- }
153
- const allPlugins = [...(config.$plugins || []), ...(config._plugins || [])];
154
- if (config._plugins) {
155
- console.log('useFeatureToggle({_plugins:[]}): Key _plugins is deprecated. Use $plugins instead. This attribute will be removed in one of the next major versions.');
156
- }
157
- if (allPlugins.length) {
158
- allPlugins.forEach(plugin => {
159
- if (typeof plugin !== 'function')
160
- throw new Error('featuretoggleapi()-constructor: config.plugins needs functions as entries, not ' + typeof plugin + '.');
161
- api.addPlugin(plugin);
162
- });
163
- }
164
- triggerEvent('init');
165
- }
166
- function triggerEvent(eventtype, param) {
167
- (globals.listeners[eventtype] || []).forEach(listener => {
168
- listener(param);
169
- });
170
- }
171
- const log = function (message) {
172
- if (!globals.showLogs)
173
- return;
174
- //Nur Browser können Syntaxhighlighting die anderen geben die Nachricht einfach aus und schneiden
175
- //die styletags raus
176
- if (typeof window === 'undefined') {
177
- const loggedMessage = message.replace(/<b>/g, "");
178
- console.log(loggedMessage);
179
- return;
180
- }
181
- var hasBoldTag = message.indexOf('<b>') != -1;
182
- var hasVisibleKeyword = message.indexOf('visible') != -1;
183
- var hasHiddenKeyword = message.indexOf('hidden') != -1;
184
- var _message = message.replace('visible', '%cvisible');
185
- _message = _message.replace('hidden', '%chidden');
186
- if (hasVisibleKeyword)
187
- console.log(_message, "color:green;font-weight:bold;");
188
- else if (hasHiddenKeyword)
189
- console.log(_message, "color:red;font-weight:bold;");
190
- else if (hasBoldTag) {
191
- _message = _message.replace('<b>', '%c');
192
- var parts = [_message, 'font-weight:bold;'];
193
- console.log.apply(null, parts);
194
- }
195
- else
196
- console.log(message);
197
- };
198
- const logAndReturn = function (returnValue, message) {
199
- log(message);
200
- log('');
201
- return returnValue;
202
- };
203
- const getVisibility = function (visibilityFn, functionname, name, variant, data) {
204
- if (visibilityFn == null)
205
- return undefined;
206
- var calculatedVisibility = visibilityFn({ name: name, variant: variant, data: data });
207
- if (typeof calculatedVisibility == 'boolean') {
208
- return calculatedVisibility;
209
- }
210
- return logAndReturn(false, `The ${functionname} returns ${calculatedVisibility}. => Please return true or false. This result (and all non-boolean results) will return false.`);
211
- };
212
- function parseKey(key) {
213
- const parts = key.split('#');
214
- return {
215
- name: parts[0],
216
- variant: parts.length > 1 ? parts[1] : undefined,
217
- data: globals.datas[key],
218
- };
219
- }
220
- /*
221
- the following calls are possible:
222
- visibility(name,result);
223
- visibility(name,variant,result);
224
- visibility(name,variant,data,result);
225
-
226
- =>
227
- param1: name
228
- param2: result || variant
229
- param3: result || data
230
- param4: result
231
- */
232
- function visibilityFnParams(param1, param2, param3, param4) {
233
- //name must always be set
234
- if (param1 == undefined)
235
- throw new Error('feature.visibility(): 1st parameter name must be defined');
236
- if (arguments.length == 1)
237
- throw new Error('feature.visibility(): 2nd parameter name must be a boolean or function, but is empty');
238
- let name = param1, variant = null, data = null, result = null;
239
- if (param3 == undefined && param4 == undefined) {
240
- result = param2;
241
- }
242
- else if (param4 == undefined) {
243
- variant = param2;
244
- result = param3;
245
- }
246
- else {
247
- variant = param2;
248
- data = param3;
249
- result = param4;
250
- }
251
- return {
252
- name,
253
- variant,
254
- data,
255
- result
256
- };
257
- }
258
- function getEvent(name, variant, data, result) {
259
- let event;
260
- event = { name, variant, data };
261
- event.key = getKey(event.name, event.variant);
262
- if (result == null)
263
- return event;
264
- event.visibilityFunction = parseToFn(result);
265
- event.result = event.visibilityFunction({
266
- name: event.name,
267
- variant: event.variant,
268
- data: event.data || {},
269
- _internalCall: true,
270
- description: 'When attaching a function, the result must be calculated internally. You can filter this out with the _internalCall:true -Flag.'
271
- });
272
- return event;
273
- }
274
- function isActive(name, variant, data) {
275
- const visibilities = globals.visibilities;
276
- log(`\nCheck Visibility of <b>Feature "${name}", variant "${variant == undefined ? '' : variant}"${data ? " with data " + JSON.stringify(data) : ""}.`);
277
- if (name == undefined)
278
- throw new Error('The attribute "name" is required for tag <feature></feature>. Example: <feature name="aname"></feature>');
279
- var requiredFn = visibilities['_required'];
280
- var requiredFnExists = visibilities['_required'] != null;
281
- var requiredFnResult = getVisibility(requiredFn, 'requiredVisibility', name, variant, data);
282
- var visibilityFnKey = getKey(name, variant);
283
- var visibilityFn = visibilities[visibilityFnKey];
284
- var visibilityFnExists = visibilities[visibilityFnKey] != null;
285
- var visibilityFnResult = getVisibility(visibilityFn, 'visibility function', name, variant, data);
286
- var variantExists = variant != null;
287
- var visibilityOnlyNameFnKey = getKey(name, null);
288
- var visibilityOnlyNameFn = visibilities[visibilityOnlyNameFnKey];
289
- var visibilityOnlyNameFnResult = getVisibility(visibilityOnlyNameFn, 'visibility function (only name)', name, variant, data);
290
- var defaultFn = visibilities['_default'];
291
- var defaultFnExists = visibilities['_default'] != null;
292
- var defaultFnResult = getVisibility(defaultFn, 'defaultVisibility', name, variant, data);
293
- if (!requiredFnExists)
294
- log("No requiredVisibility rule specified for this feature.");
295
- else if (requiredFnExists && requiredFnResult === true)
296
- log("The requiredVisibility rule returns true. This feature will be shown when no other rule rejects it.");
297
- else if (requiredFnExists && requiredFnResult === false)
298
- return logAndReturn(false, "The requiredVisibility rule returns false. This feature will be hidden.");
299
- if (visibilityFnExists)
300
- return logAndReturn(visibilityFnResult, `The visibility rule returns ${visibilityFnResult}. This feature will be ${visibilityFnResult ? 'visible' : 'hidden'}.`);
301
- log('No visibility rule found matching name and variant.');
302
- if (variantExists && typeof visibilityOnlyNameFnResult == 'boolean')
303
- return logAndReturn(visibilityOnlyNameFnResult, `Found a visibility rule for name ${name} without variants. The rule returns ${visibilityOnlyNameFnResult}. => This feature will be ${visibilityOnlyNameFnResult ? 'visible' : 'hidden'}.`);
304
- else if (variantExists)
305
- log(`No rules found for name ${name} without variants.`);
306
- if (defaultFnExists)
307
- return logAndReturn(defaultFnResult, `Found a defaultVisibility rule. The rule returns ${defaultFnResult}. => This feature will be ${defaultFnResult ? 'visible' : 'hidden'}.`);
308
- log(`No default rule found.`);
309
- if (requiredFnExists)
310
- return logAndReturn(true, `Only the requiredVisibility rule was found. This returned true. => This feature will be visible.`);
311
- return logAndReturn(false, 'No rules were found. This feature will be hidden.');
312
- }
313
- const api = {
314
- name: 'feature-toggle-api',
315
- setData: function (nameParam, variantOrDataParam, dataParam) {
316
- if (nameParam == undefined)
317
- throw new Error('setData(): The name must of the feature must be defined, but ist undefined');
318
- const variant = (dataParam != undefined ? variantOrDataParam : undefined);
319
- const data = dataParam || variantOrDataParam;
320
- const event = getEvent(nameParam, variant, data);
321
- globals.datas[event.key] = event.data;
322
- triggerEvent('visibilityrule', event);
323
- },
324
- on(eventtype, fn, config) {
325
- globals.listeners[eventtype] = globals.listeners[eventtype] || [];
326
- globals.listeners[eventtype].push(fn);
327
- triggerEvent('registerEvent', {
328
- type: eventtype
329
- });
330
- if (config != undefined && config.ignorePreviousRules)
331
- return;
332
- Object.keys(globals.visibilities).forEach(key => {
333
- const event = parseKey(key);
334
- const rule = globals.visibilities[key];
335
- event.result = rule(event);
336
- fn(event);
337
- });
338
- },
339
- trigger: triggerEvent,
340
- showLogs: function (showLogs) {
341
- globals.showLogs = showLogs == undefined ? true : showLogs;
342
- },
343
- isVisible(name, variant, data) {
344
- console.log('featureToggle.isVisible is deprecated. use featureToggle.isActive instead. This function will be removed in one of the next major versions.');
345
- return isActive(name, variant, data);
346
- },
347
- isActive,
348
- /**
349
- the following function calls are possible:
350
- visibility(name,result);
351
- visibility(name,variant,result);
352
- visibility(name,variant,data,result);
353
- */
354
- setFlag(name, resultOrVariant, resultOrData, result) {
355
- const params = visibilityFnParams(name, resultOrVariant, resultOrData, result);
356
- const event = getEvent(params.name, params.variant, params.data, params.result);
357
- globals.visibilities[event.key] = event.visibilityFunction;
358
- globals.datas[event.key] = event.data;
359
- triggerEvent('visibilityrule', event);
360
- },
361
- visibility: function (name, resultOrVariant, resultOrData, result) {
362
- console.log('featureToggle.visibility is deprecated. use featureToggle.setVisibility instead. This function will be removed in one of the next major versions.');
363
- api.setFlag(name, resultOrVariant, resultOrData, result);
364
- },
365
- requiredVisibility: function (fn) {
366
- console.log('featureToggle.requiredVisibility is deprecated. use featureToggle.setRequiredFlag instead. This function will be removed in one of the next major versions.');
367
- api.setRequiredFlag(fn);
368
- },
369
- defaultVisibility: function (fn) {
370
- console.log('featureToggle.requiredVisibility is deprecated. use featureToggle.setRequiredFlag instead. This function will be removed in one of the next major versions.');
371
- api.setDefaultFlag(fn);
372
- },
373
- setRequiredFlag(fn) {
374
- if (typeof fn != "function")
375
- throw new Error('feature.setRequiredFlag(): 1st parameter must be a function, but is ' + typeof fn);
376
- globals.visibilities['_required'] = parseToFn(fn);
377
- },
378
- setDefaultFlag(fn) {
379
- if (typeof fn != "function")
380
- throw new Error('feature.defaultVisibility(): 1st parameter must be a function, but is ' + typeof fn);
381
- globals.visibilities['_default'] = parseToFn(fn);
382
- },
383
- addPlugin: function (plugin) {
384
- if (globals.usedPlugins.includes(plugin))
385
- return;
386
- const newPlugin = plugin(api);
387
- for (let _key of Object.keys(newPlugin)) {
388
- api[_key] = newPlugin[_key];
389
- }
390
- globals.usedPlugins.push(plugin);
391
- },
392
- };
393
- init(api);
394
- return api;
93
+ function u(e = {}) {
94
+ let t = {
95
+ datas: {},
96
+ listeners: {},
97
+ visibilities: l(e),
98
+ showLogs: !1,
99
+ usedPlugins: []
100
+ };
101
+ function n(t) {
102
+ e.$default && t.setDefaultFlag(e.$default), e.$required && t.setRequiredFlag(e.$required);
103
+ let n = e._plugins || [], i = [...e.$plugins || [], ...n];
104
+ n.length && console.log("useFeatureToggle({_plugins:[]}): Key _plugins is deprecated. Use $plugins instead. This attribute will be removed in one of the next major versions."), i.length && i.forEach((e) => {
105
+ if (typeof e != "function") throw Error("featuretoggleapi()-constructor: config.plugins needs functions as entries, not " + typeof e + ".");
106
+ t.addPlugin(e);
107
+ }), r("init");
108
+ }
109
+ function r(e, n) {
110
+ (t.listeners[e] || []).forEach((e) => {
111
+ e(n);
112
+ });
113
+ }
114
+ let i = function(e) {
115
+ if (!t.showLogs) return;
116
+ if (globalThis.window === void 0) {
117
+ let t = e.replaceAll("<b>", "");
118
+ console.log(t);
119
+ return;
120
+ }
121
+ let n = e.includes("<b>"), r = e.includes("visible"), i = e.includes("hidden"), a = e.replaceAll("visible", "%cvisible");
122
+ a = a.replaceAll("hidden", "%chidden"), r ? console.log(a, "color:green;font-weight:bold;") : i ? console.log(a, "color:red;font-weight:bold;") : n ? (a = a.replace("<b>", "%c"), console.log(a, "font-weight:bold;")) : console.log(e);
123
+ }, a = function(e, t) {
124
+ return i(t), i(""), e;
125
+ }, o = function(e, t, n, r, i) {
126
+ if (e == null) return;
127
+ let o = e({
128
+ name: n,
129
+ variant: r ?? void 0,
130
+ data: i
131
+ });
132
+ return typeof o == "boolean" ? o : a(!1, `The ${t} returns ${o}. => Please return true or false. This result (and all non-boolean results) will return false.`);
133
+ };
134
+ function u(e) {
135
+ let n = e.split("#");
136
+ return {
137
+ name: n[0],
138
+ variant: n.length > 1 ? n[1] : void 0,
139
+ data: t.datas[e]
140
+ };
141
+ }
142
+ let d = function(e, t, n, r) {
143
+ if (e === void 0) throw Error("feature.visibility(): 1st parameter name must be defined");
144
+ if (arguments.length === 1) throw Error("feature.visibility(): 2nd parameter name must be a boolean or function, but is empty");
145
+ let i = e, a = null, o = null, s;
146
+ return n === void 0 && r === void 0 ? s = t : r === void 0 ? (a = t, s = n) : (a = t, o = n, s = r), {
147
+ name: i,
148
+ variant: a,
149
+ data: o,
150
+ result: s
151
+ };
152
+ }, f = (e, t, n, r) => {
153
+ let i = {
154
+ name: e,
155
+ variant: t ?? void 0,
156
+ data: n
157
+ };
158
+ return i.key = c(i.name, i.variant), r == null ? i : (i.visibilityFunction = s(r), i.result = i.visibilityFunction({
159
+ name: i.name,
160
+ variant: i.variant,
161
+ data: i.data || {},
162
+ _internalCall: !0,
163
+ description: "When attaching a function, the result must be calculated internally. You can filter this out with the _internalCall:true -Flag."
164
+ }), i);
165
+ };
166
+ function p(e, n, r) {
167
+ let s = t.visibilities, l = r ? ` with data ${JSON.stringify(r)}` : "";
168
+ if (i(`\nCheck Visibility of <b>Feature "${e}", variant "${n ?? ""}"${l}.`), e === void 0) throw Error("The attribute \"name\" is required for tag <feature></feature>. Example: <feature name=\"aname\"></feature>");
169
+ let u = s._required, d = u != null, f = o(u, "requiredVisibility", e, n, r);
170
+ if (!d) i("No requiredVisibility rule specified for this feature.");
171
+ else if (f === !0) i("The requiredVisibility rule returns true. This feature will be shown when no other rule rejects it.");
172
+ else return a(!1, "The requiredVisibility rule returns false. This feature will be hidden.");
173
+ let p = s[c(e, n)], h = p != null, g = o(p, "visibility function", e, n, r);
174
+ if (h) return a(g ?? !1, `The visibility rule returns ${g}. This feature will be ${g ? "visible" : "hidden"}.`);
175
+ i("No visibility rule found matching name and variant.");
176
+ let _ = n != null, v = s[c(e)], y = o(v, "visibility function (only name)", e, n, r);
177
+ if (_ && typeof y == "boolean") return a(y, `Found a visibility rule for name ${e} without variants. The rule returns ${y}. => This feature will be ${y ? "visible" : "hidden"}.`);
178
+ _ && i(`No rules found for name ${e} without variants.`);
179
+ let b = s._default;
180
+ return m(d, _, y, b != null, o(b, "defaultVisibility", e, n, r), e);
181
+ }
182
+ function m(e, t, n, r, o, s) {
183
+ return t && typeof n == "boolean" ? a(n, `Found a visibility rule for name ${s} without variants. The rule returns ${n}. => This feature will be ${n ? "visible" : "hidden"}.`) : (t && i(`No rules found for name ${s} without variants.`), r ? a(o ?? !1, `Found a defaultVisibility rule. The rule returns ${o}. => This feature will be ${o ? "visible" : "hidden"}.`) : (i("No default rule found."), e ? a(!0, "Only the requiredVisibility rule was found. This returned true. => This feature will be visible.") : a(!1, "No rules were found. This feature will be hidden.")));
184
+ }
185
+ let h = {
186
+ name: "feature-toggle-api",
187
+ setData: function(e, n, i) {
188
+ if (e === void 0) throw Error("setData(): The name of the feature must be defined, but is undefined");
189
+ let a = f(e, i === void 0 ? void 0 : n, i === void 0 ? n : i);
190
+ t.datas[a.key] = a.data, r("visibilityrule", a);
191
+ },
192
+ on(e, n, i) {
193
+ t.listeners[e] = t.listeners[e] || [], t.listeners[e].push(n), r("registerEvent", { type: e }), !i?.ignorePreviousRules && Object.keys(t.visibilities).forEach((e) => {
194
+ let r = u(e), i = t.visibilities[e];
195
+ r.result = i(r), n(r);
196
+ });
197
+ },
198
+ trigger: r,
199
+ showLogs: function(e) {
200
+ t.showLogs = e ?? !0;
201
+ },
202
+ isVisible(e, t, n) {
203
+ return console.log("featureToggle.isVisible is deprecated. use featureToggle.isActive instead. This function will be removed in one of the next major versions."), p(e, t, n);
204
+ },
205
+ isActive: p,
206
+ setFlag(e, n, i, a) {
207
+ let o = d(e, n, i, a), s = f(o.name, o.variant, o.data, o.result);
208
+ t.visibilities[s.key] = s.visibilityFunction, t.datas[s.key] = s.data, r("visibilityrule", s);
209
+ },
210
+ visibility: function(e, t, n, r) {
211
+ console.log("featureToggle.visibility is deprecated. use featureToggle.setVisibility instead. This function will be removed in one of the next major versions."), h.setFlag(e, t, n, r);
212
+ },
213
+ requiredVisibility: function(e) {
214
+ console.log("featureToggle.requiredVisibility is deprecated. use featureToggle.setRequiredFlag instead. This function will be removed in one of the next major versions."), h.setRequiredFlag(e);
215
+ },
216
+ defaultVisibility: function(e) {
217
+ console.log("featureToggle.requiredVisibility is deprecated. use featureToggle.setRequiredFlag instead. This function will be removed in one of the next major versions."), h.setDefaultFlag(e);
218
+ },
219
+ setRequiredFlag(e) {
220
+ if (typeof e != "function") throw Error("feature.setRequiredFlag(): 1st parameter must be a function, but is " + typeof e);
221
+ t.visibilities._required = s(e);
222
+ },
223
+ setDefaultFlag(e) {
224
+ if (typeof e != "function") throw Error("feature.defaultVisibility(): 1st parameter must be a function, but is " + typeof e);
225
+ t.visibilities._default = s(e);
226
+ },
227
+ addPlugin: function(e) {
228
+ if (t.usedPlugins.includes(e)) return;
229
+ let n = e(h);
230
+ for (let e of Object.keys(n)) h[e] = n[e];
231
+ t.usedPlugins.push(e);
232
+ }
233
+ };
234
+ return n(h), h;
395
235
  }
236
+ //#endregion
237
+ export { o as htmlPlugin, r as urlPlugin, u as useFeatureToggle };
396
238
 
397
- export { htmlPlugin, urlPlugin, useFeatureToggle };
239
+ //# sourceMappingURL=feature-toggle.js.map