@uniformdev/context 20.6.2-alpha.10 → 20.6.5-alpha.1

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/dist/index.js CHANGED
@@ -53,9 +53,7 @@ __export(src_exports, {
53
53
  PAIR_SEP: () => PAIR_SEP,
54
54
  QUIRK_SEP: () => QUIRK_SEP,
55
55
  SERVER_STATE_ID: () => SERVER_STATE_ID,
56
- STRONGEST_SCORE_PERSONALIZATION_ALGORITHM: () => STRONGEST_SCORE_PERSONALIZATION_ALGORITHM,
57
56
  ScriptType: () => ScriptType,
58
- TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM: () => TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM,
59
57
  TYPE_SEP: () => TYPE_SEP,
60
58
  TransitionDataStore: () => TransitionDataStore,
61
59
  UNIFORM_DEFAULT_COOKIE_NAME: () => UNIFORM_DEFAULT_COOKIE_NAME,
@@ -97,9 +95,7 @@ __export(src_exports, {
97
95
  serializePersonalizeVariants: () => serializePersonalizeVariants,
98
96
  serializeQuickConnect: () => serializeQuickConnect,
99
97
  serializeQuirks: () => serializeQuirks,
100
- strongestScorePersonalizationSelectionAlgorithm: () => strongestScorePersonalizationSelectionAlgorithm,
101
- testVariations: () => testVariations,
102
- topDownCriteriaPersonalizationSelectionAlgorithm: () => topDownCriteriaPersonalizationSelectionAlgorithm
98
+ testVariations: () => testVariations
103
99
  });
104
100
  module.exports = __toCommonJS(src_exports);
105
101
 
@@ -609,7 +605,7 @@ var GroupCriteriaEvaluator = class {
609
605
  _evaluators = new WeakMap();
610
606
 
611
607
  // src/placement/criteria/evaluateVariantMatch.ts
612
- function evaluateVariantMatch(variantId, match, vec, onLogMessage, quirks) {
608
+ function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
613
609
  onLogMessage == null ? void 0 : onLogMessage(["info", 301, "GROUP", { id: variantId, op: match == null ? void 0 : match.op }]);
614
610
  let result;
615
611
  try {
@@ -617,9 +613,9 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage, quirks) {
617
613
  onLogMessage == null ? void 0 : onLogMessage(["info", 302, { matched: true, description: "default variation" }]);
618
614
  result = true;
619
615
  } else if (!match.op || match.op === "&") {
620
- result = match.crit.every((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
616
+ result = match.crit.every((c) => evaluateDimensionMatch(c, vec, onLogMessage));
621
617
  } else {
622
- result = match.crit.some((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
618
+ result = match.crit.some((c) => evaluateDimensionMatch(c, vec, onLogMessage));
623
619
  }
624
620
  onLogMessage == null ? void 0 : onLogMessage(["info", 303, result]);
625
621
  } finally {
@@ -627,16 +623,9 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage, quirks) {
627
623
  }
628
624
  return result;
629
625
  }
630
- function evaluateMatch(crit, vec, quirks, onLogMessage) {
631
- if ("t" in crit && crit.t === "q") {
632
- return evaluateQuirkMatch(crit, quirks, onLogMessage);
633
- } else {
634
- return evaluateDimensionMatch(crit, vec, onLogMessage);
635
- }
636
- }
637
626
  function evaluateDimensionMatch(crit, vec, onLogMessage) {
638
627
  var _a, _b;
639
- const { l: lhs, op } = crit;
628
+ const { op, l: lhs } = crit;
640
629
  const lhsScore = (_a = vec[lhs]) != null ? _a : 0;
641
630
  if (op === "^") {
642
631
  const [cat] = lhs.split(ENR_SEPARATOR);
@@ -703,65 +692,33 @@ function evaluateDimensionMatch(crit, vec, onLogMessage) {
703
692
  }
704
693
  if (op === ">") {
705
694
  const result = lhsScore > rhsScore;
706
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
695
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
707
696
  return result;
708
697
  } else if (op === ">=") {
709
698
  const result = lhsScore >= rhsScore;
710
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
699
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
711
700
  return result;
712
701
  } else if (op === "<") {
713
702
  const result = lhsScore < rhsScore;
714
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
703
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
715
704
  return result;
716
705
  } else if (op === "<=") {
717
706
  const result = lhsScore <= rhsScore;
718
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
707
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
719
708
  return result;
720
709
  } else if (op === "=") {
721
710
  const result = lhsScore === rhsScore;
722
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
711
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
723
712
  return result;
724
713
  } else if (op === "!=") {
725
714
  const result = lhsScore !== rhsScore;
726
- explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
715
+ explain(onLogMessage, result, crit, lhsScore, rhsScore);
727
716
  return result;
728
717
  } else {
729
- onLogMessage == null ? void 0 : onLogMessage([
730
- "error",
731
- 302,
732
- {
733
- matched: false,
734
- description: `${crit.l} ${crit.op} ${crit.rDim ? `${crit.rDim}` : crit.r}: Unknown op ${crit.op}.`
735
- }
736
- ]);
737
- return false;
738
- }
739
- }
740
- function evaluateQuirkMatch(crit, quirks, onLogMessage) {
741
- var _a;
742
- const { l: targetQuirk, op, r: targetValue } = crit;
743
- const targetQuirkValue = (_a = quirks[targetQuirk]) != null ? _a : "";
744
- if (op === "=") {
745
- const result = targetQuirkValue === targetValue;
746
- explainQuirk(onLogMessage, result, crit, targetQuirkValue);
747
- return result;
748
- } else if (op === "!=") {
749
- const result = targetQuirkValue !== targetValue;
750
- explainQuirk(onLogMessage, result, crit, targetQuirkValue);
751
- return result;
752
- } else {
753
- onLogMessage == null ? void 0 : onLogMessage([
754
- "error",
755
- 302,
756
- {
757
- matched: false,
758
- description: `Quirk ${crit.l} ${crit.op} ${crit.r}: Unknown quirk op ${crit.op}.`
759
- }
760
- ]);
761
- return false;
718
+ throw new Error(`Unknown op: ${op}`);
762
719
  }
763
720
  }
764
- function explainScore(onLogMessage, result, crit, lhsScore, rhsScore) {
721
+ function explain(onLogMessage, result, crit, lhsScore, rhsScore) {
765
722
  onLogMessage == null ? void 0 : onLogMessage([
766
723
  "info",
767
724
  302,
@@ -771,20 +728,9 @@ function explainScore(onLogMessage, result, crit, lhsScore, rhsScore) {
771
728
  }
772
729
  ]);
773
730
  }
774
- function explainQuirk(onLogMessage, result, crit, lhs) {
775
- onLogMessage == null ? void 0 : onLogMessage([
776
- "info",
777
- 302,
778
- {
779
- matched: result,
780
- description: `Quirk ${crit.l}[${lhs}] ${crit.op} ${crit.r}`
781
- }
782
- ]);
783
- }
784
731
 
785
- // src/placement/topDownCriteriaPersonalizationSelectionAlgorithm.ts
786
- var TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM = "default";
787
- function topDownCriteriaPersonalizationSelectionAlgorithm({
732
+ // src/placement/personalize.ts
733
+ function personalizeVariations({
788
734
  name,
789
735
  context,
790
736
  variations,
@@ -794,34 +740,27 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
794
740
  var _a, _b, _c;
795
741
  onLogMessage == null ? void 0 : onLogMessage(["info", 300, "GROUP", { name, take }]);
796
742
  try {
797
- const variationMatches = [];
798
- const defaultVariations = [];
799
- const needsConsentToPersonalize = context.requireConsentForPersonalization;
800
- const personalizationAllowed = !needsConsentToPersonalize || context.storage.data.consent;
801
- for (const variation of variations) {
802
- const isInvalidFormat = variation.pz && (typeof variation.pz !== "object" || variation.pz === null || !("crit" in variation.pz && Array.isArray(variation.pz.crit)));
803
- let validVariation;
804
- if (isInvalidFormat) {
805
- validVariation = { id: variation.id };
806
- } else {
807
- validVariation = variation;
808
- }
809
- if ((_a = validVariation.pz) == null ? void 0 : _a.crit.length) {
810
- if (personalizationAllowed && variationMatches.length !== take && evaluateVariantMatch(variation.id, validVariation.pz, context.scores, onLogMessage, context.quirks)) {
811
- variationMatches.push(validVariation);
743
+ const variantMatches = [];
744
+ const defaultVariants = [];
745
+ const needsConsent = context.requireConsentForPersonalization;
746
+ const canEvaluate = !needsConsent || context.storage.data.consent;
747
+ for (const variant of variations) {
748
+ if ((_a = variant.pz) == null ? void 0 : _a.crit.length) {
749
+ if (canEvaluate && variantMatches.length !== take && evaluateVariantMatch(variant.id, variant.pz, context.scores, onLogMessage)) {
750
+ variantMatches.push(variant);
812
751
  }
813
752
  } else {
814
- defaultVariations.push(validVariation);
753
+ defaultVariants.push(variant);
815
754
  }
816
755
  }
817
756
  const result = [];
818
- for (let i = 0; i < variationMatches.length; i++) {
757
+ for (let i = 0; i < variantMatches.length; i++) {
819
758
  let isControl = (_b = context.storage.data.controlGroup) != null ? _b : false;
820
- const variation = variationMatches[i];
821
- if (!isControl && typeof ((_c = variation.pz) == null ? void 0 : _c.control) === "number") {
759
+ const variant = variantMatches[i];
760
+ if (!isControl && typeof ((_c = variant.pz) == null ? void 0 : _c.control) === "number") {
822
761
  isControl = context.getPersonalizeVariantControl(name, i);
823
762
  if (typeof isControl === "undefined") {
824
- isControl = rollForControlGroup(variation.pz.control);
763
+ isControl = rollForControlGroup(variant.pz.control);
825
764
  context.storage.updateData([
826
765
  {
827
766
  type: "setpersonalizecontrol",
@@ -834,13 +773,13 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
834
773
  ]);
835
774
  }
836
775
  }
837
- let variantToAdd = variation;
776
+ let variantToAdd = variant;
838
777
  if (isControl) {
839
- const defaultReplacement = defaultVariations.shift();
778
+ const defaultReplacement = defaultVariants.shift();
840
779
  if (defaultReplacement) {
841
780
  variantToAdd = {
842
781
  ...defaultReplacement,
843
- id: variation.id
782
+ id: variant.id
844
783
  };
845
784
  } else {
846
785
  variantToAdd = void 0;
@@ -850,8 +789,8 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
850
789
  result.push({ ...variantToAdd, control: isControl });
851
790
  }
852
791
  }
853
- while (result.length < take && defaultVariations.length) {
854
- result.push({ ...defaultVariations.shift(), control: false });
792
+ while (result.length < take && defaultVariants.length) {
793
+ result.push({ ...defaultVariants.shift(), control: false });
855
794
  }
856
795
  const personalized = result.some((v) => {
857
796
  var _a2;
@@ -866,82 +805,7 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
866
805
  }
867
806
  }
868
807
 
869
- // src/placement/personalizeVariations.ts
870
- function personalizeVariations(options) {
871
- return topDownCriteriaPersonalizationSelectionAlgorithm(options);
872
- }
873
-
874
- // src/placement/strongestScorePersonalizationSelectionAlgorithm.ts
875
- var STRONGEST_SCORE_PERSONALIZATION_ALGORITHM = "ssc";
876
- function strongestScorePersonalizationSelectionAlgorithm({
877
- name,
878
- context,
879
- variations,
880
- take = 1,
881
- onLogMessage
882
- }) {
883
- var _a, _b;
884
- onLogMessage == null ? void 0 : onLogMessage(["info", 300, "GROUP", { name, take }]);
885
- try {
886
- const variationMatches = [];
887
- const defaultVariations = [];
888
- const needsConsentToPersonalize = context.requireConsentForPersonalization;
889
- const isInGlobalControlGroup = (_a = context.storage.data.controlGroup) != null ? _a : false;
890
- const personalizationAllowed = !needsConsentToPersonalize || context.storage.data.consent;
891
- for (const variation of variations) {
892
- const isInvalidFormat = variation.pz && (typeof variation.pz !== "object" || variation.pz === null);
893
- let validVariation;
894
- if (isInvalidFormat) {
895
- validVariation = { id: variation.id };
896
- } else {
897
- validVariation = variation;
898
- }
899
- if ((_b = validVariation.pz) == null ? void 0 : _b.dim) {
900
- if (!personalizationAllowed) {
901
- continue;
902
- }
903
- const score = context.scores[validVariation.pz.dim];
904
- if (score === void 0 || score <= 0) {
905
- continue;
906
- }
907
- variationMatches.push({ variation: validVariation, score });
908
- } else {
909
- defaultVariations.push(validVariation);
910
- }
911
- }
912
- variationMatches.sort((a, b) => b.score - a.score);
913
- const result = [];
914
- for (let i = 0; i < variationMatches.length; i++) {
915
- const variationMatch = variationMatches[i];
916
- let variantToAdd = variationMatch.variation;
917
- if (isInGlobalControlGroup) {
918
- const defaultReplacement = defaultVariations.shift();
919
- if (defaultReplacement) {
920
- variantToAdd = {
921
- ...defaultReplacement,
922
- id: variationMatch.variation.id
923
- };
924
- } else {
925
- variantToAdd = void 0;
926
- }
927
- }
928
- if (variantToAdd) {
929
- result.push({ ...variantToAdd, control: isInGlobalControlGroup });
930
- }
931
- }
932
- while (result.length < take && defaultVariations.length) {
933
- result.push({ ...defaultVariations.shift(), control: false });
934
- }
935
- return {
936
- personalized: variationMatches.length > 0 && !isInGlobalControlGroup,
937
- variations: result
938
- };
939
- } finally {
940
- onLogMessage == null ? void 0 : onLogMessage(["info", 300, "ENDGROUP"]);
941
- }
942
- }
943
-
944
- // src/placement/normalizeVariationDistributions.ts
808
+ // src/placement/test.ts
945
809
  var normalizeVariationDistributions = (variations) => {
946
810
  const { values, total, missingDistribution } = variations.reduce(
947
811
  (previous, current) => {
@@ -960,12 +824,7 @@ var normalizeVariationDistributions = (variations) => {
960
824
  }
961
825
  );
962
826
  if (total > 100) {
963
- const autoScaleFactor = 100 / total;
964
- values.forEach((value, index) => {
965
- if (typeof value === "number") {
966
- values[index] = value * autoScaleFactor;
967
- }
968
- });
827
+ throw new Error(`Total distribution ${total} is over the maximum 100.`);
969
828
  } else if (total < 100) {
970
829
  const remainder = 100 - total;
971
830
  const missingSlice = remainder / missingDistribution;
@@ -977,8 +836,6 @@ var normalizeVariationDistributions = (variations) => {
977
836
  }
978
837
  return values;
979
838
  };
980
-
981
- // src/placement/testVariations.ts
982
839
  var testVariations = ({
983
840
  name,
984
841
  context,
@@ -1005,11 +862,11 @@ var testVariations = ({
1005
862
  }
1006
863
  if (!selectedVariant) {
1007
864
  const distributions = normalizeVariationDistributions(variations);
1008
- const random = Math.floor(Math.random() * 100);
865
+ const random = Math.random() * 100;
1009
866
  let distributionOffset = 0;
1010
867
  selectedVariant = variations.find((variant, index) => {
1011
868
  const distribution = distributions[index];
1012
- if ((random > distributionOffset || random === 0) && random <= distributionOffset + distribution) {
869
+ if (random >= distributionOffset && random < distributionOffset + distribution) {
1013
870
  return variant;
1014
871
  }
1015
872
  distributionOffset += distribution;
@@ -1745,12 +1602,11 @@ var import_lite5 = require("dequal/lite");
1745
1602
  var import_mitt3 = __toESM(require("mitt"));
1746
1603
  var CONTEXTUAL_EDITING_TEST_NAME = "contextual_editing_test";
1747
1604
  var CONTEXTUAL_EDITING_TEST_SELECTED_VARIANT_ID = "contextual_editing_test_selected_variant";
1748
- var _personalizationSelectionAlgorithms, _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1605
+ var _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1749
1606
  var Context = class {
1750
1607
  constructor(options) {
1751
1608
  __privateAdd(this, _Context_instances);
1752
1609
  __publicField(this, "manifest");
1753
- __privateAdd(this, _personalizationSelectionAlgorithms);
1754
1610
  __privateAdd(this, _serverTransitionState);
1755
1611
  __privateAdd(this, _scores, {});
1756
1612
  __privateAdd(this, _state);
@@ -1767,7 +1623,7 @@ var Context = class {
1767
1623
  off: __privateGet(this, _mitt3).off
1768
1624
  });
1769
1625
  __publicField(this, "storage");
1770
- var _a, _b, _c;
1626
+ var _a, _b;
1771
1627
  const { manifest, ...storageOptions } = options;
1772
1628
  __privateSet(this, _state, {});
1773
1629
  __privateSet(this, _plugins, options.plugins);
@@ -1775,20 +1631,7 @@ var Context = class {
1775
1631
  if (typeof options.transitionStore !== "undefined") {
1776
1632
  __privateSet(this, _commands, []);
1777
1633
  }
1778
- __privateSet(this, _personalizationSelectionAlgorithms, {
1779
- [TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM]: topDownCriteriaPersonalizationSelectionAlgorithm,
1780
- [STRONGEST_SCORE_PERSONALIZATION_ALGORITHM]: strongestScorePersonalizationSelectionAlgorithm
1781
- });
1782
1634
  (_a = __privateGet(this, _plugins)) == null ? void 0 : _a.forEach((plugin) => {
1783
- if (!plugin.personalizationSelectionAlgorithms) {
1784
- return;
1785
- }
1786
- __privateSet(this, _personalizationSelectionAlgorithms, {
1787
- ...__privateGet(this, _personalizationSelectionAlgorithms),
1788
- ...plugin.personalizationSelectionAlgorithms
1789
- });
1790
- });
1791
- (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1792
1635
  if (!plugin.logDrain) {
1793
1636
  return;
1794
1637
  }
@@ -1839,7 +1682,7 @@ var Context = class {
1839
1682
  __privateGet(this, _mitt3).emit("quirksUpdated", quirks.quirks);
1840
1683
  __privateGet(this, _mitt3).emit("log", ["info", 4, quirks.quirks]);
1841
1684
  });
1842
- (_c = __privateGet(this, _plugins)) == null ? void 0 : _c.forEach((plugin) => {
1685
+ (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1843
1686
  if (!plugin.init) {
1844
1687
  return;
1845
1688
  }
@@ -2089,17 +1932,7 @@ var Context = class {
2089
1932
  }
2090
1933
  /** Executes a personalized placement with a given set of variants */
2091
1934
  personalize(options) {
2092
- var _a;
2093
- const algorithmName = (_a = options.algorithm) != null ? _a : TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM;
2094
- const algorithm = __privateGet(this, _personalizationSelectionAlgorithms)[algorithmName];
2095
- if (!algorithm) {
2096
- __privateGet(this, _mitt3).emit("log", ["warn", 304, { algorithm: algorithmName }]);
2097
- return {
2098
- personalized: false,
2099
- variations: []
2100
- };
2101
- }
2102
- const value = algorithm({
1935
+ const value = personalizeVariations({
2103
1936
  ...options,
2104
1937
  context: this,
2105
1938
  onLogMessage: (message) => __privateGet(this, _mitt3).emit("log", message)
@@ -2173,7 +2006,6 @@ var Context = class {
2173
2006
  __privateGet(this, _mitt3).emit("personalizationResult", event);
2174
2007
  }
2175
2008
  };
2176
- _personalizationSelectionAlgorithms = new WeakMap();
2177
2009
  _serverTransitionState = new WeakMap();
2178
2010
  _scores = new WeakMap();
2179
2011
  _state = new WeakMap();
@@ -2769,10 +2601,6 @@ var messageContent = {
2769
2601
  301: ({ id, op }) => ["personalization", `testing variation ${id} (${op === "|" ? "OR" : "AND"})`],
2770
2602
  302: ({ matched, description }) => ["personalization", `${description} is ${matched}`],
2771
2603
  303: (selected) => ["personalization", selected ? "selected variation" : "did not select variation"],
2772
- 304: ({ algorithm }) => [
2773
- "personalization",
2774
- `personalization selection algorithm '${algorithm}' not found. Hiding placement.`
2775
- ],
2776
2604
  // TESTING
2777
2605
  400: (name) => ["testing", `executing A/B test '${name}'`],
2778
2606
  401: (testName) => ["testing", `${testName} is not registered in the manifest; it will not be run.`],
@@ -2878,9 +2706,7 @@ function parseQuickConnect(serialized) {
2878
2706
  PAIR_SEP,
2879
2707
  QUIRK_SEP,
2880
2708
  SERVER_STATE_ID,
2881
- STRONGEST_SCORE_PERSONALIZATION_ALGORITHM,
2882
2709
  ScriptType,
2883
- TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM,
2884
2710
  TYPE_SEP,
2885
2711
  TransitionDataStore,
2886
2712
  UNIFORM_DEFAULT_COOKIE_NAME,
@@ -2922,7 +2748,5 @@ function parseQuickConnect(serialized) {
2922
2748
  serializePersonalizeVariants,
2923
2749
  serializeQuickConnect,
2924
2750
  serializeQuirks,
2925
- strongestScorePersonalizationSelectionAlgorithm,
2926
- testVariations,
2927
- topDownCriteriaPersonalizationSelectionAlgorithm
2751
+ testVariations
2928
2752
  });