@featurevisor/sdk 0.46.0 → 0.46.2

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.gz CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@featurevisor/sdk",
3
- "version": "0.46.0",
3
+ "version": "0.46.2",
4
4
  "description": "Featurevisor SDK for Node.js and the browser",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib/index.js",
@@ -46,5 +46,5 @@
46
46
  "compare-versions": "^6.0.0-rc.1",
47
47
  "murmurhash": "^2.0.1"
48
48
  },
49
- "gitHead": "2404707e0f8506be540726a372c06abe116b7152"
49
+ "gitHead": "a99373ed8e07ad7ab3e284d01307fe7f2b380648"
50
50
  }
@@ -39,6 +39,110 @@ describe("sdk: Conditions", function () {
39
39
  expect(allConditionsAreMatched(conditions, { browser_type: "chrome" })).toEqual(false);
40
40
  });
41
41
 
42
+ it("should match with operator: startsWith", function () {
43
+ const conditions: Condition[] = [
44
+ {
45
+ attribute: "name",
46
+ operator: "startsWith",
47
+ value: "Hello",
48
+ },
49
+ ];
50
+
51
+ // match
52
+ expect(allConditionsAreMatched(conditions, { name: "Hello World" })).toEqual(true);
53
+ expect(allConditionsAreMatched(conditions, { name: "Hello Universe" })).toEqual(true);
54
+
55
+ // not match
56
+ expect(allConditionsAreMatched(conditions, { name: "Hi World" })).toEqual(false);
57
+ });
58
+
59
+ it("should match with operator: endsWith", function () {
60
+ const conditions: Condition[] = [
61
+ {
62
+ attribute: "name",
63
+ operator: "endsWith",
64
+ value: "World",
65
+ },
66
+ ];
67
+
68
+ // match
69
+ expect(allConditionsAreMatched(conditions, { name: "Hello World" })).toEqual(true);
70
+ expect(allConditionsAreMatched(conditions, { name: "Hi World" })).toEqual(true);
71
+
72
+ // not match
73
+ expect(allConditionsAreMatched(conditions, { name: "Hi Universe" })).toEqual(false);
74
+ });
75
+
76
+ it("should match with operator: contains", function () {
77
+ const conditions: Condition[] = [
78
+ {
79
+ attribute: "name",
80
+ operator: "contains",
81
+ value: "Hello",
82
+ },
83
+ ];
84
+
85
+ // match
86
+ expect(allConditionsAreMatched(conditions, { name: "Hello World" })).toEqual(true);
87
+ expect(allConditionsAreMatched(conditions, { name: "Yo! Hello!" })).toEqual(true);
88
+
89
+ // not match
90
+ expect(allConditionsAreMatched(conditions, { name: "Hi World" })).toEqual(false);
91
+ });
92
+
93
+ it("should match with operator: notContains", function () {
94
+ const conditions: Condition[] = [
95
+ {
96
+ attribute: "name",
97
+ operator: "notContains",
98
+ value: "Hello",
99
+ },
100
+ ];
101
+
102
+ // match
103
+ expect(allConditionsAreMatched(conditions, { name: "Hi World" })).toEqual(true);
104
+
105
+ // not match
106
+ expect(allConditionsAreMatched(conditions, { name: "Hello World" })).toEqual(false);
107
+ expect(allConditionsAreMatched(conditions, { name: "Yo! Hello!" })).toEqual(false);
108
+ });
109
+
110
+ it("should match with operator: in", function () {
111
+ const conditions: Condition[] = [
112
+ {
113
+ attribute: "browser_type",
114
+ operator: "in",
115
+ value: ["chrome", "firefox"],
116
+ },
117
+ ];
118
+
119
+ // match
120
+ expect(allConditionsAreMatched(conditions, { browser_type: "chrome" })).toEqual(true);
121
+ expect(allConditionsAreMatched(conditions, { browser_type: "firefox" })).toEqual(true);
122
+
123
+ // not match
124
+ expect(allConditionsAreMatched(conditions, { browser_type: "edge" })).toEqual(false);
125
+ expect(allConditionsAreMatched(conditions, { browser_type: "safari" })).toEqual(false);
126
+ });
127
+
128
+ it("should match with operator: notIn", function () {
129
+ const conditions: Condition[] = [
130
+ {
131
+ attribute: "browser_type",
132
+ operator: "notIn",
133
+ value: ["chrome", "firefox"],
134
+ },
135
+ ];
136
+
137
+ // match
138
+ expect(allConditionsAreMatched(conditions, { browser_type: "edge" })).toEqual(true);
139
+ expect(allConditionsAreMatched(conditions, { browser_type: "safari" })).toEqual(true);
140
+
141
+ // not match
142
+ expect(allConditionsAreMatched(conditions, { browser_type: "chrome" })).toEqual(false);
143
+ expect(allConditionsAreMatched(conditions, { browser_type: "firefox" })).toEqual(false);
144
+ });
145
+
42
146
  it("should match with operator: greaterThan", function () {
43
147
  const conditions: Condition[] = [
44
148
  {
@@ -55,6 +159,24 @@ describe("sdk: Conditions", function () {
55
159
  expect(allConditionsAreMatched(conditions, { age: 17 })).toEqual(false);
56
160
  });
57
161
 
162
+ it("should match with operator: greaterThanOrEquals", function () {
163
+ const conditions: Condition[] = [
164
+ {
165
+ attribute: "age",
166
+ operator: "greaterThanOrEquals",
167
+ value: 18,
168
+ },
169
+ ];
170
+
171
+ // match
172
+ expect(allConditionsAreMatched(conditions, { age: 18 })).toEqual(true);
173
+ expect(allConditionsAreMatched(conditions, { age: 19 })).toEqual(true);
174
+
175
+ // not match
176
+ expect(allConditionsAreMatched(conditions, { age: 17 })).toEqual(false);
177
+ expect(allConditionsAreMatched(conditions, { age: 16 })).toEqual(false);
178
+ });
179
+
58
180
  it("should match with operator: lessThan", function () {
59
181
  const conditions: Condition[] = [
60
182
  {
@@ -71,6 +193,24 @@ describe("sdk: Conditions", function () {
71
193
  expect(allConditionsAreMatched(conditions, { age: 19 })).toEqual(false);
72
194
  });
73
195
 
196
+ it("should match with operator: lessThanOrEquals", function () {
197
+ const conditions: Condition[] = [
198
+ {
199
+ attribute: "age",
200
+ operator: "lessThanOrEquals",
201
+ value: 18,
202
+ },
203
+ ];
204
+
205
+ // match
206
+ expect(allConditionsAreMatched(conditions, { age: 17 })).toEqual(true);
207
+ expect(allConditionsAreMatched(conditions, { age: 18 })).toEqual(true);
208
+
209
+ // not match
210
+ expect(allConditionsAreMatched(conditions, { age: 19 })).toEqual(false);
211
+ expect(allConditionsAreMatched(conditions, { age: 20 })).toEqual(false);
212
+ });
213
+
74
214
  it("should match with operator: semverEquals", function () {
75
215
  const conditions: Condition[] = [
76
216
  {
@@ -314,7 +314,8 @@ describe("sdk: instance", function () {
314
314
  it("should refresh datafile", function (done) {
315
315
  let revision = 1;
316
316
  let refreshed = false;
317
- let updated = false;
317
+ let updatedViaOption = false;
318
+ let updatedViaEventListener = false;
318
319
 
319
320
  function getDatafileContent(): DatafileContent {
320
321
  const content: DatafileContent = {
@@ -359,15 +360,24 @@ describe("sdk: instance", function () {
359
360
  refreshed = true;
360
361
  },
361
362
  onUpdate() {
362
- updated = true;
363
+ updatedViaOption = true;
363
364
  },
364
365
  });
365
366
 
367
+ const onUpdateCallback = function () {
368
+ updatedViaEventListener = true;
369
+ };
370
+
371
+ sdk.on("update", onUpdateCallback);
372
+
366
373
  expect(sdk.isReady()).toEqual(false);
367
374
 
368
375
  setTimeout(function () {
369
376
  expect(refreshed).toEqual(true);
370
- expect(updated).toEqual(true);
377
+ expect(updatedViaOption).toEqual(true);
378
+ expect(updatedViaEventListener).toEqual(true);
379
+
380
+ sdk.off("update", onUpdateCallback);
371
381
 
372
382
  expect(sdk.isReady()).toEqual(true);
373
383
 
@@ -754,4 +764,121 @@ describe("sdk: instance", function () {
754
764
  expect(deprecatedTestVariation).toEqual("control");
755
765
  expect(deprecatedCount).toEqual(1);
756
766
  });
767
+
768
+ it("should get variable", function () {
769
+ const sdk = createInstance({
770
+ datafile: {
771
+ schemaVersion: "1",
772
+ revision: "1.0",
773
+ features: [
774
+ {
775
+ key: "test",
776
+ bucketBy: "userId",
777
+ variablesSchema: [
778
+ {
779
+ key: "color",
780
+ type: "string",
781
+ defaultValue: "red",
782
+ },
783
+ {
784
+ key: "showSidebar",
785
+ type: "boolean",
786
+ defaultValue: false,
787
+ },
788
+ {
789
+ key: "count",
790
+ type: "integer",
791
+ defaultValue: 0,
792
+ },
793
+ {
794
+ key: "paymentMethods",
795
+ type: "array",
796
+ defaultValue: ["paypal", "creditcard"],
797
+ },
798
+ {
799
+ key: "flatConfig",
800
+ type: "object",
801
+ defaultValue: {
802
+ key: "value",
803
+ },
804
+ },
805
+ {
806
+ key: "nestedConfig",
807
+ type: "json",
808
+ defaultValue: JSON.stringify({
809
+ key: {
810
+ nested: "value",
811
+ },
812
+ }),
813
+ },
814
+ ],
815
+ variations: [
816
+ { value: "control" },
817
+ {
818
+ value: "treatment",
819
+ variables: [
820
+ {
821
+ key: "showSidebar",
822
+ value: true,
823
+ },
824
+ ],
825
+ },
826
+ ],
827
+ traffic: [
828
+ {
829
+ key: "1",
830
+ segments: "*",
831
+ percentage: 100000,
832
+ allocation: [
833
+ { variation: "control", range: [0, 0] },
834
+ { variation: "treatment", range: [0, 100000] },
835
+ ],
836
+ },
837
+ ],
838
+ },
839
+ ],
840
+ attributes: [],
841
+ segments: [],
842
+ },
843
+ });
844
+
845
+ const context = {
846
+ userId: "123",
847
+ };
848
+
849
+ expect(sdk.getVariation("test", context)).toEqual("treatment");
850
+
851
+ expect(sdk.getVariable("test", "color", context)).toEqual("red");
852
+ expect(sdk.getVariableString("test", "color", context)).toEqual("red");
853
+
854
+ expect(sdk.getVariable("test", "showSidebar", context)).toEqual(true);
855
+ expect(sdk.getVariableBoolean("test", "showSidebar", context)).toEqual(true);
856
+
857
+ expect(sdk.getVariable("test", "count", context)).toEqual(0);
858
+ expect(sdk.getVariableInteger("test", "count", context)).toEqual(0);
859
+
860
+ expect(sdk.getVariable("test", "paymentMethods", context)).toEqual(["paypal", "creditcard"]);
861
+ expect(sdk.getVariableArray("test", "paymentMethods", context)).toEqual([
862
+ "paypal",
863
+ "creditcard",
864
+ ]);
865
+
866
+ expect(sdk.getVariable("test", "flatConfig", context)).toEqual({
867
+ key: "value",
868
+ });
869
+ expect(sdk.getVariableObject("test", "flatConfig", context)).toEqual({
870
+ key: "value",
871
+ });
872
+
873
+ expect(sdk.getVariable("test", "nestedConfig", context)).toEqual({
874
+ key: {
875
+ nested: "value",
876
+ },
877
+ });
878
+ expect(sdk.getVariableJSON("test", "nestedConfig", context)).toEqual({
879
+ key: {
880
+ nested: "value",
881
+ },
882
+ });
883
+ });
757
884
  });