-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.js
112 lines (94 loc) · 2.99 KB
/
create.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
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(["./transform", "./process"], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("./transform"), require("./process"));
} else {
root["mu-create/create"] = factory(root["mu-create/transform"], root["mu-create/process"]);
}
})(this, function (transform, process) {
var root = this;
var array = Array.prototype;
var slice = array.slice;
var concat = array.concat;
var count = 0;
var prefix = "blueprint-" + count++;
function clean(data) {
return !!data;
}
function identify(result, data) {
data[prefix] = count++;
return result;
}
function anonymize(result, data) {
delete data[prefix];
return result;
}
function unique(data) {
var me = this;
var id = data[prefix];
return me.hasOwnProperty(id)
? false
: me[id] = data;
}
return function configure() {
var rules = slice.call(arguments);
function create() {
var config = this === root ? {} : this;
var result = slice.call(arguments);
var blueprints;
// Flatten & Clean
result = concat.apply(array, result).filter(clean, config);
// Identify
result = result.reduce(identify, result);
// Unique
result = result.filter(unique, {});
// Anonymize
result = result.reduce(anonymize, result);
// Transform
result = result.map(config.transform || transform, config);
// Flatten & Clean
result = blueprints = concat.apply(array, result).filter(clean, config);
// Process
result = result.reduce(process.apply(config, rules), function Constructor() {
var self = this;
(this.constructor.constructors || array).reduce(function (args, c) {
var r = c.apply(self, args);
switch (toString.call(r)) {
case "[object String]":
case "[object Object]":
case "[object Number]":
case "[object Boolean]":
r = [r];
break;
default:
r = r && r.length ? r : args;
}
return r;
}, arguments);
});
result.concat = function () {
var r = concat.apply(blueprints, arguments);
r = r.reduce(identify, r);
r = r.filter(unique, {});
r = r.reduce(anonymize, r);
return r;
};
result.extend = function () {
return create.apply(this, result.concat.apply(this, arguments));
};
return result;
}
create.concat = function () {
var r = concat.apply(rules, arguments);
r = r.reduce(identify, r);
r = r.filter(unique, {});
r = r.reduce(anonymize, r);
return r;
};
create.extend = function () {
return configure.apply(this, create.concat.apply(this, arguments));
};
return create;
}
});