-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
310 lines (282 loc) · 8.17 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*jslint node:true, es5:true, bitwise:true, white: true, continue: true, regexp: true, sloppy: true, nomen: true, regexp: true, maxerr: 50, indent: 4 */
var expound, extend, throwExpound = function (msg) {
throw {
name: "expoundError",
message: msg
};
},
util = require('util'),
_ = require('underscore'),
defaultProps = {
writable: true,
enumerable: true,
configurable: false,
required: false,
lazy: true,
valueHasBeenSet: false,
coerce: false
},
//Private function to build values or throw error
buildValue = function (target) {
try {
this.value = this.builder.call(target);
} catch (err) {
throwExpound('Builder Function does not return for attribute ' + this.name + '. Gives: ' + err.message);
}
},
property = function (spec) {
var self = this,
target = _.include(Object.keys(this), 'obj') ? this.obj : this,
prop = {
//tool for type constraint checking....
passesTypeConstraint: function () {
var coercions, coercionNames, activeCoercion, resetValueAndthrowExpound = function (message) {
this.value = undefined;
this.valueHasBeenSet = false;
throwExpound(message);
},
checkConstraint = function (type) {
var checkType;
//Bail if we have nothing to check
if (!type) {
return true;
}
//Throw error if the type doesn't exist
checkType = self.types[type];
if (!checkType) {
throwExpound('Type Constraint "' + type + '" No longer exists.');
}
//Check parents recursively
if (checkType.extendsType) {
checkConstraint.call(this, checkType.extendsType);
}
if (!checkType.constraint(this.value)) {
throwExpound('Value ' + this.name + ' does not pass type constraint. Expecting: ' + type);
}
return true;
};
try {
return checkConstraint.call(this, this.type);
}
//This is the entire coercion funcitonality
//
catch (err) {
//Check to see if we have coerce set and if the type has coercions
coercions = self.types[this.type].coerceFrom;
coercionNames = Object.keys(coercions);
if (!this.coerce || coercionNames.length === 0) {
resetValueAndthrowExpound(err.message);
}
//If it does, find the first coercion that matches the value
activeCoercion = _.find(coercionNames, function (fromType) {
try {
var foo = checkConstraint.call(this, fromType);
return checkConstraint.call(this, fromType);
} catch (err) {
return false;
}
}, this);
//and then run the function associated with that coercion
if (activeCoercion) {
this.value = coercions[activeCoercion](this.value);
} else {
resetValueAndthrowExpound(err.message + '. Coerce is set to true, but the value does not match any registered coercions for the type');
}
//try to check the constraint against the new value
try {
checkConstraint.call(this, this.type);
} catch (e) {
resetValueAndthrowExpound('Coerced ' + e.message);
}
}
return true;
}
};
//make sure the spec isn't full of garbage
//Name
if (!_.isString(spec.name)) {
throwExpound('The name of the attribute must be a string');
}
// Booleans
_.each(_.intersection(['writable', 'enumerable', 'configurable', 'required', 'lazy', 'coerce'], Object.keys(spec)), function (attr) {
if (!_.isBoolean(spec[attr])) {
throwExpound(attr + ' attribute must be boolean');
}
});
//Functions
_.each(_.intersection(['wrap', 'trigger', 'builder'], Object.keys(spec)), function (attr) {
if (!_.isFunction(spec[attr])) {
throwExpound(attr + ' attribute must be ia Function');
}
});
//Type Constraints
if (spec.type && (typeof self.types[spec.type] === 'undefined')) {
throwExpound(spec.type + ' is not a defined Type Constraint for' + spec.name);
}
//Extend our model
_.extend(prop, defaultProps, spec);
//Flag whether the value has been set
if (_.include(Object.keys(prop), 'value')) {
prop.valueHasBeenSet = true;
}
//test for required
if (prop.required && !prop.valueHasBeenSet && !prop.builder) {
throwExpound('Required Property with no value or builder method for ' + spec.name);
}
//Assign a value to default or non-lazy builders
if (!prop.valueHasBeenSet && prop.builder && !prop.lazy) {
buildValue.call(prop, target);
prop.valueHasBeenSet = true;
}
//check for type passing and reset values if need be
if (prop.valueHasBeenSet) {
prop.passesTypeConstraint();
}
//Create the base setFunciton
prop.setValue = function (newValue) {
prop.oldValue = prop.value;
//Check for writability
if (!prop.writable && prop.valueHasBeenSet) {
throwExpound('Can not rewrite to a non-writable property for attribute: ' + spec.name);
}
prop.value = newValue;
prop.valueHasBeenSet = true;
prop.passesTypeConstraint();
return prop.value;
};
//wrap the setFunction if wrapping
if (prop.wrap) {
prop.setValue = _.wrap(prop.setValue, function (func, newValue) {
prop.wrap.call(target, func, newValue);
});
}
Object.defineProperty(target, prop.name, (function () {
var definition = {};
definition.get = function () {
if (!prop.valueHasBeenSet && prop.builder) {
buildValue.call(prop, target);
}
prop.valueHasBeenSet = true;
prop.passesTypeConstraint();
return prop.value;
};
definition.set = function (newValue) {
prop.setValue(newValue);
//fire trigger
if (prop.trigger) {
prop.trigger.call(target, prop.value, prop.oldValue);
}
};
definition.enumerable = prop.enumerable;
definition.configurable = prop.configurable;
return definition;
}()));
return self;
};
expound = function (obj) {
// return a version of expound derived from prototype.
// that has the object tied to it.
var instance = Object.create(expound);
instance.obj = obj;
//for subclassing of types
instance.types = Object.create(expound.types);
return instance;
};
property.call(expound, {
name: "property",
value: property,
writable: false
});
//Can't use expound on this as its needed in the constructor
expound.types = {
"isArray": {
constraint: function (arg) {
return _.isArray(arg);
}
},
"isBoolean": {
constraint: function (arg) {
return _.isBoolean(arg);
}
},
"isDate": {
constraint: function (arg) {
return _.isDate(arg);
}
},
"isFunction": {
constraint: function (arg) {
return _.isFunction(arg);
}
},
"isNumber": {
constraint: function (arg) {
return _.isNumber(arg);
}
},
"isString": {
constraint: function (arg) {
return _.isString(arg);
}
},
"isRegExp": {
constraint: function (arg) {
return _.isRegExp(arg);
}
},
"isObject": {
constraint: function (arg) {
return (typeof arg === 'object' && !_.isArray(arg) && !_.isRegExp(arg) && !_.isFunction(arg));
}
}
};
//Set the base constraint as Null
_.each(expound.types, function (type) {
type.extendsType = null;
type.coerceFrom = {};
});
// Grab a handle to expound myself
extend = expound(expound);
//expound.property when called without an object extends expound itself. Useful for plug-ins
extend.property({
name: "addType",
value: function (type, extendsType, func) {
if (typeof this.types[extendsType] === 'undefined' && extendsType !== null) {
throwExpound('When adding type ' + extendsType + ' is not "null" or a defined Type Constraint.');
}
this.types[type] = {
constraint: func,
extendsType: extendsType,
coerceFrom: {}
};
return this;
},
writable: false
});
extend.property({
name: 'hasType',
value: function (name) {
return typeof this.types[name] === 'object' ? true : false;
},
writable: false
});
extend.property({
name: 'addCoercion',
value: function (type, from, func) {
//Check Variables
if (typeof this.types[type] === 'undefined') {
throwExpound(type + ' is not a defined Type Constraint.');
}
if (typeof this.types[from] === 'undefined') {
throwExpound(from + ' is not a defined Type Constraint.');
}
if (!_.isFunction(func)) {
throwExpound('You must coerce using a function.');
}
//Assign
this.types[type].coerceFrom[from] = func;
return this;
},
writable: false
});
module.exports = expound;