-
Notifications
You must be signed in to change notification settings - Fork 102
/
stampit.js
427 lines (353 loc) · 14 KB
/
stampit.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
!function() {
'use strict';
var _undefined;
var var1 = 'roperties';
var var2 = 'ropertyDescriptors';
var var3 = 'static';
var var4 = 'onfiguration';
var _properties = 'p' + var1;
var _deepProperties = 'deepP' + var1;
var _propertyDescriptors = 'p' + var2;
var _staticProperties = var3 + 'P' + var1;
var _staticDeepProperties = var3 + 'DeepP' + var1;
var _staticPropertyDescriptors = var3 + 'P' + var2;
var _configuration = 'c' + var4;
var _deepConfiguration = 'deepC' + var4;
var _deepProps = 'deepProps';
var _deepStatics = 'deepStatics';
var _deepConf = 'deepConf';
var _initializers = 'initializers';
var _methods = 'methods';
var _composers = 'composers';
var _compose = 'compose';
var _object = 'object';
var _length = 'length';
var _create = 'create';
var _Object = Object;
var isArray = Array.isArray;
var defineProperties = _Object.defineProperties;
var defineProperty = _Object.defineProperty;
var getOwnPropertyDescriptor = _Object.getOwnPropertyDescriptor;
var getOwnPropertySymbols = _Object.getOwnPropertySymbols;
var baseStampit = Array.prototype; // temporary reusing the variable
var concat = baseStampit.concat;
var slice = baseStampit.slice;
function getOwnPropertyKeys(obj) {
return _Object.getOwnPropertyNames(obj).concat(getOwnPropertySymbols ? getOwnPropertySymbols(obj) : []);
}
function _mergeOrAssign(action, dst) {
return slice.call(arguments, 2).reduce(action, dst);
}
function assignOne(dst, src) {
if (src) {
// We need to copy regular props, symbols, getters and setters.
var keys = getOwnPropertyKeys(src), i = 0, desc;
for (; i < keys.length; i += 1) {
desc = getOwnPropertyDescriptor(src, keys[i]);
// Make it rewritable because two stamps can have same named getter/setter
desc.configurable = true;
defineProperty(dst, keys[i], desc);
}
}
return dst;
}
var assign = _mergeOrAssign.bind(0, assignOne);
function isFunction(obj) {
return typeof obj == 'function';
}
function isObject(obj) {
return obj && typeof obj == _object || isFunction(obj);
}
function isPlainObject(value) {
return value && typeof value == _object &&
value.__proto__ == _Object.prototype;
}
/**
* The 'src' argument plays the command role.
* The returned values is always of the same type as the 'src'.
* @param {Array|Object|*} dst Destination
* @param {Array|Object|*} src Source
* @returns {Array|Object|*} The `dst` argument
*/
function mergeOne(dst, src) {
if (src === _undefined) return dst;
// According to specification arrays must be concatenated.
// Also, the '.concat' creates a new array instance. Overrides the 'dst'.
if (isArray(src)) return (isArray(dst) ? dst : []).concat(src);
// Now deal with non plain 'src' object. 'src' overrides 'dst'
// Note that functions are also assigned! We do not deep merge functions.
if (!isPlainObject(src)) return src;
var keys = getOwnPropertyKeys(src), i = 0, key, desc;
for (; i < keys[_length];) {
key = keys[i++];
desc = getOwnPropertyDescriptor(src, key);
if (desc.hasOwnProperty('value')) { // is this a regular property?
// Do not merge properties with the 'undefined' value.
if (desc.value !== _undefined) {
// deep merge each property. Recursion!
dst[key] = mergeOne(isPlainObject(dst[key]) || isArray(src[key]) ? dst[key] : {}, src[key]);
}
} else { // nope, it looks like a getter/setter
// Make it rewritable because two stamps can have same named getter/setter
desc.configurable = true;
defineProperty(dst, key, desc);
}
}
return dst;
}
var merge = _mergeOrAssign.bind(0, mergeOne);
function extractUniqueFunctions() {
var1 = concat.apply([], arguments).filter(function(elem, index, array) {
return isFunction(elem) && array.indexOf(elem) === index;
});
return var1[_length] ? var1 : _undefined;
}
/**
* Converts stampit extended descriptor to a standard one.
* @param {Object|*} descr
* methods
* properties
* props
* initializers
* init
* deepProperties
* deepProps
* propertyDescriptors
* staticProperties
* statics
* staticDeepProperties
* deepStatics
* staticPropertyDescriptors
* configuration
* conf
* deepConfiguration
* deepConf
* composers
* @returns {Descriptor} Standardised descriptor
*/
function standardiseDescriptor(descr) {
var4 = {};
var4[_methods] = descr[_methods] || _undefined;
var1 = descr[_properties];
var2 = descr.props;
var4[_properties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
var4[_initializers] = extractUniqueFunctions(descr.init, descr[_initializers]);
var4[_composers] = extractUniqueFunctions(descr[_composers]);
var1 = descr[_deepProperties];
var2 = descr[_deepProps];
var4[_deepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
var4[_propertyDescriptors] = descr[_propertyDescriptors];
var1 = descr[_staticProperties];
var2 = descr.statics;
var4[_staticProperties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
var1 = descr[_staticDeepProperties];
var2 = descr[_deepStatics];
var4[_staticDeepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
var1 = descr[_staticPropertyDescriptors];
var2 = descr.name && {name: {value: descr.name}};
var4[_staticPropertyDescriptors] = isObject(var2 || var1) ? assign({}, var1, var2) : _undefined;
var1 = descr[_configuration];
var2 = descr.conf;
var4[_configuration] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
var1 = descr[_deepConfiguration];
var2 = descr[_deepConf];
var4[_deepConfiguration] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
return var4;
}
/**
* Creates new factory instance.
* @returns {Function} The new factory function.
*/
function createFactory() {
return function Stamp(options) {
var i = Stamp[_compose] || {};
// Next line was optimized for most JS VMs. Please, be careful here!
var obj = {__proto__: i[_methods]};
var inits = i[_initializers], args = slice.apply(arguments);
var initializer, returnedValue;
var tmp = i[_deepProperties];
if (tmp) merge(obj, tmp);
tmp = i[_properties];
if (tmp) assign(obj, tmp);
tmp = i[_propertyDescriptors];
if (tmp) defineProperties(obj, tmp);
if (!inits || !inits[_length]) return obj;
if (options === _undefined) options = {};
for (i = 0; i < inits[_length];) {
initializer = inits[i++];
if (isFunction(initializer)) {
returnedValue = initializer.call(obj, options,
{instance: obj, stamp: Stamp, args: args});
obj = returnedValue === _undefined ? obj : returnedValue;
}
}
return obj;
};
}
/**
* Returns a new stamp given a descriptor and a compose function implementation.
* @param {Descriptor} [descriptor={}] The information about the object the stamp will be creating.
* @returns {Stamp} The new stamp
*/
function createStamp(descriptor) {
var1 = createFactory();
var2 = descriptor[_staticDeepProperties];
if (var2) merge(var1, var2);
var2 = descriptor[_staticProperties];
if (var2) assign(var1, var2);
var2 = descriptor[_staticPropertyDescriptors];
if (var2) defineProperties(var1, var2);
var2 = isFunction(var1[_compose]) ? var1[_compose] : compose;
assign(var1[_compose] = function() {
return var2.apply(this, arguments);
}, descriptor);
return var1;
}
/**
* Mutates the dstDescriptor by merging the srcComposable data into it.
* @param {Descriptor} dstDescriptor The descriptor object to merge into.
* @param {Composable} [srcComposable] The composable
* (either descriptor or stamp) to merge data form.
* @returns {Descriptor} Returns the dstDescriptor argument.
*/
function mergeComposable(dstDescriptor, srcComposable) {
function mergeAssign(propName, deep) {
if (!isObject(srcComposable[propName])) {
return;
}
if (!isObject(dstDescriptor[propName])) {
dstDescriptor[propName] = {};
}
(deep || assign)(dstDescriptor[propName], srcComposable[propName]);
}
function concatAssignFunctions(propName) {
var1 = extractUniqueFunctions(dstDescriptor[propName], srcComposable[propName]);
if (var1) dstDescriptor[propName] = var1;
}
if (srcComposable && isObject(srcComposable = srcComposable[_compose] || srcComposable)) {
mergeAssign(_methods);
mergeAssign(_properties);
mergeAssign(_deepProperties, merge);
mergeAssign(_propertyDescriptors);
mergeAssign(_staticProperties);
mergeAssign(_staticDeepProperties, merge);
mergeAssign(_staticPropertyDescriptors);
mergeAssign(_configuration);
mergeAssign(_deepConfiguration, merge);
concatAssignFunctions(_initializers);
concatAssignFunctions(_composers);
}
return dstDescriptor;
}
/**
* Given the list of composables (stamp descriptors and stamps) returns
* a new stamp (composable factory function).
* @typedef {Function} Compose
* Parameters: {...Composable} The list of composables.
* @returns {Stamp} A new stamp (aka composable factory function)
*/
function compose() {
var descriptor = concat.apply([this], arguments)
.reduce(mergeComposable, {});
return createStamp(descriptor);
}
/**
* The Stamp Descriptor
* @typedef {Function|Object} Descriptor
* @returns {Stamp} A new stamp based on this Stamp
* @property {Object} [methods] Methods or other data used as object instances' prototype
* @property {Array<Function>} [initializers] List of initializers called for each object instance
* @property {Object} [properties] Shallow assigned properties of object instances
* @property {Object} [deepProperties] Deeply merged properties of object instances
* @property {Object} [staticProperties] Shallow assigned properties of Stamps
* @property {Object} [staticDeepProperties] Deeply merged properties of Stamps
* @property {Object} [configuration] Shallow assigned properties of Stamp arbitrary metadata
* @property {Object} [deepConfiguration] Deeply merged properties of Stamp arbitrary metadata
* @property {Object} [propertyDescriptors] ES5 Property Descriptors applied to object instances
* @property {Object} [staticPropertyDescriptors] ES5 Property Descriptors applied to Stamps
*/
/**
* The Stamp factory function
* @typedef {Function} Stamp
* @returns {*} Instantiated object
* @property {Descriptor} compose - The Stamp descriptor and composition function
*/
/**
* A composable object - stamp or descriptor
* @typedef {Stamp|Descriptor} Composable
*/
/**
* Returns true if argument is a stamp.
* @param {*} obj Any object
* @returns {Boolean} True is the obj is a stamp
*/
function isStamp(obj) {
return isFunction(obj) && isFunction(obj[_compose]);
}
var allUtilities = {};
allUtilities[_methods] = createUtilityFunction(_methods, assign);
allUtilities[_properties] = allUtilities.props =
createUtilityFunction(_properties, assign);
allUtilities[_initializers] = allUtilities.init =
createUtilityFunction(_initializers, extractUniqueFunctions);
allUtilities[_composers] = createUtilityFunction(_composers, extractUniqueFunctions);
allUtilities[_deepProperties] = allUtilities[_deepProps] =
createUtilityFunction(_deepProperties, merge);
allUtilities[_staticProperties] = allUtilities.statics =
createUtilityFunction(_staticProperties, assign);
allUtilities[_staticDeepProperties] = allUtilities[_deepStatics] =
createUtilityFunction(_staticDeepProperties, merge);
allUtilities[_configuration] = allUtilities.conf =
createUtilityFunction(_configuration, assign);
allUtilities[_deepConfiguration] = allUtilities[_deepConf] =
createUtilityFunction(_deepConfiguration, merge);
allUtilities[_propertyDescriptors] = createUtilityFunction(_propertyDescriptors, assign);
allUtilities[_staticPropertyDescriptors] = createUtilityFunction(_staticPropertyDescriptors, assign);
function createUtilityFunction(propName, action) {
return function() {
var4 = {};
var4[propName] = action.apply(_undefined, concat.apply([{}], arguments));
var1 = this;
return ((var1 && var1[_compose]) || var2).call(var1, var4);
};
}
/**
* Infected compose
* Parameters: {...Composable} The list of composables.
* @return {Stamp} The Stampit-flavoured stamp
*/
var2 = allUtilities[_compose] = assign(function stampit() {
var i = 0, composable, composables = [], array = arguments, composerResult = this;
for (; i < array[_length];) {
composable = array[i++];
if (isObject(composable)) {
composables.push(isStamp(composable) ? composable : standardiseDescriptor(composable));
}
}
// Calling the standard pure compose function here.
composable = compose.apply(composerResult || baseStampit, composables);
if (composerResult) composables.unshift(composerResult);
array = composable[_compose][_composers];
if (isArray(array)) {
for (i = 0; i < array[_length];) {
composerResult = array[i++]({stamp: composable, composables: composables});
composable = isStamp(composerResult) ? composerResult : composable;
}
}
return composable;
}, allUtilities); // Setting up the shortcut functions
allUtilities[_create] = function() {
return this.apply(_undefined, arguments);
};
var4 = {};
var4[_staticProperties] = allUtilities;
/**
* Infected stamp. Used as a storage of the infection metadata
* @type {Function}
* @return {Stamp}
*/
baseStampit = compose(var4);
var2[_compose] = var2.bind(); // bind to undefined
var2.version = 'VERSION';
if (typeof _undefined != typeof module) module.exports = var2; else self.stampit = var2;
}();