-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.js
93 lines (79 loc) · 2.33 KB
/
helpers.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var utils = require('utilities');
function formatModelProperties(properties) {
var obj = {default: {name: '', type: ''}};
if (!properties) {
return obj;
}
obj['default'].name = 'id';
obj['default'].type = 'string';
var itemsArr = properties
, name
, type
, args
, i
, value;
i = -1;
while (++i < itemsArr.length) {
// ignore invalid property names
if (['_','-'].indexOf(itemsArr[i].charAt(0)) != -1) continue;
value = itemsArr[i].split(':');
name = utils.string.camelize(value.shift());
type = value.shift() || '';
args = value.shift() || '';
// Take off any args on the type
type = type.replace(/:.*/g, '');
// Defaults and aliases
if (!type) {
type = 'string';
}
if (args === 'def') {
args = 'default';
}
switch (type) {
case 'integer':
type = 'int';
break;
case 'bool':
type = 'boolean';
break;
case 'default':
case 'def':
type = 'string';
args = 'default';
break;
}
// Manage properties that deal with changing default properties
if (args === 'default') {
// Reset old default property to its own property, only if it's not
// already the default
if (name !== obj['default'].name) {
// If the new default item already exists then delete it
if (obj[name]) {
delete obj[name];
}
obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
}
// Add new default property
obj['default'] = {name: name, type: type};
continue;
}
// If ID property is given and it matches the default
// then rewrite the default with the new ID property
if (name === 'id' && obj['default'].name === 'id') {
obj['default'] = {name: name, type: type};
continue;
}
// If the name is name or title then set them to default, otherwise add
// the property normally
if (name === 'name' || name === 'title') {
// Reset old default to its own property
obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
// Add new default property
obj['default'] = {name: name, type: type};
} else {
obj[name] = {name: name, type: type};
}
}
return obj;
}
module.exports.formatModelProperties = formatModelProperties;