@twin.org/entity 0.10.1-next.6 → 0.10.1-next.9

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.
@@ -55,138 +55,133 @@ export class EntityConditions {
55
55
  * @returns True if the entity matches.
56
56
  */
57
57
  static compare(entity, comparator) {
58
- const val = ObjectHelper.propertyGet(entity, comparator.property);
58
+ const value = ObjectHelper.propertyGet(entity, comparator.property);
59
59
  const conditionValue = comparator.value;
60
+ const comparison = comparator.comparison;
60
61
  if (Is.undefined(conditionValue)) {
61
- const valUndefined = Is.undefined(val);
62
- if (valUndefined && comparator.comparison === ComparisonOperator.Equals) {
63
- return true;
64
- }
65
- else if (!valUndefined && comparator.comparison === ComparisonOperator.NotEquals) {
66
- return true;
67
- }
68
- return false;
62
+ return EntityConditions.matchEquality(Is.undefined(value), comparison);
69
63
  }
70
- else if (conditionValue === null) {
71
- const valNull = val === null;
72
- if (valNull && comparator.comparison === ComparisonOperator.Equals) {
73
- return true;
74
- }
75
- else if (!valNull && comparator.comparison === ComparisonOperator.NotEquals) {
76
- return true;
77
- }
78
- return false;
79
- }
80
- else if (Is.string(val)) {
81
- if (Is.string(conditionValue)) {
82
- if (!((comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||
83
- (comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||
84
- (comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||
85
- (comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||
86
- (comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&
87
- val >= conditionValue) ||
88
- (comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue) ||
89
- (comparator.comparison === ComparisonOperator.Includes && val.includes(conditionValue)) ||
90
- (comparator.comparison === ComparisonOperator.NotIncludes &&
91
- !val.includes(conditionValue)) ||
92
- (comparator.comparison === ComparisonOperator.StartsWith &&
93
- val.startsWith(conditionValue)))) {
94
- return false;
95
- }
96
- return true;
97
- }
98
- else if (Is.array(conditionValue)) {
99
- if (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {
100
- return false;
101
- }
102
- return true;
103
- }
104
- return false;
105
- }
106
- else if (Is.number(val)) {
107
- if (Is.number(conditionValue)) {
108
- if (!((comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||
109
- (comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||
110
- (comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||
111
- (comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||
112
- (comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&
113
- val >= conditionValue) ||
114
- (comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue))) {
115
- return false;
116
- }
117
- return true;
118
- }
119
- else if (Is.array(conditionValue)) {
120
- if (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {
121
- return false;
122
- }
123
- return true;
124
- }
125
- return false;
64
+ if (conditionValue === null) {
65
+ return EntityConditions.matchEquality(value === null, comparison);
126
66
  }
127
- else if (Is.boolean(val)) {
128
- if (Is.boolean(conditionValue)) {
129
- if (!((comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||
130
- (comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue))) {
131
- return false;
132
- }
133
- return true;
134
- }
135
- return false;
136
- }
137
- else if (Is.array(val)) {
138
- if (Is.array(conditionValue)) {
139
- if (comparator.comparison === ComparisonOperator.Equals ||
140
- comparator.comparison === ComparisonOperator.NotEquals) {
141
- const matches = ArrayHelper.matches(val, conditionValue);
142
- if (!((comparator.comparison === ComparisonOperator.Equals && matches) ||
143
- (comparator.comparison === ComparisonOperator.NotEquals && !matches))) {
144
- return false;
145
- }
146
- return true;
147
- }
148
- return false;
149
- }
150
- else if (Is.number(conditionValue) || Is.string(conditionValue)) {
151
- if (comparator.comparison === ComparisonOperator.Includes ||
152
- comparator.comparison === ComparisonOperator.NotIncludes ||
153
- comparator.comparison === ComparisonOperator.In) {
154
- const includes = val.includes(conditionValue);
155
- if (!((comparator.comparison === ComparisonOperator.Includes && includes) ||
156
- (comparator.comparison === ComparisonOperator.NotIncludes && !includes) ||
157
- (comparator.comparison === ComparisonOperator.In && includes))) {
158
- return false;
159
- }
160
- return true;
161
- }
162
- return false;
163
- }
164
- else if (Is.object(conditionValue)) {
165
- if (comparator.comparison === ComparisonOperator.Includes) {
166
- for (const v of val) {
167
- if (ObjectHelper.equal(v, conditionValue)) {
168
- return true;
169
- }
170
- }
171
- }
172
- else if (comparator.comparison === ComparisonOperator.NotIncludes) {
173
- for (const v of val) {
174
- if (ObjectHelper.equal(v, conditionValue)) {
175
- return false;
176
- }
177
- }
178
- return true;
179
- }
180
- }
181
- return false;
67
+ if (Is.string(value)) {
68
+ return Is.string(conditionValue)
69
+ ? EntityConditions.compareString(value, conditionValue, comparison)
70
+ : EntityConditions.compareIn(value, conditionValue, comparison);
182
71
  }
183
- else if (Is.object(val)) {
184
- if (comparator.comparison === ComparisonOperator.Equals) {
185
- return ObjectHelper.equal(val, conditionValue);
186
- }
187
- else if (comparator.comparison === ComparisonOperator.NotEquals) {
188
- return !ObjectHelper.equal(val, conditionValue);
189
- }
72
+ if (Is.number(value)) {
73
+ return Is.number(conditionValue)
74
+ ? EntityConditions.compareOrdered(value, conditionValue, comparison)
75
+ : EntityConditions.compareIn(value, conditionValue, comparison);
76
+ }
77
+ if (Is.boolean(value)) {
78
+ return Is.boolean(conditionValue)
79
+ ? EntityConditions.matchEquality(value === conditionValue, comparison)
80
+ : EntityConditions.compareIn(value, conditionValue, comparison);
81
+ }
82
+ if (Is.array(value)) {
83
+ return EntityConditions.compareArray(value, conditionValue, comparison);
84
+ }
85
+ if (Is.object(value)) {
86
+ return EntityConditions.matchEquality(ObjectHelper.equal(value, conditionValue), comparison);
87
+ }
88
+ return false;
89
+ }
90
+ /**
91
+ * Resolve an equality result for the equals and not equals operators.
92
+ * @param isEqual Whether the two values are equal.
93
+ * @param comparison The comparison to perform.
94
+ * @returns True if the comparison is satisfied.
95
+ * @internal
96
+ */
97
+ static matchEquality(isEqual, comparison) {
98
+ return ((comparison === ComparisonOperator.Equals && isEqual) ||
99
+ (comparison === ComparisonOperator.NotEquals && !isEqual));
100
+ }
101
+ /**
102
+ * Resolve an inclusion result for the includes and not includes operators.
103
+ * @param isIncluded Whether the value is included.
104
+ * @param comparison The comparison to perform.
105
+ * @returns True if the comparison is satisfied.
106
+ * @internal
107
+ */
108
+ static matchInclusion(isIncluded, comparison) {
109
+ return ((comparison === ComparisonOperator.Includes && isIncluded) ||
110
+ (comparison === ComparisonOperator.NotIncludes && !isIncluded));
111
+ }
112
+ /**
113
+ * Compare a value with the in operator against a list of condition values.
114
+ * @param value The value to test.
115
+ * @param conditionValue The condition value which should be a list.
116
+ * @param comparison The comparison to perform.
117
+ * @returns True if the comparison is satisfied.
118
+ * @internal
119
+ */
120
+ static compareIn(value, conditionValue, comparison) {
121
+ return (comparison === ComparisonOperator.In &&
122
+ Is.array(conditionValue) &&
123
+ conditionValue.includes(value));
124
+ }
125
+ /**
126
+ * Compare two values which support ordering.
127
+ * @param value The value to test.
128
+ * @param conditionValue The condition value to test against.
129
+ * @param comparison The comparison to perform.
130
+ * @returns True if the comparison is satisfied.
131
+ * @internal
132
+ */
133
+ static compareOrdered(value, conditionValue, comparison) {
134
+ switch (comparison) {
135
+ case ComparisonOperator.GreaterThan:
136
+ return value > conditionValue;
137
+ case ComparisonOperator.LessThan:
138
+ return value < conditionValue;
139
+ case ComparisonOperator.GreaterThanOrEqual:
140
+ return value >= conditionValue;
141
+ case ComparisonOperator.LessThanOrEqual:
142
+ return value <= conditionValue;
143
+ default:
144
+ return EntityConditions.matchEquality(value === conditionValue, comparison);
145
+ }
146
+ }
147
+ /**
148
+ * Compare two string values.
149
+ * @param value The value to test.
150
+ * @param conditionValue The condition value to test against.
151
+ * @param comparison The comparison to perform.
152
+ * @returns True if the comparison is satisfied.
153
+ * @internal
154
+ */
155
+ static compareString(value, conditionValue, comparison) {
156
+ if (comparison === ComparisonOperator.StartsWith) {
157
+ return value.startsWith(conditionValue);
158
+ }
159
+ if (comparison === ComparisonOperator.Includes ||
160
+ comparison === ComparisonOperator.NotIncludes) {
161
+ return EntityConditions.matchInclusion(value.includes(conditionValue), comparison);
162
+ }
163
+ return EntityConditions.compareOrdered(value, conditionValue, comparison);
164
+ }
165
+ /**
166
+ * Compare an array value against a condition value.
167
+ * @param value The array to test.
168
+ * @param conditionValue The condition value to test against.
169
+ * @param comparison The comparison to perform.
170
+ * @returns True if the comparison is satisfied.
171
+ * @internal
172
+ */
173
+ static compareArray(value, conditionValue, comparison) {
174
+ if (Is.array(conditionValue)) {
175
+ return EntityConditions.matchEquality(ArrayHelper.matches(value, conditionValue), comparison);
176
+ }
177
+ if (Is.string(conditionValue) || Is.number(conditionValue)) {
178
+ const isIncluded = value.includes(conditionValue);
179
+ return comparison === ComparisonOperator.In
180
+ ? isIncluded
181
+ : EntityConditions.matchInclusion(isIncluded, comparison);
182
+ }
183
+ if (Is.object(conditionValue)) {
184
+ return EntityConditions.matchInclusion(value.some(v => ObjectHelper.equal(v, conditionValue)), comparison);
190
185
  }
191
186
  return false;
192
187
  }
@@ -1 +1 @@
1
- {"version":3,"file":"entityConditions.js","sourceRoot":"","sources":["../../../src/utils/entityConditions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAGrE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAI,MAAS,EAAE,SAA8B;QAC/D,iDAAiD;QACjD,IAAI,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;YAC7B,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BAAkB,CAAC,SAAS,EAAE,CAAC;gBACnE,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;YACjD,CAAC;QACF,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ArrayHelper, Is, ObjectHelper } from \"@twin.org/core\";\nimport { ComparisonOperator } from \"../models/comparisonOperator.js\";\nimport type { EntityCondition } from \"../models/entityCondition.js\";\nimport type { IComparator } from \"../models/IComparator.js\";\nimport { LogicalOperator } from \"../models/logicalOperator.js\";\n\n/**\n * Class to perform condition checks.\n */\nexport class EntityConditions {\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param condition The conditions to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static check<T>(entity: T, condition?: EntityCondition<T>): boolean {\n\t\t// If no conditions are defined then it's a match\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst results: boolean[] = condition.conditions.map(c => EntityConditions.check(entity, c));\n\t\t\tif ((condition.logicalOperator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\t\treturn results.every(Boolean);\n\t\t\t}\n\t\t\treturn results.some(Boolean);\n\t\t}\n\n\t\tif (condition.property.includes(\".\")) {\n\t\t\t// It's a child property comparison, so evaluate the child property\n\t\t\t// and then compare it to the conditions\n\t\t\tconst path = condition.property.split(\".\");\n\n\t\t\tconst child = ObjectHelper.propertyGet(entity, path[0]);\n\n\t\t\t// If the child is an array then check each item\n\t\t\tif (Is.array(child)) {\n\t\t\t\tfor (const c of child) {\n\t\t\t\t\tconst check = EntityConditions.check(c, {\n\t\t\t\t\t\t...condition,\n\t\t\t\t\t\tproperty: path.slice(1).join(\".\")\n\t\t\t\t\t});\n\t\t\t\t\tif (check) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\t// It's a single value so just check the condition\n\t\treturn EntityConditions.compare(entity, condition);\n\t}\n\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param comparator The condition to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static compare<T>(entity: T, comparator: IComparator): boolean {\n\t\tconst val = ObjectHelper.propertyGet(entity, comparator.property);\n\t\tconst conditionValue = comparator.value;\n\n\t\tif (Is.undefined(conditionValue)) {\n\t\t\tconst valUndefined = Is.undefined(val);\n\t\t\tif (valUndefined && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn true;\n\t\t\t} else if (!valUndefined && comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (conditionValue === null) {\n\t\t\tconst valNull = val === null;\n\t\t\tif (valNull && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn true;\n\t\t\t} else if (!valNull && comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.string(val)) {\n\t\t\tif (Is.string(conditionValue)) {\n\t\t\t\tif (!(\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes && val.includes(conditionValue)) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes &&\n\t\t\t\t\t\t!val.includes(conditionValue)) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.StartsWith &&\n\t\t\t\t\t\tval.startsWith(conditionValue))\n\t\t\t\t)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.number(val)) {\n\t\t\tif (Is.number(conditionValue)) {\n\t\t\t\tif (!(\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue)\n\t\t\t\t)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.boolean(val)) {\n\t\t\tif (Is.boolean(conditionValue)) {\n\t\t\t\tif (!(\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue)\n\t\t\t\t)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.array(val)) {\n\t\t\tif (Is.array(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals\n\t\t\t\t) {\n\t\t\t\t\tconst matches = ArrayHelper.matches(val, conditionValue);\n\t\t\t\t\tif (!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && matches) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && !matches)\n\t\t\t\t\t)) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.number(conditionValue) || Is.string(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Includes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotIncludes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.In\n\t\t\t\t) {\n\t\t\t\t\tconst includes = val.includes(conditionValue);\n\t\t\t\t\tif (!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes && includes) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes && !includes) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.In && includes)\n\t\t\t\t\t)) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.object(conditionValue)) {\n\t\t\t\tif (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (comparator.comparison === ComparisonOperator.NotIncludes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.object(val)) {\n\t\t\tif (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn ObjectHelper.equal(val, conditionValue);\n\t\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn !ObjectHelper.equal(val, conditionValue);\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"]}
1
+ {"version":3,"file":"entityConditions.js","sourceRoot":"","sources":["../../../src/utils/entityConditions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAGrE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAI,MAAS,EAAE,SAA8B;QAC/D,iDAAiD;QACjD,IAAI,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAC;QACb,CAAC;QAED,IAAI,YAAY,IAAI,SAAS,EAAE,CAAC;YAC/B,mGAAmG;YACnG,MAAM,OAAO,GAAc,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;YAC5F,IAAI,CAAC,SAAS,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CA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C,CAAC;YAClD,OAAO,UAAU,KAAK,kBAAkB,CAAC,EAAE;gBAC1C,CAAC,CAAC,UAAU;gBACZ,CAAC,CAAC,gBAAgB,CAAC,cAAc,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAC5D,CAAC;QAED,IAAI,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;YAC/B,OAAO,gBAAgB,CAAC,cAAc,CACrC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC,EACtD,UAAU,CACV,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ArrayHelper, Is, ObjectHelper } from \"@twin.org/core\";\nimport { ComparisonOperator } from \"../models/comparisonOperator.js\";\nimport type { EntityCondition } from \"../models/entityCondition.js\";\nimport type { IComparator } from \"../models/IComparator.js\";\nimport { LogicalOperator } from \"../models/logicalOperator.js\";\n\n/**\n * Class to perform condition checks.\n */\nexport class EntityConditions {\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param condition The conditions to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static check<T>(entity: T, condition?: EntityCondition<T>): boolean {\n\t\t// If no conditions are defined then it's a match\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst results: boolean[] = condition.conditions.map(c => EntityConditions.check(entity, c));\n\t\t\tif ((condition.logicalOperator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\t\treturn results.every(Boolean);\n\t\t\t}\n\t\t\treturn results.some(Boolean);\n\t\t}\n\n\t\tif (condition.property.includes(\".\")) {\n\t\t\t// It's a child property comparison, so evaluate the child property\n\t\t\t// and then compare it to the conditions\n\t\t\tconst path = condition.property.split(\".\");\n\n\t\t\tconst child = ObjectHelper.propertyGet(entity, path[0]);\n\n\t\t\t// If the child is an array then check each item\n\t\t\tif (Is.array(child)) {\n\t\t\t\tfor (const c of child) {\n\t\t\t\t\tconst check = EntityConditions.check(c, {\n\t\t\t\t\t\t...condition,\n\t\t\t\t\t\tproperty: path.slice(1).join(\".\")\n\t\t\t\t\t});\n\t\t\t\t\tif (check) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\t// It's a single value so just check the condition\n\t\treturn EntityConditions.compare(entity, condition);\n\t}\n\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param comparator The condition to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static compare<T>(entity: T, comparator: IComparator): boolean {\n\t\tconst value = ObjectHelper.propertyGet(entity, comparator.property);\n\t\tconst conditionValue = comparator.value;\n\t\tconst comparison = comparator.comparison;\n\n\t\tif (Is.undefined(conditionValue)) {\n\t\t\treturn EntityConditions.matchEquality(Is.undefined(value), comparison);\n\t\t}\n\n\t\tif (conditionValue === null) {\n\t\t\treturn EntityConditions.matchEquality(value === null, comparison);\n\t\t}\n\n\t\tif (Is.string(value)) {\n\t\t\treturn Is.string(conditionValue)\n\t\t\t\t? EntityConditions.compareString(value, conditionValue, comparison)\n\t\t\t\t: EntityConditions.compareIn(value, conditionValue, comparison);\n\t\t}\n\n\t\tif (Is.number(value)) {\n\t\t\treturn Is.number(conditionValue)\n\t\t\t\t? EntityConditions.compareOrdered(value, conditionValue, comparison)\n\t\t\t\t: EntityConditions.compareIn(value, conditionValue, comparison);\n\t\t}\n\n\t\tif (Is.boolean(value)) {\n\t\t\treturn Is.boolean(conditionValue)\n\t\t\t\t? EntityConditions.matchEquality(value === conditionValue, comparison)\n\t\t\t\t: EntityConditions.compareIn(value, conditionValue, comparison);\n\t\t}\n\n\t\tif (Is.array(value)) {\n\t\t\treturn EntityConditions.compareArray(value, conditionValue, comparison);\n\t\t}\n\n\t\tif (Is.object(value)) {\n\t\t\treturn EntityConditions.matchEquality(ObjectHelper.equal(value, conditionValue), comparison);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Resolve an equality result for the equals and not equals operators.\n\t * @param isEqual Whether the two values are equal.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static matchEquality(isEqual: boolean, comparison: ComparisonOperator): boolean {\n\t\treturn (\n\t\t\t(comparison === ComparisonOperator.Equals && isEqual) ||\n\t\t\t(comparison === ComparisonOperator.NotEquals && !isEqual)\n\t\t);\n\t}\n\n\t/**\n\t * Resolve an inclusion result for the includes and not includes operators.\n\t * @param isIncluded Whether the value is included.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static matchInclusion(isIncluded: boolean, comparison: ComparisonOperator): boolean {\n\t\treturn (\n\t\t\t(comparison === ComparisonOperator.Includes && isIncluded) ||\n\t\t\t(comparison === ComparisonOperator.NotIncludes && !isIncluded)\n\t\t);\n\t}\n\n\t/**\n\t * Compare a value with the in operator against a list of condition values.\n\t * @param value The value to test.\n\t * @param conditionValue The condition value which should be a list.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static compareIn(\n\t\tvalue: unknown,\n\t\tconditionValue: unknown,\n\t\tcomparison: ComparisonOperator\n\t): boolean {\n\t\treturn (\n\t\t\tcomparison === ComparisonOperator.In &&\n\t\t\tIs.array(conditionValue) &&\n\t\t\tconditionValue.includes(value)\n\t\t);\n\t}\n\n\t/**\n\t * Compare two values which support ordering.\n\t * @param value The value to test.\n\t * @param conditionValue The condition value to test against.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static compareOrdered<U extends string | number>(\n\t\tvalue: U,\n\t\tconditionValue: U,\n\t\tcomparison: ComparisonOperator\n\t): boolean {\n\t\tswitch (comparison) {\n\t\t\tcase ComparisonOperator.GreaterThan:\n\t\t\t\treturn value > conditionValue;\n\t\t\tcase ComparisonOperator.LessThan:\n\t\t\t\treturn value < conditionValue;\n\t\t\tcase ComparisonOperator.GreaterThanOrEqual:\n\t\t\t\treturn value >= conditionValue;\n\t\t\tcase ComparisonOperator.LessThanOrEqual:\n\t\t\t\treturn value <= conditionValue;\n\t\t\tdefault:\n\t\t\t\treturn EntityConditions.matchEquality(value === conditionValue, comparison);\n\t\t}\n\t}\n\n\t/**\n\t * Compare two string values.\n\t * @param value The value to test.\n\t * @param conditionValue The condition value to test against.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static compareString(\n\t\tvalue: string,\n\t\tconditionValue: string,\n\t\tcomparison: ComparisonOperator\n\t): boolean {\n\t\tif (comparison === ComparisonOperator.StartsWith) {\n\t\t\treturn value.startsWith(conditionValue);\n\t\t}\n\n\t\tif (\n\t\t\tcomparison === ComparisonOperator.Includes ||\n\t\t\tcomparison === ComparisonOperator.NotIncludes\n\t\t) {\n\t\t\treturn EntityConditions.matchInclusion(value.includes(conditionValue), comparison);\n\t\t}\n\n\t\treturn EntityConditions.compareOrdered(value, conditionValue, comparison);\n\t}\n\n\t/**\n\t * Compare an array value against a condition value.\n\t * @param value The array to test.\n\t * @param conditionValue The condition value to test against.\n\t * @param comparison The comparison to perform.\n\t * @returns True if the comparison is satisfied.\n\t * @internal\n\t */\n\tprivate static compareArray(\n\t\tvalue: unknown[],\n\t\tconditionValue: unknown,\n\t\tcomparison: ComparisonOperator\n\t): boolean {\n\t\tif (Is.array(conditionValue)) {\n\t\t\treturn EntityConditions.matchEquality(ArrayHelper.matches(value, conditionValue), comparison);\n\t\t}\n\n\t\tif (Is.string(conditionValue) || Is.number(conditionValue)) {\n\t\t\tconst isIncluded = value.includes(conditionValue);\n\t\t\treturn comparison === ComparisonOperator.In\n\t\t\t\t? isIncluded\n\t\t\t\t: EntityConditions.matchInclusion(isIncluded, comparison);\n\t\t}\n\n\t\tif (Is.object(conditionValue)) {\n\t\t\treturn EntityConditions.matchInclusion(\n\t\t\t\tvalue.some(v => ObjectHelper.equal(v, conditionValue)),\n\t\t\t\tcomparison\n\t\t\t);\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"]}
package/docs/changelog.md CHANGED
@@ -1,5 +1,114 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.10.1-next.9](https://github.com/iotaledger/twin-framework/compare/entity-v0.10.1-next.8...entity-v0.10.1-next.9) (2026-09-21)
4
+
5
+
6
+ ### Features
7
+
8
+ * add IEntitySchemaDiff and entitySchemaDiff utility ([#282](https://github.com/iotaledger/twin-framework/issues/282)) ([9d63e94](https://github.com/iotaledger/twin-framework/commit/9d63e94021ee2ffc138004ee68cf53d08a6b17f9))
9
+ * add StartsWith comparison operator ([#517](https://github.com/iotaledger/twin-framework/issues/517)) ([5c39b5b](https://github.com/iotaledger/twin-framework/commit/5c39b5b77bb5d59199e6dad2000d1806e7f4035d))
10
+ * add support for null in EntityConditions.compare ([922c4ba](https://github.com/iotaledger/twin-framework/commit/922c4ba8af578b4e7eaaf21b3c37a9d788941487))
11
+ * add version field to IEntitySchema and EntitySchemaHelper.getVersion for migration support ([#346](https://github.com/iotaledger/twin-framework/issues/346)) ([e74557e](https://github.com/iotaledger/twin-framework/commit/e74557e4ccbda5b9971f4cfcd0852ba5957cead0))
12
+ * composite indexes ([b506d95](https://github.com/iotaledger/twin-framework/commit/b506d957a5eff6ff6012be773e98af36e750be4a))
13
+ * composite indexes ([#509](https://github.com/iotaledger/twin-framework/issues/509)) ([44b3327](https://github.com/iotaledger/twin-framework/commit/44b33270e6c8dabcc81cc4aa8d27a69255a5e7d4))
14
+ * dev tools ([#500](https://github.com/iotaledger/twin-framework/issues/500)) ([0879e55](https://github.com/iotaledger/twin-framework/commit/0879e558c71dca5e992d14914c6bde560a191df2))
15
+ * entity conditions boolean in ([#530](https://github.com/iotaledger/twin-framework/issues/530)) ([50cc09a](https://github.com/iotaledger/twin-framework/commit/50cc09aa7b32e0dd7936ed0c2f074b0b166bcbf5))
16
+ * entity max string lengths ([#497](https://github.com/iotaledger/twin-framework/issues/497)) ([7fc91e2](https://github.com/iotaledger/twin-framework/commit/7fc91e2a49b9f1e2113bfe10983e083298ef2539))
17
+ * entity schema decorators default value ([33397c2](https://github.com/iotaledger/twin-framework/commit/33397c2e24978a91257371a4c63ce7f6a7125d0c))
18
+ * entity schema diff updates ([#294](https://github.com/iotaledger/twin-framework/issues/294)) ([7a7a94d](https://github.com/iotaledger/twin-framework/commit/7a7a94d14ea5e785dd68fd6de1c5a84941721d28))
19
+ * linting and dependency update ([676b4e9](https://github.com/iotaledger/twin-framework/commit/676b4e9d9bce158065200bbf875bb31da81d166d))
20
+ * native enhancements ([#527](https://github.com/iotaledger/twin-framework/issues/527)) ([dc77328](https://github.com/iotaledger/twin-framework/commit/dc77328df201ff77809d8e8792c27780bfb6d8c4))
21
+ * optimistic locking version property for schemas ([#443](https://github.com/iotaledger/twin-framework/issues/443)) ([1b90987](https://github.com/iotaledger/twin-framework/commit/1b909875dc915a0ec133d8424d013ae7e4ddfb48))
22
+ * refactor EntityConditions compare ([576f894](https://github.com/iotaledger/twin-framework/commit/576f894f67186353d112a95ff57c94bc92e7f93c))
23
+ * typescript 6 update ([1d10f31](https://github.com/iotaledger/twin-framework/commit/1d10f31e6516ec622773f45e88af82fe749b384a))
24
+ * update dependencies ([4da77ab](https://github.com/iotaledger/twin-framework/commit/4da77ab30f499e52825ac5a76f51436ceb59c26e))
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * allow vacuous truth to match SQL semantics ([5bbc5e5](https://github.com/iotaledger/twin-framework/commit/5bbc5e53069f90bd39485a74a284eda9e5864e66))
30
+ * coerce and includes ([#429](https://github.com/iotaledger/twin-framework/issues/429)) ([98c9132](https://github.com/iotaledger/twin-framework/commit/98c9132316c0393ede9d6e634bbae9e5ece2d6b9))
31
+ * ensure __decorate is defined for decorators ([103a563](https://github.com/iotaledger/twin-framework/commit/103a563ce01ebdef6240d2e590e7b026e8692684))
32
+ * update copyright year ([#260](https://github.com/iotaledger/twin-framework/issues/260)) ([c4ad930](https://github.com/iotaledger/twin-framework/commit/c4ad930fcc84ba6b5447a8074574329870b4c3f5))
33
+ * vacuous entity condition ([#385](https://github.com/iotaledger/twin-framework/issues/385)) ([e1082bf](https://github.com/iotaledger/twin-framework/commit/e1082bff4a66ac270efc9c6654d974c19f889d2d))
34
+
35
+
36
+ ### Dependencies
37
+
38
+ * The following workspace dependencies were updated
39
+ * dependencies
40
+ * @twin.org/core bumped from 0.10.1-next.8 to 0.10.1-next.9
41
+ * @twin.org/nameof bumped from 0.10.1-next.8 to 0.10.1-next.9
42
+ * devDependencies
43
+ * @twin.org/nameof-transformer bumped from 0.10.1-next.8 to 0.10.1-next.9
44
+ * @twin.org/nameof-vitest-plugin bumped from 0.10.1-next.8 to 0.10.1-next.9
45
+ * @twin.org/validate-locales bumped from 0.10.1-next.8 to 0.10.1-next.9
46
+
47
+ ## [0.10.1-next.8](https://github.com/iotaledger/twin-framework/compare/entity-v0.10.1-next.7...entity-v0.10.1-next.8) (2026-09-21)
48
+
49
+
50
+ ### Features
51
+
52
+ * add IEntitySchemaDiff and entitySchemaDiff utility ([#282](https://github.com/iotaledger/twin-framework/issues/282)) ([9d63e94](https://github.com/iotaledger/twin-framework/commit/9d63e94021ee2ffc138004ee68cf53d08a6b17f9))
53
+ * add StartsWith comparison operator ([#517](https://github.com/iotaledger/twin-framework/issues/517)) ([5c39b5b](https://github.com/iotaledger/twin-framework/commit/5c39b5b77bb5d59199e6dad2000d1806e7f4035d))
54
+ * add support for null in EntityConditions.compare ([922c4ba](https://github.com/iotaledger/twin-framework/commit/922c4ba8af578b4e7eaaf21b3c37a9d788941487))
55
+ * add version field to IEntitySchema and EntitySchemaHelper.getVersion for migration support ([#346](https://github.com/iotaledger/twin-framework/issues/346)) ([e74557e](https://github.com/iotaledger/twin-framework/commit/e74557e4ccbda5b9971f4cfcd0852ba5957cead0))
56
+ * composite indexes ([b506d95](https://github.com/iotaledger/twin-framework/commit/b506d957a5eff6ff6012be773e98af36e750be4a))
57
+ * composite indexes ([#509](https://github.com/iotaledger/twin-framework/issues/509)) ([44b3327](https://github.com/iotaledger/twin-framework/commit/44b33270e6c8dabcc81cc4aa8d27a69255a5e7d4))
58
+ * dev tools ([#500](https://github.com/iotaledger/twin-framework/issues/500)) ([0879e55](https://github.com/iotaledger/twin-framework/commit/0879e558c71dca5e992d14914c6bde560a191df2))
59
+ * entity conditions boolean in ([#530](https://github.com/iotaledger/twin-framework/issues/530)) ([50cc09a](https://github.com/iotaledger/twin-framework/commit/50cc09aa7b32e0dd7936ed0c2f074b0b166bcbf5))
60
+ * entity max string lengths ([#497](https://github.com/iotaledger/twin-framework/issues/497)) ([7fc91e2](https://github.com/iotaledger/twin-framework/commit/7fc91e2a49b9f1e2113bfe10983e083298ef2539))
61
+ * entity schema decorators default value ([33397c2](https://github.com/iotaledger/twin-framework/commit/33397c2e24978a91257371a4c63ce7f6a7125d0c))
62
+ * entity schema diff updates ([#294](https://github.com/iotaledger/twin-framework/issues/294)) ([7a7a94d](https://github.com/iotaledger/twin-framework/commit/7a7a94d14ea5e785dd68fd6de1c5a84941721d28))
63
+ * linting and dependency update ([676b4e9](https://github.com/iotaledger/twin-framework/commit/676b4e9d9bce158065200bbf875bb31da81d166d))
64
+ * native enhancements ([#527](https://github.com/iotaledger/twin-framework/issues/527)) ([dc77328](https://github.com/iotaledger/twin-framework/commit/dc77328df201ff77809d8e8792c27780bfb6d8c4))
65
+ * optimistic locking version property for schemas ([#443](https://github.com/iotaledger/twin-framework/issues/443)) ([1b90987](https://github.com/iotaledger/twin-framework/commit/1b909875dc915a0ec133d8424d013ae7e4ddfb48))
66
+ * refactor EntityConditions compare ([576f894](https://github.com/iotaledger/twin-framework/commit/576f894f67186353d112a95ff57c94bc92e7f93c))
67
+ * typescript 6 update ([1d10f31](https://github.com/iotaledger/twin-framework/commit/1d10f31e6516ec622773f45e88af82fe749b384a))
68
+ * update dependencies ([4da77ab](https://github.com/iotaledger/twin-framework/commit/4da77ab30f499e52825ac5a76f51436ceb59c26e))
69
+
70
+
71
+ ### Bug Fixes
72
+
73
+ * allow vacuous truth to match SQL semantics ([5bbc5e5](https://github.com/iotaledger/twin-framework/commit/5bbc5e53069f90bd39485a74a284eda9e5864e66))
74
+ * coerce and includes ([#429](https://github.com/iotaledger/twin-framework/issues/429)) ([98c9132](https://github.com/iotaledger/twin-framework/commit/98c9132316c0393ede9d6e634bbae9e5ece2d6b9))
75
+ * ensure __decorate is defined for decorators ([103a563](https://github.com/iotaledger/twin-framework/commit/103a563ce01ebdef6240d2e590e7b026e8692684))
76
+ * update copyright year ([#260](https://github.com/iotaledger/twin-framework/issues/260)) ([c4ad930](https://github.com/iotaledger/twin-framework/commit/c4ad930fcc84ba6b5447a8074574329870b4c3f5))
77
+ * vacuous entity condition ([#385](https://github.com/iotaledger/twin-framework/issues/385)) ([e1082bf](https://github.com/iotaledger/twin-framework/commit/e1082bff4a66ac270efc9c6654d974c19f889d2d))
78
+
79
+
80
+ ### Dependencies
81
+
82
+ * The following workspace dependencies were updated
83
+ * dependencies
84
+ * @twin.org/core bumped from 0.10.1-next.7 to 0.10.1-next.8
85
+ * @twin.org/nameof bumped from 0.10.1-next.7 to 0.10.1-next.8
86
+ * devDependencies
87
+ * @twin.org/nameof-transformer bumped from 0.10.1-next.7 to 0.10.1-next.8
88
+ * @twin.org/nameof-vitest-plugin bumped from 0.10.1-next.7 to 0.10.1-next.8
89
+ * @twin.org/validate-locales bumped from 0.10.1-next.7 to 0.10.1-next.8
90
+
91
+ ## [0.10.1-next.7](https://github.com/iotaledger/twin-framework/compare/entity-v0.10.1-next.6...entity-v0.10.1-next.7) (2026-09-21)
92
+
93
+
94
+ ### Features
95
+
96
+ * dev tools ([#500](https://github.com/iotaledger/twin-framework/issues/500)) ([0879e55](https://github.com/iotaledger/twin-framework/commit/0879e558c71dca5e992d14914c6bde560a191df2))
97
+ * entity conditions boolean in ([#530](https://github.com/iotaledger/twin-framework/issues/530)) ([50cc09a](https://github.com/iotaledger/twin-framework/commit/50cc09aa7b32e0dd7936ed0c2f074b0b166bcbf5))
98
+ * refactor EntityConditions compare ([576f894](https://github.com/iotaledger/twin-framework/commit/576f894f67186353d112a95ff57c94bc92e7f93c))
99
+
100
+
101
+ ### Dependencies
102
+
103
+ * The following workspace dependencies were updated
104
+ * dependencies
105
+ * @twin.org/core bumped from 0.10.1-next.6 to 0.10.1-next.7
106
+ * @twin.org/nameof bumped from 0.10.1-next.6 to 0.10.1-next.7
107
+ * devDependencies
108
+ * @twin.org/nameof-transformer bumped from 0.10.1-next.6 to 0.10.1-next.7
109
+ * @twin.org/nameof-vitest-plugin bumped from 0.10.1-next.6 to 0.10.1-next.7
110
+ * @twin.org/validate-locales bumped from 0.10.1-next.6 to 0.10.1-next.7
111
+
3
112
  ## [0.10.1-next.6](https://github.com/iotaledger/twin-framework/compare/entity-v0.10.1-next.5...entity-v0.10.1-next.6) (2026-09-21)
4
113
 
5
114
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity",
3
- "version": "0.10.1-next.6",
3
+ "version": "0.10.1-next.9",
4
4
  "description": "Helpers for defining and working with entities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=24.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.10.1-next.6",
18
- "@twin.org/nameof": "0.10.1-next.6",
17
+ "@twin.org/core": "0.10.1-next.9",
18
+ "@twin.org/nameof": "0.10.1-next.9",
19
19
  "reflect-metadata": "0.2.2",
20
20
  "tslib": "2.8.1"
21
21
  },