-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
164 lines (141 loc) · 4.53 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
/*
* Create functions that have composable type inferencing.
*
* We export three functions (fn, TypeDefinition, declareProps).
* Whether client-side or server-side, use fn to create components.
* Application developers will use declareProps to create prop types.
* Library authors will use TypeDefinition to add runtime functionality to types.
* We export five modifiers (MOptional, MHide, MClass, MTry, MCast).
*/
// global state used by ST
const Executables = {};
const ST = fn.symbol = Symbol("(st)composable");
// add function to executables
function TypeDefinition(description, constructor, consumer = () => {}) {
const S = Symbol(description)
Executables[S] = consumer
constructor.symbol = S
return [constructor, (o,v=S)=>v==S?o[S]:o[S]=v]
}
// decorate function using executables
function fn(funcName, factoryFunction, ...dependUnions) {
if(typeof funcName == "string")
Object.defineProperty(factoryFunction, "name", { value: funcName || "(st)fallbackFunctionName" })
else {
if(factoryFunction)
dependUnions.unshift(factoryFunction)
factoryFunction = funcName
funcName = factoryFunction.name || "(st)fallbackFunctionName"
}
// // DEBUG MODE
// const st = eval(`({
// ["(st)${funcName}"](props) {
// utilFallbackAssign(props, defaults)
// return factoryFunction(props, props)
// }
// }["(st)${funcName}"])`)
// // RELEASE MODE
Object.defineProperty(st, "name", { value: funcName })
const defaults = {}
st[ST] = {}
for (const propObj of dependUnions)
for (const [name, value] of utilComposeProps(propObj, st[ST]))
for (const S of Object.getOwnPropertySymbols(value))
Executables[S]?.(name, defaults, value)
return st
function st(props) {
utilFallbackAssign(props, defaults)
return factoryFunction(props, props)
}
}
// built-ins
const [Declare, inDeclare] = TypeDefinition("(st)declareProps",
(manyPropDefs = {}) => {
const outObj = {};
for (const [key, prop] of Object.entries(manyPropDefs)) {
let propDef = inDeclare(prop);
if(propDef !== undefined) propDef = prop;
else propDef = utilFlatWrap(prop);
outObj[utilCapitalize(key)] = { [key]: propDef };
}
return outObj;
},
(propName, componentFallbackProps, propValue) => {
if(inFallback(propValue))
componentFallbackProps[propName] = inDeclare(propValue);
}
);
const [Negate] = TypeDefinition("(st)negate",
(funcComposable = () => {}, manyPropDefs = {}) => {
const outObj = Object.assign({}, funcComposable[ST]);
for (const name in manyPropDefs)
delete outObj[name];
return outObj;
}
);
const [Fallback, inFallback] = TypeDefinition("(st)fallback",
(value) => {
const wrapper = utilFlatWrap(value);
inFallback(wrapper, true);
return wrapper;
}
);
const [Brand, inBrand] = TypeDefinition("(st)brand",
(validator) => {
const wrapper = utilFlatWrap(validator);
inBrand(wrapper, true);
return wrapper;
}
);
const constructor = (funcObj, props) => {
for (const key in funcObj)
if(inBrand(funcObj[key]))
return inDeclare(funcObj[key])(props);
};
const [Legacy, inLegacy] = TypeDefinition("(st)legacy",
(like, metadata) => {
const wrapper = utilFlatWrap(like);
inLegacy(wrapper, metadata);
return wrapper;
}
);
// exports
export {
Declare as declareProps,
Fallback as MOptional,
Negate as MHide,
Brand as MClass,
constructor as MTry,
Legacy as MCast,
fn,
TypeDefinition
};
// utils
function utilFlatWrap(object) {
if(inDeclare(object)) return object;
const wrapper = {};
inDeclare(wrapper, object);
return wrapper;
}
function utilComposeProps(propObj, symbols) {
const entries = [];
let props = propObj;
if(ST in propObj)
props = propObj[ST];
else
for (const key of Object.getOwnPropertySymbols(props))
entries.push([key, symbols[key] = props[key]]);
for (const key in props)
entries.push([key, symbols[key] = props[key]]);
return entries;
}
function utilFallbackAssign(target, source) {
for (const key in source)
if(!(key in target))
target[key] = source[key];
return target;
}
function utilCapitalize(string = "") {
const s = string[0].toUpperCase();
return `${s}${string.substr(1)}`;
}