-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.js
360 lines (320 loc) · 11.6 KB
/
generate.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @name generate.js
* @author Michaelangelo Jong
*/
(function GeneratorScope() {
/**
* Assert Error function.
* @param {Boolean} condition Whether or not to throw error.
* @param {String} message Error message.
*/
function assertError(condition, message) {
if (!condition) {
throw new Error(message);
}
}
/**
* Assert TypeError function.
* @param {Boolean} condition Whether or not to throw error.
* @param {String} message Error message.
*/
function assertTypeError(test, type) {
if (typeof test !== type) {
throw new TypeError('Expected \'' + type +
'\' but instead found \'' +
typeof test + '\'');
}
}
/**
* Returns the name of function 'func'.
* @param {Function} func Any function.
* @return {String} Name of 'func'.
*/
function getFunctionName(func) {
if (func.name !== void(0)) {
return func.name;
}
// Else use IE Shim
var funcNameMatch = func.toString()
.match(/function\s*([^\s]*)\s*\(/);
func.name = (funcNameMatch && funcNameMatch[1]) || '';
return func.name;
}
/**
* Returns true if 'obj' is an object containing only get and set functions, false otherwise.
* @param {Any} obj Value to be tested.
* @return {Boolean} true or false.
*/
function isGetSet(obj) {
var keys, length;
if (obj && typeof obj === 'object') {
keys = Object.getOwnPropertyNames(obj)
.sort();
length = keys.length;
if ((length === 1 && (keys[0] === 'get' && typeof obj.get ===
'function' ||
keys[0] === 'set' && typeof obj.set === 'function'
)) ||
(length === 2 && (keys[0] === 'get' && typeof obj.get ===
'function' &&
keys[1] === 'set' && typeof obj.set === 'function'
))) {
return true;
}
}
return false;
}
/**
* Defines properties on 'obj'.
* @param {Object} obj An object that 'properties' will be attached to.
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties on 'properties'.
* @param {Object} properties An object who's properties will be attached to 'obj'.
* @return {Generator} 'obj'.
*/
function defineObjectProperties(obj, descriptor, properties) {
var setProperties = {},
i,
keys,
length,
p = properties || descriptor,
d = properties && descriptor;
properties = (p && typeof p === 'object') ? p : {};
descriptor = (d && typeof d === 'object') ? d : {};
keys = Object.getOwnPropertyNames(properties);
length = keys.length;
for (i = 0; i < length; i++) {
if (isGetSet(properties[keys[i]])) {
setProperties[keys[i]] = {
configurable: !!descriptor.configurable,
enumerable: !!descriptor.enumerable,
get: properties[keys[i]].get,
set: properties[keys[i]].set
};
} else {
setProperties[keys[i]] = {
configurable: !!descriptor.configurable,
enumerable: !!descriptor.enumerable,
writable: !!descriptor.writable,
value: properties[keys[i]]
};
}
}
Object.defineProperties(obj, setProperties);
return obj;
}
var Creation = {
/**
* Defines properties on this object.
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties.
* @param {Object} properties An object who's properties will be attached to this object.
* @return {Object} This object.
*/
defineProperties: function defineProperties(descriptor,
properties) {
defineObjectProperties(this, descriptor,
properties);
return this;
},
/**
* returns the prototype of `this` Creation.
* @return {Object} Prototype of `this` Creation.
*/
getProto: function getProto() {
return Object.getPrototypeOf(this);
},
/**
* returns the prototype of `this` super Creation.
* @return {Object} Prototype of `this` super Creation.
*/
getSuper: function getSuper() {
return Object.getPrototypeOf(this.constructor.prototype);
}
};
var Generation = {
/**
* Returns true if 'generator' was generated by this Generator.
* @param {Generator} generator A Generator.
* @return {Boolean} true or false.
*/
isGeneration: function isGeneration(generator) {
assertTypeError(generator, 'function');
var _ = this;
return _.prototype.isPrototypeOf(generator.prototype);
},
/**
* Returns true if 'object' was created by this Generator.
* @param {Object} object An Object.
* @return {Boolean} true or false.
*/
isCreation: function isCreation(object) {
var _ = this;
return object instanceof _;
},
/**
* Generates a new generator that inherits from `this` generator.
* @param {Generator} ParentGenerator Generator to inherit from.
* @param {Function} create Create method that gets called when creating a new instance of new generator.
* @return {Generator} New Generator that inherits from 'ParentGenerator'.
*/
generate: function generate(construct) {
assertTypeError(construct, 'function');
var _ = this;
defineObjectProperties(
construct, {
configurable: false,
enumerable: false,
writable: false
}, {
prototype: Object.create(_.prototype)
}
);
defineObjectProperties(
construct, {
configurable: false,
enumerable: false,
writable: false
},
Generation
);
defineObjectProperties(
construct.prototype, {
configurable: false,
enumerable: false,
writable: false
}, {
constructor: construct,
generator: construct,
}
);
return construct;
},
/**
* Defines shared properties for all objects created by this generator.
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties.
* @param {Object} properties An object who's properties will be attached to this generator's prototype.
* @return {Generator} This generator.
*/
definePrototype: function definePrototype(descriptor,
properties) {
defineObjectProperties(this.prototype,
descriptor,
properties);
return this;
}
};
function Generator() {}
defineObjectProperties(
Generator, {
configurable: false,
enumerable: false,
writable: false
}, {
prototype: Generator.prototype
}
);
defineObjectProperties(
Generator.prototype, {
configurable: false,
enumerable: false,
writable: false
},
Creation
);
defineObjectProperties(
Generator, {
configurable: false,
enumerable: false,
writable: false
},
Generation
);
defineObjectProperties(
Generator, {
configurable: false,
enumerable: false,
writable: false
}, {
/**
* Returns true if 'generator' was generated by this Generator.
* @param {Generator} generator A Generator.
* @return {Boolean} true or false.
*/
isGenerator: function isGenerator(generator) {
return this.isGeneration(generator);
},
/**
* Generates a new generator that inherits from `this` generator.
* @param {Generator} extendFrom Constructor to inherit from.
* @param {Function} create Create method that gets called when creating a new instance of new generator.
* @return {Generator} New Generator that inherits from 'ParentGenerator'.
*/
toGenerator: function toGenerator(extendFrom, create) {
console.warn(
'Generator.toGenerator is depreciated please use Generator.generateFrom'
);
return this.generateFrom(extendFrom, create);
},
/**
* Generates a new generator that inherits from `this` generator.
* @param {Constructor} extendFrom Constructor to inherit from.
* @param {Function} create Create method that gets called when creating a new instance of new generator.
* @return {Generator} New Generator that inherits from 'ParentGenerator'.
*/
generateFrom: function generateFrom(extendFrom, create) {
assertTypeError(extendFrom, 'function');
assertTypeError(create, 'function');
defineObjectProperties(
create, {
configurable: false,
enumerable: false,
writable: false
}, {
prototype: Object.create(extendFrom.prototype),
}
);
defineObjectProperties(
create, {
configurable: false,
enumerable: false,
writable: false
},
Generation
);
defineObjectProperties(
create.prototype, {
configurable: false,
enumerable: false,
writable: false
}, {
constructor: create,
generator: create,
}
);
defineObjectProperties(
create.prototype, {
configurable: false,
enumerable: false,
writable: false
},
Creation
);
return create;
}
}
);
Object.freeze(Generator);
Object.freeze(Generator.prototype);
// Exports
if (typeof define === 'function' && define.amd) {
// AMD
define(function () {
return Generator;
});
} else if (typeof module === 'object' && typeof exports === 'object') {
// Node/CommonJS
module.exports = Generator;
} else {
// Browser global
window.Generator = Generator;
}
}());