@convertcom/js-sdk-rules 0.0.1 → 1.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/LICENSE.txt +202 -0
- package/README.md +11 -0
- package/lib/index.d.ts +87 -1
- package/lib/index.js +240 -0
- package/lib/index.js.map +1 -0
- package/lib/index.min.js +2 -0
- package/lib/index.min.js.map +1 -0
- package/lib/index.min.mjs +1 -0
- package/lib/index.min.mjs.map +1 -0
- package/lib/index.mjs +240 -0
- package/lib/index.mjs.map +1 -0
- package/lib/legacy/index.js +304 -0
- package/lib/legacy/index.js.map +1 -0
- package/lib/legacy/index.min.js +8 -0
- package/lib/legacy/index.min.js.map +1 -0
- package/lib/package.json +18 -0
- package/package.json +55 -1
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Convert Insights, Inc JavaScript SDK - Rules Manager
|
|
2
|
+
|
|
3
|
+
[](https://choosealicense.com/licenses/apache-2.0/)
|
|
4
|
+
|
|
5
|
+
## About
|
|
6
|
+
|
|
7
|
+
This repository contains the **Rules Manager** dependency for **JavaScript SDK** needed to integrate Convert.com's **Fullstack Product (feature tests, feature flags, feature rollouts)** inside third-party apps. This is maintained by the Convert.com team.
|
|
8
|
+
|
|
9
|
+
## Credits
|
|
10
|
+
|
|
11
|
+
Copyright(c) 2023 Convert Insights, Inc.
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,92 @@
|
|
|
1
|
+
import { RuleSet, Rule, Config } from '@convertcom/js-sdk-types';
|
|
2
|
+
import { RuleError } from '@convertcom/js-sdk-enums';
|
|
3
|
+
import { LogManagerInterface } from '@convertcom/js-sdk-logger';
|
|
4
|
+
|
|
1
5
|
/*!
|
|
2
|
-
* Convert JS SDK
|
|
6
|
+
* Convert JS SDK
|
|
3
7
|
* Version 1.0.0
|
|
4
8
|
* Copyright(c) 2020 Convert Insights, Inc
|
|
5
9
|
* License Apache-2.0
|
|
6
10
|
*/
|
|
11
|
+
|
|
12
|
+
interface RuleManagerInterface {
|
|
13
|
+
comparisonProcessor: Record<string, any>;
|
|
14
|
+
getComparisonProcessorMethods(): Array<string>;
|
|
15
|
+
isRuleMatched(data: Record<string, any>, ruleSet: RuleSet, logEntry?: string): boolean | RuleError;
|
|
16
|
+
isValidRule(rule: Rule): boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Provides rule processing calculations with corresponding comparisons methods
|
|
21
|
+
* @category Modules
|
|
22
|
+
* @constructor
|
|
23
|
+
* @implements {RuleManagerInterface}
|
|
24
|
+
*/
|
|
25
|
+
declare class RuleManager implements RuleManagerInterface {
|
|
26
|
+
private _comparisonProcessor;
|
|
27
|
+
private readonly _negation;
|
|
28
|
+
private readonly _keys_case_sensitive;
|
|
29
|
+
private _loggerManager;
|
|
30
|
+
private _mapper;
|
|
31
|
+
/**
|
|
32
|
+
* @param {Config=} config
|
|
33
|
+
* @param {Object=} dependencies
|
|
34
|
+
* @param {LogManagerInterface=} dependencies.loggerManager
|
|
35
|
+
*/
|
|
36
|
+
constructor(config?: Config, { loggerManager }?: {
|
|
37
|
+
loggerManager?: LogManagerInterface;
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Setter for comparison processor
|
|
41
|
+
* @param {Record<string, any>} comparisonProcessor
|
|
42
|
+
*/
|
|
43
|
+
set comparisonProcessor(comparisonProcessor: Record<string, any>);
|
|
44
|
+
/**
|
|
45
|
+
* Getter for comparison processor
|
|
46
|
+
*/
|
|
47
|
+
get comparisonProcessor(): Record<string, any>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieve comparison methods from comparison processor
|
|
50
|
+
* @return {Array<string>} List of methods of comparison processor
|
|
51
|
+
*/
|
|
52
|
+
getComparisonProcessorMethods(): Array<string>;
|
|
53
|
+
/**
|
|
54
|
+
* Check input data matching to rule set
|
|
55
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
56
|
+
* @param {RuleSet} ruleSet
|
|
57
|
+
* @return {boolean | RuleError}
|
|
58
|
+
*/
|
|
59
|
+
isRuleMatched(data: Record<string, any>, ruleSet: RuleSet, logEntry?: string): boolean | RuleError;
|
|
60
|
+
/**
|
|
61
|
+
* Check is rule object valid
|
|
62
|
+
* @param {Rule} rule
|
|
63
|
+
* @return {boolean}
|
|
64
|
+
*/
|
|
65
|
+
isValidRule(rule: Rule): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Process AND block of rule set. Return first false if found
|
|
68
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
69
|
+
* @param {RuleAnd} rulesSubset
|
|
70
|
+
* @return {boolean | RuleError}
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
private _processAND;
|
|
74
|
+
/**
|
|
75
|
+
* Process OR block of rule set. Return first true if found
|
|
76
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
77
|
+
* @param {RuleOrWhen} rulesSubset
|
|
78
|
+
* @return {boolean | RuleError}
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
81
|
+
private _processORWHEN;
|
|
82
|
+
/**
|
|
83
|
+
* Process single rule item
|
|
84
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
85
|
+
* @param {Rule} rule A single rule to compare
|
|
86
|
+
* @return {boolean | RuleError} Comparison result
|
|
87
|
+
* @private
|
|
88
|
+
*/
|
|
89
|
+
private _processRuleItem;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { RuleManager, type RuleManagerInterface };
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,248 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var jsSdkUtils = require('@convertcom/js-sdk-utils');
|
|
4
|
+
var jsSdkEnums = require('@convertcom/js-sdk-enums');
|
|
5
|
+
|
|
3
6
|
/*!
|
|
4
7
|
* Convert JS SDK
|
|
5
8
|
* Version 1.0.0
|
|
6
9
|
* Copyright(c) 2020 Convert Insights, Inc
|
|
7
10
|
* License Apache-2.0
|
|
8
11
|
*/
|
|
12
|
+
const DEFAULT_KEYS_CASE_SENSITIVE = true;
|
|
13
|
+
const DEFAULT_NEGATION = '!';
|
|
14
|
+
/**
|
|
15
|
+
* Provides rule processing calculations with corresponding comparisons methods
|
|
16
|
+
* @category Modules
|
|
17
|
+
* @constructor
|
|
18
|
+
* @implements {RuleManagerInterface}
|
|
19
|
+
*/
|
|
20
|
+
class RuleManager {
|
|
21
|
+
/**
|
|
22
|
+
* @param {Config=} config
|
|
23
|
+
* @param {Object=} dependencies
|
|
24
|
+
* @param {LogManagerInterface=} dependencies.loggerManager
|
|
25
|
+
*/
|
|
26
|
+
constructor(config, { loggerManager } = {}) {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
this._comparisonProcessor = jsSdkUtils.Comparisons;
|
|
29
|
+
this._negation = DEFAULT_NEGATION;
|
|
30
|
+
this._keys_case_sensitive = DEFAULT_KEYS_CASE_SENSITIVE;
|
|
31
|
+
this._loggerManager = loggerManager;
|
|
32
|
+
this._comparisonProcessor = jsSdkUtils.objectDeepValue(config, 'rules.comparisonProcessor', jsSdkUtils.Comparisons);
|
|
33
|
+
this._negation = String(jsSdkUtils.objectDeepValue(config, 'rules.negation', DEFAULT_NEGATION)).valueOf();
|
|
34
|
+
this._keys_case_sensitive = jsSdkUtils.objectDeepValue(config, 'rules.keys_case_sensitive', DEFAULT_KEYS_CASE_SENSITIVE, true);
|
|
35
|
+
this._mapper = (config === null || config === void 0 ? void 0 : config.mapper) || ((value) => value);
|
|
36
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager()', jsSdkEnums.MESSAGES.RULE_CONSTRUCTOR, this);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Setter for comparison processor
|
|
40
|
+
* @param {Record<string, any>} comparisonProcessor
|
|
41
|
+
*/
|
|
42
|
+
set comparisonProcessor(comparisonProcessor) {
|
|
43
|
+
this._comparisonProcessor = comparisonProcessor;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Getter for comparison processor
|
|
47
|
+
*/
|
|
48
|
+
get comparisonProcessor() {
|
|
49
|
+
return this._comparisonProcessor;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Retrieve comparison methods from comparison processor
|
|
53
|
+
* @return {Array<string>} List of methods of comparison processor
|
|
54
|
+
*/
|
|
55
|
+
getComparisonProcessorMethods() {
|
|
56
|
+
return Object.getOwnPropertyNames(this._comparisonProcessor).filter((name) => typeof this._comparisonProcessor[name] === 'function');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check input data matching to rule set
|
|
60
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
61
|
+
* @param {RuleSet} ruleSet
|
|
62
|
+
* @return {boolean | RuleError}
|
|
63
|
+
*/
|
|
64
|
+
isRuleMatched(data, ruleSet, logEntry) {
|
|
65
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
66
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isRuleMatched()', this._mapper({
|
|
67
|
+
data: data,
|
|
68
|
+
ruleSet: ruleSet
|
|
69
|
+
}));
|
|
70
|
+
if (logEntry) {
|
|
71
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager.isRuleMatched()', jsSdkEnums.MESSAGES.PROCESSING_ENTITY.replace('#', logEntry));
|
|
72
|
+
}
|
|
73
|
+
// Top OR level
|
|
74
|
+
let match;
|
|
75
|
+
if (Object.prototype.hasOwnProperty.call(ruleSet, 'OR') &&
|
|
76
|
+
jsSdkUtils.arrayNotEmpty(ruleSet === null || ruleSet === void 0 ? void 0 : ruleSet.OR)) {
|
|
77
|
+
for (let i = 0, l = ruleSet.OR.length; i < l; i++) {
|
|
78
|
+
match = this._processAND(data, ruleSet.OR[i]);
|
|
79
|
+
if (Object.values(jsSdkEnums.RuleError).includes(match)) {
|
|
80
|
+
(_f = (_e = this._loggerManager) === null || _e === void 0 ? void 0 : _e.info) === null || _f === void 0 ? void 0 : _f.call(_e, 'RuleManager.isRuleMatched()', logEntry || '', jsSdkEnums.ERROR_MESSAGES.RULE_ERROR);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
(_h = (_g = this._loggerManager) === null || _g === void 0 ? void 0 : _g.info) === null || _h === void 0 ? void 0 : _h.call(_g, 'RuleManager.isRuleMatched()', logEntry || '', match === false
|
|
84
|
+
? jsSdkEnums.MESSAGES.RULE_NOT_MATCH
|
|
85
|
+
: jsSdkEnums.MESSAGES.RULE_MATCH.replace('#', String(i)));
|
|
86
|
+
}
|
|
87
|
+
if (match !== false) {
|
|
88
|
+
return match;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
(_k = (_j = this._loggerManager) === null || _j === void 0 ? void 0 : _j.warn) === null || _k === void 0 ? void 0 : _k.call(_j, 'RuleManager.isRuleMatched()', logEntry || '', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check is rule object valid
|
|
99
|
+
* @param {Rule} rule
|
|
100
|
+
* @return {boolean}
|
|
101
|
+
*/
|
|
102
|
+
isValidRule(rule) {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isValidRule()', this._mapper({
|
|
105
|
+
rule: rule
|
|
106
|
+
}));
|
|
107
|
+
return (Object.prototype.hasOwnProperty.call(rule, 'matching') &&
|
|
108
|
+
typeof rule.matching === 'object' &&
|
|
109
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'match_type') &&
|
|
110
|
+
typeof rule.matching.match_type === 'string' &&
|
|
111
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'negated') &&
|
|
112
|
+
typeof rule.matching.negated === 'boolean' &&
|
|
113
|
+
Object.prototype.hasOwnProperty.call(rule, 'value'));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Process AND block of rule set. Return first false if found
|
|
117
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
118
|
+
* @param {RuleAnd} rulesSubset
|
|
119
|
+
* @return {boolean | RuleError}
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
_processAND(data, rulesSubset) {
|
|
123
|
+
var _a, _b, _c, _d;
|
|
124
|
+
// Second AND level
|
|
125
|
+
let match;
|
|
126
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'AND') &&
|
|
127
|
+
jsSdkUtils.arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.AND)) {
|
|
128
|
+
for (let i = 0, l = rulesSubset.AND.length; i < l; i++) {
|
|
129
|
+
match = this._processORWHEN(data, rulesSubset.AND[i]);
|
|
130
|
+
if (match === false) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (match !== false) {
|
|
135
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processAND()', jsSdkEnums.MESSAGES.RULE_MATCH_AND);
|
|
136
|
+
}
|
|
137
|
+
return match;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.warn) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager._processAND()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Process OR block of rule set. Return first true if found
|
|
146
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
147
|
+
* @param {RuleOrWhen} rulesSubset
|
|
148
|
+
* @return {boolean | RuleError}
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
_processORWHEN(data, rulesSubset) {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
// Third OR level. Called OR_WHEN.
|
|
154
|
+
let match;
|
|
155
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'OR_WHEN') &&
|
|
156
|
+
jsSdkUtils.arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.OR_WHEN)) {
|
|
157
|
+
for (let i = 0, l = rulesSubset.OR_WHEN.length; i < l; i++) {
|
|
158
|
+
match = this._processRuleItem(data, rulesSubset.OR_WHEN[i]);
|
|
159
|
+
if (match !== false) {
|
|
160
|
+
return match;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.warn) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processORWHEN()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Process single rule item
|
|
171
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
172
|
+
* @param {Rule} rule A single rule to compare
|
|
173
|
+
* @return {boolean | RuleError} Comparison result
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
_processRuleItem(data, rule) {
|
|
177
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
178
|
+
if (this.isValidRule(rule)) {
|
|
179
|
+
try {
|
|
180
|
+
const negation = rule.matching.negated || false;
|
|
181
|
+
const matching = rule.matching.match_type;
|
|
182
|
+
if (this.getComparisonProcessorMethods().indexOf(matching) !== -1) {
|
|
183
|
+
if (data && typeof data === 'object' && (data === null || data === void 0 ? void 0 : data.constructor)) {
|
|
184
|
+
// Validate data key-value set.
|
|
185
|
+
if (data.constructor === Object) {
|
|
186
|
+
// Rule object has to have `key` field
|
|
187
|
+
for (const key of Object.keys(data)) {
|
|
188
|
+
const k = this._keys_case_sensitive ? key : key.toLowerCase();
|
|
189
|
+
const rule_k = this._keys_case_sensitive
|
|
190
|
+
? rule.key
|
|
191
|
+
: rule.key.toLowerCase();
|
|
192
|
+
if (k === rule_k) {
|
|
193
|
+
return this._comparisonProcessor[matching](data[key], rule.value, negation);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (rule === null || rule === void 0 ? void 0 : rule.rule_type) {
|
|
198
|
+
// Rule object has to have `rule_type` field
|
|
199
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processRuleItem()', jsSdkEnums.MESSAGES.RULE_MATCH_START.replace('#', rule.rule_type));
|
|
200
|
+
for (const method of Object.getOwnPropertyNames(data.constructor.prototype)) {
|
|
201
|
+
if (method === 'constructor')
|
|
202
|
+
continue;
|
|
203
|
+
const rule_method = jsSdkUtils.camelCase(`get ${rule.rule_type.replace(/_/g, ' ')}`);
|
|
204
|
+
if (method === rule_method ||
|
|
205
|
+
((_c = data === null || data === void 0 ? void 0 : data.mapper) === null || _c === void 0 ? void 0 : _c.call(data, method)) === rule_method) {
|
|
206
|
+
const dataValue = data[method](rule);
|
|
207
|
+
if (Object.values(jsSdkEnums.RuleError).includes(dataValue))
|
|
208
|
+
return dataValue;
|
|
209
|
+
if (rule.rule_type === 'js_condition')
|
|
210
|
+
return dataValue;
|
|
211
|
+
return this._comparisonProcessor[matching](dataValue, rule.value, negation);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
(_e = (_d = this._loggerManager) === null || _d === void 0 ? void 0 : _d.trace) === null || _e === void 0 ? void 0 : _e.call(_d, 'RuleManager._processRuleItem()', {
|
|
217
|
+
warn: jsSdkEnums.ERROR_MESSAGES.RULE_DATA_NOT_VALID,
|
|
218
|
+
data
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
(_g = (_f = this._loggerManager) === null || _f === void 0 ? void 0 : _f.trace) === null || _g === void 0 ? void 0 : _g.call(_f, 'RuleManager._processRuleItem()', {
|
|
224
|
+
warn: jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID,
|
|
225
|
+
data,
|
|
226
|
+
rule
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
(_j = (_h = this._loggerManager) === null || _h === void 0 ? void 0 : _h.warn) === null || _j === void 0 ? void 0 : _j.call(_h, 'RuleManager._processRuleItem()', jsSdkEnums.ERROR_MESSAGES.RULE_MATCH_TYPE_NOT_SUPPORTED.replace('#', matching));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
(_l = (_k = this._loggerManager) === null || _k === void 0 ? void 0 : _k.error) === null || _l === void 0 ? void 0 : _l.call(_k, 'RuleManager._processRuleItem()', {
|
|
236
|
+
error: error.message
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
(_o = (_m = this._loggerManager) === null || _m === void 0 ? void 0 : _m.warn) === null || _o === void 0 ? void 0 : _o.call(_m, 'RuleManager._processRuleItem()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
exports.RuleManager = RuleManager;
|
|
248
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/lib/index.min.js
CHANGED
|
@@ -4,3 +4,5 @@
|
|
|
4
4
|
* Copyright(c) 2020-2022 Convert Insights, Inc
|
|
5
5
|
* License Apache-2.0
|
|
6
6
|
*/
|
|
7
|
+
"use strict";var e=require("@convertcom/js-sdk-utils"),r=require("@convertcom/js-sdk-enums");exports.RuleManager=class{constructor(o,{loggerManager:l}={}){var t,a;this._comparisonProcessor=e.Comparisons,this._negation="!",this._keys_case_sensitive=true,this._loggerManager=l,this._comparisonProcessor=e.objectDeepValue(o,"rules.comparisonProcessor",e.Comparisons),this._negation=String(e.objectDeepValue(o,"rules.negation","!")).valueOf(),this._keys_case_sensitive=e.objectDeepValue(o,"rules.keys_case_sensitive",true,!0),this._mapper=(null==o?void 0:o.mapper)||(e=>e),null===(a=null===(t=this._loggerManager)||void 0===t?void 0:t.trace)||void 0===a||a.call(t,"RuleManager()",r.MESSAGES.RULE_CONSTRUCTOR,this)}set comparisonProcessor(e){this._comparisonProcessor=e}get comparisonProcessor(){return this._comparisonProcessor}getComparisonProcessorMethods(){return Object.getOwnPropertyNames(this._comparisonProcessor).filter((e=>"function"==typeof this._comparisonProcessor[e]))}isRuleMatched(o,l,t){var a,s,i,n,c,u,_,g,R,p;let d;if(null===(s=null===(a=this._loggerManager)||void 0===a?void 0:a.trace)||void 0===s||s.call(a,"RuleManager.isRuleMatched()",this._mapper({data:o,ruleSet:l})),t&&(null===(n=null===(i=this._loggerManager)||void 0===i?void 0:i.info)||void 0===n||n.call(i,"RuleManager.isRuleMatched()",r.MESSAGES.PROCESSING_ENTITY.replace("#",t))),Object.prototype.hasOwnProperty.call(l,"OR")&&e.arrayNotEmpty(null==l?void 0:l.OR)){for(let e=0,a=l.OR.length;e<a;e++)if(d=this._processAND(o,l.OR[e]),Object.values(r.RuleError).includes(d)?null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.info)||void 0===u||u.call(c,"RuleManager.isRuleMatched()",t||"",r.ERROR_MESSAGES.RULE_ERROR):null===(g=null===(_=this._loggerManager)||void 0===_?void 0:_.info)||void 0===g||g.call(_,"RuleManager.isRuleMatched()",t||"",!1===d?r.MESSAGES.RULE_NOT_MATCH:r.MESSAGES.RULE_MATCH.replace("#",String(e))),!1!==d)return d}else null===(p=null===(R=this._loggerManager)||void 0===R?void 0:R.warn)||void 0===p||p.call(R,"RuleManager.isRuleMatched()",t||"",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1}isValidRule(e){var r,o;return null===(o=null===(r=this._loggerManager)||void 0===r?void 0:r.trace)||void 0===o||o.call(r,"RuleManager.isValidRule()",this._mapper({rule:e})),Object.prototype.hasOwnProperty.call(e,"matching")&&"object"==typeof e.matching&&Object.prototype.hasOwnProperty.call(e.matching,"match_type")&&"string"==typeof e.matching.match_type&&Object.prototype.hasOwnProperty.call(e.matching,"negated")&&"boolean"==typeof e.matching.negated&&Object.prototype.hasOwnProperty.call(e,"value")}_processAND(o,l){var t,a,s,i;let n;if(Object.prototype.hasOwnProperty.call(l,"AND")&&e.arrayNotEmpty(null==l?void 0:l.AND)){for(let e=0,r=l.AND.length;e<r;e++)if(n=this._processORWHEN(o,l.AND[e]),!1===n)return!1;return!1!==n&&(null===(a=null===(t=this._loggerManager)||void 0===t?void 0:t.info)||void 0===a||a.call(t,"RuleManager._processAND()",r.MESSAGES.RULE_MATCH_AND)),n}return null===(i=null===(s=this._loggerManager)||void 0===s?void 0:s.warn)||void 0===i||i.call(s,"RuleManager._processAND()",r.ERROR_MESSAGES.RULE_NOT_VALID),!1}_processORWHEN(o,l){var t,a;let s;if(Object.prototype.hasOwnProperty.call(l,"OR_WHEN")&&e.arrayNotEmpty(null==l?void 0:l.OR_WHEN)){for(let e=0,r=l.OR_WHEN.length;e<r;e++)if(s=this._processRuleItem(o,l.OR_WHEN[e]),!1!==s)return s}else null===(a=null===(t=this._loggerManager)||void 0===t?void 0:t.warn)||void 0===a||a.call(t,"RuleManager._processORWHEN()",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1}_processRuleItem(o,l){var t,a,s,i,n,c,u,_,g,R,p,d,v;if(this.isValidRule(l))try{const R=l.matching.negated||!1,p=l.matching.match_type;if(-1!==this.getComparisonProcessorMethods().indexOf(p))if(o&&"object"==typeof o&&(null==o?void 0:o.constructor))if(o.constructor===Object)for(const e of Object.keys(o)){const r=this._keys_case_sensitive?e:e.toLowerCase();if(r===(this._keys_case_sensitive?l.key:l.key.toLowerCase()))return this._comparisonProcessor[p](o[e],l.value,R)}else if(null==l?void 0:l.rule_type){null===(a=null===(t=this._loggerManager)||void 0===t?void 0:t.info)||void 0===a||a.call(t,"RuleManager._processRuleItem()",r.MESSAGES.RULE_MATCH_START.replace("#",l.rule_type));for(const t of Object.getOwnPropertyNames(o.constructor.prototype)){if("constructor"===t)continue;const a=e.camelCase(`get ${l.rule_type.replace(/_/g," ")}`);if(t===a||(null===(s=null==o?void 0:o.mapper)||void 0===s?void 0:s.call(o,t))===a){const e=o[t](l);return Object.values(r.RuleError).includes(e)||"js_condition"===l.rule_type?e:this._comparisonProcessor[p](e,l.value,R)}}}else null===(n=null===(i=this._loggerManager)||void 0===i?void 0:i.trace)||void 0===n||n.call(i,"RuleManager._processRuleItem()",{warn:r.ERROR_MESSAGES.RULE_DATA_NOT_VALID,data:o});else null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.trace)||void 0===u||u.call(c,"RuleManager._processRuleItem()",{warn:r.ERROR_MESSAGES.RULE_NOT_VALID,data:o,rule:l});else null===(g=null===(_=this._loggerManager)||void 0===_?void 0:_.warn)||void 0===g||g.call(_,"RuleManager._processRuleItem()",r.ERROR_MESSAGES.RULE_MATCH_TYPE_NOT_SUPPORTED.replace("#",p))}catch(e){null===(p=null===(R=this._loggerManager)||void 0===R?void 0:R.error)||void 0===p||p.call(R,"RuleManager._processRuleItem()",{error:e.message})}else null===(v=null===(d=this._loggerManager)||void 0===d?void 0:d.warn)||void 0===v||v.call(d,"RuleManager._processRuleItem()",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1}};
|
|
8
|
+
//# sourceMappingURL=index.min.js.map
|
package/lib/index.min.js.map
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.min.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/lib/index.min.mjs
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
* Copyright(c) 2020-2022 Convert Insights, Inc
|
|
5
5
|
* License Apache-2.0
|
|
6
6
|
*/
|
|
7
|
+
import{Comparisons as e,objectDeepValue as r,arrayNotEmpty as o,camelCase as l}from"@convertcom/js-sdk-utils";import{MESSAGES as t,RuleError as a,ERROR_MESSAGES as s}from"@convertcom/js-sdk-enums";class i{constructor(o,{loggerManager:l}={}){var a,s;this._comparisonProcessor=e,this._negation="!",this._keys_case_sensitive=true,this._loggerManager=l,this._comparisonProcessor=r(o,"rules.comparisonProcessor",e),this._negation=String(r(o,"rules.negation","!")).valueOf(),this._keys_case_sensitive=r(o,"rules.keys_case_sensitive",true,!0),this._mapper=(null==o?void 0:o.mapper)||(e=>e),null===(s=null===(a=this._loggerManager)||void 0===a?void 0:a.trace)||void 0===s||s.call(a,"RuleManager()",t.RULE_CONSTRUCTOR,this)}set comparisonProcessor(e){this._comparisonProcessor=e}get comparisonProcessor(){return this._comparisonProcessor}getComparisonProcessorMethods(){return Object.getOwnPropertyNames(this._comparisonProcessor).filter((e=>"function"==typeof this._comparisonProcessor[e]))}isRuleMatched(e,r,l){var i,n,c,u,_,g,p,d,v,h;let R;if(null===(n=null===(i=this._loggerManager)||void 0===i?void 0:i.trace)||void 0===n||n.call(i,"RuleManager.isRuleMatched()",this._mapper({data:e,ruleSet:r})),l&&(null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.info)||void 0===u||u.call(c,"RuleManager.isRuleMatched()",t.PROCESSING_ENTITY.replace("#",l))),Object.prototype.hasOwnProperty.call(r,"OR")&&o(null==r?void 0:r.OR)){for(let o=0,i=r.OR.length;o<i;o++)if(R=this._processAND(e,r.OR[o]),Object.values(a).includes(R)?null===(g=null===(_=this._loggerManager)||void 0===_?void 0:_.info)||void 0===g||g.call(_,"RuleManager.isRuleMatched()",l||"",s.RULE_ERROR):null===(d=null===(p=this._loggerManager)||void 0===p?void 0:p.info)||void 0===d||d.call(p,"RuleManager.isRuleMatched()",l||"",!1===R?t.RULE_NOT_MATCH:t.RULE_MATCH.replace("#",String(o))),!1!==R)return R}else null===(h=null===(v=this._loggerManager)||void 0===v?void 0:v.warn)||void 0===h||h.call(v,"RuleManager.isRuleMatched()",l||"",s.RULE_NOT_VALID);return!1}isValidRule(e){var r,o;return null===(o=null===(r=this._loggerManager)||void 0===r?void 0:r.trace)||void 0===o||o.call(r,"RuleManager.isValidRule()",this._mapper({rule:e})),Object.prototype.hasOwnProperty.call(e,"matching")&&"object"==typeof e.matching&&Object.prototype.hasOwnProperty.call(e.matching,"match_type")&&"string"==typeof e.matching.match_type&&Object.prototype.hasOwnProperty.call(e.matching,"negated")&&"boolean"==typeof e.matching.negated&&Object.prototype.hasOwnProperty.call(e,"value")}_processAND(e,r){var l,a,i,n;let c;if(Object.prototype.hasOwnProperty.call(r,"AND")&&o(null==r?void 0:r.AND)){for(let o=0,l=r.AND.length;o<l;o++)if(c=this._processORWHEN(e,r.AND[o]),!1===c)return!1;return!1!==c&&(null===(a=null===(l=this._loggerManager)||void 0===l?void 0:l.info)||void 0===a||a.call(l,"RuleManager._processAND()",t.RULE_MATCH_AND)),c}return null===(n=null===(i=this._loggerManager)||void 0===i?void 0:i.warn)||void 0===n||n.call(i,"RuleManager._processAND()",s.RULE_NOT_VALID),!1}_processORWHEN(e,r){var l,t;let a;if(Object.prototype.hasOwnProperty.call(r,"OR_WHEN")&&o(null==r?void 0:r.OR_WHEN)){for(let o=0,l=r.OR_WHEN.length;o<l;o++)if(a=this._processRuleItem(e,r.OR_WHEN[o]),!1!==a)return a}else null===(t=null===(l=this._loggerManager)||void 0===l?void 0:l.warn)||void 0===t||t.call(l,"RuleManager._processORWHEN()",s.RULE_NOT_VALID);return!1}_processRuleItem(e,r){var o,i,n,c,u,_,g,p,d,v,h,R,O;if(this.isValidRule(r))try{const v=r.matching.negated||!1,h=r.matching.match_type;if(-1!==this.getComparisonProcessorMethods().indexOf(h))if(e&&"object"==typeof e&&(null==e?void 0:e.constructor))if(e.constructor===Object)for(const o of Object.keys(e)){const l=this._keys_case_sensitive?o:o.toLowerCase();if(l===(this._keys_case_sensitive?r.key:r.key.toLowerCase()))return this._comparisonProcessor[h](e[o],r.value,v)}else if(null==r?void 0:r.rule_type){null===(i=null===(o=this._loggerManager)||void 0===o?void 0:o.info)||void 0===i||i.call(o,"RuleManager._processRuleItem()",t.RULE_MATCH_START.replace("#",r.rule_type));for(const o of Object.getOwnPropertyNames(e.constructor.prototype)){if("constructor"===o)continue;const t=l(`get ${r.rule_type.replace(/_/g," ")}`);if(o===t||(null===(n=null==e?void 0:e.mapper)||void 0===n?void 0:n.call(e,o))===t){const l=e[o](r);return Object.values(a).includes(l)||"js_condition"===r.rule_type?l:this._comparisonProcessor[h](l,r.value,v)}}}else null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.trace)||void 0===u||u.call(c,"RuleManager._processRuleItem()",{warn:s.RULE_DATA_NOT_VALID,data:e});else null===(g=null===(_=this._loggerManager)||void 0===_?void 0:_.trace)||void 0===g||g.call(_,"RuleManager._processRuleItem()",{warn:s.RULE_NOT_VALID,data:e,rule:r});else null===(d=null===(p=this._loggerManager)||void 0===p?void 0:p.warn)||void 0===d||d.call(p,"RuleManager._processRuleItem()",s.RULE_MATCH_TYPE_NOT_SUPPORTED.replace("#",h))}catch(e){null===(h=null===(v=this._loggerManager)||void 0===v?void 0:v.error)||void 0===h||h.call(v,"RuleManager._processRuleItem()",{error:e.message})}else null===(O=null===(R=this._loggerManager)||void 0===R?void 0:R.warn)||void 0===O||O.call(R,"RuleManager._processRuleItem()",s.RULE_NOT_VALID);return!1}}export{i as RuleManager};
|
|
7
8
|
//# sourceMappingURL=index.min.mjs.map
|
package/lib/index.min.mjs.map
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.min.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/lib/index.mjs
CHANGED
|
@@ -1,6 +1,246 @@
|
|
|
1
|
+
import { Comparisons, objectDeepValue, arrayNotEmpty, camelCase } from '@convertcom/js-sdk-utils';
|
|
2
|
+
import { MESSAGES, RuleError, ERROR_MESSAGES } from '@convertcom/js-sdk-enums';
|
|
3
|
+
|
|
1
4
|
/*!
|
|
2
5
|
* Convert JS SDK
|
|
3
6
|
* Version 1.0.0
|
|
4
7
|
* Copyright(c) 2020 Convert Insights, Inc
|
|
5
8
|
* License Apache-2.0
|
|
6
9
|
*/
|
|
10
|
+
const DEFAULT_KEYS_CASE_SENSITIVE = true;
|
|
11
|
+
const DEFAULT_NEGATION = '!';
|
|
12
|
+
/**
|
|
13
|
+
* Provides rule processing calculations with corresponding comparisons methods
|
|
14
|
+
* @category Modules
|
|
15
|
+
* @constructor
|
|
16
|
+
* @implements {RuleManagerInterface}
|
|
17
|
+
*/
|
|
18
|
+
class RuleManager {
|
|
19
|
+
/**
|
|
20
|
+
* @param {Config=} config
|
|
21
|
+
* @param {Object=} dependencies
|
|
22
|
+
* @param {LogManagerInterface=} dependencies.loggerManager
|
|
23
|
+
*/
|
|
24
|
+
constructor(config, { loggerManager } = {}) {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
this._comparisonProcessor = Comparisons;
|
|
27
|
+
this._negation = DEFAULT_NEGATION;
|
|
28
|
+
this._keys_case_sensitive = DEFAULT_KEYS_CASE_SENSITIVE;
|
|
29
|
+
this._loggerManager = loggerManager;
|
|
30
|
+
this._comparisonProcessor = objectDeepValue(config, 'rules.comparisonProcessor', Comparisons);
|
|
31
|
+
this._negation = String(objectDeepValue(config, 'rules.negation', DEFAULT_NEGATION)).valueOf();
|
|
32
|
+
this._keys_case_sensitive = objectDeepValue(config, 'rules.keys_case_sensitive', DEFAULT_KEYS_CASE_SENSITIVE, true);
|
|
33
|
+
this._mapper = (config === null || config === void 0 ? void 0 : config.mapper) || ((value) => value);
|
|
34
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager()', MESSAGES.RULE_CONSTRUCTOR, this);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Setter for comparison processor
|
|
38
|
+
* @param {Record<string, any>} comparisonProcessor
|
|
39
|
+
*/
|
|
40
|
+
set comparisonProcessor(comparisonProcessor) {
|
|
41
|
+
this._comparisonProcessor = comparisonProcessor;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Getter for comparison processor
|
|
45
|
+
*/
|
|
46
|
+
get comparisonProcessor() {
|
|
47
|
+
return this._comparisonProcessor;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Retrieve comparison methods from comparison processor
|
|
51
|
+
* @return {Array<string>} List of methods of comparison processor
|
|
52
|
+
*/
|
|
53
|
+
getComparisonProcessorMethods() {
|
|
54
|
+
return Object.getOwnPropertyNames(this._comparisonProcessor).filter((name) => typeof this._comparisonProcessor[name] === 'function');
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check input data matching to rule set
|
|
58
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
59
|
+
* @param {RuleSet} ruleSet
|
|
60
|
+
* @return {boolean | RuleError}
|
|
61
|
+
*/
|
|
62
|
+
isRuleMatched(data, ruleSet, logEntry) {
|
|
63
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
64
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isRuleMatched()', this._mapper({
|
|
65
|
+
data: data,
|
|
66
|
+
ruleSet: ruleSet
|
|
67
|
+
}));
|
|
68
|
+
if (logEntry) {
|
|
69
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager.isRuleMatched()', MESSAGES.PROCESSING_ENTITY.replace('#', logEntry));
|
|
70
|
+
}
|
|
71
|
+
// Top OR level
|
|
72
|
+
let match;
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(ruleSet, 'OR') &&
|
|
74
|
+
arrayNotEmpty(ruleSet === null || ruleSet === void 0 ? void 0 : ruleSet.OR)) {
|
|
75
|
+
for (let i = 0, l = ruleSet.OR.length; i < l; i++) {
|
|
76
|
+
match = this._processAND(data, ruleSet.OR[i]);
|
|
77
|
+
if (Object.values(RuleError).includes(match)) {
|
|
78
|
+
(_f = (_e = this._loggerManager) === null || _e === void 0 ? void 0 : _e.info) === null || _f === void 0 ? void 0 : _f.call(_e, 'RuleManager.isRuleMatched()', logEntry || '', ERROR_MESSAGES.RULE_ERROR);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
(_h = (_g = this._loggerManager) === null || _g === void 0 ? void 0 : _g.info) === null || _h === void 0 ? void 0 : _h.call(_g, 'RuleManager.isRuleMatched()', logEntry || '', match === false
|
|
82
|
+
? MESSAGES.RULE_NOT_MATCH
|
|
83
|
+
: MESSAGES.RULE_MATCH.replace('#', String(i)));
|
|
84
|
+
}
|
|
85
|
+
if (match !== false) {
|
|
86
|
+
return match;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
(_k = (_j = this._loggerManager) === null || _j === void 0 ? void 0 : _j.warn) === null || _k === void 0 ? void 0 : _k.call(_j, 'RuleManager.isRuleMatched()', logEntry || '', ERROR_MESSAGES.RULE_NOT_VALID);
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Check is rule object valid
|
|
97
|
+
* @param {Rule} rule
|
|
98
|
+
* @return {boolean}
|
|
99
|
+
*/
|
|
100
|
+
isValidRule(rule) {
|
|
101
|
+
var _a, _b;
|
|
102
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isValidRule()', this._mapper({
|
|
103
|
+
rule: rule
|
|
104
|
+
}));
|
|
105
|
+
return (Object.prototype.hasOwnProperty.call(rule, 'matching') &&
|
|
106
|
+
typeof rule.matching === 'object' &&
|
|
107
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'match_type') &&
|
|
108
|
+
typeof rule.matching.match_type === 'string' &&
|
|
109
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'negated') &&
|
|
110
|
+
typeof rule.matching.negated === 'boolean' &&
|
|
111
|
+
Object.prototype.hasOwnProperty.call(rule, 'value'));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Process AND block of rule set. Return first false if found
|
|
115
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
116
|
+
* @param {RuleAnd} rulesSubset
|
|
117
|
+
* @return {boolean | RuleError}
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
_processAND(data, rulesSubset) {
|
|
121
|
+
var _a, _b, _c, _d;
|
|
122
|
+
// Second AND level
|
|
123
|
+
let match;
|
|
124
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'AND') &&
|
|
125
|
+
arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.AND)) {
|
|
126
|
+
for (let i = 0, l = rulesSubset.AND.length; i < l; i++) {
|
|
127
|
+
match = this._processORWHEN(data, rulesSubset.AND[i]);
|
|
128
|
+
if (match === false) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (match !== false) {
|
|
133
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processAND()', MESSAGES.RULE_MATCH_AND);
|
|
134
|
+
}
|
|
135
|
+
return match;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.warn) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager._processAND()', ERROR_MESSAGES.RULE_NOT_VALID);
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Process OR block of rule set. Return first true if found
|
|
144
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
145
|
+
* @param {RuleOrWhen} rulesSubset
|
|
146
|
+
* @return {boolean | RuleError}
|
|
147
|
+
* @private
|
|
148
|
+
*/
|
|
149
|
+
_processORWHEN(data, rulesSubset) {
|
|
150
|
+
var _a, _b;
|
|
151
|
+
// Third OR level. Called OR_WHEN.
|
|
152
|
+
let match;
|
|
153
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'OR_WHEN') &&
|
|
154
|
+
arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.OR_WHEN)) {
|
|
155
|
+
for (let i = 0, l = rulesSubset.OR_WHEN.length; i < l; i++) {
|
|
156
|
+
match = this._processRuleItem(data, rulesSubset.OR_WHEN[i]);
|
|
157
|
+
if (match !== false) {
|
|
158
|
+
return match;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.warn) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processORWHEN()', ERROR_MESSAGES.RULE_NOT_VALID);
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Process single rule item
|
|
169
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
170
|
+
* @param {Rule} rule A single rule to compare
|
|
171
|
+
* @return {boolean | RuleError} Comparison result
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
_processRuleItem(data, rule) {
|
|
175
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
176
|
+
if (this.isValidRule(rule)) {
|
|
177
|
+
try {
|
|
178
|
+
const negation = rule.matching.negated || false;
|
|
179
|
+
const matching = rule.matching.match_type;
|
|
180
|
+
if (this.getComparisonProcessorMethods().indexOf(matching) !== -1) {
|
|
181
|
+
if (data && typeof data === 'object' && (data === null || data === void 0 ? void 0 : data.constructor)) {
|
|
182
|
+
// Validate data key-value set.
|
|
183
|
+
if (data.constructor === Object) {
|
|
184
|
+
// Rule object has to have `key` field
|
|
185
|
+
for (const key of Object.keys(data)) {
|
|
186
|
+
const k = this._keys_case_sensitive ? key : key.toLowerCase();
|
|
187
|
+
const rule_k = this._keys_case_sensitive
|
|
188
|
+
? rule.key
|
|
189
|
+
: rule.key.toLowerCase();
|
|
190
|
+
if (k === rule_k) {
|
|
191
|
+
return this._comparisonProcessor[matching](data[key], rule.value, negation);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else if (rule === null || rule === void 0 ? void 0 : rule.rule_type) {
|
|
196
|
+
// Rule object has to have `rule_type` field
|
|
197
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processRuleItem()', MESSAGES.RULE_MATCH_START.replace('#', rule.rule_type));
|
|
198
|
+
for (const method of Object.getOwnPropertyNames(data.constructor.prototype)) {
|
|
199
|
+
if (method === 'constructor')
|
|
200
|
+
continue;
|
|
201
|
+
const rule_method = camelCase(`get ${rule.rule_type.replace(/_/g, ' ')}`);
|
|
202
|
+
if (method === rule_method ||
|
|
203
|
+
((_c = data === null || data === void 0 ? void 0 : data.mapper) === null || _c === void 0 ? void 0 : _c.call(data, method)) === rule_method) {
|
|
204
|
+
const dataValue = data[method](rule);
|
|
205
|
+
if (Object.values(RuleError).includes(dataValue))
|
|
206
|
+
return dataValue;
|
|
207
|
+
if (rule.rule_type === 'js_condition')
|
|
208
|
+
return dataValue;
|
|
209
|
+
return this._comparisonProcessor[matching](dataValue, rule.value, negation);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
(_e = (_d = this._loggerManager) === null || _d === void 0 ? void 0 : _d.trace) === null || _e === void 0 ? void 0 : _e.call(_d, 'RuleManager._processRuleItem()', {
|
|
215
|
+
warn: ERROR_MESSAGES.RULE_DATA_NOT_VALID,
|
|
216
|
+
data
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
(_g = (_f = this._loggerManager) === null || _f === void 0 ? void 0 : _f.trace) === null || _g === void 0 ? void 0 : _g.call(_f, 'RuleManager._processRuleItem()', {
|
|
222
|
+
warn: ERROR_MESSAGES.RULE_NOT_VALID,
|
|
223
|
+
data,
|
|
224
|
+
rule
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
(_j = (_h = this._loggerManager) === null || _h === void 0 ? void 0 : _h.warn) === null || _j === void 0 ? void 0 : _j.call(_h, 'RuleManager._processRuleItem()', ERROR_MESSAGES.RULE_MATCH_TYPE_NOT_SUPPORTED.replace('#', matching));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
(_l = (_k = this._loggerManager) === null || _k === void 0 ? void 0 : _k.error) === null || _l === void 0 ? void 0 : _l.call(_k, 'RuleManager._processRuleItem()', {
|
|
234
|
+
error: error.message
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
(_o = (_m = this._loggerManager) === null || _m === void 0 ? void 0 : _m.warn) === null || _o === void 0 ? void 0 : _o.call(_m, 'RuleManager._processRuleItem()', ERROR_MESSAGES.RULE_NOT_VALID);
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { RuleManager };
|
|
246
|
+
//# sourceMappingURL=index.mjs.map
|
package/lib/index.mjs.map
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsSdkUtils = require('@convertcom/js-sdk-utils');
|
|
4
|
+
var jsSdkEnums = require('@convertcom/js-sdk-enums');
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Copyright (c) Microsoft Corporation.
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
10
|
+
purpose with or without fee is hereby granted.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
13
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
14
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
15
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
16
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
17
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
+
***************************************************************************** */
|
|
20
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
function __values(o) {
|
|
24
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
25
|
+
if (m) return m.call(o);
|
|
26
|
+
if (o && typeof o.length === "number") return {
|
|
27
|
+
next: function () {
|
|
28
|
+
if (o && i >= o.length) o = void 0;
|
|
29
|
+
return { value: o && o[i++], done: !o };
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
36
|
+
var e = new Error(message);
|
|
37
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
var DEFAULT_KEYS_CASE_SENSITIVE = true;
|
|
41
|
+
var DEFAULT_NEGATION = '!';
|
|
42
|
+
/**
|
|
43
|
+
* Provides rule processing calculations with corresponding comparisons methods
|
|
44
|
+
* @category Modules
|
|
45
|
+
* @constructor
|
|
46
|
+
* @implements {RuleManagerInterface}
|
|
47
|
+
*/
|
|
48
|
+
var RuleManager = /** @class */ (function () {
|
|
49
|
+
/**
|
|
50
|
+
* @param {Config=} config
|
|
51
|
+
* @param {Object=} dependencies
|
|
52
|
+
* @param {LogManagerInterface=} dependencies.loggerManager
|
|
53
|
+
*/
|
|
54
|
+
function RuleManager(config, _a) {
|
|
55
|
+
var _b = _a === void 0 ? {} : _a, loggerManager = _b.loggerManager;
|
|
56
|
+
var _c, _d;
|
|
57
|
+
this._comparisonProcessor = jsSdkUtils.Comparisons;
|
|
58
|
+
this._negation = DEFAULT_NEGATION;
|
|
59
|
+
this._keys_case_sensitive = DEFAULT_KEYS_CASE_SENSITIVE;
|
|
60
|
+
this._loggerManager = loggerManager;
|
|
61
|
+
this._comparisonProcessor = jsSdkUtils.objectDeepValue(config, 'rules.comparisonProcessor', jsSdkUtils.Comparisons);
|
|
62
|
+
this._negation = String(jsSdkUtils.objectDeepValue(config, 'rules.negation', DEFAULT_NEGATION)).valueOf();
|
|
63
|
+
this._keys_case_sensitive = jsSdkUtils.objectDeepValue(config, 'rules.keys_case_sensitive', DEFAULT_KEYS_CASE_SENSITIVE, true);
|
|
64
|
+
this._mapper = (config === null || config === void 0 ? void 0 : config.mapper) || (function (value) { return value; });
|
|
65
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.trace) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager()', jsSdkEnums.MESSAGES.RULE_CONSTRUCTOR, this);
|
|
66
|
+
}
|
|
67
|
+
Object.defineProperty(RuleManager.prototype, "comparisonProcessor", {
|
|
68
|
+
/**
|
|
69
|
+
* Getter for comparison processor
|
|
70
|
+
*/
|
|
71
|
+
get: function () {
|
|
72
|
+
return this._comparisonProcessor;
|
|
73
|
+
},
|
|
74
|
+
/**
|
|
75
|
+
* Setter for comparison processor
|
|
76
|
+
* @param {Record<string, any>} comparisonProcessor
|
|
77
|
+
*/
|
|
78
|
+
set: function (comparisonProcessor) {
|
|
79
|
+
this._comparisonProcessor = comparisonProcessor;
|
|
80
|
+
},
|
|
81
|
+
enumerable: false,
|
|
82
|
+
configurable: true
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Retrieve comparison methods from comparison processor
|
|
86
|
+
* @return {Array<string>} List of methods of comparison processor
|
|
87
|
+
*/
|
|
88
|
+
RuleManager.prototype.getComparisonProcessorMethods = function () {
|
|
89
|
+
var _this = this;
|
|
90
|
+
return Object.getOwnPropertyNames(this._comparisonProcessor).filter(function (name) { return typeof _this._comparisonProcessor[name] === 'function'; });
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Check input data matching to rule set
|
|
94
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
95
|
+
* @param {RuleSet} ruleSet
|
|
96
|
+
* @return {boolean | RuleError}
|
|
97
|
+
*/
|
|
98
|
+
RuleManager.prototype.isRuleMatched = function (data, ruleSet, logEntry) {
|
|
99
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
100
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isRuleMatched()', this._mapper({
|
|
101
|
+
data: data,
|
|
102
|
+
ruleSet: ruleSet
|
|
103
|
+
}));
|
|
104
|
+
if (logEntry) {
|
|
105
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager.isRuleMatched()', jsSdkEnums.MESSAGES.PROCESSING_ENTITY.replace('#', logEntry));
|
|
106
|
+
}
|
|
107
|
+
// Top OR level
|
|
108
|
+
var match;
|
|
109
|
+
if (Object.prototype.hasOwnProperty.call(ruleSet, 'OR') &&
|
|
110
|
+
jsSdkUtils.arrayNotEmpty(ruleSet === null || ruleSet === void 0 ? void 0 : ruleSet.OR)) {
|
|
111
|
+
for (var i = 0, l = ruleSet.OR.length; i < l; i++) {
|
|
112
|
+
match = this._processAND(data, ruleSet.OR[i]);
|
|
113
|
+
if (Object.values(jsSdkEnums.RuleError).includes(match)) {
|
|
114
|
+
(_f = (_e = this._loggerManager) === null || _e === void 0 ? void 0 : _e.info) === null || _f === void 0 ? void 0 : _f.call(_e, 'RuleManager.isRuleMatched()', logEntry || '', jsSdkEnums.ERROR_MESSAGES.RULE_ERROR);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
(_h = (_g = this._loggerManager) === null || _g === void 0 ? void 0 : _g.info) === null || _h === void 0 ? void 0 : _h.call(_g, 'RuleManager.isRuleMatched()', logEntry || '', match === false
|
|
118
|
+
? jsSdkEnums.MESSAGES.RULE_NOT_MATCH
|
|
119
|
+
: jsSdkEnums.MESSAGES.RULE_MATCH.replace('#', String(i)));
|
|
120
|
+
}
|
|
121
|
+
if (match !== false) {
|
|
122
|
+
return match;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
(_k = (_j = this._loggerManager) === null || _j === void 0 ? void 0 : _j.warn) === null || _k === void 0 ? void 0 : _k.call(_j, 'RuleManager.isRuleMatched()', logEntry || '', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Check is rule object valid
|
|
133
|
+
* @param {Rule} rule
|
|
134
|
+
* @return {boolean}
|
|
135
|
+
*/
|
|
136
|
+
RuleManager.prototype.isValidRule = function (rule) {
|
|
137
|
+
var _a, _b;
|
|
138
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.trace) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager.isValidRule()', this._mapper({
|
|
139
|
+
rule: rule
|
|
140
|
+
}));
|
|
141
|
+
return (Object.prototype.hasOwnProperty.call(rule, 'matching') &&
|
|
142
|
+
typeof rule.matching === 'object' &&
|
|
143
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'match_type') &&
|
|
144
|
+
typeof rule.matching.match_type === 'string' &&
|
|
145
|
+
Object.prototype.hasOwnProperty.call(rule.matching, 'negated') &&
|
|
146
|
+
typeof rule.matching.negated === 'boolean' &&
|
|
147
|
+
Object.prototype.hasOwnProperty.call(rule, 'value'));
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Process AND block of rule set. Return first false if found
|
|
151
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
152
|
+
* @param {RuleAnd} rulesSubset
|
|
153
|
+
* @return {boolean | RuleError}
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
RuleManager.prototype._processAND = function (data, rulesSubset) {
|
|
157
|
+
var _a, _b, _c, _d;
|
|
158
|
+
// Second AND level
|
|
159
|
+
var match;
|
|
160
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'AND') &&
|
|
161
|
+
jsSdkUtils.arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.AND)) {
|
|
162
|
+
for (var i = 0, l = rulesSubset.AND.length; i < l; i++) {
|
|
163
|
+
match = this._processORWHEN(data, rulesSubset.AND[i]);
|
|
164
|
+
if (match === false) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (match !== false) {
|
|
169
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processAND()', jsSdkEnums.MESSAGES.RULE_MATCH_AND);
|
|
170
|
+
}
|
|
171
|
+
return match;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.warn) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager._processAND()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Process OR block of rule set. Return first true if found
|
|
180
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
181
|
+
* @param {RuleOrWhen} rulesSubset
|
|
182
|
+
* @return {boolean | RuleError}
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
RuleManager.prototype._processORWHEN = function (data, rulesSubset) {
|
|
186
|
+
var _a, _b;
|
|
187
|
+
// Third OR level. Called OR_WHEN.
|
|
188
|
+
var match;
|
|
189
|
+
if (Object.prototype.hasOwnProperty.call(rulesSubset, 'OR_WHEN') &&
|
|
190
|
+
jsSdkUtils.arrayNotEmpty(rulesSubset === null || rulesSubset === void 0 ? void 0 : rulesSubset.OR_WHEN)) {
|
|
191
|
+
for (var i = 0, l = rulesSubset.OR_WHEN.length; i < l; i++) {
|
|
192
|
+
match = this._processRuleItem(data, rulesSubset.OR_WHEN[i]);
|
|
193
|
+
if (match !== false) {
|
|
194
|
+
return match;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
(_b = (_a = this._loggerManager) === null || _a === void 0 ? void 0 : _a.warn) === null || _b === void 0 ? void 0 : _b.call(_a, 'RuleManager._processORWHEN()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Process single rule item
|
|
205
|
+
* @param {Record<string, any>} data Single value or key-value data set to compare
|
|
206
|
+
* @param {Rule} rule A single rule to compare
|
|
207
|
+
* @return {boolean | RuleError} Comparison result
|
|
208
|
+
* @private
|
|
209
|
+
*/
|
|
210
|
+
RuleManager.prototype._processRuleItem = function (data, rule) {
|
|
211
|
+
var e_1, _a, e_2, _b;
|
|
212
|
+
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
213
|
+
if (this.isValidRule(rule)) {
|
|
214
|
+
try {
|
|
215
|
+
var negation = rule.matching.negated || false;
|
|
216
|
+
var matching = rule.matching.match_type;
|
|
217
|
+
if (this.getComparisonProcessorMethods().indexOf(matching) !== -1) {
|
|
218
|
+
if (data && typeof data === 'object' && (data === null || data === void 0 ? void 0 : data.constructor)) {
|
|
219
|
+
// Validate data key-value set.
|
|
220
|
+
if (data.constructor === Object) {
|
|
221
|
+
try {
|
|
222
|
+
// Rule object has to have `key` field
|
|
223
|
+
for (var _r = __values(Object.keys(data)), _s = _r.next(); !_s.done; _s = _r.next()) {
|
|
224
|
+
var key = _s.value;
|
|
225
|
+
var k = this._keys_case_sensitive ? key : key.toLowerCase();
|
|
226
|
+
var rule_k = this._keys_case_sensitive
|
|
227
|
+
? rule.key
|
|
228
|
+
: rule.key.toLowerCase();
|
|
229
|
+
if (k === rule_k) {
|
|
230
|
+
return this._comparisonProcessor[matching](data[key], rule.value, negation);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
235
|
+
finally {
|
|
236
|
+
try {
|
|
237
|
+
if (_s && !_s.done && (_a = _r.return)) _a.call(_r);
|
|
238
|
+
}
|
|
239
|
+
finally { if (e_1) throw e_1.error; }
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
else if (rule === null || rule === void 0 ? void 0 : rule.rule_type) {
|
|
243
|
+
// Rule object has to have `rule_type` field
|
|
244
|
+
(_d = (_c = this._loggerManager) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.call(_c, 'RuleManager._processRuleItem()', jsSdkEnums.MESSAGES.RULE_MATCH_START.replace('#', rule.rule_type));
|
|
245
|
+
try {
|
|
246
|
+
for (var _t = __values(Object.getOwnPropertyNames(data.constructor.prototype)), _u = _t.next(); !_u.done; _u = _t.next()) {
|
|
247
|
+
var method = _u.value;
|
|
248
|
+
if (method === 'constructor')
|
|
249
|
+
continue;
|
|
250
|
+
var rule_method = jsSdkUtils.camelCase("get ".concat(rule.rule_type.replace(/_/g, ' ')));
|
|
251
|
+
if (method === rule_method ||
|
|
252
|
+
((_e = data === null || data === void 0 ? void 0 : data.mapper) === null || _e === void 0 ? void 0 : _e.call(data, method)) === rule_method) {
|
|
253
|
+
var dataValue = data[method](rule);
|
|
254
|
+
if (Object.values(jsSdkEnums.RuleError).includes(dataValue))
|
|
255
|
+
return dataValue;
|
|
256
|
+
if (rule.rule_type === 'js_condition')
|
|
257
|
+
return dataValue;
|
|
258
|
+
return this._comparisonProcessor[matching](dataValue, rule.value, negation);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
263
|
+
finally {
|
|
264
|
+
try {
|
|
265
|
+
if (_u && !_u.done && (_b = _t.return)) _b.call(_t);
|
|
266
|
+
}
|
|
267
|
+
finally { if (e_2) throw e_2.error; }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
(_g = (_f = this._loggerManager) === null || _f === void 0 ? void 0 : _f.trace) === null || _g === void 0 ? void 0 : _g.call(_f, 'RuleManager._processRuleItem()', {
|
|
272
|
+
warn: jsSdkEnums.ERROR_MESSAGES.RULE_DATA_NOT_VALID,
|
|
273
|
+
data: data
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
(_j = (_h = this._loggerManager) === null || _h === void 0 ? void 0 : _h.trace) === null || _j === void 0 ? void 0 : _j.call(_h, 'RuleManager._processRuleItem()', {
|
|
279
|
+
warn: jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID,
|
|
280
|
+
data: data,
|
|
281
|
+
rule: rule
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
(_l = (_k = this._loggerManager) === null || _k === void 0 ? void 0 : _k.warn) === null || _l === void 0 ? void 0 : _l.call(_k, 'RuleManager._processRuleItem()', jsSdkEnums.ERROR_MESSAGES.RULE_MATCH_TYPE_NOT_SUPPORTED.replace('#', matching));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
(_o = (_m = this._loggerManager) === null || _m === void 0 ? void 0 : _m.error) === null || _o === void 0 ? void 0 : _o.call(_m, 'RuleManager._processRuleItem()', {
|
|
291
|
+
error: error.message
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
(_q = (_p = this._loggerManager) === null || _p === void 0 ? void 0 : _p.warn) === null || _q === void 0 ? void 0 : _q.call(_p, 'RuleManager._processRuleItem()', jsSdkEnums.ERROR_MESSAGES.RULE_NOT_VALID);
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
};
|
|
300
|
+
return RuleManager;
|
|
301
|
+
}());
|
|
302
|
+
|
|
303
|
+
exports.RuleManager = RuleManager;
|
|
304
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Convert JS SDK
|
|
3
|
+
* Version 1.0.0
|
|
4
|
+
* Copyright(c) 2020-2022 Convert Insights, Inc
|
|
5
|
+
* License Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
"use strict";var e=require("@convertcom/js-sdk-utils"),r=require("@convertcom/js-sdk-enums");function o(e){var r="function"==typeof Symbol&&Symbol.iterator,o=r&&e[r],t=0;if(o)return o.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")}"function"==typeof SuppressedError&&SuppressedError;var t=function(){function t(o,t){var l,a,n=(void 0===t?{}:t).loggerManager;this._comparisonProcessor=e.Comparisons,this._negation="!",this._keys_case_sensitive=true,this._loggerManager=n,this._comparisonProcessor=e.objectDeepValue(o,"rules.comparisonProcessor",e.Comparisons),this._negation=String(e.objectDeepValue(o,"rules.negation","!")).valueOf(),this._keys_case_sensitive=e.objectDeepValue(o,"rules.keys_case_sensitive",true,!0),this._mapper=(null==o?void 0:o.mapper)||function(e){return e},null===(a=null===(l=this._loggerManager)||void 0===l?void 0:l.trace)||void 0===a||a.call(l,"RuleManager()",r.MESSAGES.RULE_CONSTRUCTOR,this)}return Object.defineProperty(t.prototype,"comparisonProcessor",{get:function(){return this._comparisonProcessor},set:function(e){this._comparisonProcessor=e},enumerable:!1,configurable:!0}),t.prototype.getComparisonProcessorMethods=function(){var e=this;return Object.getOwnPropertyNames(this._comparisonProcessor).filter((function(r){return"function"==typeof e._comparisonProcessor[r]}))},t.prototype.isRuleMatched=function(o,t,l){var a,n,i,s,c,u,p,_,g,v,d;if(null===(n=null===(a=this._loggerManager)||void 0===a?void 0:a.trace)||void 0===n||n.call(a,"RuleManager.isRuleMatched()",this._mapper({data:o,ruleSet:t})),l&&(null===(s=null===(i=this._loggerManager)||void 0===i?void 0:i.info)||void 0===s||s.call(i,"RuleManager.isRuleMatched()",r.MESSAGES.PROCESSING_ENTITY.replace("#",l))),Object.prototype.hasOwnProperty.call(t,"OR")&&e.arrayNotEmpty(null==t?void 0:t.OR)){for(var R=0,h=t.OR.length;R<h;R++)if(d=this._processAND(o,t.OR[R]),Object.values(r.RuleError).includes(d)?null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.info)||void 0===u||u.call(c,"RuleManager.isRuleMatched()",l||"",r.ERROR_MESSAGES.RULE_ERROR):null===(_=null===(p=this._loggerManager)||void 0===p?void 0:p.info)||void 0===_||_.call(p,"RuleManager.isRuleMatched()",l||"",!1===d?r.MESSAGES.RULE_NOT_MATCH:r.MESSAGES.RULE_MATCH.replace("#",String(R))),!1!==d)return d}else null===(v=null===(g=this._loggerManager)||void 0===g?void 0:g.warn)||void 0===v||v.call(g,"RuleManager.isRuleMatched()",l||"",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1},t.prototype.isValidRule=function(e){var r,o;return null===(o=null===(r=this._loggerManager)||void 0===r?void 0:r.trace)||void 0===o||o.call(r,"RuleManager.isValidRule()",this._mapper({rule:e})),Object.prototype.hasOwnProperty.call(e,"matching")&&"object"==typeof e.matching&&Object.prototype.hasOwnProperty.call(e.matching,"match_type")&&"string"==typeof e.matching.match_type&&Object.prototype.hasOwnProperty.call(e.matching,"negated")&&"boolean"==typeof e.matching.negated&&Object.prototype.hasOwnProperty.call(e,"value")},t.prototype._processAND=function(o,t){var l,a,n,i,s;if(Object.prototype.hasOwnProperty.call(t,"AND")&&e.arrayNotEmpty(null==t?void 0:t.AND)){for(var c=0,u=t.AND.length;c<u;c++)if(!1===(s=this._processORWHEN(o,t.AND[c])))return!1;return!1!==s&&(null===(a=null===(l=this._loggerManager)||void 0===l?void 0:l.info)||void 0===a||a.call(l,"RuleManager._processAND()",r.MESSAGES.RULE_MATCH_AND)),s}return null===(i=null===(n=this._loggerManager)||void 0===n?void 0:n.warn)||void 0===i||i.call(n,"RuleManager._processAND()",r.ERROR_MESSAGES.RULE_NOT_VALID),!1},t.prototype._processORWHEN=function(o,t){var l,a,n;if(Object.prototype.hasOwnProperty.call(t,"OR_WHEN")&&e.arrayNotEmpty(null==t?void 0:t.OR_WHEN)){for(var i=0,s=t.OR_WHEN.length;i<s;i++)if(!1!==(n=this._processRuleItem(o,t.OR_WHEN[i])))return n}else null===(a=null===(l=this._loggerManager)||void 0===l?void 0:l.warn)||void 0===a||a.call(l,"RuleManager._processORWHEN()",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1},t.prototype._processRuleItem=function(t,l){var a,n,i,s,c,u,p,_,g,v,d,R,h,E,y,f,M;if(this.isValidRule(l))try{var O=l.matching.negated||!1,S=l.matching.match_type;if(-1!==this.getComparisonProcessorMethods().indexOf(S))if(t&&"object"==typeof t&&(null==t?void 0:t.constructor))if(t.constructor===Object)try{for(var m=o(Object.keys(t)),A=m.next();!A.done;A=m.next()){var N=A.value;if((this._keys_case_sensitive?N:N.toLowerCase())===(this._keys_case_sensitive?l.key:l.key.toLowerCase()))return this._comparisonProcessor[S](t[N],l.value,O)}}catch(e){a={error:e}}finally{try{A&&!A.done&&(n=m.return)&&n.call(m)}finally{if(a)throw a.error}}else if(null==l?void 0:l.rule_type){null===(u=null===(c=this._loggerManager)||void 0===c?void 0:c.info)||void 0===u||u.call(c,"RuleManager._processRuleItem()",r.MESSAGES.RULE_MATCH_START.replace("#",l.rule_type));try{for(var b=o(Object.getOwnPropertyNames(t.constructor.prototype)),P=b.next();!P.done;P=b.next()){var j=P.value;if("constructor"!==j){var T=e.camelCase("get ".concat(l.rule_type.replace(/_/g," ")));if(j===T||(null===(p=null==t?void 0:t.mapper)||void 0===p?void 0:p.call(t,j))===T){var w=t[j](l);return Object.values(r.RuleError).includes(w)||"js_condition"===l.rule_type?w:this._comparisonProcessor[S](w,l.value,O)}}}}catch(e){i={error:e}}finally{try{P&&!P.done&&(s=b.return)&&s.call(b)}finally{if(i)throw i.error}}}else null===(g=null===(_=this._loggerManager)||void 0===_?void 0:_.trace)||void 0===g||g.call(_,"RuleManager._processRuleItem()",{warn:r.ERROR_MESSAGES.RULE_DATA_NOT_VALID,data:t});else null===(d=null===(v=this._loggerManager)||void 0===v?void 0:v.trace)||void 0===d||d.call(v,"RuleManager._processRuleItem()",{warn:r.ERROR_MESSAGES.RULE_NOT_VALID,data:t,rule:l});else null===(h=null===(R=this._loggerManager)||void 0===R?void 0:R.warn)||void 0===h||h.call(R,"RuleManager._processRuleItem()",r.ERROR_MESSAGES.RULE_MATCH_TYPE_NOT_SUPPORTED.replace("#",S))}catch(e){null===(y=null===(E=this._loggerManager)||void 0===E?void 0:E.error)||void 0===y||y.call(E,"RuleManager._processRuleItem()",{error:e.message})}else null===(M=null===(f=this._loggerManager)||void 0===f?void 0:f.warn)||void 0===M||M.call(f,"RuleManager._processRuleItem()",r.ERROR_MESSAGES.RULE_NOT_VALID);return!1},t}();exports.RuleManager=t;
|
|
8
|
+
//# sourceMappingURL=index.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.min.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/lib/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@convertcom/js-sdk-rules",
|
|
3
|
+
"main": "index.min.js",
|
|
4
|
+
"module": "index.min.mjs",
|
|
5
|
+
"browser": "index.umd.min.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"**/**/*"
|
|
9
|
+
],
|
|
10
|
+
"author": "Convert Insights, Inc",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/convertcom/javascript-sdk.git",
|
|
14
|
+
"directory": "packages/rules"
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"version": "1.0.1"
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -14,8 +14,62 @@
|
|
|
14
14
|
"directory": "packages/rules"
|
|
15
15
|
},
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"pretest": "mkdir -p webpack-build && rm -rf coverage",
|
|
19
|
+
"test": "nyc yarn test:server && yarn coverage",
|
|
20
|
+
"test:server": "nyc yarn test:mocha",
|
|
21
|
+
"test:mocha": "mocha -r ts-node/register --recursive \"tests/**/*.tests.ts\" --exit",
|
|
22
|
+
"clean": "rm -rf lib",
|
|
23
|
+
"prebuild:version": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
|
|
24
|
+
"prebuild": "yarn clean",
|
|
25
|
+
"build": "rollup -c build.mjs",
|
|
26
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
27
|
+
"lint:fix": "yarn lint -- --fix",
|
|
28
|
+
"coverage": "nyc report -r cobertura --reporter=text-summary",
|
|
29
|
+
"postcoverage": "istanbul-cobertura-badger -e 85 -g 70 -r coverage/cobertura-coverage.xml -d ./coverage -b coverage"
|
|
30
|
+
},
|
|
17
31
|
"engines": {
|
|
18
32
|
"node": ">=8.0.0"
|
|
19
33
|
},
|
|
20
|
-
"
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@babel/core": "^7.23.2",
|
|
36
|
+
"@babel/preset-env": "^7.23.2",
|
|
37
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
38
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
39
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
40
|
+
"@types/chai": "^4.3.9",
|
|
41
|
+
"@types/chai-string": "^1.4.4",
|
|
42
|
+
"@types/mocha": "^10.0.3",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^6.9.0",
|
|
44
|
+
"@typescript-eslint/parser": "^6.9.0",
|
|
45
|
+
"assert": "^2.1.0",
|
|
46
|
+
"babel-cli": "^6.26.0",
|
|
47
|
+
"babel-preset-es2015": "^6.24.1",
|
|
48
|
+
"chai": "^4.3.10",
|
|
49
|
+
"chai-string": "^1.5.0",
|
|
50
|
+
"dotenv": "^16.3.1",
|
|
51
|
+
"eslint": "^8.52.0",
|
|
52
|
+
"eslint-config-prettier": "^9.0.0",
|
|
53
|
+
"eslint-plugin-jasmine": "^4.1.3",
|
|
54
|
+
"eslint-plugin-local-rules": "^2.0.0",
|
|
55
|
+
"eslint-plugin-mocha": "^10.2.0",
|
|
56
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
57
|
+
"istanbul-cobertura-badger": "^1.3.1",
|
|
58
|
+
"mocha": "^10.2.0",
|
|
59
|
+
"nyc": "^15.1.0",
|
|
60
|
+
"prettier": "^3.0.3",
|
|
61
|
+
"require": "^2.4.20",
|
|
62
|
+
"rollup": "^3.20.2",
|
|
63
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
64
|
+
"rollup-plugin-generate-package-json": "^3.2.0",
|
|
65
|
+
"rollup-plugin-modify": "^3.0.0",
|
|
66
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
67
|
+
"ts-node": "^10.9.1",
|
|
68
|
+
"tslint": "^6.1.3",
|
|
69
|
+
"tslint-config-prettier": "^1.18.0",
|
|
70
|
+
"typescript": "^5.2.2",
|
|
71
|
+
"typings": "^2.1.1",
|
|
72
|
+
"webpack": "^5.89.0"
|
|
73
|
+
},
|
|
74
|
+
"version": "1.0.1"
|
|
21
75
|
}
|