Comparing version 2.0.51 to 2.0.52
@@ -0,1 +1,3 @@ | ||
export declare function optionNodeLabelOrText(node: Element, allowTextContent?: "allow textContent" | "no textContent"): string; | ||
export declare function optionsForNodeChildren(children: HTMLCollection): string[]; | ||
export declare function startHTMLExperiments(): void; |
@@ -5,5 +5,18 @@ "use strict"; | ||
var util_1 = require("./util"); | ||
function optionNodeLabelOrText(node) { | ||
return node.getAttribute("option") || util_1.hash(node.outerHTML).toString(); | ||
function optionNodeLabelOrText(node, allowTextContent) { | ||
if (allowTextContent === void 0) { allowTextContent = "allow textContent"; } | ||
return (node.getAttribute("data-option") || | ||
node.getAttribute("option") || | ||
(allowTextContent ? node.textContent : undefined) || | ||
util_1.hash(node.outerHTML).toString()); | ||
} | ||
exports.optionNodeLabelOrText = optionNodeLabelOrText; | ||
function optionsForNodeChildren(children) { | ||
var options = util_1.unique(util_1.map(children, function (c) { return optionNodeLabelOrText(c); })); | ||
if (options.length !== children.length) { | ||
options = util_1.map(children, function (c) { return optionNodeLabelOrText(c, "no textContent"); }); | ||
} | ||
return options; | ||
} | ||
exports.optionsForNodeChildren = optionsForNodeChildren; | ||
function getTagExperiments() { | ||
@@ -13,5 +26,6 @@ var experiments = {}; | ||
util_1.each(customTagNodes, function (node) { | ||
var name = node.getAttribute("experiment") || | ||
var name = node.getAttribute("data-experiment") || | ||
node.getAttribute("experiment") || | ||
util_1.hash(node.innerHTML).toString(); | ||
var data = { node: node, options: util_1.map(node.children, optionNodeLabelOrText) }; | ||
var data = { node: node, options: optionsForNodeChildren(node.children) }; | ||
var list = experiments[name] || []; | ||
@@ -18,0 +32,0 @@ list.push(data); |
@@ -535,5 +535,18 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.autotune = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
var util_1 = require("./util"); | ||
function optionNodeLabelOrText(node) { | ||
return node.getAttribute("option") || util_1.hash(node.outerHTML).toString(); | ||
function optionNodeLabelOrText(node, allowTextContent) { | ||
if (allowTextContent === void 0) { allowTextContent = "allow textContent"; } | ||
return (node.getAttribute("data-option") || | ||
node.getAttribute("option") || | ||
(allowTextContent ? node.textContent : undefined) || | ||
util_1.hash(node.outerHTML).toString()); | ||
} | ||
exports.optionNodeLabelOrText = optionNodeLabelOrText; | ||
function optionsForNodeChildren(children) { | ||
var options = util_1.unique(util_1.map(children, function (c) { return optionNodeLabelOrText(c); })); | ||
if (options.length !== children.length) { | ||
options = util_1.map(children, function (c) { return optionNodeLabelOrText(c, "no textContent"); }); | ||
} | ||
return options; | ||
} | ||
exports.optionsForNodeChildren = optionsForNodeChildren; | ||
function getTagExperiments() { | ||
@@ -543,5 +556,6 @@ var experiments = {}; | ||
util_1.each(customTagNodes, function (node) { | ||
var name = node.getAttribute("experiment") || | ||
var name = node.getAttribute("data-experiment") || | ||
node.getAttribute("experiment") || | ||
util_1.hash(node.innerHTML).toString(); | ||
var data = { node: node, options: util_1.map(node.children, optionNodeLabelOrText) }; | ||
var data = { node: node, options: optionsForNodeChildren(node.children) }; | ||
var list = experiments[name] || []; | ||
@@ -746,2 +760,11 @@ list.push(data); | ||
exports.getOwnPropertyValues = getOwnPropertyValues; | ||
function unique(xs) { | ||
var o = {}; | ||
for (var _i = 0, xs_1 = xs; _i < xs_1.length; _i++) { | ||
var x = xs_1[_i]; | ||
o[x] = x; | ||
} | ||
return getOwnPropertyValues(o); | ||
} | ||
exports.unique = unique; | ||
function mapObject(x, f) { | ||
@@ -748,0 +771,0 @@ var result = {}; |
@@ -1,1 +0,1 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).autotune=e()}}(function(){return function r(a,u,s){function c(t,e){if(!u[t]){if(!a[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(p)return p(t,!0);var o=new Error("Cannot find module '"+t+"'");throw o.code="MODULE_NOT_FOUND",o}var i=u[t]={exports:{}};a[t][0].call(i.exports,function(e){return c(a[t][1][e]||e)},i,i.exports,r,a,u,s)}return u[t].exports}for(var p="function"==typeof require&&require,e=0;e<s.length;e++)c(s[e]);return c}({1:[function(e,t,n){"use strict";n.__esModule=!0;var o=e("./html"),i=!!("undefined"!=typeof window)&&("localhost"===window.location.hostname||-1!==window.location.search.indexOf("autotune-debug")),r=function(){function e(){}return e.prototype.log=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];i&&console.log.apply(console,["AUTOTUNE"].concat(e))},e.prototype.error=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.error.apply(console,["AUTOTUNE"].concat(e))},e.prototype.getTimeZoneOffset=function(){return(new Date).getTimezoneOffset()},e.prototype.getLocalLanguage=function(){try{var e=navigator;return e.language||e.userLanguage}catch(e){return}},e.prototype.http=function(e,t,n,o,i){if("undefined"==typeof XMLHttpRequest)return i(new Error("Not running in browser"));try{var r=new XMLHttpRequest;r.open(e,t,!0),r.setRequestHeader("Content-Type","application/json"),r.onerror=function(){return i(new Error(r.statusText))},r.onreadystatechange=function(){4===r.readyState&&(200===r.status?o(JSON.parse(r.responseText)):i(new Error("Request failed with status "+r.status)))},void 0!==n?r.send(JSON.stringify(n)):r.send()}catch(e){return i(e)}},e.prototype.getLocalStorage=function(e){var t=localStorage[e];if("string"==typeof t)return t},e.prototype.setLocalStorage=function(e,t){localStorage[e]=t},e.prototype.startHTMLExperiments=function(){o.startHTMLExperiments()},e.prototype.autocomplete=function(n){for(var e=function(e,t){setTimeout(function(){return n(t)},1e3*e)},t=0,o=[[10,.1],[60,.2]];t<o.length;t++){var i=o[t];e(i[0],i[1])}},e}();n.BrowserEnvironment=r},{"./html":7}],2:[function(e,t,n){"use strict";n.__esModule=!0;var a=e("./common/ClientAPI"),s=e("./common/models"),p=e("./util"),c=e("./debounce"),u=e("./DecisionTree");function f(e){return"https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod/"+e}n.apiURL=f;var l=function(e,t){void 0===e&&(e=void 0),void 0===t&&(t=1),this.bestOption=e,this.epsilon=t};n.ExperimentOptions=l;var o=function(){function e(e,t,n,o){var u=this;this.environment=e,this.appKey=t,this.experimentOptions={},this.experiments={},this.defaultCompletions={},this.queuedCompletedExperiments={},this.queuedStartedExperiments={},this.preinitialized=!1,this.serializeStateDebounced=c.debounce(function(){u.log("Writing state",u.serialized);try{var e=s.Convert.serializedStateToJson(u.serialized);u.environment.setLocalStorage(u.storageKey("state"),e)}catch(e){u.error("Could not save state:",e.message)}},100),this.startExperimentsDebounced=c.debounce(function(){var e=p.mapObject(u.queuedStartedExperiments,function(e){return{instanceKey:e.key,options:e.optionNames,pick:e.pick,pickedBest:e.pickedBest}});u.log("Starting experiments",e),u.queuedStartedExperiments={};var t={version:2,appKey:u.appKey,experiments:e,ctx:u.getClientContext()};u.environment.http("POST",f("startExperiments"),t,function(){},function(e){return u.error("Failed to start experiments",e)})},100),this.completeExperimentsDebounced=c.debounce(function(e){var t=p.getOwnPropertyValues(u.queuedCompletedExperiments);u.queuedCompletedExperiments={};for(var n={},o=0,i=t;o<i.length;o++){var r=i[o];void 0!==r.payoff&&(n[r.key]={pick:r.pick,payoff:r.payoff})}function a(){void 0!==e&&e()}u.log("Completing experiments",n),u.environment.http("POST",f("completeExperiments"),{version:1,appKey:u.appKey,experiments:n},function(){return a()},function(e){u.error("Failed to complete experiments",e),a()})},10),this.log("Initialize",t),this.preinitialized=void 0!==o,this.serialized={lastInitialized:0,experimentPicks:{}};try{var i=e.getLocalStorage(this.storageKey("state"));void 0!==i&&(this.serialized=s.Convert.toSerializedState(i))}catch(e){}var r=(new Date).getTime();864e5<r-this.serialized.lastInitialized&&(this.log("Starting a new session"),this.serialized.experimentPicks={}),this.serialized.lastInitialized=r,this.serializeStateDebounced(),void 0===o?e.http("GET",a.outcomesUrl(t),void 0,function(e){u.log("Got outcomes",e),u.finishInit(e),n(u)},function(e){u.error("Could not get outcomes",e),u.finishInit({}),n(u)}):this.finishInit(o)}return e.prototype.log=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];(e=this.environment).log.apply(e,t)},e.prototype.error=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];(e=this.environment).error.apply(e,t)},e.prototype.getClientContext=function(){if(void 0===this.clientContext){this.clientContext={tzo:this.environment.getTimeZoneOffset()};var e=this.environment.getLocalLanguage();void 0!==e&&(this.clientContext.lang=e)}return this.clientContext},e.prototype.startExperiment=function(e){this.queuedStartedExperiments[e.name]=e,this.startExperimentsDebounced()},e.prototype.completeExperiment=function(e,t){this.queuedCompletedExperiments[e.name]=e,this.completeExperimentsDebounced(t)},e.prototype.completeDefaults=function(t,n){var o=this.defaultCompletions;Object.getOwnPropertyNames(o).forEach(function(e){return o[e].complete(t,n)})},e.prototype.finishInit=function(i){var r=this;try{var a=this.getClientContext();Object.getOwnPropertyNames(i).forEach(function(e){if(void 0===r.experimentOptions[e]){var t=u.lookupBestOption(a,i[e]),n=t.option,o=t.epsilon;r.experimentOptions[e]=new l(n,o)}}),this.preinitialized&&(this.environment.startHTMLExperiments(),this.environment.autocomplete(function(e){return r.completeDefaults(e,void 0)}))}catch(e){this.error("Couldn't finish init",e)}},e.prototype.experiment=function(e,t){var n=this.experiments[e];if(void 0!==n)return n;var o=this.experimentOptions[e];return void 0===o&&(o=this.experimentOptions[e]=new l),n=this.experiments[e]=new i(this,e,t,o),this.defaultCompletions[e]=n,this.startExperiment(n),n},e.prototype.storageKey=function(e){return"autotune.v1."+this.appKey+"."+e},e.prototype.loadPick=function(e){return this.serialized.experimentPicks[e]},e.prototype.savePick=function(e,t){this.serialized.experimentPicks[e]=t,this.serializeStateDebounced()},e}();n.Client=o;var i=function(){function e(e,t,n,o){this.client=e,this.name=t,this.optionNames=n,this.options=o,this.key=p.uuidv4();var i=this.options,r=i.bestOption,a=i.epsilon,u=this.client.loadPick(this.name);if(void 0!==u&&-1!==n.indexOf(u))this.pick=u,this.pickedBest=u===r;else{var s=void 0===r||-1===n.indexOf(r)||Math.random()<a,c=void 0;c=s||void 0===r?n[Math.floor(Math.random()*n.length)]:r,this.client.savePick(this.name,c),this.pick=c,this.pickedBest=!s}}return e.prototype.complete=function(e,t){void 0===e&&(e=1),this.payoff=e,this.client.completeExperiment(this,t)},e}();n.Experiment=i},{"./DecisionTree":3,"./common/ClientAPI":4,"./common/models":5,"./debounce":6,"./util":9}],3:[function(e,t,n){"use strict";n.__esModule=!0,n.lookupBestOption=function(r,e){if(void 0===e)return{};var a=e.options;return function e(t){if(void 0!==t.best)return{option:a[t.best],epsilon:t.eps};if(void 0===t.at||void 0===t.op||void 0===t.v)return{};var n,o=r[t.at];switch("string"==typeof t.v?"string"!=typeof o&&(o="null"):"number"!=typeof o&&(o=0),t.op){case"lt":n=o<t.v;break;case"eq":n=o===t.v;break;default:return{}}var i=n?t.l:t.r;return void 0===i?{}:e(i)}(e.tree)}},{}],4:[function(e,t,n){"use strict";function o(e){return"https://s3.us-east-2.amazonaws.com/js.autotune.xyz/"+e}n.__esModule=!0,n.s3Url=o,n.outcomesUrl=function(e){return o(e+".json")},n.clientUrl=function(e){return o(e+".js")}},{}],5:[function(e,t,n){"use strict";n.__esModule=!0,function(e){function u(e,t){if("any"===e)return!0;if(null===e)return null===t;if(!1===e)return!1;for(;"object"==typeof e&&void 0!==e.ref;)e=s[e.ref];return Array.isArray(e)?(n=t,-1!==e.indexOf(n)):"object"==typeof e?e.hasOwnProperty("unionMembers")?(r=e.unionMembers,a=t,r.some(function(e){return u(e,a)})):e.hasOwnProperty("arrayItems")?(o=e.arrayItems,i=t,Array.isArray(i)&&i.every(function(e){return u(o,e)})):!!e.hasOwnProperty("props")&&function(n,o,i){if(null===i||"object"!=typeof i||Array.isArray(i))return!1;return Object.getOwnPropertyNames(i).every(function(e){var t=i[e];return Object.prototype.hasOwnProperty.call(n,e)?u(n[e],t):u(o,t)})}(e.props,e.additional,t):typeof e==typeof t;var n,o,i,r,a}e.toSerializedState=function(e){return function(e,t){if(!u(t,e))throw Error("Invalid value");return e}(JSON.parse(e),{ref:"SerializedState"})},e.serializedStateToJson=function(e){return JSON.stringify(e,null,2)};var t,n,o,s={SerializedState:(t={experimentPicks:(o="",{props:{},additional:o}),lastInitialized:0},n="any",{props:t,additional:n})}}(n.Convert||(n.Convert={}))},{}],6:[function(e,t,n){"use strict";n.__esModule=!0,n.debounce=function(i,r,a){var u;return void 0===r&&(r=50),void 0===a&&(a={isImmediate:!1}),function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=this,o=a.isImmediate&&void 0===u;void 0!==u&&clearTimeout(u),u=setTimeout(function(){u=void 0,a.isImmediate||i.apply(n,e)},r),o&&i.apply(n,e)}}},{}],7:[function(e,t,n){"use strict";n.__esModule=!0;var c=e("."),p=e("./util");function f(e){return e.getAttribute("option")||p.hash(e.outerHTML).toString()}n.startHTMLExperiments=function(){function e(){var e;!function(){var i,e,r,t,a=(r={},t=document.querySelectorAll("[data-experiment]"),p.each(t,function(e){var t=e.getAttribute("data-experiment"),n=e.getAttribute("data-option");if(null!==t&&null!==n){var o=r[t];void 0===o&&(o=r[t]={options:[],nodes:[]}),-1===o.options.indexOf(n)&&o.options.push(n),o.nodes.push(e)}}),r),o=(i={},e=document.getElementsByTagName("autotune"),p.each(e,function(e){var t=e.getAttribute("experiment")||p.hash(e.innerHTML).toString(),n={node:e,options:p.map(e.children,f)},o=i[t]||[];o.push(n),i[t]=o}),i);Object.getOwnPropertyNames(a).forEach(function(e){var t=a[e],n=t.options,o=t.nodes,i=c.oneOf(e,n);o.forEach(function(e){e.getAttribute("data-option")!==i&&e.remove()})}),Object.getOwnPropertyNames(o).forEach(function(e){var t={};o[e].forEach(function(e){e.options.forEach(function(e){return t[e]=null})});var n=c.oneOf(e,Object.getOwnPropertyNames(t));o[e].forEach(function(e){p.map(e.node.children,function(e){return e}).forEach(function(e){f(e)!==n&&e.remove()})})});var u,n,s=(u={},n=document.querySelectorAll("[autotune-class],[data-autotune-class]"),p.each(n,function(e){var t=(e.getAttribute("autotune-class")||e.getAttribute("data-autotune-class")||"").split(" "),n=e.getAttribute("autotune-experiment")||e.getAttribute("data-autotune-experiment")||p.hash(e.innerHTML)+"-class";u[n]={options:t,node:e}}),u);Object.getOwnPropertyNames(s).forEach(function(e){var t=s[e],n=c.oneOf(e,t.options);t.node.classList.add(n)})}(),e=document.querySelectorAll("a[autotune],a[data-autotune],button[autotune],button[data-autotune]"),p.each(e,function(t){var n=t.onclick;t.onclick=function(e){e.preventDefault(),c.complete(function(){"function"==typeof n?n.bind(t)(e):t instanceof HTMLAnchorElement&&(window.location.href=t.href)})}})}"complete"===document.readyState?e():document.addEventListener("DOMContentLoaded",function(){e()})}},{".":8,"./util":9}],8:[function(e,t,n){"use strict";n.__esModule=!0;var r,o=e("./Client"),a=new(e("./BrowserEnvironment").BrowserEnvironment);function i(e,t,n){void 0===r?r=new o.Client(a,e,t,n):a.error("autotune initialized more than once")}if(n.initialize=i,n.flipCoin=function(e){return void 0===r?(a.error("autotune must be initialized before calling flipCoin"),!0):"true"===r.experiment(e,["true","false"]).pick},n.oneOf=function(e,t){var n,o,i="[object Array]"===Object.prototype.toString.call(t);return n=i?t:Object.getOwnPropertyNames(t),void 0===r?(a.error("autotune must be initialized before calling oneOf"),o=n[0]):o=r.experiment(e,n).pick,i?o:t[o]},n.complete=function(e,t){var n,o;void 0!==r?(n="number"==typeof e?e:1,o="function"==typeof e?e:t,r.completeDefaults(n,o)):a.error("autotune must be initialized before calling complete")},"undefined"!=typeof window&&void 0!==window.autotuneConfig){var u=window.autotuneConfig;i(u.appKey,function(){},u.outcomes)}},{"./BrowserEnvironment":1,"./Client":2}],9:[function(e,t,n){"use strict";function o(e,t){for(var n=[],o=0;o<e.length;o++)n.push(t(e.item(o),o));return n}n.__esModule=!0,n.uuidv4=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})},n.getOwnPropertyValues=function(t){return Object.getOwnPropertyNames(t).map(function(e){return t[e]})},n.mapObject=function(t,n){var o={};return Object.getOwnPropertyNames(t).forEach(function(e){return o[e]=n(t[e],e)}),o},n.map=o,n.each=function(e,n){o(e,function(e,t){n(e,t)})},n.hash=function(e){for(var t=0,n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n),t&=t;return Math.abs(t)}},{}]},{},[8])(8)}); | ||
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).autotune=t()}}(function(){return function r(a,u,s){function c(e,t){if(!u[e]){if(!a[e]){var n="function"==typeof require&&require;if(!t&&n)return n(e,!0);if(p)return p(e,!0);var o=new Error("Cannot find module '"+e+"'");throw o.code="MODULE_NOT_FOUND",o}var i=u[e]={exports:{}};a[e][0].call(i.exports,function(t){return c(a[e][1][t]||t)},i,i.exports,r,a,u,s)}return u[e].exports}for(var p="function"==typeof require&&require,t=0;t<s.length;t++)c(s[t]);return c}({1:[function(t,e,n){"use strict";n.__esModule=!0;var o=t("./html"),i=!!("undefined"!=typeof window)&&("localhost"===window.location.hostname||-1!==window.location.search.indexOf("autotune-debug")),r=function(){function t(){}return t.prototype.log=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];i&&console.log.apply(console,["AUTOTUNE"].concat(t))},t.prototype.error=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];console.error.apply(console,["AUTOTUNE"].concat(t))},t.prototype.getTimeZoneOffset=function(){return(new Date).getTimezoneOffset()},t.prototype.getLocalLanguage=function(){try{var t=navigator;return t.language||t.userLanguage}catch(t){return}},t.prototype.http=function(t,e,n,o,i){if("undefined"==typeof XMLHttpRequest)return i(new Error("Not running in browser"));try{var r=new XMLHttpRequest;r.open(t,e,!0),r.setRequestHeader("Content-Type","application/json"),r.onerror=function(){return i(new Error(r.statusText))},r.onreadystatechange=function(){4===r.readyState&&(200===r.status?o(JSON.parse(r.responseText)):i(new Error("Request failed with status "+r.status)))},void 0!==n?r.send(JSON.stringify(n)):r.send()}catch(t){return i(t)}},t.prototype.getLocalStorage=function(t){var e=localStorage[t];if("string"==typeof e)return e},t.prototype.setLocalStorage=function(t,e){localStorage[t]=e},t.prototype.startHTMLExperiments=function(){o.startHTMLExperiments()},t.prototype.autocomplete=function(n){for(var t=function(t,e){setTimeout(function(){return n(e)},1e3*t)},e=0,o=[[10,.1],[60,.2]];e<o.length;e++){var i=o[e];t(i[0],i[1])}},t}();n.BrowserEnvironment=r},{"./html":7}],2:[function(t,e,n){"use strict";n.__esModule=!0;var a=t("./common/ClientAPI"),s=t("./common/models"),p=t("./util"),c=t("./debounce"),u=t("./DecisionTree");function f(t){return"https://2vyiuehl9j.execute-api.us-east-2.amazonaws.com/prod/"+t}n.apiURL=f;var l=function(t,e){void 0===t&&(t=void 0),void 0===e&&(e=1),this.bestOption=t,this.epsilon=e};n.ExperimentOptions=l;var o=function(){function t(t,e,n,o){var u=this;this.environment=t,this.appKey=e,this.experimentOptions={},this.experiments={},this.defaultCompletions={},this.queuedCompletedExperiments={},this.queuedStartedExperiments={},this.preinitialized=!1,this.serializeStateDebounced=c.debounce(function(){u.log("Writing state",u.serialized);try{var t=s.Convert.serializedStateToJson(u.serialized);u.environment.setLocalStorage(u.storageKey("state"),t)}catch(t){u.error("Could not save state:",t.message)}},100),this.startExperimentsDebounced=c.debounce(function(){var t=p.mapObject(u.queuedStartedExperiments,function(t){return{instanceKey:t.key,options:t.optionNames,pick:t.pick,pickedBest:t.pickedBest}});u.log("Starting experiments",t),u.queuedStartedExperiments={};var e={version:2,appKey:u.appKey,experiments:t,ctx:u.getClientContext()};u.environment.http("POST",f("startExperiments"),e,function(){},function(t){return u.error("Failed to start experiments",t)})},100),this.completeExperimentsDebounced=c.debounce(function(t){var e=p.getOwnPropertyValues(u.queuedCompletedExperiments);u.queuedCompletedExperiments={};for(var n={},o=0,i=e;o<i.length;o++){var r=i[o];void 0!==r.payoff&&(n[r.key]={pick:r.pick,payoff:r.payoff})}function a(){void 0!==t&&t()}u.log("Completing experiments",n),u.environment.http("POST",f("completeExperiments"),{version:1,appKey:u.appKey,experiments:n},function(){return a()},function(t){u.error("Failed to complete experiments",t),a()})},10),this.log("Initialize",e),this.preinitialized=void 0!==o,this.serialized={lastInitialized:0,experimentPicks:{}};try{var i=t.getLocalStorage(this.storageKey("state"));void 0!==i&&(this.serialized=s.Convert.toSerializedState(i))}catch(t){}var r=(new Date).getTime();864e5<r-this.serialized.lastInitialized&&(this.log("Starting a new session"),this.serialized.experimentPicks={}),this.serialized.lastInitialized=r,this.serializeStateDebounced(),void 0===o?t.http("GET",a.outcomesUrl(e),void 0,function(t){u.log("Got outcomes",t),u.finishInit(t),n(u)},function(t){u.error("Could not get outcomes",t),u.finishInit({}),n(u)}):this.finishInit(o)}return t.prototype.log=function(){for(var t,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];(t=this.environment).log.apply(t,e)},t.prototype.error=function(){for(var t,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];(t=this.environment).error.apply(t,e)},t.prototype.getClientContext=function(){if(void 0===this.clientContext){this.clientContext={tzo:this.environment.getTimeZoneOffset()};var t=this.environment.getLocalLanguage();void 0!==t&&(this.clientContext.lang=t)}return this.clientContext},t.prototype.startExperiment=function(t){this.queuedStartedExperiments[t.name]=t,this.startExperimentsDebounced()},t.prototype.completeExperiment=function(t,e){this.queuedCompletedExperiments[t.name]=t,this.completeExperimentsDebounced(e)},t.prototype.completeDefaults=function(e,n){var o=this.defaultCompletions;Object.getOwnPropertyNames(o).forEach(function(t){return o[t].complete(e,n)})},t.prototype.finishInit=function(i){var r=this;try{var a=this.getClientContext();Object.getOwnPropertyNames(i).forEach(function(t){if(void 0===r.experimentOptions[t]){var e=u.lookupBestOption(a,i[t]),n=e.option,o=e.epsilon;r.experimentOptions[t]=new l(n,o)}}),this.preinitialized&&(this.environment.startHTMLExperiments(),this.environment.autocomplete(function(t){return r.completeDefaults(t,void 0)}))}catch(t){this.error("Couldn't finish init",t)}},t.prototype.experiment=function(t,e){var n=this.experiments[t];if(void 0!==n)return n;var o=this.experimentOptions[t];return void 0===o&&(o=this.experimentOptions[t]=new l),n=this.experiments[t]=new i(this,t,e,o),this.defaultCompletions[t]=n,this.startExperiment(n),n},t.prototype.storageKey=function(t){return"autotune.v1."+this.appKey+"."+t},t.prototype.loadPick=function(t){return this.serialized.experimentPicks[t]},t.prototype.savePick=function(t,e){this.serialized.experimentPicks[t]=e,this.serializeStateDebounced()},t}();n.Client=o;var i=function(){function t(t,e,n,o){this.client=t,this.name=e,this.optionNames=n,this.options=o,this.key=p.uuidv4();var i=this.options,r=i.bestOption,a=i.epsilon,u=this.client.loadPick(this.name);if(void 0!==u&&-1!==n.indexOf(u))this.pick=u,this.pickedBest=u===r;else{var s=void 0===r||-1===n.indexOf(r)||Math.random()<a,c=void 0;c=s||void 0===r?n[Math.floor(Math.random()*n.length)]:r,this.client.savePick(this.name,c),this.pick=c,this.pickedBest=!s}}return t.prototype.complete=function(t,e){void 0===t&&(t=1),this.payoff=t,this.client.completeExperiment(this,e)},t}();n.Experiment=i},{"./DecisionTree":3,"./common/ClientAPI":4,"./common/models":5,"./debounce":6,"./util":9}],3:[function(t,e,n){"use strict";n.__esModule=!0,n.lookupBestOption=function(r,t){if(void 0===t)return{};var a=t.options;return function t(e){if(void 0!==e.best)return{option:a[e.best],epsilon:e.eps};if(void 0===e.at||void 0===e.op||void 0===e.v)return{};var n,o=r[e.at];switch("string"==typeof e.v?"string"!=typeof o&&(o="null"):"number"!=typeof o&&(o=0),e.op){case"lt":n=o<e.v;break;case"eq":n=o===e.v;break;default:return{}}var i=n?e.l:e.r;return void 0===i?{}:t(i)}(t.tree)}},{}],4:[function(t,e,n){"use strict";function o(t){return"https://s3.us-east-2.amazonaws.com/js.autotune.xyz/"+t}n.__esModule=!0,n.s3Url=o,n.outcomesUrl=function(t){return o(t+".json")},n.clientUrl=function(t){return o(t+".js")}},{}],5:[function(t,e,n){"use strict";n.__esModule=!0,function(t){function u(t,e){if("any"===t)return!0;if(null===t)return null===e;if(!1===t)return!1;for(;"object"==typeof t&&void 0!==t.ref;)t=s[t.ref];return Array.isArray(t)?(n=e,-1!==t.indexOf(n)):"object"==typeof t?t.hasOwnProperty("unionMembers")?(r=t.unionMembers,a=e,r.some(function(t){return u(t,a)})):t.hasOwnProperty("arrayItems")?(o=t.arrayItems,i=e,Array.isArray(i)&&i.every(function(t){return u(o,t)})):!!t.hasOwnProperty("props")&&function(n,o,i){if(null===i||"object"!=typeof i||Array.isArray(i))return!1;return Object.getOwnPropertyNames(i).every(function(t){var e=i[t];return Object.prototype.hasOwnProperty.call(n,t)?u(n[t],e):u(o,e)})}(t.props,t.additional,e):typeof t==typeof e;var n,o,i,r,a}t.toSerializedState=function(t){return function(t,e){if(!u(e,t))throw Error("Invalid value");return t}(JSON.parse(t),{ref:"SerializedState"})},t.serializedStateToJson=function(t){return JSON.stringify(t,null,2)};var e,n,o,s={SerializedState:(e={experimentPicks:(o="",{props:{},additional:o}),lastInitialized:0},n="any",{props:e,additional:n})}}(n.Convert||(n.Convert={}))},{}],6:[function(t,e,n){"use strict";n.__esModule=!0,n.debounce=function(i,r,a){var u;return void 0===r&&(r=50),void 0===a&&(a={isImmediate:!1}),function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=this,o=a.isImmediate&&void 0===u;void 0!==u&&clearTimeout(u),u=setTimeout(function(){u=void 0,a.isImmediate||i.apply(n,t)},r),o&&i.apply(n,t)}}},{}],7:[function(t,e,n){"use strict";n.__esModule=!0;var c=t("."),p=t("./util");function f(t,e){return void 0===e&&(e="allow textContent"),t.getAttribute("data-option")||t.getAttribute("option")||(e?t.textContent:void 0)||p.hash(t.outerHTML).toString()}function l(t){var e=p.unique(p.map(t,function(t){return f(t)}));return e.length!==t.length&&(e=p.map(t,function(t){return f(t,"no textContent")})),e}n.optionNodeLabelOrText=f,n.optionsForNodeChildren=l,n.startHTMLExperiments=function(){function t(){var t;!function(){var i,t,r,e,a=(r={},e=document.querySelectorAll("[data-experiment]"),p.each(e,function(t){var e=t.getAttribute("data-experiment"),n=t.getAttribute("data-option");if(null!==e&&null!==n){var o=r[e];void 0===o&&(o=r[e]={options:[],nodes:[]}),-1===o.options.indexOf(n)&&o.options.push(n),o.nodes.push(t)}}),r),o=(i={},t=document.getElementsByTagName("autotune"),p.each(t,function(t){var e=t.getAttribute("data-experiment")||t.getAttribute("experiment")||p.hash(t.innerHTML).toString(),n={node:t,options:l(t.children)},o=i[e]||[];o.push(n),i[e]=o}),i);Object.getOwnPropertyNames(a).forEach(function(t){var e=a[t],n=e.options,o=e.nodes,i=c.oneOf(t,n);o.forEach(function(t){t.getAttribute("data-option")!==i&&t.remove()})}),Object.getOwnPropertyNames(o).forEach(function(t){var e={};o[t].forEach(function(t){t.options.forEach(function(t){return e[t]=null})});var n=c.oneOf(t,Object.getOwnPropertyNames(e));o[t].forEach(function(t){p.map(t.node.children,function(t){return t}).forEach(function(t){f(t)!==n&&t.remove()})})});var u,n,s=(u={},n=document.querySelectorAll("[autotune-class],[data-autotune-class]"),p.each(n,function(t){var e=(t.getAttribute("autotune-class")||t.getAttribute("data-autotune-class")||"").split(" "),n=t.getAttribute("autotune-experiment")||t.getAttribute("data-autotune-experiment")||p.hash(t.innerHTML)+"-class";u[n]={options:e,node:t}}),u);Object.getOwnPropertyNames(s).forEach(function(t){var e=s[t],n=c.oneOf(t,e.options);e.node.classList.add(n)})}(),t=document.querySelectorAll("a[autotune],a[data-autotune],button[autotune],button[data-autotune]"),p.each(t,function(e){var n=e.onclick;e.onclick=function(t){t.preventDefault(),c.complete(function(){"function"==typeof n?n.bind(e)(t):e instanceof HTMLAnchorElement&&(window.location.href=e.href)})}})}"complete"===document.readyState?t():document.addEventListener("DOMContentLoaded",function(){t()})}},{".":8,"./util":9}],8:[function(t,e,n){"use strict";n.__esModule=!0;var r,o=t("./Client"),a=new(t("./BrowserEnvironment").BrowserEnvironment);function i(t,e,n){void 0===r?r=new o.Client(a,t,e,n):a.error("autotune initialized more than once")}if(n.initialize=i,n.flipCoin=function(t){return void 0===r?(a.error("autotune must be initialized before calling flipCoin"),!0):"true"===r.experiment(t,["true","false"]).pick},n.oneOf=function(t,e){var n,o,i="[object Array]"===Object.prototype.toString.call(e);return n=i?e:Object.getOwnPropertyNames(e),void 0===r?(a.error("autotune must be initialized before calling oneOf"),o=n[0]):o=r.experiment(t,n).pick,i?o:e[o]},n.complete=function(t,e){var n,o;void 0!==r?(n="number"==typeof t?t:1,o="function"==typeof t?t:e,r.completeDefaults(n,o)):a.error("autotune must be initialized before calling complete")},"undefined"!=typeof window&&void 0!==window.autotuneConfig){var u=window.autotuneConfig;i(u.appKey,function(){},u.outcomes)}},{"./BrowserEnvironment":1,"./Client":2}],9:[function(t,e,n){"use strict";function r(e){return Object.getOwnPropertyNames(e).map(function(t){return e[t]})}function o(t,e){for(var n=[],o=0;o<t.length;o++)n.push(e(t.item(o),o));return n}n.__esModule=!0,n.uuidv4=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})},n.getOwnPropertyValues=r,n.unique=function(t){for(var e={},n=0,o=t;n<o.length;n++){var i=o[n];e[i]=i}return r(e)},n.mapObject=function(e,n){var o={};return Object.getOwnPropertyNames(e).forEach(function(t){return o[t]=n(e[t],t)}),o},n.map=o,n.each=function(t,n){o(t,function(t,e){n(t,e)})},n.hash=function(t){for(var e=0,n=0;n<t.length;n++)e=(e<<5)-e+t.charCodeAt(n),e&=e;return Math.abs(e)}},{}]},{},[8])(8)}); |
@@ -5,2 +5,3 @@ export declare function uuidv4(): string; | ||
}): T[]; | ||
export declare function unique(xs: string[]): string[]; | ||
export declare function mapObject<T, S>(x: { | ||
@@ -7,0 +8,0 @@ [name: string]: T; |
@@ -15,2 +15,11 @@ "use strict"; | ||
exports.getOwnPropertyValues = getOwnPropertyValues; | ||
function unique(xs) { | ||
var o = {}; | ||
for (var _i = 0, xs_1 = xs; _i < xs_1.length; _i++) { | ||
var x = xs_1[_i]; | ||
o[x] = x; | ||
} | ||
return getOwnPropertyValues(o); | ||
} | ||
exports.unique = unique; | ||
function mapObject(x, f) { | ||
@@ -17,0 +26,0 @@ var result = {}; |
{ | ||
"name": "autotune", | ||
"license": "Apache-2.0", | ||
"version": "2.0.51", | ||
"version": "2.0.52", | ||
"description": "Automatically tune JavaScript apps", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
274222
56
2966