@featurevisor/sdk 1.35.3 → 2.0.0

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.
Files changed (85) hide show
  1. package/README.md +2 -381
  2. package/coverage/clover.xml +707 -645
  3. package/coverage/coverage-final.json +11 -9
  4. package/coverage/lcov-report/{segments.ts.html → bucketer.ts.html} +155 -77
  5. package/coverage/lcov-report/child.ts.html +940 -0
  6. package/coverage/lcov-report/conditions.ts.html +107 -158
  7. package/coverage/lcov-report/datafileReader.ts.html +763 -103
  8. package/coverage/lcov-report/emitter.ts.html +77 -59
  9. package/coverage/lcov-report/evaluate.ts.html +689 -416
  10. package/coverage/lcov-report/events.ts.html +334 -0
  11. package/coverage/lcov-report/helpers.ts.html +184 -0
  12. package/coverage/lcov-report/{bucket.ts.html → hooks.ts.html} +86 -239
  13. package/coverage/lcov-report/index.html +119 -89
  14. package/coverage/lcov-report/instance.ts.html +341 -773
  15. package/coverage/lcov-report/logger.ts.html +64 -64
  16. package/coverage/lcov.info +1433 -1226
  17. package/dist/bucketer.d.ts +11 -0
  18. package/dist/child.d.ts +26 -0
  19. package/dist/compareVersions.d.ts +4 -0
  20. package/dist/conditions.d.ts +4 -4
  21. package/dist/datafileReader.d.ts +26 -6
  22. package/dist/emitter.d.ts +8 -9
  23. package/dist/evaluate.d.ts +31 -29
  24. package/dist/events.d.ts +5 -0
  25. package/dist/helpers.d.ts +5 -0
  26. package/dist/hooks.d.ts +45 -0
  27. package/dist/index.d.ts +3 -2
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/index.mjs +1 -1
  31. package/dist/index.mjs.gz +0 -0
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/instance.d.ts +40 -72
  34. package/dist/logger.d.ts +6 -5
  35. package/dist/murmurhash.d.ts +1 -0
  36. package/jest.config.js +2 -0
  37. package/lib/bucketer.d.ts +11 -0
  38. package/lib/child.d.ts +26 -0
  39. package/lib/compareVersions.d.ts +4 -0
  40. package/lib/conditions.d.ts +4 -4
  41. package/lib/datafileReader.d.ts +26 -6
  42. package/lib/emitter.d.ts +8 -9
  43. package/lib/evaluate.d.ts +31 -29
  44. package/lib/events.d.ts +5 -0
  45. package/lib/helpers.d.ts +5 -0
  46. package/lib/hooks.d.ts +45 -0
  47. package/lib/index.d.ts +3 -2
  48. package/lib/instance.d.ts +40 -72
  49. package/lib/logger.d.ts +6 -5
  50. package/lib/murmurhash.d.ts +1 -0
  51. package/package.json +3 -5
  52. package/src/bucketer.spec.ts +165 -0
  53. package/src/bucketer.ts +84 -0
  54. package/src/child.spec.ts +267 -0
  55. package/src/child.ts +285 -0
  56. package/src/compareVersions.ts +93 -0
  57. package/src/conditions.spec.ts +563 -353
  58. package/src/conditions.ts +46 -63
  59. package/src/datafileReader.spec.ts +396 -84
  60. package/src/datafileReader.ts +280 -60
  61. package/src/emitter.spec.ts +27 -86
  62. package/src/emitter.ts +38 -32
  63. package/src/evaluate.ts +349 -258
  64. package/src/events.spec.ts +154 -0
  65. package/src/events.ts +83 -0
  66. package/src/helpers.ts +33 -0
  67. package/src/hooks.ts +88 -0
  68. package/src/index.ts +3 -2
  69. package/src/instance.spec.ts +305 -489
  70. package/src/instance.ts +247 -391
  71. package/src/logger.spec.ts +212 -134
  72. package/src/logger.ts +36 -36
  73. package/src/murmurhash.ts +71 -0
  74. package/coverage/lcov-report/feature.ts.html +0 -508
  75. package/dist/bucket.d.ts +0 -30
  76. package/dist/feature.d.ts +0 -16
  77. package/dist/segments.d.ts +0 -5
  78. package/lib/bucket.d.ts +0 -30
  79. package/lib/feature.d.ts +0 -16
  80. package/lib/segments.d.ts +0 -5
  81. package/src/bucket.spec.ts +0 -37
  82. package/src/bucket.ts +0 -139
  83. package/src/feature.ts +0 -141
  84. package/src/segments.spec.ts +0 -468
  85. package/src/segments.ts +0 -58
