@uniformdev/context 20.6.2-alpha.11 → 20.7.1-alpha.4

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,35 +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
- const { pz, ...validParts } = variation;
806
- validVariation = validParts;
807
- } else {
808
- validVariation = variation;
809
- }
810
- if ((_a = validVariation.pz) == null ? void 0 : _a.crit.length) {
811
- if (personalizationAllowed && variationMatches.length !== take && evaluateVariantMatch(variation.id, validVariation.pz, context.scores, onLogMessage, context.quirks)) {
812
- 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);
813
751
  }
814
752
  } else {
815
- defaultVariations.push(validVariation);
753
+ defaultVariants.push(variant);
816
754
  }
817
755
  }
818
756
  const result = [];
819
- for (let i = 0; i < variationMatches.length; i++) {
757
+ for (let i = 0; i < variantMatches.length; i++) {
820
758
  let isControl = (_b = context.storage.data.controlGroup) != null ? _b : false;
821
- const variation = variationMatches[i];
822
- 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") {
823
761
  isControl = context.getPersonalizeVariantControl(name, i);
824
762
  if (typeof isControl === "undefined") {
825
- isControl = rollForControlGroup(variation.pz.control);
763
+ isControl = rollForControlGroup(variant.pz.control);
826
764
  context.storage.updateData([
827
765
  {
828
766
  type: "setpersonalizecontrol",
@@ -835,13 +773,13 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
835
773
  ]);
836
774
  }
837
775
  }
838
- let variantToAdd = variation;
776
+ let variantToAdd = variant;
839
777
  if (isControl) {
840
- const defaultReplacement = defaultVariations.shift();
778
+ const defaultReplacement = defaultVariants.shift();
841
779
  if (defaultReplacement) {
842
780
  variantToAdd = {
843
781
  ...defaultReplacement,
844
- id: variation.id
782
+ id: variant.id
845
783
  };
846
784
  } else {
847
785
  variantToAdd = void 0;
@@ -851,8 +789,8 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
851
789
  result.push({ ...variantToAdd, control: isControl });
852
790
  }
853
791
  }
854
- while (result.length < take && defaultVariations.length) {
855
- result.push({ ...defaultVariations.shift(), control: false });
792
+ while (result.length < take && defaultVariants.length) {
793
+ result.push({ ...defaultVariants.shift(), control: false });
856
794
  }
857
795
  const personalized = result.some((v) => {
858
796
  var _a2;
@@ -867,86 +805,7 @@ function topDownCriteriaPersonalizationSelectionAlgorithm({
867
805
  }
868
806
  }
869
807
 
870
- // src/placement/personalizeVariations.ts
871
- function personalizeVariations(options) {
872
- return topDownCriteriaPersonalizationSelectionAlgorithm(options);
873
- }
874
-
875
- // src/placement/strongestScorePersonalizationSelectionAlgorithm.ts
876
- var STRONGEST_SCORE_PERSONALIZATION_ALGORITHM = "ssc";
877
- function strongestScorePersonalizationSelectionAlgorithm({
878
- name,
879
- context,
880
- variations,
881
- take = 1,
882
- onLogMessage
883
- }) {
884
- var _a, _b;
885
- onLogMessage == null ? void 0 : onLogMessage(["info", 300, "GROUP", { name, take }]);
886
- try {
887
- const variationMatches = [];
888
- const defaultVariations = [];
889
- const needsConsentToPersonalize = context.requireConsentForPersonalization;
890
- const isInGlobalControlGroup = (_a = context.storage.data.controlGroup) != null ? _a : false;
891
- const personalizationAllowed = !needsConsentToPersonalize || context.storage.data.consent;
892
- for (const variation of variations) {
893
- const isInvalidFormat = variation.pz && typeof variation.pz !== "object";
894
- let validVariation;
895
- if (isInvalidFormat) {
896
- const { pz, ...validParts } = variation;
897
- validVariation = validParts;
898
- } else {
899
- validVariation = variation;
900
- }
901
- if ((_b = validVariation.pz) == null ? void 0 : _b.dim) {
902
- if (!personalizationAllowed) {
903
- continue;
904
- }
905
- const score = context.scores[validVariation.pz.dim];
906
- if (score === void 0 || score <= 0) {
907
- continue;
908
- }
909
- variationMatches.push({ variation: validVariation, score });
910
- } else {
911
- defaultVariations.push(validVariation);
912
- }
913
- }
914
- variationMatches.sort((a, b) => b.score - a.score);
915
- const result = [];
916
- for (let i = 0; i < variationMatches.length; i++) {
917
- const variationMatch = variationMatches[i];
918
- let variantToAdd = variationMatch.variation;
919
- if (i >= take) {
920
- continue;
921
- }
922
- if (isInGlobalControlGroup) {
923
- const defaultReplacement = defaultVariations.shift();
924
- if (defaultReplacement) {
925
- variantToAdd = {
926
- ...defaultReplacement,
927
- id: variationMatch.variation.id
928
- };
929
- } else {
930
- variantToAdd = void 0;
931
- }
932
- }
933
- if (variantToAdd) {
934
- result.push({ ...variantToAdd, control: isInGlobalControlGroup });
935
- }
936
- }
937
- while (result.length < take && defaultVariations.length) {
938
- result.push({ ...defaultVariations.shift(), control: false });
939
- }
940
- return {
941
- personalized: variationMatches.length > 0 && !isInGlobalControlGroup,
942
- variations: result
943
- };
944
- } finally {
945
- onLogMessage == null ? void 0 : onLogMessage(["info", 300, "ENDGROUP"]);
946
- }
947
- }
948
-
949
- // src/placement/normalizeVariationDistributions.ts
808
+ // src/placement/test.ts
950
809
  var normalizeVariationDistributions = (variations) => {
951
810
  const { values, total, missingDistribution } = variations.reduce(
952
811
  (previous, current) => {
@@ -965,12 +824,7 @@ var normalizeVariationDistributions = (variations) => {
965
824
  }
966
825
  );
967
826
  if (total > 100) {
968
- const autoScaleFactor = 100 / total;
969
- values.forEach((value, index) => {
970
- if (typeof value === "number") {
971
- values[index] = value * autoScaleFactor;
972
- }
973
- });
827
+ throw new Error(`Total distribution ${total} is over the maximum 100.`);
974
828
  } else if (total < 100) {
975
829
  const remainder = 100 - total;
976
830
  const missingSlice = remainder / missingDistribution;
@@ -982,8 +836,6 @@ var normalizeVariationDistributions = (variations) => {
982
836
  }
983
837
  return values;
984
838
  };
985
-
986
- // src/placement/testVariations.ts
987
839
  var testVariations = ({
988
840
  name,
989
841
  context,
@@ -1750,12 +1602,11 @@ var import_lite5 = require("dequal/lite");
1750
1602
  var import_mitt3 = __toESM(require("mitt"));
1751
1603
  var CONTEXTUAL_EDITING_TEST_NAME = "contextual_editing_test";
1752
1604
  var CONTEXTUAL_EDITING_TEST_SELECTED_VARIANT_ID = "contextual_editing_test_selected_variant";
1753
- 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;
1754
1606
  var Context = class {
1755
1607
  constructor(options) {
1756
1608
  __privateAdd(this, _Context_instances);
1757
1609
  __publicField(this, "manifest");
1758
- __privateAdd(this, _personalizationSelectionAlgorithms);
1759
1610
  __privateAdd(this, _serverTransitionState);
1760
1611
  __privateAdd(this, _scores, {});
1761
1612
  __privateAdd(this, _state);
@@ -1772,7 +1623,7 @@ var Context = class {
1772
1623
  off: __privateGet(this, _mitt3).off
1773
1624
  });
1774
1625
  __publicField(this, "storage");
1775
- var _a, _b, _c;
1626
+ var _a, _b;
1776
1627
  const { manifest, ...storageOptions } = options;
1777
1628
  __privateSet(this, _state, {});
1778
1629
  __privateSet(this, _plugins, options.plugins);
@@ -1780,20 +1631,7 @@ var Context = class {
1780
1631
  if (typeof options.transitionStore !== "undefined") {
1781
1632
  __privateSet(this, _commands, []);
1782
1633
  }
1783
- __privateSet(this, _personalizationSelectionAlgorithms, {
1784
- [TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM]: topDownCriteriaPersonalizationSelectionAlgorithm,
1785
- [STRONGEST_SCORE_PERSONALIZATION_ALGORITHM]: strongestScorePersonalizationSelectionAlgorithm
1786
- });
1787
1634
  (_a = __privateGet(this, _plugins)) == null ? void 0 : _a.forEach((plugin) => {
1788
- if (!plugin.personalizationSelectionAlgorithms) {
1789
- return;
1790
- }
1791
- __privateSet(this, _personalizationSelectionAlgorithms, {
1792
- ...__privateGet(this, _personalizationSelectionAlgorithms),
1793
- ...plugin.personalizationSelectionAlgorithms
1794
- });
1795
- });
1796
- (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1797
1635
  if (!plugin.logDrain) {
1798
1636
  return;
1799
1637
  }
@@ -1844,7 +1682,7 @@ var Context = class {
1844
1682
  __privateGet(this, _mitt3).emit("quirksUpdated", quirks.quirks);
1845
1683
  __privateGet(this, _mitt3).emit("log", ["info", 4, quirks.quirks]);
1846
1684
  });
1847
- (_c = __privateGet(this, _plugins)) == null ? void 0 : _c.forEach((plugin) => {
1685
+ (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1848
1686
  if (!plugin.init) {
1849
1687
  return;
1850
1688
  }
@@ -2094,17 +1932,7 @@ var Context = class {
2094
1932
  }
2095
1933
  /** Executes a personalized placement with a given set of variants */
2096
1934
  personalize(options) {
2097
- var _a;
2098
- const algorithmName = (_a = options.algorithm) != null ? _a : TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM;
2099
- const algorithm = __privateGet(this, _personalizationSelectionAlgorithms)[algorithmName];
2100
- if (!algorithm) {
2101
- __privateGet(this, _mitt3).emit("log", ["warn", 304, { algorithm: algorithmName }]);
2102
- return {
2103
- personalized: false,
2104
- variations: []
2105
- };
2106
- }
2107
- const value = algorithm({
1935
+ const value = personalizeVariations({
2108
1936
  ...options,
2109
1937
  context: this,
2110
1938
  onLogMessage: (message) => __privateGet(this, _mitt3).emit("log", message)
@@ -2178,7 +2006,6 @@ var Context = class {
2178
2006
  __privateGet(this, _mitt3).emit("personalizationResult", event);
2179
2007
  }
2180
2008
  };
2181
- _personalizationSelectionAlgorithms = new WeakMap();
2182
2009
  _serverTransitionState = new WeakMap();
2183
2010
  _scores = new WeakMap();
2184
2011
  _state = new WeakMap();
@@ -2774,10 +2601,6 @@ var messageContent = {
2774
2601
  301: ({ id, op }) => ["personalization", `testing variation ${id} (${op === "|" ? "OR" : "AND"})`],
2775
2602
  302: ({ matched, description }) => ["personalization", `${description} is ${matched}`],
2776
2603
  303: (selected) => ["personalization", selected ? "selected variation" : "did not select variation"],
2777
- 304: ({ algorithm }) => [
2778
- "personalization",
2779
- `personalization selection algorithm '${algorithm}' not found. Hiding placement.`
2780
- ],
2781
2604
  // TESTING
2782
2605
  400: (name) => ["testing", `executing A/B test '${name}'`],
2783
2606
  401: (testName) => ["testing", `${testName} is not registered in the manifest; it will not be run.`],
@@ -2883,9 +2706,7 @@ function parseQuickConnect(serialized) {
2883
2706
  PAIR_SEP,
2884
2707
  QUIRK_SEP,
2885
2708
  SERVER_STATE_ID,
2886
- STRONGEST_SCORE_PERSONALIZATION_ALGORITHM,
2887
2709
  ScriptType,
2888
- TOP_DOWN_CRITERIA_PERSONALIZATION_ALGORITHM,
2889
2710
  TYPE_SEP,
2890
2711
  TransitionDataStore,
2891
2712
  UNIFORM_DEFAULT_COOKIE_NAME,
@@ -2927,7 +2748,5 @@ function parseQuickConnect(serialized) {
2927
2748
  serializePersonalizeVariants,
2928
2749
  serializeQuickConnect,
2929
2750
  serializeQuirks,
2930
- strongestScorePersonalizationSelectionAlgorithm,
2931
- testVariations,
2932
- topDownCriteriaPersonalizationSelectionAlgorithm
2751
+ testVariations
2933
2752
  });