@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/conditions.ts CHANGED
@@ -1,19 +1,31 @@
1
- import { compareVersions } from "compare-versions";
1
+ import type { Context, PlainCondition, AttributeValue } from "@featurevisor/types";
2
2
 
3
- import { Context, Condition, PlainCondition } from "@featurevisor/types";
3
+ import { GetRegex } from "./datafileReader";
4
+ import { compareVersions } from "./compareVersions";
4
5
 
5
- import { Logger } from "./logger";
6
+ export function getValueFromContext(obj, path): AttributeValue {
7
+ if (path.indexOf(".") === -1) {
8
+ return obj[path];
9
+ }
10
+
11
+ return path.split(".").reduce((o, i) => (o ? o[i] : undefined), obj);
12
+ }
6
13
 
7
- export function conditionIsMatched(condition: PlainCondition, context: Context): boolean {
8
- const { attribute, operator, value } = condition;
14
+ export function conditionIsMatched(
15
+ condition: PlainCondition,
16
+ context: Context,
17
+ getRegex: GetRegex,
18
+ ): boolean {
19
+ const { attribute, operator, value, regexFlags } = condition;
20
+ const contextValueFromPath = getValueFromContext(context, attribute) as AttributeValue;
9
21
 
10
22
  if (operator === "equals") {
11
- return context[attribute] === value;
23
+ return contextValueFromPath === value;
12
24
  } else if (operator === "notEquals") {
13
- return context[attribute] !== value;
25
+ return contextValueFromPath !== value;
14
26
  } else if (operator === "before" || operator === "after") {
15
27
  // date comparisons
16
- const valueInContext = context[attribute] as string | Date;
28
+ const valueInContext = contextValueFromPath as string | Date;
17
29
 
18
30
  const dateInContext =
19
31
  valueInContext instanceof Date ? valueInContext : new Date(valueInContext);
@@ -24,19 +36,20 @@ export function conditionIsMatched(condition: PlainCondition, context: Context):
24
36
  : dateInContext > dateInCondition;
25
37
  } else if (
26
38
  Array.isArray(value) &&
27
- (["string", "number"].indexOf(typeof context[attribute]) !== -1 || context[attribute] === null)
39
+ (["string", "number"].indexOf(typeof contextValueFromPath) !== -1 ||
40
+ contextValueFromPath === null)
28
41
  ) {
29
- // array
30
- const valueInContext = context[attribute] as string;
42
+ // in / notIn (where condition value is an array)
43
+ const valueInContext = contextValueFromPath as string;
31
44
 
32
45
  if (operator === "in") {
33
46
  return value.indexOf(valueInContext) !== -1;
34
47
  } else if (operator === "notIn") {
35
48
  return value.indexOf(valueInContext) === -1;
36
49
  }
37
- } else if (typeof context[attribute] === "string" && typeof value === "string") {
50
+ } else if (typeof contextValueFromPath === "string" && typeof value === "string") {
38
51
  // string
39
- const valueInContext = context[attribute] as string;
52
+ const valueInContext = contextValueFromPath as string;
40
53
 
41
54
  if (operator === "contains") {
42
55
  return valueInContext.indexOf(value) !== -1;
@@ -58,10 +71,16 @@ export function conditionIsMatched(condition: PlainCondition, context: Context):
58
71
  return compareVersions(valueInContext, value) === -1;
59
72
  } else if (operator === "semverLessThanOrEquals") {
60
73
  return compareVersions(valueInContext, value) <= 0;
74
+ } else if (operator === "matches") {
75
+ const regex = getRegex(value, regexFlags || "");
76
+ return regex.test(valueInContext);
77
+ } else if (operator === "notMatches") {
78
+ const regex = getRegex(value, regexFlags || "");
79
+ return !regex.test(valueInContext);
61
80
  }
62
- } else if (typeof context[attribute] === "number" && typeof value === "number") {
81
+ } else if (typeof contextValueFromPath === "number" && typeof value === "number") {
63
82
  // numeric
64
- const valueInContext = context[attribute] as number;
83
+ const valueInContext = contextValueFromPath as number;
65
84
 
66
85
  if (operator === "greaterThan") {
67
86
  return valueInContext > value;
@@ -72,56 +91,20 @@ export function conditionIsMatched(condition: PlainCondition, context: Context):
72
91
  } else if (operator === "lessThanOrEquals") {
73
92
  return valueInContext <= value;
74
93
  }
75
- }
76
-
77
- return false;
78
- }
79
-
80
- export function allConditionsAreMatched(
81
- conditions: Condition[] | Condition,
82
- context: Context,
83
- logger: Logger,
84
- ): boolean {
85
- if ("attribute" in conditions) {
86
- try {
87
- return conditionIsMatched(conditions, context);
88
- } catch (e) {
89
- logger.warn(e.message, {
90
- error: e,
91
- details: {
92
- condition: conditions,
93
- context,
94
- },
95
- });
96
-
97
- return false;
94
+ } else if (operator === "exists") {
95
+ return typeof contextValueFromPath !== "undefined";
96
+ } else if (operator === "notExists") {
97
+ return typeof contextValueFromPath === "undefined";
98
+ } else if (Array.isArray(contextValueFromPath) && typeof value === "string") {
99
+ // includes / notIncludes (where context value is an array)
100
+ const valueInContext = contextValueFromPath as string[];
101
+
102
+ if (operator === "includes") {
103
+ return valueInContext.indexOf(value) > -1;
104
+ } else if (operator === "notIncludes") {
105
+ return valueInContext.indexOf(value) === -1;
98
106
  }
99
107
  }
100
108
 
101
- if ("and" in conditions && Array.isArray(conditions.and)) {
102
- return conditions.and.every((c) => allConditionsAreMatched(c, context, logger));
103
- }
104
-
105
- if ("or" in conditions && Array.isArray(conditions.or)) {
106
- return conditions.or.some((c) => allConditionsAreMatched(c, context, logger));
107
- }
108
-
109
- if ("not" in conditions && Array.isArray(conditions.not)) {
110
- return conditions.not.every(
111
- () =>
112
- allConditionsAreMatched(
113
- {
114
- and: conditions.not,
115
- },
116
- context,
117
- logger,
118
- ) === false,
119
- );
120
- }
121
-
122
- if (Array.isArray(conditions)) {
123
- return conditions.every((c) => allConditionsAreMatched(c, context, logger));
124
- }
125
-
126
109
  return false;
127
110
  }