package/src/child.ts ADDED
@@ -0,0 +1,285 @@
1
+ import type {
2
+ Context,
3
+ StickyFeatures,
4
+ FeatureKey,
5
+ VariationValue,
6
+ VariableValue,
7
+ EvaluatedFeatures,
8
+ } from "@featurevisor/types";
9
+
10
+ import { EventName, EventCallback, Emitter } from "./emitter";
11
+ import type { FeaturevisorInstance, OverrideOptions } from "./instance";
12
+ import { getParamsForStickySetEvent } from "./events";
13
+
14
+ export class FeaturevisorChildInstance {
15
+ private parent: FeaturevisorInstance;
16
+ private context: Context;
17
+ private sticky: StickyFeatures;
18
+ private emitter: Emitter;
19
+
20
+ constructor(options) {
21
+ this.parent = options.parent;
22
+ this.context = options.context;
23
+ this.sticky = options.sticky || {};
24
+ this.emitter = new Emitter();
25
+ }
26
+
27
+ on(eventName: EventName, callback: EventCallback) {
28
+ if (eventName === "context_set" || eventName === "sticky_set") {
29
+ return this.emitter.on(eventName, callback);
30
+ }
31
+
32
+ return this.parent.on(eventName, callback);
33
+ }
34
+
35
+ close() {
36
+ this.emitter.clearAll();
37
+ }
38
+
39
+ setContext(context: Context, replace = false) {
40
+ if (replace) {
41
+ this.context = context;
42
+ } else {
43
+ this.context = { ...this.context, ...context };
44
+ }
45
+
46
+ this.emitter.trigger("context_set", {
47
+ context: this.context,
48
+ replaced: replace,
49
+ });
50
+ }
51
+
52
+ getContext(context?: Context): Context {
53
+ return this.parent.getContext({
54
+ ...this.context,
55
+ ...context,
56
+ });
57
+ }
58
+
59
+ setSticky(sticky: StickyFeatures, replace = false) {
60
+ const previousStickyFeatures = this.sticky || {};
61
+
62
+ if (replace) {
63
+ this.sticky = { ...sticky };
64
+ } else {
65
+ this.sticky = {
66
+ ...this.sticky,
67
+ ...sticky,
68
+ };
69
+ }
70
+
71
+ const params = getParamsForStickySetEvent(previousStickyFeatures, this.sticky, replace);
72
+
73
+ this.emitter.trigger("sticky_set", params);
74
+ }
75
+
76
+ isEnabled(featureKey: FeatureKey, context: Context = {}, options: OverrideOptions = {}): boolean {
77
+ return this.parent.isEnabled(
78
+ featureKey,
79
+ {
80
+ ...this.context,
81
+ ...context,
82
+ },
83
+ {
84
+ sticky: this.sticky,
85
+ ...options,
86
+ },
87
+ );
88
+ }
89
+
90
+ getVariation(
91
+ featureKey: FeatureKey,
92
+ context: Context = {},
93
+ options: OverrideOptions = {},
94
+ ): VariationValue | null {
95
+ return this.parent.getVariation(
96
+ featureKey,
97
+ {
98
+ ...this.context,
99
+ ...context,
100
+ },
101
+ {
102
+ sticky: this.sticky,
103
+ ...options,
104
+ },
105
+ );
106
+ }
107
+
108
+ getVariable(
109
+ featureKey: FeatureKey,
110
+ variableKey: string,
111
+ context: Context = {},
112
+ options: OverrideOptions = {},
113
+ ): VariableValue | null {
114
+ return this.parent.getVariable(
115
+ featureKey,
116
+ variableKey,
117
+ {
118
+ ...this.context,
119
+ ...context,
120
+ },
121
+ {
122
+ sticky: this.sticky,
123
+ ...options,
124
+ },
125
+ );
126
+ }
127
+
128
+ getVariableBoolean(
129
+ featureKey: FeatureKey,
130
+ variableKey: string,
131
+ context: Context = {},
132
+ options: OverrideOptions = {},
133
+ ): boolean | null {
134
+ return this.parent.getVariableBoolean(
135
+ featureKey,
136
+ variableKey,
137
+ {
138
+ ...this.context,
139
+ ...context,
140
+ },
141
+ {
142
+ sticky: this.sticky,
143
+ ...options,
144
+ },
145
+ );
146
+ }
147
+
148
+ getVariableString(
149
+ featureKey: FeatureKey,
150
+ variableKey: string,
151
+ context: Context = {},
152
+ options: OverrideOptions = {},
153
+ ): string | null {
154
+ return this.parent.getVariableString(
155
+ featureKey,
156
+ variableKey,
157
+ {
158
+ ...this.context,
159
+ ...context,
160
+ },
161
+ {
162
+ sticky: this.sticky,
163
+ ...options,
164
+ },
165
+ );
166
+ }
167
+
168
+ getVariableInteger(
169
+ featureKey: FeatureKey,
170
+ variableKey: string,
171
+ context: Context = {},
172
+ options: OverrideOptions = {},
173
+ ): number | null {
174
+ return this.parent.getVariableInteger(
175
+ featureKey,
176
+ variableKey,
177
+ {
178
+ ...this.context,
179
+ ...context,
180
+ },
181
+ {
182
+ sticky: this.sticky,
183
+ ...options,
184
+ },
185
+ );
186
+ }
187
+
188
+ getVariableDouble(
189
+ featureKey: FeatureKey,
190
+ variableKey: string,
191
+ context: Context = {},
192
+ options: OverrideOptions = {},
193
+ ): number | null {
194
+ return this.parent.getVariableDouble(
195
+ featureKey,
196
+ variableKey,
197
+ {
198
+ ...this.context,
199
+ ...context,
200
+ },
201
+ {
202
+ sticky: this.sticky,
203
+ ...options,
204
+ },
205
+ );
206
+ }
207
+
208
+ getVariableArray(
209
+ featureKey: FeatureKey,
210
+ variableKey: string,
211
+ context: Context = {},
212
+ options: OverrideOptions = {},
213
+ ): string[] | null {
214
+ return this.parent.getVariableArray(
215
+ featureKey,
216
+ variableKey,
217
+ {
218
+ ...this.context,
219
+ ...context,
220
+ },
221
+ {
222
+ sticky: this.sticky,
223
+ ...options,
224
+ },
225
+ );
226
+ }
227
+
228
+ getVariableObject<T>(
229
+ featureKey: FeatureKey,
230
+ variableKey: string,
231
+ context: Context = {},
232
+ options: OverrideOptions = {},
233
+ ): T | null {
234
+ return this.parent.getVariableObject<T>(
235
+ featureKey,
236
+ variableKey,
237
+ {
238
+ ...this.context,
239
+ ...context,
240
+ },
241
+ {
242
+ sticky: this.sticky,
243
+ ...options,
244
+ },
245
+ );
246
+ }
247
+
248
+ getVariableJSON<T>(
249
+ featureKey: FeatureKey,
250
+ variableKey: string,
251
+ context: Context = {},
252
+ options: OverrideOptions = {},
253
+ ): T | null {
254
+ return this.parent.getVariableJSON<T>(
255
+ featureKey,
256
+ variableKey,
257
+ {
258
+ ...this.context,
259
+ ...context,
260
+ },
261
+ {
262
+ sticky: this.sticky,
263
+ ...options,
264
+ },
265
+ );
266
+ }
267
+
268
+ getAllEvaluations(
269
+ context: Context = {},
270
+ featureKeys: string[] = [],
271
+ options: OverrideOptions = {},
272
+ ): EvaluatedFeatures {
273
+ return this.parent.getAllEvaluations(
274
+ {
275
+ ...this.context,
276
+ ...context,
277
+ },
278
+ featureKeys,
279
+ {
280
+ sticky: this.sticky,
281
+ ...options,
282
+ },
283
+ );
284
+ }
285
+ }
@@ -0,0 +1,93 @@
1
+ // taken from: https://github.com/omichelsen/compare-versions
2
+ // this is done to avoid loading the whole package
3
+
4
+ /*
5
+ The MIT License (MIT)
6
+
7
+ Copyright (c) 2015-2021 Ole Michelsen
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ */
27
+
28
+ export const semver =
29
+ /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
30
+
31
+ export const validateAndParse = (version: string) => {
32
+ if (typeof version !== "string") {
33
+ throw new TypeError("Invalid argument expected string");
34
+ }
35
+ const match = version.match(semver);
36
+ if (!match) {
37
+ throw new Error(`Invalid argument not valid semver ('${version}' received)`);
38
+ }
39
+ match.shift();
40
+ return match;
41
+ };
42
+
43
+ const isWildcard = (s: string) => s === "*" || s === "x" || s === "X";
44
+
45
+ const forceType = (a: string | number, b: string | number) =>
46
+ typeof a !== typeof b ? [String(a), String(b)] : [a, b];
47
+
48
+ const tryParse = (v: string) => {
49
+ const n = parseInt(v, 10);
50
+ return isNaN(n) ? v : n;
51
+ };
52
+
53
+ const compareStrings = (a: string, b: string) => {
54
+ if (isWildcard(a) || isWildcard(b)) return 0;
55
+ const [ap, bp] = forceType(tryParse(a), tryParse(b));
56
+ if (ap > bp) return 1;
57
+ if (ap < bp) return -1;
58
+ return 0;
59
+ };
60
+
61
+ export const compareSegments = (
62
+ a: string | string[] | RegExpMatchArray,
63
+ b: string | string[] | RegExpMatchArray,
64
+ ) => {
65
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
66
+ const r = compareStrings(a[i] || "0", b[i] || "0");
67
+ if (r !== 0) return r;
68
+ }
69
+ return 0;
70
+ };
71
+
72
+ export const compareVersions = (v1: string, v2: string) => {
73
+ // validate input and split into segments
74
+ const n1 = validateAndParse(v1);
75
+ const n2 = validateAndParse(v2);
76
+
77
+ // pop off the patch
78
+ const p1 = n1.pop();
79
+ const p2 = n2.pop();
80
+
81
+ // validate numbers
82
+ const r = compareSegments(n1, n2);
83
+ if (r !== 0) return r;
84
+
85
+ // validate pre-release
86
+ if (p1 && p2) {
87
+ return compareSegments(p1.split("."), p2.split("."));
88
+ } else if (p1 || p2) {
89
+ return p1 ? -1 : 1;
90
+ }
91
+
92
+ return 0;
93
+ };