@featurevisor/sdk 1.35.3 → 2.0.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +2 -381
  3. package/coverage/clover.xml +707 -645
  4. package/coverage/coverage-final.json +11 -9
  5. package/coverage/lcov-report/{segments.ts.html → bucketer.ts.html} +155 -77
  6. package/coverage/lcov-report/child.ts.html +940 -0
  7. package/coverage/lcov-report/conditions.ts.html +107 -158
  8. package/coverage/lcov-report/datafileReader.ts.html +763 -103
  9. package/coverage/lcov-report/emitter.ts.html +77 -59
  10. package/coverage/lcov-report/evaluate.ts.html +689 -416
  11. package/coverage/lcov-report/events.ts.html +334 -0
  12. package/coverage/lcov-report/helpers.ts.html +184 -0
  13. package/coverage/lcov-report/{bucket.ts.html → hooks.ts.html} +86 -239
  14. package/coverage/lcov-report/index.html +119 -89
  15. package/coverage/lcov-report/instance.ts.html +341 -773
  16. package/coverage/lcov-report/logger.ts.html +64 -64
  17. package/coverage/lcov.info +1433 -1226
  18. package/dist/bucketer.d.ts +11 -0
  19. package/dist/child.d.ts +26 -0
  20. package/dist/compareVersions.d.ts +4 -0
  21. package/dist/conditions.d.ts +4 -4
  22. package/dist/datafileReader.d.ts +26 -6
  23. package/dist/emitter.d.ts +8 -9
  24. package/dist/evaluate.d.ts +31 -29
  25. package/dist/events.d.ts +5 -0
  26. package/dist/helpers.d.ts +5 -0
  27. package/dist/hooks.d.ts +45 -0
  28. package/dist/index.d.ts +3 -2
  29. package/dist/index.js +1 -1
  30. package/dist/index.js.map +1 -1
  31. package/dist/index.mjs +1 -1
  32. package/dist/index.mjs.gz +0 -0
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/instance.d.ts +40 -72
  35. package/dist/logger.d.ts +6 -5
  36. package/dist/murmurhash.d.ts +1 -0
  37. package/jest.config.js +2 -0
  38. package/lib/bucketer.d.ts +11 -0
  39. package/lib/child.d.ts +26 -0
  40. package/lib/compareVersions.d.ts +4 -0
  41. package/lib/conditions.d.ts +4 -4
  42. package/lib/datafileReader.d.ts +26 -6
  43. package/lib/emitter.d.ts +8 -9
  44. package/lib/evaluate.d.ts +31 -29
  45. package/lib/events.d.ts +5 -0
  46. package/lib/helpers.d.ts +5 -0
  47. package/lib/hooks.d.ts +45 -0
  48. package/lib/index.d.ts +3 -2
  49. package/lib/instance.d.ts +40 -72
  50. package/lib/logger.d.ts +6 -5
  51. package/lib/murmurhash.d.ts +1 -0
  52. package/package.json +3 -5
  53. package/src/bucketer.spec.ts +165 -0
  54. package/src/bucketer.ts +84 -0
  55. package/src/child.spec.ts +267 -0
  56. package/src/child.ts +285 -0
  57. package/src/compareVersions.ts +93 -0
  58. package/src/conditions.spec.ts +563 -353
  59. package/src/conditions.ts +46 -63
  60. package/src/datafileReader.spec.ts +396 -84
  61. package/src/datafileReader.ts +280 -60
  62. package/src/emitter.spec.ts +27 -86
  63. package/src/emitter.ts +38 -32
  64. package/src/evaluate.ts +349 -258
  65. package/src/events.spec.ts +154 -0
  66. package/src/events.ts +83 -0
  67. package/src/helpers.ts +33 -0
  68. package/src/hooks.ts +88 -0
  69. package/src/index.ts +3 -2
  70. package/src/instance.spec.ts +305 -489
  71. package/src/instance.ts +247 -391
  72. package/src/logger.spec.ts +212 -134
  73. package/src/logger.ts +36 -36
  74. package/src/murmurhash.ts +71 -0
  75. package/coverage/lcov-report/feature.ts.html +0 -508
  76. package/dist/bucket.d.ts +0 -30
  77. package/dist/feature.d.ts +0 -16
  78. package/dist/segments.d.ts +0 -5
  79. package/lib/bucket.d.ts +0 -30
  80. package/lib/feature.d.ts +0 -16
  81. package/lib/segments.d.ts +0 -5
  82. package/src/bucket.spec.ts +0 -37
  83. package/src/bucket.ts +0 -139
  84. package/src/feature.ts +0 -141
  85. package/src/segments.spec.ts +0 -468
  86. 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
  }