split-testing
Version:
Lightweight split-testing library (A/B or multivariant)
3 lines (2 loc) • 3.55 kB
JavaScript
var SplitTesting=(()=>{var g=Object.defineProperty,x=Object.defineProperties;var V=Object.getOwnPropertyDescriptors;var f=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var c=(e,n,t)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,h=(e,n)=>{for(var t in n||(n={}))W.call(n,t)&&c(e,t,n[t]);if(f)for(var t of f(n))k.call(n,t)&&c(e,t,n[t]);return e},p=(e,n)=>x(e,V(n)),E=e=>g(e,"__esModule",{value:!0});var I=(e,n)=>{E(e);for(var t in n)g(e,t,{get:n[t],enumerable:!0})};var L={};I(L,{setExperiment:()=>R});function A(e,n){n&&console.log("SplitTesting.js -",e)}function w(e){return n=>{A(n,e)}}function l(e){console.warn("SplitTesting.js -",e)}function d(e){return new Error(`SplitTesting.js - ${e}`)}function u(e){if(typeof e=="function")return e;let n=Array.isArray(e)?[]:{};for(var t in e){let r=e[t],a={}.toString.call(r).slice(8,-1);a==="Array"||a==="Object"?n[t]=u(r):a==="Date"?n[t]=new Date(r.getTime()):n[t]=r}return n}function v(e){let n=e.some(a=>a.weight!==void 0),t=e.every(a=>a.weight!==void 0),r=e.reduce((a,i)=>{var o;return a+((o=i.weight)!=null?o:0)},0);return n&&!t?(l("Some items have a weight but not all of them, reset of all weight"),!1):t&&r!==1?(l("The total of all weight is not equal to 1, reset of all weight"),!1):!!n}function y(e){let n=1/e.length;return e.map(r=>(r.weight=n,r))}function S(e,n){let t=n!==void 0?P(n):Math.random();return e.find(a=>{if(t<a.weight)return a;t-=a.weight})}function P(e){return(e.split("").map(a=>a.charCodeAt(0)).reduce((a,i)=>a+i,0)*9301+49297)%233280/233280}function R(e){var i,o,m;if(typeof e.name!="string"||e.name.length===0)throw d("The experiment name must be a non-empty string");if(!Array.isArray(e.variants)||e.variants.length===0)throw d("The variants must be an array of at least one element");if(!e.variants.every(s=>s.name!==void 0&&s.name.length>0))throw d("The variants must have a name");if(e.storage=(i=e.storage)!=null?i:globalThis.window!==void 0?globalThis.window.localStorage:void 0,e.storage===void 0)throw d("No storage available, please define a custom storage property");let t=p(h({},e),{name:e.name,variants:u(e.variants),seed:e.seed,isDebugMode:(o=e.isDebugMode)!=null?o:!1,isResolvingSeedConflictAllowed:(m=e.isResolvingSeedConflictAllowed)!=null?m:!0,storage:e.storage}),r=w(t.isDebugMode);r("Running split testing with these options :"),r(t);let a=T(t);if(a===null){r("No variant picked in storage, picking it now");let s=b(t,r);return t.onFirstPicking!==void 0&&t.onFirstPicking(s),s}else{if(r(`Variant already picked, named ${a.name}`),T(t)===void 0)throw d(`The variant named ${a.name} is not valid (not found in the variants)`);return t.isResolvingSeedConflictAllowed&&t.seed!==void 0&&!$(t)?(l("Conflict between the old seed and the current seed, updating the variant for the current seed"),b(t,r)):a}}function T(e){let n=e.storage.getItem(`ST-${e.name}-variant-name`);if(n===null)return null;let t=e.variants.find(r=>r.name===n);return t!=null?t:null}function O({name:e,storage:n,seed:t},r,a){n.setItem(`ST-${e}-variant-name`,r.name),a(`New picked variant: ${r.name} ${t!==void 0?"(with seed)":""}`),t!==void 0?n.setItem(`ST-${e}-seed`,t):n.removeItem(`ST-${e}-seed`)}function b(e,n){let t=e.variants,r=e.seed,a=v(t),i=a?t:y(t);a||n("Making all weight equal so the variants have the same probability of being picked");let o=S(i,r);return O(e,o,n),o}function $({name:e,seed:n,storage:t}){return t.getItem(`ST-${e}-seed`)===n}return L;})();
//# sourceMappingURL=bundle.js.map