amis-formula 1.2.9 → 1.3.0
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/dist/evalutor.d.ts +4 -0
- package/dist/index.js +50 -21
- package/package.json +1 -1
package/dist/evalutor.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* amis-formula v1.
|
|
2
|
+
* amis-formula v1.3.0
|
|
3
3
|
* Copyright 2021 fex
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -1236,6 +1236,7 @@ var Evaluator = /** @class */ (function () {
|
|
|
1236
1236
|
var input = this.evalute(ast.input);
|
|
1237
1237
|
var filters = ast.filters.concat();
|
|
1238
1238
|
var context = {
|
|
1239
|
+
filter: undefined,
|
|
1239
1240
|
data: this.context,
|
|
1240
1241
|
restFilters: filters
|
|
1241
1242
|
};
|
|
@@ -1245,6 +1246,7 @@ var Evaluator = /** @class */ (function () {
|
|
|
1245
1246
|
if (!fn) {
|
|
1246
1247
|
throw new Error("filter `".concat(filter.name, "` not exits"));
|
|
1247
1248
|
}
|
|
1249
|
+
context.filter = filter;
|
|
1248
1250
|
input = fn.apply(context, [input].concat(filter.args.map(function (item) {
|
|
1249
1251
|
if ((item === null || item === void 0 ? void 0 : item.type) === 'mixed') {
|
|
1250
1252
|
return item.body
|
|
@@ -4669,20 +4671,28 @@ var filters = {
|
|
|
4669
4671
|
},
|
|
4670
4672
|
last: function (input) { return input && (input.length ? input[input.length - 1] : null); },
|
|
4671
4673
|
minus: function (input, step) {
|
|
4674
|
+
var _a;
|
|
4672
4675
|
if (step === void 0) { step = 1; }
|
|
4673
|
-
return stripNumber((Number(input) || 0) -
|
|
4676
|
+
return stripNumber((Number(input) || 0) -
|
|
4677
|
+
Number(getStrOrVariable(step, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])));
|
|
4674
4678
|
},
|
|
4675
4679
|
plus: function (input, step) {
|
|
4680
|
+
var _a;
|
|
4676
4681
|
if (step === void 0) { step = 1; }
|
|
4677
|
-
return stripNumber((Number(input) || 0) +
|
|
4682
|
+
return stripNumber((Number(input) || 0) +
|
|
4683
|
+
Number(getStrOrVariable(step, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])));
|
|
4678
4684
|
},
|
|
4679
4685
|
times: function (input, step) {
|
|
4686
|
+
var _a;
|
|
4680
4687
|
if (step === void 0) { step = 1; }
|
|
4681
|
-
return stripNumber((Number(input) || 0) *
|
|
4688
|
+
return stripNumber((Number(input) || 0) *
|
|
4689
|
+
Number(getStrOrVariable(step, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])));
|
|
4682
4690
|
},
|
|
4683
4691
|
division: function (input, step) {
|
|
4692
|
+
var _a;
|
|
4684
4693
|
if (step === void 0) { step = 1; }
|
|
4685
|
-
return stripNumber((Number(input) || 0) /
|
|
4694
|
+
return stripNumber((Number(input) || 0) /
|
|
4695
|
+
Number(getStrOrVariable(step, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])));
|
|
4686
4696
|
},
|
|
4687
4697
|
count: function (input) {
|
|
4688
4698
|
return Array.isArray(input) || typeof input === 'string' ? input.length : 0;
|
|
@@ -4726,9 +4736,10 @@ var filters = {
|
|
|
4726
4736
|
args[_i - 1] = arguments[_i];
|
|
4727
4737
|
}
|
|
4728
4738
|
return Array.isArray(input)
|
|
4729
|
-
? input.concat.apply(input, args.map(function (arg) { return getStrOrVariable(arg, _this.data); })) : input;
|
|
4739
|
+
? input.concat.apply(input, args.map(function (arg, index) { var _a; return getStrOrVariable(arg, _this.data, (_a = _this.filter) === null || _a === void 0 ? void 0 : _a.args[index]); })) : input;
|
|
4730
4740
|
},
|
|
4731
4741
|
filter: function (input, keys, expOrDirective, arg1) {
|
|
4742
|
+
var _a, _b, _c, _d;
|
|
4732
4743
|
if (!Array.isArray(input) || !keys || !expOrDirective) {
|
|
4733
4744
|
return input;
|
|
4734
4745
|
}
|
|
@@ -4744,17 +4755,23 @@ var filters = {
|
|
|
4744
4755
|
fn = function (value) { return typeof value !== 'undefined'; };
|
|
4745
4756
|
}
|
|
4746
4757
|
else if (directive === 'equals' || directive === 'equal') {
|
|
4747
|
-
arg1 = arg1
|
|
4758
|
+
arg1 = arg1
|
|
4759
|
+
? getStrOrVariable(arg1, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[2])
|
|
4760
|
+
: '';
|
|
4748
4761
|
fn = function (value) { return arg1 == value; };
|
|
4749
4762
|
}
|
|
4750
4763
|
else if (directive === 'isIn') {
|
|
4751
|
-
var list_1 = arg1
|
|
4764
|
+
var list_1 = arg1
|
|
4765
|
+
? getStrOrVariable(arg1, this.data, (_b = this.filter) === null || _b === void 0 ? void 0 : _b.args[2])
|
|
4766
|
+
: [];
|
|
4752
4767
|
list_1 = str2array(list_1);
|
|
4753
4768
|
list_1 = Array.isArray(list_1) ? list_1 : list_1 ? [list_1] : [];
|
|
4754
4769
|
fn = function (value) { return (list_1.length ? !!~list_1.indexOf(value) : true); };
|
|
4755
4770
|
}
|
|
4756
4771
|
else if (directive === 'notIn') {
|
|
4757
|
-
var list_2 = arg1
|
|
4772
|
+
var list_2 = arg1
|
|
4773
|
+
? getStrOrVariable(arg1, this.data, (_c = this.filter) === null || _c === void 0 ? void 0 : _c.args[2])
|
|
4774
|
+
: [];
|
|
4758
4775
|
list_2 = str2array(list_2);
|
|
4759
4776
|
list_2 = Array.isArray(list_2) ? list_2 : list_2 ? [list_2] : [];
|
|
4760
4777
|
fn = function (value) { return !~list_2.indexOf(value); };
|
|
@@ -4764,7 +4781,9 @@ var filters = {
|
|
|
4764
4781
|
directive = 'match';
|
|
4765
4782
|
arg1 = expOrDirective;
|
|
4766
4783
|
}
|
|
4767
|
-
arg1 = arg1
|
|
4784
|
+
arg1 = arg1
|
|
4785
|
+
? getStrOrVariable(arg1, this.data, (_d = this.filter) === null || _d === void 0 ? void 0 : _d.args[2])
|
|
4786
|
+
: '';
|
|
4768
4787
|
// 比对的值是空时直接返回。
|
|
4769
4788
|
if (!arg1) {
|
|
4770
4789
|
return input;
|
|
@@ -4815,38 +4834,43 @@ var filters = {
|
|
|
4815
4834
|
return conditionalFilter(input, hasAlternate, this, !input, trueValue, falseValue);
|
|
4816
4835
|
},
|
|
4817
4836
|
isMatch: function (input, matchArg, trueValue, falseValue) {
|
|
4818
|
-
var _a;
|
|
4837
|
+
var _a, _b;
|
|
4819
4838
|
var hasAlternate = arguments.length > 3;
|
|
4820
|
-
matchArg =
|
|
4839
|
+
matchArg =
|
|
4840
|
+
(_b = getStrOrVariable(matchArg, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])) !== null && _b !== void 0 ? _b : matchArg;
|
|
4821
4841
|
return conditionalFilter(input, hasAlternate, this, matchArg && string2regExp("".concat(matchArg), false).test(String(input)), trueValue, falseValue);
|
|
4822
4842
|
},
|
|
4823
4843
|
notMatch: function (input, matchArg, trueValue, falseValue) {
|
|
4824
|
-
var _a;
|
|
4844
|
+
var _a, _b;
|
|
4825
4845
|
var hasAlternate = arguments.length > 3;
|
|
4826
|
-
matchArg =
|
|
4846
|
+
matchArg =
|
|
4847
|
+
(_b = getStrOrVariable(matchArg, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])) !== null && _b !== void 0 ? _b : matchArg;
|
|
4827
4848
|
return conditionalFilter(input, hasAlternate, this, matchArg && !string2regExp("".concat(matchArg), false).test(String(input)), trueValue, falseValue);
|
|
4828
4849
|
},
|
|
4829
4850
|
isEquals: function (input, equalsValue, trueValue, falseValue) {
|
|
4830
|
-
var _a;
|
|
4851
|
+
var _a, _b;
|
|
4831
4852
|
equalsValue =
|
|
4832
|
-
(
|
|
4853
|
+
(_b = getStrOrVariable(equalsValue, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])) !== null && _b !== void 0 ? _b : equalsValue;
|
|
4833
4854
|
var hasAlternate = arguments.length > 3;
|
|
4834
4855
|
return conditionalFilter(input, hasAlternate, this, input === equalsValue, trueValue, falseValue);
|
|
4835
4856
|
},
|
|
4836
4857
|
notEquals: function (input, equalsValue, trueValue, falseValue) {
|
|
4837
|
-
var _a;
|
|
4858
|
+
var _a, _b;
|
|
4838
4859
|
equalsValue =
|
|
4839
|
-
(
|
|
4860
|
+
(_b = getStrOrVariable(equalsValue, this.data, (_a = this.filter) === null || _a === void 0 ? void 0 : _a.args[0])) !== null && _b !== void 0 ? _b : equalsValue;
|
|
4840
4861
|
var hasAlternate = arguments.length > 3;
|
|
4841
4862
|
return conditionalFilter(input, hasAlternate, this, input !== equalsValue, trueValue, falseValue);
|
|
4842
4863
|
}
|
|
4843
4864
|
};
|
|
4844
4865
|
function conditionalFilter(input, hasAlternate, filterContext, test, trueValue, falseValue) {
|
|
4845
|
-
var _a;
|
|
4866
|
+
var _a, _b, _c;
|
|
4846
4867
|
(hasAlternate || test) && skipRestTest(filterContext.restFilters);
|
|
4847
4868
|
var result = test ? trueValue : falseValue;
|
|
4869
|
+
var ast = test
|
|
4870
|
+
? (_a = filterContext.filter) === null || _a === void 0 ? void 0 : _a.args[1]
|
|
4871
|
+
: (_b = filterContext.filter) === null || _b === void 0 ? void 0 : _b.args[2];
|
|
4848
4872
|
return test || hasAlternate
|
|
4849
|
-
? (
|
|
4873
|
+
? (_c = getStrOrVariable(result, filterContext.data, ast)) !== null && _c !== void 0 ? _c : result
|
|
4850
4874
|
: input;
|
|
4851
4875
|
}
|
|
4852
4876
|
/**
|
|
@@ -4856,7 +4880,12 @@ function conditionalFilter(input, hasAlternate, filterContext, test, trueValue,
|
|
|
4856
4880
|
* @param value 传入字符
|
|
4857
4881
|
* @param data 数据域
|
|
4858
4882
|
*/
|
|
4859
|
-
function getStrOrVariable(value, data) {
|
|
4883
|
+
function getStrOrVariable(value, data, ast) {
|
|
4884
|
+
// 通过读取 ast 来判断,只有 literal 才可能是变量,也可能是字符串
|
|
4885
|
+
// 其他的直接返回值即可。
|
|
4886
|
+
if ((ast === null || ast === void 0 ? void 0 : ast.type) && ast.type !== 'literal') {
|
|
4887
|
+
return value;
|
|
4888
|
+
}
|
|
4860
4889
|
return typeof value === 'string' && /,/.test(value)
|
|
4861
4890
|
? value.split(/\s*,\s*/).filter(function (item) { return item; })
|
|
4862
4891
|
: typeof value === 'string'
|