-
Notifications
You must be signed in to change notification settings - Fork 0
/
stampit.js
494 lines (409 loc) · 16.1 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.stampit = global.stampit || {})));
}(this, (function (exports) { 'use strict';
/**
* The 'src' argument plays the command role.
* The returned values is always of the same type as the 'src'.
* @param dst
* @param src
* @returns {*}
*/
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; }
// See if 'dst' is allowed to be mutated. If not - it's overridden with a new plain object.
var returnValue = isObject(dst) ? dst : {};
var keys = Object.keys(src);
for (var i = 0; i < keys.length; i += 1) {
var key = keys[i];
var srcValue = src[key];
// Do not merge properties with the 'undefined' value.
if (srcValue !== undefined) {
var dstValue = returnValue[key];
// Recursive calls to mergeOne() must allow only plain objects or arrays in dst
var newDst = isPlainObject(dstValue) || isArray(srcValue) ? dstValue : {};
// deep merge each property. Recursion!
returnValue[key] = mergeOne(newDst, srcValue);
}
}
return returnValue;
}
var merge = function (dst) {
var srcs = [], len = arguments.length - 1;
while ( len-- > 0 ) srcs[ len ] = arguments[ len + 1 ];
return srcs.reduce(mergeOne, dst);
};
function isFunction(obj) {
return typeof obj === 'function';
}
function isObject(obj) {
var type = typeof obj;
return !!obj && (type === 'object' || type === 'function');
}
var assign = Object.assign;
var isArray = Array.isArray;
function isPlainObject(value) {
return !!value && typeof value === 'object' &&
Object.getPrototypeOf(value) === Object.prototype;
}
var concat = Array.prototype.concat;
function extractFunctions() {
var fns = concat.apply([], arguments).filter(isFunction);
return fns.length === 0 ? undefined : fns;
}
function concatAssignFunctions(dstObject, srcArray, propName) {
if (!isArray(srcArray)) { return; }
var length = srcArray.length;
var dstArray = dstObject[propName] || [];
dstObject[propName] = dstArray;
for (var i = 0; i < length; i += 1) {
var fn = srcArray[i];
if (isFunction(fn) && dstArray.indexOf(fn) < 0) {
dstArray.push(fn);
}
}
}
function combineProperties(dstObject, srcObject, propName, action) {
if (!isObject(srcObject[propName])) { return; }
if (!isObject(dstObject[propName])) { dstObject[propName] = {}; }
action(dstObject[propName], srcObject[propName]);
}
function deepMergeAssign(dstObject, srcObject, propName) {
combineProperties(dstObject, srcObject, propName, merge);
}
function mergeAssign(dstObject, srcObject, propName) {
combineProperties(dstObject, srcObject, propName, assign);
}
/**
* Converts stampit extended descriptor to a standard one.
* @param [methods]
* @param [properties]
* @param [props]
* @param [refs]
* @param [initializers]
* @param [init]
* @param [deepProperties]
* @param [deepProps]
* @param [propertyDescriptors]
* @param [staticProperties]
* @param [statics]
* @param [staticDeepProperties]
* @param [deepStatics]
* @param [staticPropertyDescriptors]
* @param [configuration]
* @param [conf]
* @param [deepConfiguration]
* @param [deepConf]
* @param [composers]
* @returns {Descriptor}
*/
var standardiseDescriptor = function (ref) {
if ( ref === void 0 ) ref = {};
var methods = ref.methods;
var properties = ref.properties;
var props = ref.props;
var refs = ref.refs;
var initializers = ref.initializers;
var init = ref.init;
var composers = ref.composers;
var deepProperties = ref.deepProperties;
var deepProps = ref.deepProps;
var propertyDescriptors = ref.propertyDescriptors;
var staticProperties = ref.staticProperties;
var statics = ref.statics;
var staticDeepProperties = ref.staticDeepProperties;
var deepStatics = ref.deepStatics;
var staticPropertyDescriptors = ref.staticPropertyDescriptors;
var configuration = ref.configuration;
var conf = ref.conf;
var deepConfiguration = ref.deepConfiguration;
var deepConf = ref.deepConf;
var p = isObject(props) || isObject(refs) || isObject(properties) ?
assign({}, props, refs, properties) : undefined;
var dp = isObject(deepProps) ? merge({}, deepProps) : undefined;
dp = isObject(deepProperties) ? merge(dp, deepProperties) : dp;
var sp = isObject(statics) || isObject(staticProperties) ?
assign({}, statics, staticProperties) : undefined;
var dsp = isObject(deepStatics) ? merge({}, deepStatics) : undefined;
dsp = isObject(staticDeepProperties) ? merge(dsp, staticDeepProperties) : dsp;
var c = isObject(conf) || isObject(configuration) ?
assign({}, conf, configuration) : undefined;
var dc = isObject(deepConf) ? merge({}, deepConf) : undefined;
dc = isObject(deepConfiguration) ? merge(dc, deepConfiguration) : dc;
var ii = extractFunctions(init, initializers);
var composerFunctions = extractFunctions(composers);
if (composerFunctions) {
dc = dc || {};
concatAssignFunctions(dc, composerFunctions, 'composers');
}
var descriptor = {};
if (methods) { descriptor.methods = methods; }
if (p) { descriptor.properties = p; }
if (ii) { descriptor.initializers = ii; }
if (dp) { descriptor.deepProperties = dp; }
if (sp) { descriptor.staticProperties = sp; }
if (methods) { descriptor.methods = methods; }
if (dsp) { descriptor.staticDeepProperties = dsp; }
if (propertyDescriptors) { descriptor.propertyDescriptors = propertyDescriptors; }
if (staticPropertyDescriptors) { descriptor.staticPropertyDescriptors = staticPropertyDescriptors; }
if (c) { descriptor.configuration = c; }
if (dc) { descriptor.deepConfiguration = dc; }
return descriptor;
};
/**
* Creates new factory instance.
* @param {Descriptor} descriptor The information about the object the factory will be creating.
* @returns {Function} The new factory function.
*/
function createFactory(descriptor) {
return function Stamp(options) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
// Next line was optimized for most JS VMs. Please, be careful here!
var obj = Object.create(descriptor.methods || null);
merge(obj, descriptor.deepProperties);
assign(obj, descriptor.properties);
Object.defineProperties(obj, descriptor.propertyDescriptors || {});
if (!descriptor.initializers || descriptor.initializers.length === 0) { return obj; }
if (options === undefined) { options = {}; }
var inits = descriptor.initializers;
var length = inits.length;
for (var i = 0; i < length; i += 1) {
var initializer = inits[i];
if (isFunction(initializer)) {
var returnedValue = initializer.call(obj, options,
{instance: obj, stamp: Stamp, args: [options].concat(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.
* @param {Compose} composeFunction The "compose" function implementation.
* @returns {Stamp}
*/
function createStamp(descriptor, composeFunction) {
var Stamp = createFactory(descriptor);
merge(Stamp, descriptor.staticDeepProperties);
assign(Stamp, descriptor.staticProperties);
Object.defineProperties(Stamp, descriptor.staticPropertyDescriptors || {});
var composeImplementation = isFunction(Stamp.compose) ? Stamp.compose : composeFunction;
Stamp.compose = function _compose() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return composeImplementation.apply(this, args);
};
assign(Stamp.compose, descriptor);
return Stamp;
}
/**
* 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) {
var srcDescriptor = (srcComposable && srcComposable.compose) || srcComposable;
if (!isObject(srcDescriptor)) { return dstDescriptor; }
mergeAssign(dstDescriptor, srcDescriptor, 'methods');
mergeAssign(dstDescriptor, srcDescriptor, 'properties');
deepMergeAssign(dstDescriptor, srcDescriptor, 'deepProperties');
mergeAssign(dstDescriptor, srcDescriptor, 'propertyDescriptors');
mergeAssign(dstDescriptor, srcDescriptor, 'staticProperties');
deepMergeAssign(dstDescriptor, srcDescriptor, 'staticDeepProperties');
mergeAssign(dstDescriptor, srcDescriptor, 'staticPropertyDescriptors');
mergeAssign(dstDescriptor, srcDescriptor, 'configuration');
deepMergeAssign(dstDescriptor, srcDescriptor, 'deepConfiguration');
concatAssignFunctions(dstDescriptor, srcDescriptor.initializers, 'initializers');
return dstDescriptor;
}
/**
* Given the list of composables (stamp descriptors and stamps) returns
* a new stamp (composable factory function).
* @typedef {Function} Compose
* @param {...(Composable)} [composables] The list of composables.
* @returns {Stamp} A new stamp (aka composable factory function)
*/
function compose() {
var composables = [], len = arguments.length;
while ( len-- ) composables[ len ] = arguments[ len ];
var descriptor = [this]
.concat(composables)
.filter(isObject)
.reduce(mergeComposable, {});
return createStamp(descriptor, compose);
}
/**
* 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
* @returns {Boolean}
*/
function isStamp(obj) {
return isFunction(obj) && isFunction(obj.compose);
}
function createUtilityFunction(propName, action) {
return function composeUtil() {
var i = arguments.length, argsArray = Array(i);
while ( i-- ) argsArray[i] = arguments[i];
return ((this && this.compose) || stampit).call(this, ( obj = {}, obj[propName] = action.apply(void 0, [ {} ].concat( argsArray )), obj ));
var obj;
};
}
var methods = createUtilityFunction('methods', assign);
var properties = createUtilityFunction('properties', assign);
function initializers() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return ((this && this.compose) || stampit).call(this, {
initializers: extractFunctions.apply(void 0, args)
});
}
function composers() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return ((this && this.compose) || stampit).call(this, {
composers: extractFunctions.apply(void 0, args)
});
}
var deepProperties = createUtilityFunction('deepProperties', merge);
var staticProperties = createUtilityFunction('staticProperties', assign);
var staticDeepProperties = createUtilityFunction('staticDeepProperties', merge);
var configuration = createUtilityFunction('configuration', assign);
var deepConfiguration = createUtilityFunction('deepConfiguration', merge);
var propertyDescriptors = createUtilityFunction('propertyDescriptors', assign);
var staticPropertyDescriptors = createUtilityFunction('staticPropertyDescriptors', assign);
var allUtilities = {
methods: methods,
properties: properties,
refs: properties,
props: properties,
initializers: initializers,
init: initializers,
composers: composers,
deepProperties: deepProperties,
deepProps: deepProperties,
staticProperties: staticProperties,
statics: staticProperties,
staticDeepProperties: staticDeepProperties,
deepStatics: staticDeepProperties,
configuration: configuration,
conf: configuration,
deepConfiguration: deepConfiguration,
deepConf: deepConfiguration,
propertyDescriptors: propertyDescriptors,
staticPropertyDescriptors: staticPropertyDescriptors
};
/**
* Infected stamp. Used as a storage of the infection metadata
* @type {Function}
* @return {Stamp}
*/
var baseStampit = compose(
{staticProperties: allUtilities},
{
staticProperties: {
create: function create() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return this.apply(void 0, args);
},
compose: stampit // infecting
}
}
);
/**
* Infected compose
* @param {...(Composable)} [args] The list of composables.
* @return {Stamp}
*/
function stampit() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var composables = args.filter(isObject)
.map(function (arg) { return isStamp(arg) ? arg : standardiseDescriptor(arg); });
// Calling the standard pure compose function here.
var stamp = compose.apply(this || baseStampit, composables);
var composerFunctions = stamp.compose.deepConfiguration &&
stamp.compose.deepConfiguration.composers;
if (isArray(composerFunctions) && composerFunctions.length > 0) {
var uniqueComposers = [];
for (var i = 0; i < composerFunctions.length; i += 1) {
var composer = composerFunctions[i];
if (isFunction(composer) && !uniqueComposers.includes(composer)) {
uniqueComposers.push(composer);
}
}
stamp.compose.deepConfiguration.composers = uniqueComposers;
if (isStamp(this)) { composables.unshift(this); }
for (var i$1 = 0; i$1 < uniqueComposers.length; i$1 += 1) {
var composer$1 = uniqueComposers[i$1];
var returnedValue = composer$1({stamp: stamp, composables: composables});
stamp = isStamp(returnedValue) ? returnedValue : stamp;
}
}
return stamp;
}
var exportedCompose = stampit.bind(); // bind to 'undefined'
stampit.compose = exportedCompose;
// Setting up the shortcut functions
var stampit$1 = assign(stampit, allUtilities);
exports.methods = methods;
exports.properties = properties;
exports.refs = properties;
exports.props = properties;
exports.initializers = initializers;
exports.init = initializers;
exports.composers = composers;
exports.deepProperties = deepProperties;
exports.deepProps = deepProperties;
exports.staticProperties = staticProperties;
exports.statics = staticProperties;
exports.staticDeepProperties = staticDeepProperties;
exports.deepStatics = staticDeepProperties;
exports.configuration = configuration;
exports.conf = configuration;
exports.deepConfiguration = deepConfiguration;
exports.deepConf = deepConfiguration;
exports.propertyDescriptors = propertyDescriptors;
exports.staticPropertyDescriptors = staticPropertyDescriptors;
exports.compose = exportedCompose;
exports['default'] = stampit$1;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=stampit.umd.js.map