@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.
- package/CHANGELOG.md +8 -0
- package/README.md +2 -381
- package/coverage/clover.xml +707 -645
- package/coverage/coverage-final.json +11 -9
- package/coverage/lcov-report/{segments.ts.html → bucketer.ts.html} +155 -77
- package/coverage/lcov-report/child.ts.html +940 -0
- package/coverage/lcov-report/conditions.ts.html +107 -158
- package/coverage/lcov-report/datafileReader.ts.html +763 -103
- package/coverage/lcov-report/emitter.ts.html +77 -59
- package/coverage/lcov-report/evaluate.ts.html +689 -416
- package/coverage/lcov-report/events.ts.html +334 -0
- package/coverage/lcov-report/helpers.ts.html +184 -0
- package/coverage/lcov-report/{bucket.ts.html → hooks.ts.html} +86 -239
- package/coverage/lcov-report/index.html +119 -89
- package/coverage/lcov-report/instance.ts.html +341 -773
- package/coverage/lcov-report/logger.ts.html +64 -64
- package/coverage/lcov.info +1433 -1226
- package/dist/bucketer.d.ts +11 -0
- package/dist/child.d.ts +26 -0
- package/dist/compareVersions.d.ts +4 -0
- package/dist/conditions.d.ts +4 -4
- package/dist/datafileReader.d.ts +26 -6
- package/dist/emitter.d.ts +8 -9
- package/dist/evaluate.d.ts +31 -29
- package/dist/events.d.ts +5 -0
- package/dist/helpers.d.ts +5 -0
- package/dist/hooks.d.ts +45 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.gz +0 -0
- package/dist/index.mjs.map +1 -1
- package/dist/instance.d.ts +40 -72
- package/dist/logger.d.ts +6 -5
- package/dist/murmurhash.d.ts +1 -0
- package/jest.config.js +2 -0
- package/lib/bucketer.d.ts +11 -0
- package/lib/child.d.ts +26 -0
- package/lib/compareVersions.d.ts +4 -0
- package/lib/conditions.d.ts +4 -4
- package/lib/datafileReader.d.ts +26 -6
- package/lib/emitter.d.ts +8 -9
- package/lib/evaluate.d.ts +31 -29
- package/lib/events.d.ts +5 -0
- package/lib/helpers.d.ts +5 -0
- package/lib/hooks.d.ts +45 -0
- package/lib/index.d.ts +3 -2
- package/lib/instance.d.ts +40 -72
- package/lib/logger.d.ts +6 -5
- package/lib/murmurhash.d.ts +1 -0
- package/package.json +3 -5
- package/src/bucketer.spec.ts +165 -0
- package/src/bucketer.ts +84 -0
- package/src/child.spec.ts +267 -0
- package/src/child.ts +285 -0
- package/src/compareVersions.ts +93 -0
- package/src/conditions.spec.ts +563 -353
- package/src/conditions.ts +46 -63
- package/src/datafileReader.spec.ts +396 -84
- package/src/datafileReader.ts +280 -60
- package/src/emitter.spec.ts +27 -86
- package/src/emitter.ts +38 -32
- package/src/evaluate.ts +349 -258
- package/src/events.spec.ts +154 -0
- package/src/events.ts +83 -0
- package/src/helpers.ts +33 -0
- package/src/hooks.ts +88 -0
- package/src/index.ts +3 -2
- package/src/instance.spec.ts +305 -489
- package/src/instance.ts +247 -391
- package/src/logger.spec.ts +212 -134
- package/src/logger.ts +36 -36
- package/src/murmurhash.ts +71 -0
- package/coverage/lcov-report/feature.ts.html +0 -508
- package/dist/bucket.d.ts +0 -30
- package/dist/feature.d.ts +0 -16
- package/dist/segments.d.ts +0 -5
- package/lib/bucket.d.ts +0 -30
- package/lib/feature.d.ts +0 -16
- package/lib/segments.d.ts +0 -5
- package/src/bucket.spec.ts +0 -37
- package/src/bucket.ts +0 -139
- package/src/feature.ts +0 -141
- package/src/segments.spec.ts +0 -468
- package/src/segments.ts +0 -58
package/src/conditions.ts
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Context, PlainCondition, AttributeValue } from "@featurevisor/types";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { GetRegex } from "./datafileReader";
|
|
4
|
+
import { compareVersions } from "./compareVersions";
|
|
4
5
|
|
|
5
|
-
|
|
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(
|
|
8
|
-
|
|
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
|
|
23
|
+
return contextValueFromPath === value;
|
|
12
24
|
} else if (operator === "notEquals") {
|
|
13
|
-
return
|
|
25
|
+
return contextValueFromPath !== value;
|
|
14
26
|
} else if (operator === "before" || operator === "after") {
|
|
15
27
|
// date comparisons
|
|
16
|
-
const valueInContext =
|
|
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
|
|
39
|
+
(["string", "number"].indexOf(typeof contextValueFromPath) !== -1 ||
|
|
40
|
+
contextValueFromPath === null)
|
|
28
41
|
) {
|
|
29
|
-
// array
|
|
30
|
-
const valueInContext =
|
|
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
|
|
50
|
+
} else if (typeof contextValueFromPath === "string" && typeof value === "string") {
|
|
38
51
|
// string
|
|
39
|
-
const valueInContext =
|
|
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
|
|
81
|
+
} else if (typeof contextValueFromPath === "number" && typeof value === "number") {
|
|
63
82
|
// numeric
|
|
64
|
-
const valueInContext =
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
|
|
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
|
}
|