-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (47 loc) · 1.19 KB
/
index.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
var _ = require('lodash')
, dbc = require('dbc.js')
, Cocktail = require('cocktail')
;
function Objectify() {
}
Cocktail.mix(Objectify, {
'@exports': module
});
Objectify.build = function (schema, defaults) {
var self = this;
if (_.isObject(schema)) {
dbc([typeof schema === 'object'], 'Schema type is required');
createObjectOfType(schema.type);
} else if (_.isString(schema)) {
var schemaFromPath = require(schema);
if (schemaFromPath == null || schemaFromPath.length === 0) {
throw new Error('Schema path is wrong');
}
} else {
throw new Error('Schema must be a string or an object');
}
};
function createObjectOfType(type) {
type = type.toLowerCase() || 'object';
if (type === 'object')
return {};
else if (type === 'boolean')
return false;
else if (type === 'string')
return ''
else if (type === 'number') {
return 0;
} else if (type === 'array') {
return []
} else if (type === 'regex') {
return /\*/;
} else if (type === 'function') {
return function () {
};
} else if (type === 'undefined') {
return undefined;
} else if (type === 'null') {
return null;
}
}
module.exports = Objectify;