@featurevisor/sdk 0.12.1 → 0.13.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.13.0](https://github.com/fahad19/featurevisor/compare/v0.12.1...v0.13.0) (2023-04-19)
7
+
8
+
9
+ ### Features
10
+
11
+ * Event listeners ([#55](https://github.com/fahad19/featurevisor/issues/55)) ([eaeb7f6](https://github.com/fahad19/featurevisor/commit/eaeb7f62d78e81bff8be28fff46f4088f4d9aa5b))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [0.12.1](https://github.com/fahad19/featurevisor/compare/v0.12.0...v0.12.1) (2023-04-18)
7
18
 
8
19
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @featurevisor/sdk
2
2
 
3
- Universal Featurevisor SDK for both Node.js and the browser.
3
+ Universal JavaScript SDK for both Node.js and the browser.
4
4
 
5
5
  Visit [https://featurevisor.com/docs/sdks/](https://featurevisor.com/docs/sdks/) for more information.
6
6
 
@@ -17,29 +17,51 @@ Initialize the SDK:
17
17
  ```js
18
18
  import { createInstance } from "@featurevisor/sdk";
19
19
 
20
- const datafileContent = fetch(datafileURL).then((res) => res.json());
21
-
22
- const sdk = createInstance({
23
- // required
24
- datafile: datafileContent,
25
-
26
- // optional
27
- onActivation: (featureKey, variant, attributes, captureAttributes) => {
28
- console.log(`Feature ${featureKey} activated with variant ${variant}`);
29
- },
30
- });
20
+ const sdk = createInstance(options);
31
21
  ```
32
22
 
23
+ ## Options
24
+
25
+ | Key | Type | Description |
26
+ |-----------------------|------------|--------------------------------------------------------|
27
+ | `datafile` | `object` | Parsed datafile object |
28
+ | `datafileUrl` | `string` | URL to fetch the datafile from |
29
+ | `onReady` | `function` | Callback to be called when the SDK is ready to be used |
30
+ | `onActivation` | `function` | Callback to be called when a feature is activated |
31
+ | `onRefresh` | `function` | Callback to be called when the datafile is refreshed |
32
+ | `onUpdate` | `function` | Callback to be called when the datafile is updated |
33
+ | `refreshInterval` | `number` | Interval in seconds to refresh the datafile |
34
+ | `handleDatafileFetch` | `function` | Callback to be called when the datafile is fetched |
35
+ | `interceptAttributes` | `function` | Callback to be called before attributes are used |
36
+ | `logger` | `Logger` | Logger object to be used by the SDK |
37
+
33
38
  ## API
34
39
 
35
40
  ### `getVariation`
36
41
 
37
42
  > `getVariation(featureKey: string, attributes: Attributes): VariationValue`
38
43
 
44
+ Also supports additional type specific methods:
45
+
46
+ - `getVariationBoolean`
47
+ - `getVariationString`
48
+ - `getVariationInteger`
49
+ - `getVariationDouble`
50
+
39
51
  ### `getVariable`
40
52
 
41
53
  > `getVariable(featureKey: string, variableKey: string, attributes: Attributes): VariableValue`
42
54
 
55
+ Also supports additional type specific methods:
56
+
57
+ - `getVariableBoolean`
58
+ - `getVariableString`
59
+ - `getVariableInteger`
60
+ - `getVariableDouble`
61
+ - `getVariableArray`
62
+ - `getVariableObject`
63
+ - `getVariableJSON`
64
+
43
65
  ### `activate`
44
66
 
45
67
  > `activate(featureKey: string, attributes: Attributes): VariationValue`
@@ -48,6 +70,58 @@ Same as `getVariation`, but also calls the `onActivation` callback.
48
70
 
49
71
  This is a convenience method meant to be called when you know the User has been exposed to your Feature, and you also want to track the activation.
50
72
 
73
+ Also supports additional type specific methods:
74
+
75
+ - `activateBoolean`
76
+ - `activateString`
77
+ - `activateInteger`
78
+ - `activateDouble`
79
+
80
+ ### `isReady`
81
+
82
+ > `isReady(): boolean`
83
+
84
+ Synchonously check if the SDK is ready to be used.
85
+
86
+ ### `refresh`
87
+
88
+ > `refresh(): void`
89
+
90
+ Manually refresh datafile.
91
+
92
+ ### `setLogLevels`
93
+
94
+ > `setLogLevels(levels: LogLevel[]): void`
95
+
96
+ Accepted values for `levels`: `["debug", "info", "warn", "error"]`.
97
+
98
+ ### `on`
99
+
100
+ > `on(event: string, callback: function): void`
101
+
102
+ Listen to SDK events, like:
103
+
104
+ - `ready`
105
+ - `activation`
106
+ - `refresh`
107
+ - `update`
108
+
109
+ ### `addListener`
110
+
111
+ Alias for `on` method.
112
+
113
+ ### `off`
114
+
115
+ > `off(event: string, callback: function): void`
116
+
117
+ ### `removeListener`
118
+
119
+ Alias for `off` method.
120
+
121
+ ### `removeAllListeners`
122
+
123
+ > `removeAllListeners(event?: string): void`
124
+
51
125
  ## License
52
126
 
53
127
  MIT © [Fahad Heylaal](https://fahad19.com)
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.FeaturevisorSDK=t():e.FeaturevisorSDK=t()}(this,(()=>(()=>{var e={480:(e,t,r)=>{"use strict";r.r(t),r.d(t,{compare:()=>a,compareVersions:()=>i,satisfies:()=>o,validate:()=>n});const i=(e,t)=>{const r=s(e),i=s(t),n=r.pop(),a=i.pop(),o=d(r,i);return 0!==o?o:n&&a?d(n.split("."),a.split(".")):n||a?n?-1:1:0},n=e=>"string"==typeof e&&/^[v\d]/.test(e)&&u.test(e),a=(e,t,r)=>{p(r);const n=i(e,t);return g[r].includes(n)},o=(e,t)=>{if(t.includes("||"))return t.split("||").some((t=>o(e,t)));if(t.includes(" "))return t.trim().replace(/\s{2,}/g," ").split(" ").every((t=>o(e,t)));const r=t.match(/^([<>=~^]+)/),i=r?r[1]:"=";if("^"!==i&&"~"!==i)return a(e,t,i);const[n,u,f,,l]=s(e),[c,g,v,,p]=s(t),y=[n,u,f],h=[c,null!=g?g:"x",null!=v?v:"x"];if(p){if(!l)return!1;if(0!==d(y,h))return!1;if(-1===d(l.split("."),p.split(".")))return!1}const b=h.findIndex((e=>"0"!==e))+1,V="~"===i?2:b>1?b:1;return 0===d(y.slice(0,V),h.slice(0,V))&&-1!==d(y.slice(V),h.slice(V))},u=/^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i,s=e=>{if("string"!=typeof e)throw new TypeError("Invalid argument expected string");const t=e.match(u);if(!t)throw new Error(`Invalid argument not valid semver ('${e}' received)`);return t.shift(),t},f=e=>"*"===e||"x"===e||"X"===e,l=e=>{const t=parseInt(e,10);return isNaN(t)?e:t},c=(e,t)=>{if(f(e)||f(t))return 0;const[r,i]=((e,t)=>typeof e!=typeof t?[String(e),String(t)]:[e,t])(l(e),l(t));return r>i?1:r<i?-1:0},d=(e,t)=>{for(let r=0;r<Math.max(e.length,t.length);r++){const i=c(e[r]||"0",t[r]||"0");if(0!==i)return i}return 0},g={">":[1],">=":[0,1],"=":[0],"<=":[-1,0],"<":[-1]},v=Object.keys(g),p=e=>{if("string"!=typeof e)throw new TypeError("Invalid operator type, expected string but got "+typeof e);if(-1===v.indexOf(e))throw new Error(`Invalid operator, expected one of ${v.join("|")}`)}},77:e=>{!function(){const t=e=>(new TextEncoder).encode(e);function r(e,r){let i,n,a,o,u,s,f,l;for("string"==typeof e&&(e=t(e)),i=3&e.length,n=e.length-i,a=r,u=3432918353,s=461845907,l=0;l<n;)f=255&e[l]|(255&e[++l])<<8|(255&e[++l])<<16|(255&e[++l])<<24,++l,f=(65535&f)*u+(((f>>>16)*u&65535)<<16)&4294967295,f=f<<15|f>>>17,f=(65535&f)*s+(((f>>>16)*s&65535)<<16)&4294967295,a^=f,a=a<<13|a>>>19,o=5*(65535&a)+((5*(a>>>16)&65535)<<16)&4294967295,a=27492+(65535&o)+((58964+(o>>>16)&65535)<<16);switch(f=0,i){case 3:f^=(255&e[l+2])<<16;case 2:f^=(255&e[l+1])<<8;case 1:f^=255&e[l],f=(65535&f)*u+(((f>>>16)*u&65535)<<16)&4294967295,f=f<<15|f>>>17,f=(65535&f)*s+(((f>>>16)*s&65535)<<16)&4294967295,a^=f}return a^=e.length,a^=a>>>16,a=2246822507*(65535&a)+((2246822507*(a>>>16)&65535)<<16)&4294967295,a^=a>>>13,a=3266489909*(65535&a)+((3266489909*(a>>>16)&65535)<<16)&4294967295,a^=a>>>16,a>>>0}const i=r;i.v2=function(e,r){"string"==typeof e&&(e=t(e));let i,n=e.length,a=r^n,o=0;for(;n>=4;)i=255&e[o]|(255&e[++o])<<8|(255&e[++o])<<16|(255&e[++o])<<24,i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16),i^=i>>>24,i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16),a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16)^i,n-=4,++o;switch(n){case 3:a^=(255&e[o+2])<<16;case 2:a^=(255&e[o+1])<<8;case 1:a^=255&e[o],a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16)}return a^=a>>>13,a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16),a^=a>>>15,a>>>0},i.v3=r,e.exports=i}()},725:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.getBucketedNumber=t.MAX_BUCKETED_NUMBER=void 0;var i=r(77),n=Math.pow(2,32);t.MAX_BUCKETED_NUMBER=1e5,t.getBucketedNumber=function(e){var r=i.v3(e,1)/n;return Math.floor(r*t.MAX_BUCKETED_NUMBER)}},352:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.FeaturevisorSDK=t.getValueByType=void 0;var i=r(913),n=r(522),a=r(725),o=r(687);function u(e,t){if(void 0!==e)switch(t){case"string":return"string"==typeof e?e:void 0;case"integer":return parseInt(e,10);case"double":return parseFloat(e);case"boolean":return!0===e;case"array":return Array.isArray(e)?e:void 0;case"object":return"object"==typeof e?e:void 0;default:return e}}t.getValueByType=u;var s=function(){function e(e){e.onActivation&&(this.onActivation=e.onActivation),e.configureBucketValue&&(this.configureBucketValue=e.configureBucketValue),this.logger=e.logger||(0,o.createLogger)(),e.interceptAttributes&&(this.interceptAttributes=e.interceptAttributes),this.setDatafile(e.datafile)}return e.prototype.setDatafile=function(e){try{this.datafileReader=new i.DatafileReader("string"==typeof e?JSON.parse(e):e)}catch(e){this.logger.error("could not parse datafile",{error:e})}},e.prototype.getRevision=function(){return this.datafileReader.getRevision()},e.prototype.getFeature=function(e){return"string"==typeof e?this.datafileReader.getFeature(e):e},e.prototype.getBucketKey=function(e,t){var r=e.key,i="string"==typeof e.bucketBy?e.bucketBy:e.bucketBy.join("_");return"".concat(i,"_").concat(r)},e.prototype.getBucketValue=function(e,t){var r=this.getBucketKey(e,t),i=(0,a.getBucketedNumber)(r);return this.configureBucketValue?this.configureBucketValue(e,t,i):i},e.prototype.getVariation=function(e,t){void 0===t&&(t={});try{var r=this.getFeature(e);if(!r)return void this.logger.warn("feature not found in datafile",{featureKey:e});var i=this.interceptAttributes?this.interceptAttributes(t):t,a=(0,n.getForcedVariation)(r,i,this.datafileReader);if(a)return this.logger.debug("forced variation found",{featureKey:e,variation:a.value}),a.value;var o=this.getBucketValue(r,i),u=(0,n.getBucketedVariation)(r,i,o,this.datafileReader,this.logger);return u?u.value:(this.logger.debug("using default variation",{featureKey:e,bucketValue:o,variation:r.defaultVariation}),r.defaultVariation)}catch(t){return void this.logger.error("getVariation",{featureKey:e,error:t})}},e.prototype.getVariationBoolean=function(e,t){return void 0===t&&(t={}),u(this.getVariation(e,t),"boolean")},e.prototype.getVariationString=function(e,t){return void 0===t&&(t={}),u(this.getVariation(e,t),"string")},e.prototype.getVariationInteger=function(e,t){return void 0===t&&(t={}),u(this.getVariation(e,t),"integer")},e.prototype.getVariationDouble=function(e,t){return void 0===t&&(t={}),u(this.getVariation(e,t),"double")},e.prototype.activate=function(e,t){void 0===t&&(t={});try{var r=this.getVariation(e,t);if(void 0===r)return;if(this.onActivation){var i=this.interceptAttributes?this.interceptAttributes(t):t,n={};this.datafileReader.getAllAttributes().filter((function(e){return!0===e.capture})).forEach((function(e){void 0!==i[e.key]&&(n[e.key]=t[e.key])})),this.onActivation(e,r,i,n)}return r}catch(t){return void this.logger.error("activate",{featureKey:e,error:t})}},e.prototype.activateBoolean=function(e,t){return void 0===t&&(t={}),u(this.activate(e,t),"boolean")},e.prototype.activateString=function(e,t){return void 0===t&&(t={}),u(this.activate(e,t),"string")},e.prototype.activateInteger=function(e,t){return void 0===t&&(t={}),u(this.activate(e,t),"integer")},e.prototype.activateDouble=function(e,t){return void 0===t&&(t={}),u(this.activate(e,t),"double")},e.prototype.getVariable=function(e,t,r){void 0===r&&(r={});try{var i=this.getFeature(e);if(!i)return void this.logger.warn("feature not found in datafile",{featureKey:e,variableKey:t});var a=Array.isArray(i.variablesSchema)?i.variablesSchema.find((function(e){return e.key===t})):void 0;if(!a)return void this.logger.warn("variable schema not found",{featureKey:e,variableKey:t});var o=this.interceptAttributes?this.interceptAttributes(r):r,u=(0,n.getForcedVariableValue)(i,a,o,this.datafileReader);if(void 0!==u)return this.logger.debug("forced variable value found",{featureKey:e,variableKey:t}),u;var s=this.getBucketValue(i,o);return(0,n.getBucketedVariableValue)(i,a,o,s,this.datafileReader,this.logger)}catch(r){return void this.logger.error("getVariable",{featureKey:e,variableKey:t,error:r})}},e.prototype.getVariableBoolean=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"boolean")},e.prototype.getVariableString=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"string")},e.prototype.getVariableInteger=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"integer")},e.prototype.getVariableDouble=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"double")},e.prototype.getVariableArray=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"array")},e.prototype.getVariableObject=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"object")},e.prototype.getVariableJSON=function(e,t,r){return void 0===r&&(r={}),u(this.getVariable(e,t,r),"json")},e}();t.FeaturevisorSDK=s},243:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.allConditionsAreMatched=t.conditionIsMatched=void 0;var i=r(480);function n(e,t){var r=e.attribute,n=e.operator,a=e.value;if("equals"===n)return t[r]===a;if("notEquals"===n)return t[r]!==a;if("string"==typeof t[r]&&Array.isArray(a)){var o=t[r];if("in"===n)return-1!==a.indexOf(o);if("notIn"===n)return-1===a.indexOf(o)}else if("string"==typeof t[r]&&"string"==typeof a){if(o=t[r],"contains"===n)return-1!==o.indexOf(a);if("notContains"===n)return-1===o.indexOf(a);if("startsWith"===n)return o.startsWith(a);if("endsWith"===n)return o.endsWith(a);if("semverEquals"===n)return 0===(0,i.compareVersions)(o,a);if("semverNotEquals"===n)return 0!==(0,i.compareVersions)(o,a);if("semverGreaterThan"===n)return 1===(0,i.compareVersions)(o,a);if("semverGreaterThanOrEquals"===n)return(0,i.compareVersions)(o,a)>=0;if("semverLessThan"===n)return-1===(0,i.compareVersions)(o,a);if("semverLessThanOrEquals"===n)return(0,i.compareVersions)(o,a)<=0}else if("number"==typeof t[r]&&"number"==typeof a){if(o=t[r],"greaterThan"===n)return o>a;if("greaterThanOrEquals"===n)return o>=a;if("lessThan"===n)return o<a;if("lessThanOrEquals"===n)return o<=a}return!1}t.conditionIsMatched=n,t.allConditionsAreMatched=function e(t,r){return"attribute"in t?n(t,r):"and"in t&&Array.isArray(t.and)?t.and.every((function(t){return e(t,r)})):"or"in t&&Array.isArray(t.or)?t.or.some((function(t){return e(t,r)})):"not"in t&&Array.isArray(t.not)?t.not.every((function(i){return!1===e({and:t.not},r)})):!!Array.isArray(t)&&t.every((function(t){return e(t,r)}))}},292:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createInstance=void 0;var i=r(352),n=r(687);function a(e,t){return t.handleDatafileFetch?t.handleDatafileFetch(e):fetch(e).then((function(e){return e.json()}))}function o(e,t,r){var i,n=!1,o={getVariation:e.getVariation.bind(e),getVariationBoolean:e.getVariationBoolean.bind(e),getVariationInteger:e.getVariationInteger.bind(e),getVariationDouble:e.getVariationDouble.bind(e),getVariationString:e.getVariationString.bind(e),activate:e.activate,activateBoolean:e.activateBoolean.bind(e),activateInteger:e.activateInteger.bind(e),activateDouble:e.activateDouble.bind(e),activateString:e.activateString.bind(e),getVariable:e.getVariable.bind(e),getVariableBoolean:e.getVariableBoolean.bind(e),getVariableInteger:e.getVariableInteger.bind(e),getVariableDouble:e.getVariableDouble.bind(e),getVariableString:e.getVariableString.bind(e),getVariableArray:e.getVariableArray.bind(e),getVariableObject:e.getVariableObject.bind(e),setLogLevels:r.setLevels.bind(r),refresh:function(){return r.debug("refreshing datafile"),n?r.warn("refresh in progress, skipping"):t.datafileUrl?(n=!0,void a(t.datafileUrl,t).then((function(i){var a=e.getRevision()!==i.revision;e.setDatafile(i),r.info("refreshed datafile"),"function"==typeof t.onRefresh&&t.onRefresh(),a&&"function"==typeof t.onUpdate&&t.onUpdate(),n=!1})).catch((function(e){r.error("failed to refresh datafile",{error:e}),n=!1}))):r.error("cannot refresh since `datafileUrl` is not provided")},startRefreshing:function(){return t.datafileUrl?i?r.warn("refreshing has already started"):t.refreshInterval?void(i=setInterval((function(){o.refresh()}),1e3*t.refreshInterval)):r.warn("no `refreshInterval` option provided"):r.error("cannot start refreshing since `datafileUrl` is not provided")},stopRefreshing:function(){if(!i)return r.warn("refreshing has not started yet");clearInterval(i)}};return t.datafileUrl&&t.refreshInterval&&o.startRefreshing(),o}var u={schemaVersion:"1",revision:"unknown",attributes:[],segments:[],features:[]};t.createInstance=function(e){if(!e.datafile&&!e.datafileUrl)throw new Error("Featurevisor SDK instance cannot be created without both `datafile` and `datafileUrl` options");var t=e.logger||(0,n.createLogger)();if(!e.datafileUrl&&e.refreshInterval&&t.warn("refreshing datafile requires `datafileUrl` option"),e.datafile){var r=new i.FeaturevisorSDK({datafile:e.datafile,onActivation:e.onActivation,configureBucketValue:e.configureBucketValue,logger:t,interceptAttributes:e.interceptAttributes});if("function"==typeof e.onReady){var s=e.onReady;setTimeout((function(){s()}),0)}return o(r,e,t)}var f=new i.FeaturevisorSDK({datafile:u,onActivation:e.onActivation,configureBucketValue:e.configureBucketValue,logger:t,interceptAttributes:e.interceptAttributes});return e.datafileUrl&&a(e.datafileUrl,e).then((function(t){f.setDatafile(t),"function"==typeof e.onReady&&e.onReady()})).catch((function(e){t.error("failed to fetch datafile:"),console.error(e)})),o(f,e,t)}},913:(e,t)=>{"use strict";function r(e,t){if("string"==typeof e[t]&&"*"!==e[t])try{e[t]=JSON.parse(e[t])}catch(e){console.error("Error parsing JSON",e)}return e}Object.defineProperty(t,"__esModule",{value:!0}),t.DatafileReader=t.parseJsonConditionsIfStringified=void 0,t.parseJsonConditionsIfStringified=r;var i=function(){function e(e){this.schemaVersion=e.schemaVersion,this.revision=e.revision,this.segments=e.segments,this.attributes=e.attributes,this.features=e.features}return e.prototype.getRevision=function(){return this.revision},e.prototype.getSchemaVersion=function(){return this.schemaVersion},e.prototype.getAllAttributes=function(){return this.attributes},e.prototype.getAttribute=function(e){return this.attributes.find((function(t){return t.key===e}))},e.prototype.getSegment=function(e){var t=this.segments.find((function(t){return t.key===e}));if(t)return r(t,"conditions")},e.prototype.getFeature=function(e){var t=this.features.find((function(t){return t.key===e}));if(t)return t},e}();t.DatafileReader=i},522:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.getBucketedVariableValue=t.getForcedVariableValue=t.getBucketedVariation=t.getForcedVariation=t.getMatchedAllocation=t.getMatchedTraffic=void 0;var i=r(590),n=r(243);function a(e,t,r,n,a){return e.find((function(e){return!(r>e.percentage||!(0,i.allGroupSegmentsAreMatched)("string"==typeof e.segments&&"*"!==e.segments?JSON.parse(e.segments):e.segments,t,n)||(a.debug("matched rule",{ruleKey:e.key}),0))}))}function o(e,t){for(var r=0,i=0,n=e.allocation;i<n.length;i++){var a=n[i];if(t<=(r+=a.percentage))return a}}function u(e,t,r){if(e.force)return e.force.find((function(e){return e.conditions?(0,n.allConditionsAreMatched)(e.conditions,t):!!e.segments&&(0,i.allGroupSegmentsAreMatched)(e.segments,t,r)}))}t.getMatchedTraffic=a,t.getMatchedAllocation=o,t.getForcedVariation=function(e,t,r){var i=u(e,t,r);if(i&&i.variation)return e.variations.find((function(e){return e.value===i.variation}))},t.getBucketedVariation=function(e,t,r,i,n){var u=a(e.traffic,t,r,i,n);if(u){if(u.variation){var s=e.variations.find((function(e){return e.value===u.variation}));if(s)return n.debug("using variation from rule",{featureKey:e.key,variation:s.value,ruleKey:u.key}),s}var f=o(u,r);if(f){var l=f.variation,c=e.variations.find((function(e){return e.value===l}));if(c)return n.debug("matched variation",{featureKey:e.key,variation:c.value,bucketValue:r}),c;n.debug("no matched variation found",{featureKey:e.key,variation:l,bucketValue:r})}else n.debug("no matched allocation found",{featureKey:e.key,bucketValue:r})}else n.debug("no matched rule found",{featureKey:e.key,bucketValue:r})},t.getForcedVariableValue=function(e,t,r,i){var n=u(e,r,i);if(n&&n.variables){var a=n.variables[t.key];return"string"==typeof a&&"json"===t.type?JSON.parse(a):a}},t.getBucketedVariableValue=function(e,t,r,u,s,f){var l,c=a(e.traffic,r,u,s,f);if(c){var d=t.key;if(c.variables&&void 0!==c.variables[d])return f.debug("using variable from rule",{featureKey:e.key,variableKey:d,bucketValue:u}),c.variables[d];var g=o(c,u);if(g){var v=g.variation,p=e.variations.find((function(e){return e.value===v}));if(p){var y=null===(l=p.variables)||void 0===l?void 0:l.find((function(e){return e.key===d}));if(!y)return f.debug("using default value as variation has no variable",{featureKey:e.key,variableKey:d,variation:v,bucketValue:u}),"json"===t.type?JSON.parse(t.defaultValue):t.defaultValue;if(y.overrides){var h=y.overrides.find((function(e){return e.conditions?(0,n.allConditionsAreMatched)("string"==typeof e.conditions?JSON.parse(e.conditions):e.conditions,r):!!e.segments&&(0,i.allGroupSegmentsAreMatched)("string"==typeof e.segments&&"*"!==e.segments?JSON.parse(e.segments):e.segments,r,s)}));if(h)return f.debug("using override value from variation",{feature:e.key,variableKey:d,variation:v,bucketValue:u}),"json"===t.type?JSON.parse(h.value):h.value}return f.debug("using value from variation",{feature:e.key,variableKey:d,variation:v,bucketValue:u}),"json"===t.type?JSON.parse(y.value):y.value}f.debug("no matched variation found",{feature:e.key,variableKey:d,variation:v,bucketValue:u})}else f.debug("no matched allocation found",{featureKey:e.key,variableKey:d,bucketValue:u})}else f.debug("no matched rule found",{featureKey:e.key,variableKey:t.key,bucketValue:u})}},97:function(e,t,r){"use strict";var i=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,n)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||i(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),n(r(725),t),n(r(352),t),n(r(292),t),n(r(687),t)},687:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createLogger=t.Logger=t.defaultLogHandler=t.defaultLogLevels=t.loggerPrefix=void 0,t.loggerPrefix="[Featurevisor]",t.defaultLogLevels=["warn","error"],t.defaultLogHandler=function(e,r,i){switch(void 0===i&&(i={}),e){case"debug":console.log(t.loggerPrefix,r,i);case"info":console.info(t.loggerPrefix,r,i);case"warn":console.warn(t.loggerPrefix,r,i);case"error":console.error(t.loggerPrefix,r,i)}};var r=function(){function e(e){this.levels=e.levels,this.handle=e.handler}return e.prototype.setLevels=function(e){this.levels=e},e.prototype.log=function(e,t,r){-1!==this.levels.indexOf(e)&&this.handle(e,t,r)},e.prototype.debug=function(e,t){this.log("debug",e,t)},e.prototype.info=function(e,t){this.log("info",e,t)},e.prototype.warn=function(e,t){this.log("warn",e,t)},e.prototype.error=function(e,t){this.log("error",e,t)},e}();t.Logger=r,t.createLogger=function(e){void 0===e&&(e={});var i=e.levels||t.defaultLogLevels,n=e.handler||t.defaultLogHandler;return new r({levels:i,handler:n})}},590:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.allGroupSegmentsAreMatched=t.segmentIsMatched=void 0;var i=r(243);function n(e,t){return(0,i.allConditionsAreMatched)(e.conditions,t)}t.segmentIsMatched=n,t.allGroupSegmentsAreMatched=function e(t,r,i){if("*"===t)return!0;if("string"==typeof t){var a=i.getSegment(t);return!!a&&n(a,r)}if("object"==typeof t){if("and"in t&&Array.isArray(t.and))return t.and.every((function(t){return e(t,r,i)}));if("or"in t&&Array.isArray(t.or))return t.or.some((function(t){return e(t,r,i)}));if("not"in t&&Array.isArray(t.not))return t.not.every((function(t){return!1===e(t,r,i)}))}return!!Array.isArray(t)&&t.every((function(t){return e(t,r,i)}))}}},t={};function r(i){var n=t[i];if(void 0!==n)return n.exports;var a=t[i]={exports:{}};return e[i].call(a.exports,a,a.exports,r),a.exports}return r.d=(e,t)=>{for(var i in t)r.o(t,i)&&!r.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(97)})()));
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.FeaturevisorSDK=t():e.FeaturevisorSDK=t()}(this,(()=>(()=>{var e={480:(e,t,r)=>{"use strict";r.r(t),r.d(t,{compare:()=>a,compareVersions:()=>i,satisfies:()=>o,validate:()=>n});const i=(e,t)=>{const r=u(e),i=u(t),n=r.pop(),a=i.pop(),o=d(r,i);return 0!==o?o:n&&a?d(n.split("."),a.split(".")):n||a?n?-1:1:0},n=e=>"string"==typeof e&&/^[v\d]/.test(e)&&s.test(e),a=(e,t,r)=>{p(r);const n=i(e,t);return g[r].includes(n)},o=(e,t)=>{if(t.includes("||"))return t.split("||").some((t=>o(e,t)));if(t.includes(" "))return t.trim().replace(/\s{2,}/g," ").split(" ").every((t=>o(e,t)));const r=t.match(/^([<>=~^]+)/),i=r?r[1]:"=";if("^"!==i&&"~"!==i)return a(e,t,i);const[n,s,f,,l]=u(e),[c,g,v,,p]=u(t),h=[n,s,f],y=[c,null!=g?g:"x",null!=v?v:"x"];if(p){if(!l)return!1;if(0!==d(h,y))return!1;if(-1===d(l.split("."),p.split(".")))return!1}const b=y.findIndex((e=>"0"!==e))+1,V="~"===i?2:b>1?b:1;return 0===d(h.slice(0,V),y.slice(0,V))&&-1!==d(h.slice(V),y.slice(V))},s=/^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i,u=e=>{if("string"!=typeof e)throw new TypeError("Invalid argument expected string");const t=e.match(s);if(!t)throw new Error(`Invalid argument not valid semver ('${e}' received)`);return t.shift(),t},f=e=>"*"===e||"x"===e||"X"===e,l=e=>{const t=parseInt(e,10);return isNaN(t)?e:t},c=(e,t)=>{if(f(e)||f(t))return 0;const[r,i]=((e,t)=>typeof e!=typeof t?[String(e),String(t)]:[e,t])(l(e),l(t));return r>i?1:r<i?-1:0},d=(e,t)=>{for(let r=0;r<Math.max(e.length,t.length);r++){const i=c(e[r]||"0",t[r]||"0");if(0!==i)return i}return 0},g={">":[1],">=":[0,1],"=":[0],"<=":[-1,0],"<":[-1]},v=Object.keys(g),p=e=>{if("string"!=typeof e)throw new TypeError("Invalid operator type, expected string but got "+typeof e);if(-1===v.indexOf(e))throw new Error(`Invalid operator, expected one of ${v.join("|")}`)}},77:e=>{!function(){const t=e=>(new TextEncoder).encode(e);function r(e,r){let i,n,a,o,s,u,f,l;for("string"==typeof e&&(e=t(e)),i=3&e.length,n=e.length-i,a=r,s=3432918353,u=461845907,l=0;l<n;)f=255&e[l]|(255&e[++l])<<8|(255&e[++l])<<16|(255&e[++l])<<24,++l,f=(65535&f)*s+(((f>>>16)*s&65535)<<16)&4294967295,f=f<<15|f>>>17,f=(65535&f)*u+(((f>>>16)*u&65535)<<16)&4294967295,a^=f,a=a<<13|a>>>19,o=5*(65535&a)+((5*(a>>>16)&65535)<<16)&4294967295,a=27492+(65535&o)+((58964+(o>>>16)&65535)<<16);switch(f=0,i){case 3:f^=(255&e[l+2])<<16;case 2:f^=(255&e[l+1])<<8;case 1:f^=255&e[l],f=(65535&f)*s+(((f>>>16)*s&65535)<<16)&4294967295,f=f<<15|f>>>17,f=(65535&f)*u+(((f>>>16)*u&65535)<<16)&4294967295,a^=f}return a^=e.length,a^=a>>>16,a=2246822507*(65535&a)+((2246822507*(a>>>16)&65535)<<16)&4294967295,a^=a>>>13,a=3266489909*(65535&a)+((3266489909*(a>>>16)&65535)<<16)&4294967295,a^=a>>>16,a>>>0}const i=r;i.v2=function(e,r){"string"==typeof e&&(e=t(e));let i,n=e.length,a=r^n,o=0;for(;n>=4;)i=255&e[o]|(255&e[++o])<<8|(255&e[++o])<<16|(255&e[++o])<<24,i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16),i^=i>>>24,i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16),a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16)^i,n-=4,++o;switch(n){case 3:a^=(255&e[o+2])<<16;case 2:a^=(255&e[o+1])<<8;case 1:a^=255&e[o],a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16)}return a^=a>>>13,a=1540483477*(65535&a)+((1540483477*(a>>>16)&65535)<<16),a^=a>>>15,a>>>0},i.v3=r,e.exports=i}()},725:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.getBucketedNumber=t.MAX_BUCKETED_NUMBER=void 0;var i=r(77),n=Math.pow(2,32);t.MAX_BUCKETED_NUMBER=1e5,t.getBucketedNumber=function(e){var r=i.v3(e,1)/n;return Math.floor(r*t.MAX_BUCKETED_NUMBER)}},352:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.FeaturevisorSDK=t.getValueByType=void 0;var i=r(913),n=r(522),a=r(725),o=r(687);function s(e,t){if(void 0!==e)switch(t){case"string":return"string"==typeof e?e:void 0;case"integer":return parseInt(e,10);case"double":return parseFloat(e);case"boolean":return!0===e;case"array":return Array.isArray(e)?e:void 0;case"object":return"object"==typeof e?e:void 0;default:return e}}t.getValueByType=s;var u=function(){function e(e){e.onActivation&&(this.onActivation=e.onActivation),e.configureBucketValue&&(this.configureBucketValue=e.configureBucketValue),this.logger=e.logger||(0,o.createLogger)(),e.interceptAttributes&&(this.interceptAttributes=e.interceptAttributes),e.emitter&&(this.emitter=e.emitter),this.setDatafile(e.datafile),this.fromInstance=e.fromInstance||!1}return e.prototype.setDatafile=function(e){try{this.datafileReader=new i.DatafileReader("string"==typeof e?JSON.parse(e):e)}catch(e){this.logger.error("could not parse datafile",{error:e})}},e.prototype.getRevision=function(){return this.datafileReader.getRevision()},e.prototype.getFeature=function(e){return"string"==typeof e?this.datafileReader.getFeature(e):e},e.prototype.getBucketKey=function(e,t){var r=e.key,i="string"==typeof e.bucketBy?e.bucketBy:e.bucketBy.join("_");return"".concat(i,"_").concat(r)},e.prototype.getBucketValue=function(e,t){var r=this.getBucketKey(e,t),i=(0,a.getBucketedNumber)(r);return this.configureBucketValue?this.configureBucketValue(e,t,i):i},e.prototype.getVariation=function(e,t){void 0===t&&(t={});try{var r=this.getFeature(e);if(!r)return void this.logger.warn("feature not found in datafile",{featureKey:e});var i=this.interceptAttributes?this.interceptAttributes(t):t,a=(0,n.getForcedVariation)(r,i,this.datafileReader);if(a)return this.logger.debug("forced variation found",{featureKey:e,variation:a.value}),a.value;var o=this.getBucketValue(r,i),s=(0,n.getBucketedVariation)(r,i,o,this.datafileReader,this.logger);return s?s.value:(this.logger.debug("using default variation",{featureKey:e,bucketValue:o,variation:r.defaultVariation}),r.defaultVariation)}catch(t){return void this.logger.error("getVariation",{featureKey:e,error:t})}},e.prototype.getVariationBoolean=function(e,t){return void 0===t&&(t={}),s(this.getVariation(e,t),"boolean")},e.prototype.getVariationString=function(e,t){return void 0===t&&(t={}),s(this.getVariation(e,t),"string")},e.prototype.getVariationInteger=function(e,t){return void 0===t&&(t={}),s(this.getVariation(e,t),"integer")},e.prototype.getVariationDouble=function(e,t){return void 0===t&&(t={}),s(this.getVariation(e,t),"double")},e.prototype.activate=function(e,t){void 0===t&&(t={});try{var r=this.getVariation(e,t);if(void 0===r)return;var i=this.interceptAttributes?this.interceptAttributes(t):t,n={};return this.datafileReader.getAllAttributes().filter((function(e){return!0===e.capture})).forEach((function(e){void 0!==i[e.key]&&(n[e.key]=t[e.key])})),this.emitter&&this.emitter.emit("activation",e,r,i,n),this.fromInstance&&this.onActivation&&this.onActivation(e,r,i,n),r}catch(t){return void this.logger.error("activate",{featureKey:e,error:t})}},e.prototype.activateBoolean=function(e,t){return void 0===t&&(t={}),s(this.activate(e,t),"boolean")},e.prototype.activateString=function(e,t){return void 0===t&&(t={}),s(this.activate(e,t),"string")},e.prototype.activateInteger=function(e,t){return void 0===t&&(t={}),s(this.activate(e,t),"integer")},e.prototype.activateDouble=function(e,t){return void 0===t&&(t={}),s(this.activate(e,t),"double")},e.prototype.getVariable=function(e,t,r){void 0===r&&(r={});try{var i=this.getFeature(e);if(!i)return void this.logger.warn("feature not found in datafile",{featureKey:e,variableKey:t});var a=Array.isArray(i.variablesSchema)?i.variablesSchema.find((function(e){return e.key===t})):void 0;if(!a)return void this.logger.warn("variable schema not found",{featureKey:e,variableKey:t});var o=this.interceptAttributes?this.interceptAttributes(r):r,s=(0,n.getForcedVariableValue)(i,a,o,this.datafileReader);if(void 0!==s)return this.logger.debug("forced variable value found",{featureKey:e,variableKey:t}),s;var u=this.getBucketValue(i,o);return(0,n.getBucketedVariableValue)(i,a,o,u,this.datafileReader,this.logger)}catch(r){return void this.logger.error("getVariable",{featureKey:e,variableKey:t,error:r})}},e.prototype.getVariableBoolean=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"boolean")},e.prototype.getVariableString=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"string")},e.prototype.getVariableInteger=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"integer")},e.prototype.getVariableDouble=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"double")},e.prototype.getVariableArray=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"array")},e.prototype.getVariableObject=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"object")},e.prototype.getVariableJSON=function(e,t,r){return void 0===r&&(r={}),s(this.getVariable(e,t,r),"json")},e}();t.FeaturevisorSDK=u},243:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.allConditionsAreMatched=t.conditionIsMatched=void 0;var i=r(480);function n(e,t){var r=e.attribute,n=e.operator,a=e.value;if("equals"===n)return t[r]===a;if("notEquals"===n)return t[r]!==a;if("string"==typeof t[r]&&Array.isArray(a)){var o=t[r];if("in"===n)return-1!==a.indexOf(o);if("notIn"===n)return-1===a.indexOf(o)}else if("string"==typeof t[r]&&"string"==typeof a){if(o=t[r],"contains"===n)return-1!==o.indexOf(a);if("notContains"===n)return-1===o.indexOf(a);if("startsWith"===n)return o.startsWith(a);if("endsWith"===n)return o.endsWith(a);if("semverEquals"===n)return 0===(0,i.compareVersions)(o,a);if("semverNotEquals"===n)return 0!==(0,i.compareVersions)(o,a);if("semverGreaterThan"===n)return 1===(0,i.compareVersions)(o,a);if("semverGreaterThanOrEquals"===n)return(0,i.compareVersions)(o,a)>=0;if("semverLessThan"===n)return-1===(0,i.compareVersions)(o,a);if("semverLessThanOrEquals"===n)return(0,i.compareVersions)(o,a)<=0}else if("number"==typeof t[r]&&"number"==typeof a){if(o=t[r],"greaterThan"===n)return o>a;if("greaterThanOrEquals"===n)return o>=a;if("lessThan"===n)return o<a;if("lessThanOrEquals"===n)return o<=a}return!1}t.conditionIsMatched=n,t.allConditionsAreMatched=function e(t,r){return"attribute"in t?n(t,r):"and"in t&&Array.isArray(t.and)?t.and.every((function(t){return e(t,r)})):"or"in t&&Array.isArray(t.or)?t.or.some((function(t){return e(t,r)})):"not"in t&&Array.isArray(t.not)?t.not.every((function(i){return!1===e({and:t.not},r)})):!!Array.isArray(t)&&t.every((function(t){return e(t,r)}))}},292:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createInstance=void 0;var i=r(352),n=r(687),a=r(186);function o(e,t){return t.handleDatafileFetch?t.handleDatafileFetch(e):fetch(e).then((function(e){return e.json()}))}function s(e,t,r,i,n){var a,s=i.addListener.bind(i),u=i.removeListener.bind(i),f={getVariation:e.getVariation.bind(e),getVariationBoolean:e.getVariationBoolean.bind(e),getVariationInteger:e.getVariationInteger.bind(e),getVariationDouble:e.getVariationDouble.bind(e),getVariationString:e.getVariationString.bind(e),activate:e.activate,activateBoolean:e.activateBoolean.bind(e),activateInteger:e.activateInteger.bind(e),activateDouble:e.activateDouble.bind(e),activateString:e.activateString.bind(e),getVariable:e.getVariable.bind(e),getVariableBoolean:e.getVariableBoolean.bind(e),getVariableInteger:e.getVariableInteger.bind(e),getVariableDouble:e.getVariableDouble.bind(e),getVariableString:e.getVariableString.bind(e),getVariableArray:e.getVariableArray.bind(e),getVariableObject:e.getVariableObject.bind(e),setLogLevels:r.setLevels.bind(r),on:s,addListener:s,off:u,removeListener:u,removeAllListeners:i.removeAllListeners.bind(i),refresh:function(){return r.debug("refreshing datafile"),n.refreshInProgress?r.warn("refresh in progress, skipping"):t.datafileUrl?(n.refreshInProgress=!0,void o(t.datafileUrl,t).then((function(t){var a=e.getRevision()!==t.revision;e.setDatafile(t),r.info("refreshed datafile"),i.emit("refresh"),a&&i.emit("update"),n.refreshInProgress=!1})).catch((function(e){r.error("failed to refresh datafile",{error:e}),n.refreshInProgress=!1}))):r.error("cannot refresh since `datafileUrl` is not provided")},startRefreshing:function(){return t.datafileUrl?a?r.warn("refreshing has already started"):t.refreshInterval?void(a=setInterval((function(){f.refresh()}),1e3*t.refreshInterval)):r.warn("no `refreshInterval` option provided"):r.error("cannot start refreshing since `datafileUrl` is not provided")},stopRefreshing:function(){if(!a)return r.warn("refreshing has not started yet");clearInterval(a)},isReady:function(){return n.ready}};return t.datafileUrl&&t.refreshInterval&&f.startRefreshing(),f}var u={schemaVersion:"1",revision:"unknown",attributes:[],segments:[],features:[]};t.createInstance=function(e){if(!e.datafile&&!e.datafileUrl)throw new Error("Featurevisor SDK instance cannot be created without both `datafile` and `datafileUrl` options");var t=e.logger||(0,n.createLogger)(),r=new a.Emitter,f={ready:!1,refreshInProgress:!1};if(!e.datafileUrl&&e.refreshInterval&&t.warn("refreshing datafile requires `datafileUrl` option"),e.onReady&&r.addListener("ready",e.onReady),e.onActivation&&r.addListener("activation",e.onActivation),e.onRefresh&&r.addListener("refresh",e.onRefresh),e.onUpdate&&r.addListener("update",e.onUpdate),e.datafile){var l=new i.FeaturevisorSDK({datafile:e.datafile,onActivation:e.onActivation,configureBucketValue:e.configureBucketValue,logger:t,emitter:r,interceptAttributes:e.interceptAttributes,fromInstance:!0});return f.ready=!0,setTimeout((function(){r.emit("ready")}),0),s(l,e,t,r,f)}var c=new i.FeaturevisorSDK({datafile:u,onActivation:e.onActivation,configureBucketValue:e.configureBucketValue,logger:t,emitter:r,interceptAttributes:e.interceptAttributes,fromInstance:!0});return e.datafileUrl&&o(e.datafileUrl,e).then((function(e){c.setDatafile(e),f.ready=!0,r.emit("ready")})).catch((function(e){t.error("failed to fetch datafile:"),console.error(e)})),s(c,e,t,r,f)}},913:(e,t)=>{"use strict";function r(e,t){if("string"==typeof e[t]&&"*"!==e[t])try{e[t]=JSON.parse(e[t])}catch(e){console.error("Error parsing JSON",e)}return e}Object.defineProperty(t,"__esModule",{value:!0}),t.DatafileReader=t.parseJsonConditionsIfStringified=void 0,t.parseJsonConditionsIfStringified=r;var i=function(){function e(e){this.schemaVersion=e.schemaVersion,this.revision=e.revision,this.segments=e.segments,this.attributes=e.attributes,this.features=e.features}return e.prototype.getRevision=function(){return this.revision},e.prototype.getSchemaVersion=function(){return this.schemaVersion},e.prototype.getAllAttributes=function(){return this.attributes},e.prototype.getAttribute=function(e){return this.attributes.find((function(t){return t.key===e}))},e.prototype.getSegment=function(e){var t=this.segments.find((function(t){return t.key===e}));if(t)return r(t,"conditions")},e.prototype.getFeature=function(e){var t=this.features.find((function(t){return t.key===e}));if(t)return t},e}();t.DatafileReader=i},186:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Emitter=void 0;var r=function(){function e(){this._listeners={}}return e.prototype.addListener=function(e,t){void 0===this._listeners[e]&&(this._listeners[e]=[]),this._listeners[e].push(t)},e.prototype.removeListener=function(e,t){if(void 0!==this._listeners[e]){var r=this._listeners[e].indexOf(t);-1!==r&&this._listeners[e].splice(r,1)}},e.prototype.removeAllListeners=function(e){var t=this;e?this._listeners[e]=[]:Object.keys(this._listeners).forEach((function(e){t._listeners[e]=[]}))},e.prototype.emit=function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];void 0!==this._listeners[e]&&this._listeners[e].forEach((function(e){e.apply(void 0,t)}))},e}();t.Emitter=r},522:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.getBucketedVariableValue=t.getForcedVariableValue=t.getBucketedVariation=t.getForcedVariation=t.getMatchedAllocation=t.getMatchedTraffic=void 0;var i=r(590),n=r(243);function a(e,t,r,n,a){return e.find((function(e){return!(r>e.percentage||!(0,i.allGroupSegmentsAreMatched)("string"==typeof e.segments&&"*"!==e.segments?JSON.parse(e.segments):e.segments,t,n)||(a.debug("matched rule",{ruleKey:e.key}),0))}))}function o(e,t){for(var r=0,i=0,n=e.allocation;i<n.length;i++){var a=n[i];if(t<=(r+=a.percentage))return a}}function s(e,t,r){if(e.force)return e.force.find((function(e){return e.conditions?(0,n.allConditionsAreMatched)(e.conditions,t):!!e.segments&&(0,i.allGroupSegmentsAreMatched)(e.segments,t,r)}))}t.getMatchedTraffic=a,t.getMatchedAllocation=o,t.getForcedVariation=function(e,t,r){var i=s(e,t,r);if(i&&i.variation)return e.variations.find((function(e){return e.value===i.variation}))},t.getBucketedVariation=function(e,t,r,i,n){var s=a(e.traffic,t,r,i,n);if(s){if(s.variation){var u=e.variations.find((function(e){return e.value===s.variation}));if(u)return n.debug("using variation from rule",{featureKey:e.key,variation:u.value,ruleKey:s.key}),u}var f=o(s,r);if(f){var l=f.variation,c=e.variations.find((function(e){return e.value===l}));if(c)return n.debug("matched variation",{featureKey:e.key,variation:c.value,bucketValue:r}),c;n.debug("no matched variation found",{featureKey:e.key,variation:l,bucketValue:r})}else n.debug("no matched allocation found",{featureKey:e.key,bucketValue:r})}else n.debug("no matched rule found",{featureKey:e.key,bucketValue:r})},t.getForcedVariableValue=function(e,t,r,i){var n=s(e,r,i);if(n&&n.variables){var a=n.variables[t.key];return"string"==typeof a&&"json"===t.type?JSON.parse(a):a}},t.getBucketedVariableValue=function(e,t,r,s,u,f){var l,c=a(e.traffic,r,s,u,f);if(c){var d=t.key;if(c.variables&&void 0!==c.variables[d])return f.debug("using variable from rule",{featureKey:e.key,variableKey:d,bucketValue:s}),c.variables[d];var g=o(c,s);if(g){var v=g.variation,p=e.variations.find((function(e){return e.value===v}));if(p){var h=null===(l=p.variables)||void 0===l?void 0:l.find((function(e){return e.key===d}));if(!h)return f.debug("using default value as variation has no variable",{featureKey:e.key,variableKey:d,variation:v,bucketValue:s}),"json"===t.type?JSON.parse(t.defaultValue):t.defaultValue;if(h.overrides){var y=h.overrides.find((function(e){return e.conditions?(0,n.allConditionsAreMatched)("string"==typeof e.conditions?JSON.parse(e.conditions):e.conditions,r):!!e.segments&&(0,i.allGroupSegmentsAreMatched)("string"==typeof e.segments&&"*"!==e.segments?JSON.parse(e.segments):e.segments,r,u)}));if(y)return f.debug("using override value from variation",{feature:e.key,variableKey:d,variation:v,bucketValue:s}),"json"===t.type?JSON.parse(y.value):y.value}return f.debug("using value from variation",{feature:e.key,variableKey:d,variation:v,bucketValue:s}),"json"===t.type?JSON.parse(h.value):h.value}f.debug("no matched variation found",{feature:e.key,variableKey:d,variation:v,bucketValue:s})}else f.debug("no matched allocation found",{featureKey:e.key,variableKey:d,bucketValue:s})}else f.debug("no matched rule found",{featureKey:e.key,variableKey:t.key,bucketValue:s})}},97:function(e,t,r){"use strict";var i=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,n)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||i(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),n(r(725),t),n(r(352),t),n(r(292),t),n(r(687),t)},687:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createLogger=t.Logger=t.defaultLogHandler=t.defaultLogLevels=t.loggerPrefix=void 0,t.loggerPrefix="[Featurevisor]",t.defaultLogLevels=["warn","error"],t.defaultLogHandler=function(e,r,i){switch(void 0===i&&(i={}),e){case"debug":console.log(t.loggerPrefix,r,i);case"info":console.info(t.loggerPrefix,r,i);case"warn":console.warn(t.loggerPrefix,r,i);case"error":console.error(t.loggerPrefix,r,i)}};var r=function(){function e(e){this.levels=e.levels,this.handle=e.handler}return e.prototype.setLevels=function(e){this.levels=e},e.prototype.log=function(e,t,r){-1!==this.levels.indexOf(e)&&this.handle(e,t,r)},e.prototype.debug=function(e,t){this.log("debug",e,t)},e.prototype.info=function(e,t){this.log("info",e,t)},e.prototype.warn=function(e,t){this.log("warn",e,t)},e.prototype.error=function(e,t){this.log("error",e,t)},e}();t.Logger=r,t.createLogger=function(e){void 0===e&&(e={});var i=e.levels||t.defaultLogLevels,n=e.handler||t.defaultLogHandler;return new r({levels:i,handler:n})}},590:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.allGroupSegmentsAreMatched=t.segmentIsMatched=void 0;var i=r(243);function n(e,t){return(0,i.allConditionsAreMatched)(e.conditions,t)}t.segmentIsMatched=n,t.allGroupSegmentsAreMatched=function e(t,r,i){if("*"===t)return!0;if("string"==typeof t){var a=i.getSegment(t);return!!a&&n(a,r)}if("object"==typeof t){if("and"in t&&Array.isArray(t.and))return t.and.every((function(t){return e(t,r,i)}));if("or"in t&&Array.isArray(t.or))return t.or.some((function(t){return e(t,r,i)}));if("not"in t&&Array.isArray(t.not))return t.not.every((function(t){return!1===e(t,r,i)}))}return!!Array.isArray(t)&&t.every((function(t){return e(t,r,i)}))}}},t={};function r(i){var n=t[i];if(void 0!==n)return n.exports;var a=t[i]={exports:{}};return e[i].call(a.exports,a,a.exports,r),a.exports}return r.d=(e,t)=>{for(var i in t)r.o(t,i)&&!r.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(97)})()));
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.gz CHANGED
Binary file