-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.mjs
345 lines (319 loc) · 9.96 KB
/
index.mjs
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
/**
* Arche v0.3.5
* https://github.com/richytong/arche
* (c) 2020-2023 Richard Tong
* Arche may be freely distributed under the MIT license.
*/
const isArray = Array.isArray
/**
* @name elementSetAttribute
*
* @synopsis
* ```coffeescript [specscript]
* Element = Object
*
* var element Element,
* key string,
* value string|number|Object,
*
* elementSetAttribute(element, key, value) -> element
* ```
*/
const elementSetAttribute = function (element, key, value) {
if (value != null && value.constructor == Object) { // style
for (const subKey in value) {
element[key][subKey] = value[subKey]
}
} else {
element.setAttribute(key, value)
}
return element
}
/**
* @name memoizeCappedWithResolver
*
* @synopsis
* ```coffeescript [specscript]
* memoizeCappedWithResolver(
* func function,
* cap number,
* resolver function,
* ) -> memoized function
* ```
*
* @description
* Memoize a function with a resolver. Clear cache when size reaches cap.
*/
const memoizeCappedWithResolver = function (func, cap, resolver) {
const cache = new Map()
const memoized = function (...args) {
if (cache.size > cap) {
cache.clear()
}
const key = resolver(...args)
if (cache.has(key)) {
return cache.get(key)
}
const result = func(...args)
cache.set(key, result)
return result
}
memoized.cache = cache
return memoized
}
/**
* @name creatorCreateElement
*
* @synopsis
* ```coffeescript [specscript]
* Element = Object
*
* var type string|function,
* props Object,
* children string|Object|Array<string|Object>,
* element Element,
* creator { createElement: (type, props?, children?)=>element },
*
* creatorCreateElement(creator, type, props, children) -> element
* ```
*/
const creatorCreateElement = function (creator, type, props, children) {
if (creator.createElement.length == 1) {
const element = creator.createElement(type) // document.createElement
for (const key in props) {
elementSetAttribute(element, key, props[key])
}
const childrenLength = children.length
let childrenIndex = -1
while (++childrenIndex < childrenLength) {
const child = children[childrenIndex]
if (typeof child == 'string') {
element.appendChild(creator.createTextNode(child))
} else {
element.appendChild(child)
}
}
return element
}
return creator.createElement(type, props, ...children) // React.createElement
}
/**
* @name Arche
*
* @synopsis
* ```coffeescript [specscript]
* Element = Object
*
* var type string|function,
* props Object,
* children string|Object|Array<string|Object>,
* element Element,
* creator { createElement: (type, props?, children?)=>element },
* rootElement type=>((props, children?)|children)=>element {
* Script: ((props, children?)|children)=>element,
* Html: ((props, children?)|children)=>element,
* Body: (props, children?)|children)=>element,
* Section: (props, children?)|children)=>element,
* Article: (props, children?)|children)=>element,
* Span: (props, children?)|children)=>element,
* Div: (props, children?)|children)=>element,
* Img: (props, children?)|children)=>element,
* H1: (props, children?)|children)=>element,
* H2: (props, children?)|children)=>element,
* H3: (props, children?)|children)=>element,
* H4: (props, children?)|children)=>element,
* H5: (props, children?)|children)=>element,
* H6: (props, children?)|children)=>element,
*
* A: (props, children?)|children)=>element,
* P: (props, children?)|children)=>element,
* B: (props, children?)|children)=>element,
* Q: (props, children?)|children)=>element,
* I: (props, children?)|children)=>element,
* Ul: (props, children?)|children)=>element,
* Ol: (props, children?)|children)=>element,
* Li: (props, children?)|children)=>element,
* Textarea: (props, children?)|children)=>element,
* Button: (props, children?)|children)=>element,
* Iframe: (props, children?)|children)=>element,
* Blockquote: (props, children?)|children)=>element,
* Br: (props, children?)|children)=>element,
* Code: (props, children?)|children)=>element,
* Pre: (props, children?)|children)=>element,
* }
*
* Arche(creator, options {
* styled?: Styled,
* styledMemoizationCap?: number,
* }) -> rootElement
* ```
*
* @description
* > Arche (/ˈɑːrki/; Ancient Greek: ἀρχή) is a Greek word with primary senses "beginning", "origin" or "source of action" (ἐξ' ἀρχῆς: from the beginning, οr ἐξ' ἀρχῆς λόγος: the original argument), and later "first principle" or "element". ([wikipedia](https://en.wikipedia.org/wiki/Arche))
*
* HTML as JavaScript.
*
* ```javascript [playground]
* const ReactElement = Arche(React)
* // supply the React library
*
* const { Div, H1, P } = ReactElement
* // some common building blocks are provided on ReactElement
* // as property functions.
*
* const myElement = Div([
* H1('I am a heading'),
* P('heyo'),
* P('lorem ipsum'),
* ])
*
* console.log(myElement)
* // {
* // $$typeof: Symbol(react.element),
* // type: 'div',
* // props: {
* // children: [
* // { $$typeof: Symbol(react.element), type: 'h1', props: { children: 'I am a heading' } },
* // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
* // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
* // ],
* // },
* // }
* ```
*
* Create dynamic components with props:
* ```javascript [playground]
* const ReactElement = Arche(React)
* const { Div, P, Button } = ReactElement
*
* const UserCard = ReactElement(({
* firstName, lastName, age,
* }) => Div([
* H1(`${firstName} ${lastName}`),
* P({ style: { color: 'lightgrey' } }, [age]),
* ]))
* ```
*
* Complete interop with React hooks (converted from [this example](https://reactjs.org/docs/hooks-intro.html)):
* ```javascript [playground]
* const ReactElement = Arche(React)
* const { Div, P, Button } = ReactElement
* const { useState } = React
*
* const Example = ReactElement(() => {
* const [count, setCount] = useState(0)
*
* return Div([
* P(`You clicked ${count} times`),
* Button({
* onClick() {
* setCount(count + 1)
* },
* }, 'Click me'),
* ])
* })
* ```
*/
const Arche = function (creator, options = {}) {
const {
styled,
styledMemoizationCap = 1000,
} = options
const originalRootElement = type => function creatingElement(arg0, arg1) {
if (isArray(arg0)) {
return creatorCreateElement(creator, type, {}, arg0)
}
if (typeof arg0 == 'string') {
return creatorCreateElement(creator, type, {}, [arg0])
}
if (isArray(arg1)) {
return creatorCreateElement(creator, type, arg0, arg1)
}
if (arg1 == null) {
return creatorCreateElement(creator, type, arg0, [])
}
return creatorCreateElement(creator, type, arg0, [arg1])
}
const styledRootElement = type => {
const styledComponent = memoizeCappedWithResolver(
styled[type],
styledMemoizationCap,
array => array[0],
)
return function creatingStyledElement(arg0, arg1) {
if (isArray(arg0)) {
return creatorCreateElement(creator, type, {}, arg0)
}
if (typeof arg0 == 'string') {
return creatorCreateElement(creator, type, {}, [arg0])
}
if (isArray(arg1)) {
if (arg0 == null || arg0.css == null) {
return creatorCreateElement(creator, type, arg0, arg1)
}
const { css, ...props } = arg0
return creatorCreateElement(creator, styledComponent([css]), props, arg1)
}
if (arg1 == null) {
if (arg0 == null || arg0.css == null) {
return creatorCreateElement(creator, type, arg0, [])
}
const { css, ...props } = arg0
return creatorCreateElement(creator, styledComponent([css]), props, [])
}
if (arg0 == null || arg0.css == null) {
return creatorCreateElement(creator, type, arg0, [arg1])
}
const { css, ...props } = arg0
return creatorCreateElement(creator, styledComponent([css]), props, [arg1])
}
}
const rootElement = (
styled == null
? originalRootElement
: type => typeof type == 'string'
? styledRootElement(type)
: originalRootElement(type)
)
rootElement.A = rootElement('a')
rootElement.P = rootElement('p')
rootElement.B = rootElement('b')
rootElement.Q = rootElement('q')
rootElement.I = rootElement('i')
rootElement.Ul = rootElement('ul')
rootElement.Ol = rootElement('ol')
rootElement.Li = rootElement('li')
rootElement.H1 = rootElement('h1')
rootElement.H2 = rootElement('h2')
rootElement.H3 = rootElement('h3')
rootElement.H4 = rootElement('h4')
rootElement.H5 = rootElement('h5')
rootElement.H6 = rootElement('h6')
rootElement.Hr = rootElement('hr')
rootElement.Br = rootElement('br')
rootElement.Script = rootElement('script')
rootElement.Html = rootElement('html')
rootElement.Body = rootElement('body')
rootElement.Nav = rootElement('nav')
rootElement.Section = rootElement('section')
rootElement.Article = rootElement('article')
rootElement.Footer = rootElement('footer')
rootElement.Span = rootElement('span')
rootElement.Div = rootElement('div')
rootElement.Img = rootElement('img')
rootElement.Video = rootElement('video')
rootElement.Form = rootElement('form')
rootElement.Fieldset = rootElement('fieldset')
rootElement.Input = rootElement('input')
rootElement.Label = rootElement('label')
rootElement.Textarea = rootElement('textarea')
rootElement.Select = rootElement('select')
rootElement.Option = rootElement('option')
rootElement.Button = rootElement('button')
rootElement.Iframe = rootElement('iframe')
rootElement.Blockquote = rootElement('blockquote')
rootElement.Code = rootElement('code')
rootElement.Pre = rootElement('pre')
return rootElement
}
export default Arche