-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildDocument.js
100 lines (97 loc) · 2.92 KB
/
buildDocument.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
94
95
96
97
98
99
100
module.exports = buildDocument;
function buildDocument(body, fields) {
var document = {};
for (var field in fields) {
if (fields.hasOwnProperty(field)) {
var value = extract(field);
if (value !== undefined) {
var parsed = parse(value, field, fields);
if (parsed) {
document[field] = parsed;
}
}
}
}
function parse(value, field, fields) {
if (typeof fields[field] === 'string') {
return convert(fields[field], value)
}
else {
if (fields[field].type === 'array' && value) {
var result = [];
if (typeof fields[field].contents === 'string') {
for (var i = 0; i < value.length; i++) {
var element = convert(fields[field].contents, value[i]);
if (element) {
result.push(element);
}
}
}
else {
if (fields[field].contents.type === 'array') {
for (var i = 0; i < value.length; i++) {
var element = db.buildDocument({ $result: value[i] }, { $result: fields[field].contents }).$result;
if (element) {
result.push(element);
}
}
}
else if (fields[field].contents.type === 'object') {
for (var i = 0; i < value.length; i++) {
var element = db.buildDocument(value[i], fields[field].contents.contents);
if (element) {
result.push(element);
}
}
}
}
return result;
}
else if (fields[field].type === 'object' && value) {
return db.buildDocument(value, fields[field].contents);
}
}
}
function convert(type, value) {
if (type === 'string' && (value || value === '')) {
return value;
}
else if (type === 'integer' && value) {
return parseInt(value);
}
else if (type === 'float' && value) {
return parseFloat(value);
}
else if (type === 'date' && value) {
return new Date(value);
}
else if (type === 'ObjectID' && value && value.length === 24) {
return ObjectID.createFromHexString(value);
}
else if (type === 'password' && value) {
var salt = bcrypt.genSaltSync(10);
return bcrypt.hashSync(value, salt);
}
}
function extract(field) {
var value = body[field] || body[underscore(field)] || body[camelcase(field)] || body[field.toLowerCase()] || body[underscore(field).toLowerCase()] || body[camelcase(field).toLowerCase()];
if (value) {
return value;
}
for (var f in body) {
if (body.hasOwnProperty(f)) {
if (f.toLowerCase() === field.toLowerCase() || underscore(f).toLowerCase() === underscore(field).toLowerCase() || camelcase(f).toLowerCase() === camelcase(field).toLowerCase()) {
return body[f];
}
}
}
return body[field];
}
function camelcase(string) {
return string.replace(/([-_][a-z])/g, function($1){return $1.toUpperCase().replace(/[-_]/,'');});
};
function underscore(string) {
return string.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
}
return document;
}