-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite-assign.js
62 lines (57 loc) · 1.83 KB
/
write-assign.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
const { boolTypes, dateTypes } = require('./constants');
module.exports = function(info, tab, conf) {
let content = `${tab}assign(props?: GenPartial<${conf.get('model')}>) {
`;
if (conf.get('base')) {
content += `${tab}${tab}super.assign(props);
`;
} else {
content += `${tab}${tab}if (props) {
`;
if (conf.get('toJSON')) {
content += `${tab}${tab}${tab}Object.assign(this, props.toJSON ? props.toJSON() : props);
`;
} else {
content += `${tab}${tab}${tab}Object.assign(this, props);
`;
}
content += `${tab}${tab}}
`;
}
let transformers = '';
Object.keys(info).forEach(name => {
const col = info[name];
if (boolTypes.indexOf(col.type) > -1 && col.length === 1) {
transformers += `${tab.repeat(3)}if (typeof props.${name} !== 'undefined') {
${tab.repeat(4)}this.${name} = booleanTransformer.from(props.${name});
${tab.repeat(3)}}
`;
} else if (dateTypes.indexOf(col.type) > -1) {
if (conf.get('moment')) {
transformers += `${tab.repeat(3)}if (typeof props.${name} !== 'undefined') {
${tab.repeat(4)}this.${name} = momentTransformer.from(props.${name});
${tab.repeat(3)}}
`;
} else {
transformers += `${tab.repeat(3)}if (typeof props.${name} !== 'undefined') {
${tab.repeat(4)}this.${name} = new Date(props.${name});
${tab.repeat(3)}}
`;
}
} else if (conf.get('big') && col.type === 'decimal') {
transformers += `${tab.repeat(3)}if (typeof props.${name} !== 'undefined') {
${tab.repeat(4)}this.${name} = bigTransformer.from(props.${name});
${tab.repeat(3)}}
`;
}
});
if (transformers !== '') {
content += `
${tab.repeat(2)}if (props) {
${transformers}${tab.repeat(2)}}
`;
}
content += `${tab}}
`;
return content;
};