-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCBF.js
68 lines (63 loc) · 2.1 KB
/
createCBF.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const lodash = require('lodash');
const std = require('./StDelegate.json');
const cbfTypes = require('./cbfTypes.json');
const elements = Object.keys(std);
//const attributes = lodash.uniq(Object.values(std).flat());
//console.log(attributes.map(o => `{"n":"${o}","t":"string"}`).join(",\n"));
/**
*
* @param {object} cbf schema object;
* @param {function} cb callback function, if you want to create other template, you can use this function;The first parameter is the name of the callback function, and the second parameter is the type of the callback function;
*
* @returns
*/
function cbfTemplate(cbf={key,val},cb) {
// use val as function name and key as returns type;
let cbfObj = lodash.filter(cbfTypes, ['n',cbf.key])[0];
let cbfType = cbfObj.t;
let cbfName = cbf.val;
let cbfTemplate = `/**
* callback function for ${cbf.key}
* @param {${cbfType}} ${cbfName}
* @returns {${cbfType}}
*/
function ${cbfName}(targetElement) {
switch (targetElement.Id) {
case 'example':
// do something
default:
break;
}
// SomeTime, Always needed return true or false, otherwise an error will be reported.
return true;
}`;
if(cb){
return cb(cbfName,cbfType);
}else{
return cbfTemplate;
}
}
/**
* create callback function for schema of xml node;
* @param {object} schemaObject xml node as json object;
* @param {string} schemaObject.element xml node name;
* @param {array} schemaObject.cbKeyAndValue xml node attributes and values;
*/
function createCBF(schemaObject = { element, cbKeyAndValue }) {
if (!schemaObject || !schemaObject.element || !schemaObject.cbKeyAndValue) return;
let element = schemaObject.element;
let cbKeyAndValue = schemaObject.cbKeyAndValue;
if (!elements.includes(element)) return;
let cb = std[element];
let cbf = [];
cbKeyAndValue.forEach(o => {
if (cb.includes(Object.keys(o)[0])) {
cbf.push(cbfTemplate(o))
}
})
return cbf.join("\n");
}
module.exports = {
createCBF,
cbfTemplate
};