UNPKG

894 BJavaScriptView Raw
1import match from 'MATCH/match';
2const expect = require('chai').expect;
3
4
5// 测试复杂的枚举赋值
6var params = {
7 type: 1
8};
9
10var enumConf = {
11 typeOptions: [
12 {
13 id: 1,
14 name: 'one'
15 },
16 {
17 id: 2,
18 name: 'two'
19 }
20 ]
21};
22
23var format = function (options, id, key, value) {
24 let k = '';
25
26 for (let i of options) {
27 if (i[key] === id) {
28 k = i[value];
29 }
30 }
31
32 return k;
33};
34
35match.register(format, 'format');
36
37var data = match.parse(params, {
38 typeId: '$${{type}}',
39 typeName: function (data, format) {
40 return format(enumConf.typeOptions, this.typeId, 'id', 'name');
41 },
42 typeNameAgain: (data, format) => format(enumConf.typeOptions, data.type, 'id', 'name')
43});
44
45expect(data).to.be.eql({
46 typeId: 1,
47 typeName: 'one',
48 typeNameAgain: 'one'
49});