@kenji71089/evaluation 0.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 (209) hide show
  1. package/.release-please-manifest.json +3 -0
  2. package/README.md +9 -0
  3. package/eslint.config.mjs +27 -0
  4. package/lib/clauseEvaluator.d.ts +22 -0
  5. package/lib/clauseEvaluator.js +299 -0
  6. package/lib/clauseEvaluator.mjs +214 -0
  7. package/lib/dependencyEvaluator.d.ts +6 -0
  8. package/lib/dependencyEvaluator.js +46 -0
  9. package/lib/dependencyEvaluator.mjs +19 -0
  10. package/lib/errors.d.ts +9 -0
  11. package/lib/errors.js +15 -0
  12. package/lib/errors.mjs +12 -0
  13. package/lib/evaluation.d.ts +25 -0
  14. package/lib/evaluation.js +503 -0
  15. package/lib/evaluation.mjs +328 -0
  16. package/lib/google/api/annotations_pb.d.ts +8 -0
  17. package/lib/google/api/annotations_pb.js +40 -0
  18. package/lib/google/api/annotations_pb.mjs +54 -0
  19. package/lib/google/api/annotations_pb_service.d.ts +3 -0
  20. package/lib/google/api/annotations_pb_service.js +3 -0
  21. package/lib/google/api/annotations_pb_service.mjs +3 -0
  22. package/lib/google/api/http_pb.d.ts +132 -0
  23. package/lib/google/api/http_pb.js +860 -0
  24. package/lib/google/api/http_pb.mjs +982 -0
  25. package/lib/google/api/http_pb_service.d.ts +3 -0
  26. package/lib/google/api/http_pb_service.js +3 -0
  27. package/lib/google/api/http_pb_service.mjs +3 -0
  28. package/lib/google/rpc/code_pb.d.ts +26 -0
  29. package/lib/google/rpc/code_pb.js +44 -0
  30. package/lib/google/rpc/code_pb.mjs +48 -0
  31. package/lib/google/rpc/code_pb_service.d.ts +3 -0
  32. package/lib/google/rpc/code_pb_service.js +3 -0
  33. package/lib/google/rpc/code_pb_service.mjs +3 -0
  34. package/lib/google/rpc/error_details_pb.d.ts +322 -0
  35. package/lib/google/rpc/error_details_pb.js +2220 -0
  36. package/lib/google/rpc/error_details_pb.mjs +2499 -0
  37. package/lib/google/rpc/error_details_pb_service.d.ts +3 -0
  38. package/lib/google/rpc/error_details_pb_service.js +3 -0
  39. package/lib/google/rpc/error_details_pb_service.mjs +3 -0
  40. package/lib/google/rpc/status_pb.d.ts +36 -0
  41. package/lib/google/rpc/status_pb.js +235 -0
  42. package/lib/google/rpc/status_pb.mjs +268 -0
  43. package/lib/google/rpc/status_pb_service.d.ts +3 -0
  44. package/lib/google/rpc/status_pb_service.js +3 -0
  45. package/lib/google/rpc/status_pb_service.mjs +3 -0
  46. package/lib/index.d.ts +20 -0
  47. package/lib/index.js +199 -0
  48. package/lib/index.mjs +46 -0
  49. package/lib/modelFactory.d.ts +64 -0
  50. package/lib/modelFactory.js +206 -0
  51. package/lib/modelFactory.mjs +176 -0
  52. package/lib/proto/event/client/event_pb.d.ts +761 -0
  53. package/lib/proto/event/client/event_pb.js +5195 -0
  54. package/lib/proto/event/client/event_pb.mjs +5865 -0
  55. package/lib/proto/event/client/event_pb_service.d.ts +3 -0
  56. package/lib/proto/event/client/event_pb_service.js +3 -0
  57. package/lib/proto/event/client/event_pb_service.mjs +3 -0
  58. package/lib/proto/event/domain/event_pb.d.ts +4518 -0
  59. package/lib/proto/event/domain/event_pb.js +10834 -0
  60. package/lib/proto/event/domain/event_pb.mjs +33315 -0
  61. package/lib/proto/event/domain/event_pb_service.d.ts +3 -0
  62. package/lib/proto/event/domain/event_pb_service.js +3 -0
  63. package/lib/proto/event/domain/event_pb_service.mjs +3 -0
  64. package/lib/proto/event/domain/localized_message_pb.d.ts +29 -0
  65. package/lib/proto/event/domain/localized_message_pb.js +183 -0
  66. package/lib/proto/event/domain/localized_message_pb.mjs +206 -0
  67. package/lib/proto/event/domain/localized_message_pb_service.d.ts +3 -0
  68. package/lib/proto/event/domain/localized_message_pb_service.js +3 -0
  69. package/lib/proto/event/domain/localized_message_pb_service.mjs +3 -0
  70. package/lib/proto/event/service/feature_pb.d.ts +44 -0
  71. package/lib/proto/event/service/feature_pb.js +277 -0
  72. package/lib/proto/event/service/feature_pb.mjs +319 -0
  73. package/lib/proto/event/service/feature_pb_service.d.ts +3 -0
  74. package/lib/proto/event/service/feature_pb_service.js +3 -0
  75. package/lib/proto/event/service/feature_pb_service.mjs +3 -0
  76. package/lib/proto/event/service/segment_pb.d.ts +51 -0
  77. package/lib/proto/event/service/segment_pb.js +324 -0
  78. package/lib/proto/event/service/segment_pb.mjs +375 -0
  79. package/lib/proto/event/service/segment_pb_service.d.ts +3 -0
  80. package/lib/proto/event/service/segment_pb_service.js +3 -0
  81. package/lib/proto/event/service/segment_pb_service.mjs +3 -0
  82. package/lib/proto/event/service/user_pb.d.ts +49 -0
  83. package/lib/proto/event/service/user_pb.js +315 -0
  84. package/lib/proto/event/service/user_pb.mjs +362 -0
  85. package/lib/proto/event/service/user_pb_service.d.ts +3 -0
  86. package/lib/proto/event/service/user_pb_service.js +3 -0
  87. package/lib/proto/event/service/user_pb_service.mjs +3 -0
  88. package/lib/proto/feature/clause_pb.d.ts +57 -0
  89. package/lib/proto/feature/clause_pb.js +277 -0
  90. package/lib/proto/feature/clause_pb.mjs +312 -0
  91. package/lib/proto/feature/clause_pb_service.d.ts +3 -0
  92. package/lib/proto/feature/clause_pb_service.js +3 -0
  93. package/lib/proto/feature/clause_pb_service.mjs +3 -0
  94. package/lib/proto/feature/command_pb.d.ts +1213 -0
  95. package/lib/proto/feature/command_pb.js +8260 -0
  96. package/lib/proto/feature/command_pb.mjs +9275 -0
  97. package/lib/proto/feature/command_pb_service.d.ts +3 -0
  98. package/lib/proto/feature/command_pb_service.js +3 -0
  99. package/lib/proto/feature/command_pb_service.mjs +3 -0
  100. package/lib/proto/feature/evaluation_pb.d.ts +111 -0
  101. package/lib/proto/feature/evaluation_pb.js +685 -0
  102. package/lib/proto/feature/evaluation_pb.mjs +793 -0
  103. package/lib/proto/feature/evaluation_pb_service.d.ts +3 -0
  104. package/lib/proto/feature/evaluation_pb_service.js +3 -0
  105. package/lib/proto/feature/evaluation_pb_service.mjs +3 -0
  106. package/lib/proto/feature/feature_last_used_info_pb.d.ts +45 -0
  107. package/lib/proto/feature/feature_last_used_info_pb.js +283 -0
  108. package/lib/proto/feature/feature_last_used_info_pb.mjs +326 -0
  109. package/lib/proto/feature/feature_last_used_info_pb_service.d.ts +3 -0
  110. package/lib/proto/feature/feature_last_used_info_pb_service.js +3 -0
  111. package/lib/proto/feature/feature_last_used_info_pb_service.mjs +3 -0
  112. package/lib/proto/feature/feature_pb.d.ts +192 -0
  113. package/lib/proto/feature/feature_pb.js +1210 -0
  114. package/lib/proto/feature/feature_pb.mjs +1413 -0
  115. package/lib/proto/feature/feature_pb_service.d.ts +3 -0
  116. package/lib/proto/feature/feature_pb_service.js +3 -0
  117. package/lib/proto/feature/feature_pb_service.mjs +3 -0
  118. package/lib/proto/feature/flag_trigger_pb.d.ts +84 -0
  119. package/lib/proto/feature/flag_trigger_pb.js +452 -0
  120. package/lib/proto/feature/flag_trigger_pb.mjs +525 -0
  121. package/lib/proto/feature/flag_trigger_pb_service.d.ts +3 -0
  122. package/lib/proto/feature/flag_trigger_pb_service.js +3 -0
  123. package/lib/proto/feature/flag_trigger_pb_service.mjs +3 -0
  124. package/lib/proto/feature/prerequisite_pb.d.ts +29 -0
  125. package/lib/proto/feature/prerequisite_pb.js +183 -0
  126. package/lib/proto/feature/prerequisite_pb.mjs +206 -0
  127. package/lib/proto/feature/prerequisite_pb_service.d.ts +3 -0
  128. package/lib/proto/feature/prerequisite_pb_service.js +3 -0
  129. package/lib/proto/feature/prerequisite_pb_service.mjs +3 -0
  130. package/lib/proto/feature/reason_pb.d.ts +40 -0
  131. package/lib/proto/feature/reason_pb.js +196 -0
  132. package/lib/proto/feature/reason_pb.mjs +219 -0
  133. package/lib/proto/feature/reason_pb_service.d.ts +3 -0
  134. package/lib/proto/feature/reason_pb_service.js +3 -0
  135. package/lib/proto/feature/reason_pb_service.mjs +3 -0
  136. package/lib/proto/feature/rule_pb.d.ts +39 -0
  137. package/lib/proto/feature/rule_pb.js +254 -0
  138. package/lib/proto/feature/rule_pb.mjs +291 -0
  139. package/lib/proto/feature/rule_pb_service.d.ts +3 -0
  140. package/lib/proto/feature/rule_pb_service.js +3 -0
  141. package/lib/proto/feature/rule_pb_service.mjs +3 -0
  142. package/lib/proto/feature/segment_pb.d.ts +161 -0
  143. package/lib/proto/feature/segment_pb.js +974 -0
  144. package/lib/proto/feature/segment_pb.mjs +1127 -0
  145. package/lib/proto/feature/segment_pb_service.d.ts +3 -0
  146. package/lib/proto/feature/segment_pb_service.js +3 -0
  147. package/lib/proto/feature/segment_pb_service.mjs +3 -0
  148. package/lib/proto/feature/service_pb.d.ts +2158 -0
  149. package/lib/proto/feature/service_pb.js +5363 -0
  150. package/lib/proto/feature/service_pb.mjs +16348 -0
  151. package/lib/proto/feature/service_pb_service.d.ts +747 -0
  152. package/lib/proto/feature/service_pb_service.js +1424 -0
  153. package/lib/proto/feature/service_pb_service.mjs +1501 -0
  154. package/lib/proto/feature/strategy_pb.d.ts +110 -0
  155. package/lib/proto/feature/strategy_pb.js +712 -0
  156. package/lib/proto/feature/strategy_pb.mjs +803 -0
  157. package/lib/proto/feature/strategy_pb_service.d.ts +3 -0
  158. package/lib/proto/feature/strategy_pb_service.js +3 -0
  159. package/lib/proto/feature/strategy_pb_service.mjs +3 -0
  160. package/lib/proto/feature/target_pb.d.ts +31 -0
  161. package/lib/proto/feature/target_pb.js +207 -0
  162. package/lib/proto/feature/target_pb.mjs +232 -0
  163. package/lib/proto/feature/target_pb_service.d.ts +3 -0
  164. package/lib/proto/feature/target_pb_service.js +3 -0
  165. package/lib/proto/feature/target_pb_service.mjs +3 -0
  166. package/lib/proto/feature/variation_pb.d.ts +37 -0
  167. package/lib/proto/feature/variation_pb.js +233 -0
  168. package/lib/proto/feature/variation_pb.mjs +266 -0
  169. package/lib/proto/feature/variation_pb_service.d.ts +3 -0
  170. package/lib/proto/feature/variation_pb_service.js +3 -0
  171. package/lib/proto/feature/variation_pb_service.mjs +3 -0
  172. package/lib/proto/gateway/service_pb.d.ts +772 -0
  173. package/lib/proto/gateway/service_pb.js +5249 -0
  174. package/lib/proto/gateway/service_pb.mjs +6001 -0
  175. package/lib/proto/gateway/service_pb_service.d.ts +253 -0
  176. package/lib/proto/gateway/service_pb_service.js +436 -0
  177. package/lib/proto/gateway/service_pb_service.mjs +461 -0
  178. package/lib/proto/user/user_pb.d.ts +58 -0
  179. package/lib/proto/user/user_pb.js +410 -0
  180. package/lib/proto/user/user_pb.mjs +460 -0
  181. package/lib/proto/user/user_pb_service.d.ts +3 -0
  182. package/lib/proto/user/user_pb_service.js +3 -0
  183. package/lib/proto/user/user_pb_service.mjs +3 -0
  184. package/lib/protoc-gen-openapiv2/options/annotations_pb.d.ts +16 -0
  185. package/lib/protoc-gen-openapiv2/options/annotations_pb.js +100 -0
  186. package/lib/protoc-gen-openapiv2/options/annotations_pb.mjs +158 -0
  187. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.d.ts +3 -0
  188. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.js +3 -0
  189. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.mjs +3 -0
  190. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.d.ts +834 -0
  191. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.js +5456 -0
  192. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.mjs +6256 -0
  193. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.d.ts +3 -0
  194. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.js +3 -0
  195. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.mjs +3 -0
  196. package/lib/ruleEvaluator.d.ts +13 -0
  197. package/lib/ruleEvaluator.js +80 -0
  198. package/lib/ruleEvaluator.mjs +41 -0
  199. package/lib/segmentEvaluator.d.ts +7 -0
  200. package/lib/segmentEvaluator.js +74 -0
  201. package/lib/segmentEvaluator.mjs +34 -0
  202. package/lib/strategyEvaluator.d.ts +10 -0
  203. package/lib/strategyEvaluator.js +135 -0
  204. package/lib/strategyEvaluator.mjs +83 -0
  205. package/lib/userEvaluation.d.ts +8 -0
  206. package/lib/userEvaluation.js +70 -0
  207. package/lib/userEvaluation.mjs +60 -0
  208. package/package.json +46 -0
  209. package/release-please-config.json +22 -0
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "0.0.1"
3
+ }
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Evaluation module for Node.JS
2
+
3
+ ## Setup
4
+ ```export NPM_TOKEN="YOUR_NPM_TOKEN"```
5
+ ```make init```
6
+ ```make copy_proto_generated_code```
7
+
8
+ ## Unit tests
9
+ ```make test```
@@ -0,0 +1,27 @@
1
+ import tsEslint from 'typescript-eslint';
2
+ import tsParser from '@typescript-eslint/parser';
3
+ import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4
+
5
+ export default [
6
+ {
7
+ files: ['src/**/*.ts', 'test/**/*.ts'],
8
+ ignores: ['**/*.d.ts', 'src/google/', 'src/protoc-gen-openapiv2/', 'src/proto/'],
9
+ languageOptions: {
10
+ parser: tsParser,
11
+ parserOptions: {
12
+ sourceType: 'module',
13
+ project: [`tsconfig.json`, `tsconfig.test.json`],
14
+ },
15
+ globals: {
16
+ node: true,
17
+ },
18
+ },
19
+ plugins: {
20
+ ...tsEslint.configs.recommended,
21
+ eslintPluginPrettierRecommended,
22
+ },
23
+ rules: {
24
+ quotes: ['error', 'single', { avoidEscape: true }],
25
+ },
26
+ },
27
+ ];
@@ -0,0 +1,22 @@
1
+ import { Clause } from './proto/feature/clause_pb';
2
+ import { SegmentUser } from './proto/feature/segment_pb';
3
+ declare class ClauseEvaluator {
4
+ private segmentEvaluator;
5
+ private dependencyEvaluator;
6
+ constructor();
7
+ evaluate(targetValue: string, clause: Clause, userID: string, segmentUsers: SegmentUser[], flagVariations: {
8
+ [key: string]: string;
9
+ }): boolean;
10
+ private equals;
11
+ private partiallyMatches;
12
+ private in;
13
+ private startsWith;
14
+ private endsWith;
15
+ private greater;
16
+ private greaterOrEqual;
17
+ private less;
18
+ private lessOrEqual;
19
+ private before;
20
+ private after;
21
+ }
22
+ export { ClauseEvaluator };
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
5
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
6
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
7
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
8
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
9
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
10
+ var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ var desc = Object.getOwnPropertyDescriptor(m, k);
13
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14
+ desc = {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return m[k];
18
+ }
19
+ };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
22
+ } : function (o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ o[k2] = m[k];
25
+ });
26
+ var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
27
+ Object.defineProperty(o, "default", {
28
+ enumerable: true,
29
+ value: v
30
+ });
31
+ } : function (o, v) {
32
+ o["default"] = v;
33
+ });
34
+ var __importStar = void 0 && (void 0).__importStar || function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ Object.defineProperty(exports, "__esModule", {
42
+ value: true
43
+ });
44
+ exports.ClauseEvaluator = void 0;
45
+ var clause_pb_1 = require("./proto/feature/clause_pb");
46
+ var segmentEvaluator_1 = require("./segmentEvaluator");
47
+ var dependencyEvaluator_1 = require("./dependencyEvaluator");
48
+ var semver = __importStar(require("semver"));
49
+ //
50
+ var ClauseEvaluator = /*#__PURE__*/function () {
51
+ function ClauseEvaluator() {
52
+ _classCallCheck(this, ClauseEvaluator);
53
+ _defineProperty(this, "segmentEvaluator", void 0);
54
+ _defineProperty(this, "dependencyEvaluator", void 0);
55
+ this.segmentEvaluator = new segmentEvaluator_1.SegmentEvaluator();
56
+ this.dependencyEvaluator = new dependencyEvaluator_1.DependencyEvaluator();
57
+ }
58
+ return _createClass(ClauseEvaluator, [{
59
+ key: "evaluate",
60
+ value: function evaluate(targetValue, clause, userID, segmentUsers, flagVariations) {
61
+ try {
62
+ switch (clause.getOperator()) {
63
+ case clause_pb_1.Clause.Operator.EQUALS:
64
+ return this.equals(targetValue, clause.getValuesList());
65
+ case clause_pb_1.Clause.Operator.IN:
66
+ return this["in"](targetValue, clause.getValuesList());
67
+ case clause_pb_1.Clause.Operator.STARTS_WITH:
68
+ return this.startsWith(targetValue, clause.getValuesList());
69
+ case clause_pb_1.Clause.Operator.ENDS_WITH:
70
+ return this.endsWith(targetValue, clause.getValuesList());
71
+ case clause_pb_1.Clause.Operator.SEGMENT:
72
+ return this.segmentEvaluator.evaluate(clause.getValuesList(), userID, segmentUsers);
73
+ case clause_pb_1.Clause.Operator.GREATER:
74
+ return this.greater(targetValue, clause.getValuesList());
75
+ case clause_pb_1.Clause.Operator.GREATER_OR_EQUAL:
76
+ return this.greaterOrEqual(targetValue, clause.getValuesList());
77
+ case clause_pb_1.Clause.Operator.LESS:
78
+ return this.less(targetValue, clause.getValuesList());
79
+ case clause_pb_1.Clause.Operator.LESS_OR_EQUAL:
80
+ return this.lessOrEqual(targetValue, clause.getValuesList());
81
+ case clause_pb_1.Clause.Operator.BEFORE:
82
+ return this.before(targetValue, clause.getValuesList());
83
+ case clause_pb_1.Clause.Operator.AFTER:
84
+ return this.after(targetValue, clause.getValuesList());
85
+ case clause_pb_1.Clause.Operator.FEATURE_FLAG:
86
+ return this.dependencyEvaluator.evaluate(clause.getAttribute(), clause.getValuesList(), flagVariations);
87
+ case clause_pb_1.Clause.Operator.PARTIALLY_MATCH:
88
+ return this.partiallyMatches(targetValue, clause.getValuesList());
89
+ default:
90
+ return false;
91
+ }
92
+ } catch (error) {
93
+ console.error('Error evaluating clause:', error);
94
+ throw error;
95
+ }
96
+ }
97
+ }, {
98
+ key: "equals",
99
+ value: function equals(targetValue, values) {
100
+ return values.includes(targetValue);
101
+ }
102
+ }, {
103
+ key: "partiallyMatches",
104
+ value: function partiallyMatches(targetValue, values) {
105
+ return values.some(function (value) {
106
+ return targetValue.includes(value);
107
+ });
108
+ }
109
+ }, {
110
+ key: "in",
111
+ value: function _in(targetValue, values) {
112
+ return values.includes(targetValue);
113
+ }
114
+ }, {
115
+ key: "startsWith",
116
+ value: function startsWith(targetValue, values) {
117
+ return values.some(function (value) {
118
+ return targetValue.startsWith(value);
119
+ });
120
+ }
121
+ }, {
122
+ key: "endsWith",
123
+ value: function endsWith(targetValue, values) {
124
+ return values.some(function (value) {
125
+ return targetValue.endsWith(value);
126
+ });
127
+ }
128
+ }, {
129
+ key: "greater",
130
+ value: function greater(targetValue, values) {
131
+ var floatTarget = validateAndParseFloat(targetValue);
132
+ var floatValues = values.map(validateAndParseFloat).filter(function (value) {
133
+ return !isNaN(value);
134
+ });
135
+ if (!isNaN(floatTarget)) {
136
+ if (floatValues.length == 0) {
137
+ return false;
138
+ }
139
+ return floatValues.some(function (value) {
140
+ return floatTarget > value;
141
+ });
142
+ }
143
+ var semverTarget = semver.valid(targetValue);
144
+ if (semverTarget) {
145
+ return values.some(function (value) {
146
+ if (semver.valid(value)) {
147
+ return semver.gt(semverTarget, value);
148
+ }
149
+ return false;
150
+ });
151
+ }
152
+ return values.some(function (value) {
153
+ return targetValue > value;
154
+ });
155
+ }
156
+ }, {
157
+ key: "greaterOrEqual",
158
+ value: function greaterOrEqual(targetValue, values) {
159
+ var floatTarget = validateAndParseFloat(targetValue);
160
+ var floatValues = values.map(validateAndParseFloat).filter(function (value) {
161
+ return !isNaN(value);
162
+ });
163
+ if (!isNaN(floatTarget)) {
164
+ if (floatValues.length == 0) {
165
+ return false;
166
+ }
167
+ return floatValues.some(function (value) {
168
+ return floatTarget >= value;
169
+ });
170
+ }
171
+ var semverTarget = semver.valid(targetValue);
172
+ if (semverTarget) {
173
+ return values.some(function (value) {
174
+ if (semver.valid(value)) {
175
+ return semver.gte(semverTarget, value);
176
+ }
177
+ return false;
178
+ });
179
+ }
180
+ return values.some(function (value) {
181
+ return targetValue >= value;
182
+ });
183
+ }
184
+ }, {
185
+ key: "less",
186
+ value: function less(targetValue, values) {
187
+ var floatTarget = validateAndParseFloat(targetValue);
188
+ var floatValues = values.map(validateAndParseFloat).filter(function (value) {
189
+ return !isNaN(value);
190
+ });
191
+ if (!isNaN(floatTarget)) {
192
+ if (floatValues.length == 0) {
193
+ return false;
194
+ }
195
+ return floatValues.some(function (value) {
196
+ return floatTarget < value;
197
+ });
198
+ }
199
+ var semverTarget = semver.valid(targetValue);
200
+ if (semverTarget) {
201
+ return values.some(function (value) {
202
+ if (semver.valid(value)) {
203
+ return semver.lt(semverTarget, value);
204
+ }
205
+ return false;
206
+ });
207
+ }
208
+ return values.some(function (value) {
209
+ return targetValue < value;
210
+ });
211
+ }
212
+ }, {
213
+ key: "lessOrEqual",
214
+ value: function lessOrEqual(targetValue, values) {
215
+ var floatTarget = validateAndParseFloat(targetValue);
216
+ var floatValues = values.map(validateAndParseFloat).filter(function (value) {
217
+ return !isNaN(value);
218
+ });
219
+ if (!isNaN(floatTarget)) {
220
+ if (floatValues.length == 0) {
221
+ return false;
222
+ }
223
+ return floatValues.some(function (value) {
224
+ return floatTarget <= value;
225
+ });
226
+ }
227
+ var semverTarget = semver.valid(targetValue);
228
+ if (semverTarget) {
229
+ return values.some(function (value) {
230
+ if (semver.valid(value)) {
231
+ return semver.lte(semverTarget, value);
232
+ }
233
+ return false;
234
+ });
235
+ }
236
+ return values.some(function (value) {
237
+ return targetValue <= value;
238
+ });
239
+ }
240
+ }, {
241
+ key: "before",
242
+ value: function before(targetValue, values) {
243
+ var intTarget = validateAndParseInt(targetValue);
244
+ if (isNaN(intTarget)) {
245
+ return false;
246
+ }
247
+ var intValues = values.map(function (value) {
248
+ return parseInt(value, 10);
249
+ }).filter(function (value) {
250
+ return !isNaN(value);
251
+ });
252
+ if (intValues.length == 0) {
253
+ return false;
254
+ }
255
+ return intValues.some(function (value) {
256
+ return intTarget < value;
257
+ });
258
+ }
259
+ }, {
260
+ key: "after",
261
+ value: function after(targetValue, values) {
262
+ var intTarget = parseInt(targetValue, 10);
263
+ var intValues = values.map(function (value) {
264
+ return parseInt(value, 10);
265
+ }).filter(function (value) {
266
+ return !isNaN(value);
267
+ });
268
+ return intValues.some(function (value) {
269
+ return intTarget > value;
270
+ });
271
+ }
272
+ }]);
273
+ }();
274
+ exports.ClauseEvaluator = ClauseEvaluator;
275
+ function isNumericString(input) {
276
+ return /^-?\d*\.?\d+$/.test(input);
277
+ }
278
+ function validateAndParseInt(input) {
279
+ // Check if the string is a valid number using regex
280
+ if (!isNumericString(input)) {
281
+ // Will not allow a string like `1a`, `0b`
282
+ return NaN;
283
+ }
284
+ // Parse the string to float
285
+ var result = parseInt(input, 10);
286
+ // If parsing fails, return NaN
287
+ return isNaN(result) ? NaN : result;
288
+ }
289
+ function validateAndParseFloat(input) {
290
+ // Check if the string is a valid number using regex
291
+ if (!isNumericString(input)) {
292
+ // Will not allow a string like `1a`, `0b`
293
+ return NaN;
294
+ }
295
+ // Parse the string to float
296
+ var result = parseFloat(input);
297
+ // If parsing fails, return NaN
298
+ return isNaN(result) ? NaN : result;
299
+ }
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ClauseEvaluator = void 0;
27
+ const clause_pb_1 = require("./proto/feature/clause_pb");
28
+ const segmentEvaluator_1 = require("./segmentEvaluator");
29
+ const dependencyEvaluator_1 = require("./dependencyEvaluator");
30
+ const semver = __importStar(require("semver"));
31
+ //
32
+ class ClauseEvaluator {
33
+ segmentEvaluator;
34
+ dependencyEvaluator;
35
+ constructor() {
36
+ this.segmentEvaluator = new segmentEvaluator_1.SegmentEvaluator();
37
+ this.dependencyEvaluator = new dependencyEvaluator_1.DependencyEvaluator();
38
+ }
39
+ evaluate(targetValue, clause, userID, segmentUsers, flagVariations) {
40
+ try {
41
+ switch (clause.getOperator()) {
42
+ case clause_pb_1.Clause.Operator.EQUALS:
43
+ return this.equals(targetValue, clause.getValuesList());
44
+ case clause_pb_1.Clause.Operator.IN:
45
+ return this.in(targetValue, clause.getValuesList());
46
+ case clause_pb_1.Clause.Operator.STARTS_WITH:
47
+ return this.startsWith(targetValue, clause.getValuesList());
48
+ case clause_pb_1.Clause.Operator.ENDS_WITH:
49
+ return this.endsWith(targetValue, clause.getValuesList());
50
+ case clause_pb_1.Clause.Operator.SEGMENT:
51
+ return this.segmentEvaluator.evaluate(clause.getValuesList(), userID, segmentUsers);
52
+ case clause_pb_1.Clause.Operator.GREATER:
53
+ return this.greater(targetValue, clause.getValuesList());
54
+ case clause_pb_1.Clause.Operator.GREATER_OR_EQUAL:
55
+ return this.greaterOrEqual(targetValue, clause.getValuesList());
56
+ case clause_pb_1.Clause.Operator.LESS:
57
+ return this.less(targetValue, clause.getValuesList());
58
+ case clause_pb_1.Clause.Operator.LESS_OR_EQUAL:
59
+ return this.lessOrEqual(targetValue, clause.getValuesList());
60
+ case clause_pb_1.Clause.Operator.BEFORE:
61
+ return this.before(targetValue, clause.getValuesList());
62
+ case clause_pb_1.Clause.Operator.AFTER:
63
+ return this.after(targetValue, clause.getValuesList());
64
+ case clause_pb_1.Clause.Operator.FEATURE_FLAG:
65
+ return this.dependencyEvaluator.evaluate(clause.getAttribute(), clause.getValuesList(), flagVariations);
66
+ case clause_pb_1.Clause.Operator.PARTIALLY_MATCH:
67
+ return this.partiallyMatches(targetValue, clause.getValuesList());
68
+ default:
69
+ return false;
70
+ }
71
+ }
72
+ catch (error) {
73
+ console.error('Error evaluating clause:', error);
74
+ throw error;
75
+ }
76
+ }
77
+ equals(targetValue, values) {
78
+ return values.includes(targetValue);
79
+ }
80
+ partiallyMatches(targetValue, values) {
81
+ return values.some((value) => targetValue.includes(value));
82
+ }
83
+ in(targetValue, values) {
84
+ return values.includes(targetValue);
85
+ }
86
+ startsWith(targetValue, values) {
87
+ return values.some((value) => targetValue.startsWith(value));
88
+ }
89
+ endsWith(targetValue, values) {
90
+ return values.some((value) => targetValue.endsWith(value));
91
+ }
92
+ greater(targetValue, values) {
93
+ const floatTarget = validateAndParseFloat(targetValue);
94
+ const floatValues = values.map(validateAndParseFloat).filter((value) => !isNaN(value));
95
+ if (!isNaN(floatTarget)) {
96
+ if (floatValues.length == 0) {
97
+ return false;
98
+ }
99
+ return floatValues.some((value) => floatTarget > value);
100
+ }
101
+ const semverTarget = semver.valid(targetValue);
102
+ if (semverTarget) {
103
+ return values.some((value) => {
104
+ if (semver.valid(value)) {
105
+ return semver.gt(semverTarget, value);
106
+ }
107
+ return false;
108
+ });
109
+ }
110
+ return values.some((value) => targetValue > value);
111
+ }
112
+ greaterOrEqual(targetValue, values) {
113
+ const floatTarget = validateAndParseFloat(targetValue);
114
+ const floatValues = values.map(validateAndParseFloat).filter((value) => !isNaN(value));
115
+ if (!isNaN(floatTarget)) {
116
+ if (floatValues.length == 0) {
117
+ return false;
118
+ }
119
+ return floatValues.some((value) => floatTarget >= value);
120
+ }
121
+ const semverTarget = semver.valid(targetValue);
122
+ if (semverTarget) {
123
+ return values.some((value) => {
124
+ if (semver.valid(value)) {
125
+ return semver.gte(semverTarget, value);
126
+ }
127
+ return false;
128
+ });
129
+ }
130
+ return values.some((value) => targetValue >= value);
131
+ }
132
+ less(targetValue, values) {
133
+ const floatTarget = validateAndParseFloat(targetValue);
134
+ const floatValues = values.map(validateAndParseFloat).filter((value) => !isNaN(value));
135
+ if (!isNaN(floatTarget)) {
136
+ if (floatValues.length == 0) {
137
+ return false;
138
+ }
139
+ return floatValues.some((value) => floatTarget < value);
140
+ }
141
+ const semverTarget = semver.valid(targetValue);
142
+ if (semverTarget) {
143
+ return values.some((value) => {
144
+ if (semver.valid(value)) {
145
+ return semver.lt(semverTarget, value);
146
+ }
147
+ return false;
148
+ });
149
+ }
150
+ return values.some((value) => targetValue < value);
151
+ }
152
+ lessOrEqual(targetValue, values) {
153
+ const floatTarget = validateAndParseFloat(targetValue);
154
+ const floatValues = values.map(validateAndParseFloat).filter((value) => !isNaN(value));
155
+ if (!isNaN(floatTarget)) {
156
+ if (floatValues.length == 0) {
157
+ return false;
158
+ }
159
+ return floatValues.some((value) => floatTarget <= value);
160
+ }
161
+ const semverTarget = semver.valid(targetValue);
162
+ if (semverTarget) {
163
+ return values.some((value) => {
164
+ if (semver.valid(value)) {
165
+ return semver.lte(semverTarget, value);
166
+ }
167
+ return false;
168
+ });
169
+ }
170
+ return values.some((value) => targetValue <= value);
171
+ }
172
+ before(targetValue, values) {
173
+ const intTarget = validateAndParseInt(targetValue);
174
+ if (isNaN(intTarget)) {
175
+ return false;
176
+ }
177
+ const intValues = values.map((value) => parseInt(value, 10)).filter((value) => !isNaN(value));
178
+ if (intValues.length == 0) {
179
+ return false;
180
+ }
181
+ return intValues.some((value) => intTarget < value);
182
+ }
183
+ after(targetValue, values) {
184
+ const intTarget = parseInt(targetValue, 10);
185
+ const intValues = values.map((value) => parseInt(value, 10)).filter((value) => !isNaN(value));
186
+ return intValues.some((value) => intTarget > value);
187
+ }
188
+ }
189
+ exports.ClauseEvaluator = ClauseEvaluator;
190
+ function isNumericString(input) {
191
+ return /^-?\d*\.?\d+$/.test(input);
192
+ }
193
+ function validateAndParseInt(input) {
194
+ // Check if the string is a valid number using regex
195
+ if (!isNumericString(input)) {
196
+ // Will not allow a string like `1a`, `0b`
197
+ return NaN;
198
+ }
199
+ // Parse the string to float
200
+ const result = parseInt(input, 10);
201
+ // If parsing fails, return NaN
202
+ return isNaN(result) ? NaN : result;
203
+ }
204
+ function validateAndParseFloat(input) {
205
+ // Check if the string is a valid number using regex
206
+ if (!isNumericString(input)) {
207
+ // Will not allow a string like `1a`, `0b`
208
+ return NaN;
209
+ }
210
+ // Parse the string to float
211
+ const result = parseFloat(input);
212
+ // If parsing fails, return NaN
213
+ return isNaN(result) ? NaN : result;
214
+ }
@@ -0,0 +1,6 @@
1
+ declare class DependencyEvaluator {
2
+ evaluate(featureID: string, variationIDs: string[], flagVariations: {
3
+ [key: string]: string;
4
+ }): boolean;
5
+ }
6
+ export { DependencyEvaluator };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
5
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
6
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
7
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
8
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
9
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
10
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
11
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.DependencyEvaluator = void 0;
16
+ var errors_1 = require("./errors");
17
+ var DependencyEvaluator = /*#__PURE__*/function () {
18
+ function DependencyEvaluator() {
19
+ _classCallCheck(this, DependencyEvaluator);
20
+ }
21
+ return _createClass(DependencyEvaluator, [{
22
+ key: "evaluate",
23
+ value: function evaluate(featureID, variationIDs, flagVariations) {
24
+ var targetVarID = flagVariations[featureID];
25
+ if (!targetVarID) {
26
+ throw errors_1.EVALUATOR_ERRORS.FeatureNotFound; // Throwing an error if feature is not found
27
+ }
28
+ var _iterator = _createForOfIteratorHelper(variationIDs),
29
+ _step;
30
+ try {
31
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
32
+ var varID = _step.value;
33
+ if (varID === targetVarID) {
34
+ return true; // Match found
35
+ }
36
+ }
37
+ } catch (err) {
38
+ _iterator.e(err);
39
+ } finally {
40
+ _iterator.f();
41
+ }
42
+ return false; // No match found
43
+ }
44
+ }]);
45
+ }();
46
+ exports.DependencyEvaluator = DependencyEvaluator;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DependencyEvaluator = void 0;
4
+ const errors_1 = require("./errors");
5
+ class DependencyEvaluator {
6
+ evaluate(featureID, variationIDs, flagVariations) {
7
+ const targetVarID = flagVariations[featureID];
8
+ if (!targetVarID) {
9
+ throw errors_1.EVALUATOR_ERRORS.FeatureNotFound; // Throwing an error if feature is not found
10
+ }
11
+ for (const varID of variationIDs) {
12
+ if (varID === targetVarID) {
13
+ return true; // Match found
14
+ }
15
+ }
16
+ return false; // No match found
17
+ }
18
+ }
19
+ exports.DependencyEvaluator = DependencyEvaluator;
@@ -0,0 +1,9 @@
1
+ declare const EVALUATOR_ERRORS: {
2
+ StrategyNotFound: Error;
3
+ DefaultStrategyNotFound: Error;
4
+ FeatureNotFound: Error;
5
+ PrerequisiteVariationNotFound: Error;
6
+ VariationNotFound: Error;
7
+ UnsupportedStrategy: Error;
8
+ };
9
+ export { EVALUATOR_ERRORS };
package/lib/errors.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.EVALUATOR_ERRORS = void 0;
7
+ var EVALUATOR_ERRORS = {
8
+ StrategyNotFound: new Error('evaluator: strategy not found'),
9
+ DefaultStrategyNotFound: new Error('evaluator: default strategy not found'),
10
+ FeatureNotFound: new Error('evaluator: feature not found'),
11
+ PrerequisiteVariationNotFound: new Error('evaluator: prerequisite variation not found'),
12
+ VariationNotFound: new Error('evaluator: variation not found'),
13
+ UnsupportedStrategy: new Error('evaluator: unsupported strategy')
14
+ };
15
+ exports.EVALUATOR_ERRORS = EVALUATOR_ERRORS;