-
Notifications
You must be signed in to change notification settings - Fork 1
/
Struct.js
39 lines (29 loc) · 765 Bytes
/
Struct.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
module.exports = createStruct
function createStruct (name, propTypes) {
CStruct.prototype = Object.create(Struct.prototype)
return Struct
function Struct (props) {
if (props instanceof CStruct) {
return props
}
var value = {}
for (var propName in propTypes) {
if (propName === 'type') continue
const prop = props[propName]
const propType = propTypes[propName]
const propValue = propType(prop)
if (propValue instanceof TypeError) {
return propValue
}
value[propName] = propValue
}
return new CStruct(value)
}
function CStruct (props) {
this.type = name
for (var propName in props) {
const prop = props[propName]
this[propName] = prop
}
}
}