arche 0.3.10 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Arche v0.3.10
2
+ * Arche v1.0.0
3
3
  * https://github.com/richytong/arche
4
- * (c) 2020-2023 Richard Tong
4
+ * (c) 2026 Richard Tong
5
5
  * Arche may be freely distributed under the MIT license.
6
6
  */
7
7
 
@@ -86,27 +86,37 @@ const memoizeCappedWithResolver = function (func, cap, resolver) {
86
86
  *
87
87
  * @synopsis
88
88
  * ```coffeescript [specscript]
89
- * Element = Object
90
- *
91
- * var type string|function,
92
- * props Object,
93
- * children string|Object|Array<string|Object>,
94
- * element Element,
95
- * creator { createElement: (type, props?, children?)=>element },
89
+ * creator {
90
+ * createElement: (
91
+ * elementType string,
92
+ * props? object,
93
+ * children? Array<creator.Element>
94
+ * )=>creator.Element
95
+ * }
96
96
  *
97
- * creatorCreateElement(creator, type, props, children) -> element
97
+ * creatorCreateElement(
98
+ * creator,
99
+ * elementType string|function,
100
+ * propsOrTextOrChildren object|string|Array,
101
+ * textOrChildren string|Array,
102
+ * ) -> creator.Element
98
103
  * ```
99
104
  */
100
- const creatorCreateElement = function (creator, type, props, children) {
105
+ function creatorCreateElement(
106
+ creator,
107
+ elementType,
108
+ propsOrTextOrChildren,
109
+ textOrChildren
110
+ ) {
101
111
  if (creator.createElement.length == 1) {
102
- const element = creator.createElement(type) // document.createElement
103
- for (const key in props) {
104
- elementSetAttribute(element, key, props[key])
112
+ const element = creator.createElement(elementType) // document.createElement
113
+ for (const key in propsOrTextOrChildren) {
114
+ elementSetAttribute(element, key, propsOrTextOrChildren[key])
105
115
  }
106
- const childrenLength = children.length
116
+ const childrenLength = textOrChildren.length
107
117
  let childrenIndex = -1
108
118
  while (++childrenIndex < childrenLength) {
109
- const child = children[childrenIndex]
119
+ const child = textOrChildren[childrenIndex]
110
120
  if (typeof child == 'string') {
111
121
  element.appendChild(creator.createTextNode(child))
112
122
  } else {
@@ -115,7 +125,312 @@ const creatorCreateElement = function (creator, type, props, children) {
115
125
  }
116
126
  return element
117
127
  }
118
- return creator.createElement(type, props, ...children) // React.createElement
128
+
129
+ return creator.createElement(
130
+ elementType,
131
+ propsOrTextOrChildren,
132
+ ...textOrChildren
133
+ )
134
+ }
135
+
136
+ /**
137
+ * @name CreatorElement
138
+ *
139
+ * @synopsis
140
+ * ```coffeescript [specscript]
141
+ * CreatorElement(
142
+ * creator {
143
+ * createElement: (
144
+ * elementType string,
145
+ * props? object,
146
+ * children? Array<creator.Element>
147
+ * )=>creator.Element
148
+ * },
149
+ * elementType string,
150
+ * propsOrTextOrChildren object|string|Array,
151
+ * textOrChildren string|Array,
152
+ * )
153
+ * -> creator.Element
154
+ * ```
155
+ */
156
+ function CreatorElement(
157
+ creator,
158
+ elementType,
159
+ propsOrTextOrChildren,
160
+ textOrChildren
161
+ ) {
162
+ if (isArray(propsOrTextOrChildren)) {
163
+ return creatorCreateElement(
164
+ creator,
165
+ elementType,
166
+ {},
167
+ propsOrTextOrChildren
168
+ )
169
+ }
170
+
171
+ if (typeof propsOrTextOrChildren == 'string') {
172
+ return creatorCreateElement(
173
+ creator,
174
+ elementType,
175
+ {},
176
+ [propsOrTextOrChildren]
177
+ )
178
+ }
179
+
180
+ if (isArray(textOrChildren)) {
181
+ return creatorCreateElement(
182
+ creator,
183
+ elementType,
184
+ propsOrTextOrChildren,
185
+ textOrChildren
186
+ )
187
+ }
188
+
189
+ if (textOrChildren == null) {
190
+ return creatorCreateElement(
191
+ creator,
192
+ elementType,
193
+ propsOrTextOrChildren,
194
+ []
195
+ )
196
+ }
197
+
198
+ return creatorCreateElement(
199
+ creator,
200
+ elementType,
201
+ propsOrTextOrChildren,
202
+ [textOrChildren]
203
+ )
204
+ }
205
+
206
+
207
+ /**
208
+ * @name __CreatorElement
209
+ *
210
+ * @synopsis
211
+ * ```coffeescript [specscript]
212
+ * creator {
213
+ * createElement: (
214
+ * elementType string,
215
+ * props? object,
216
+ * children? Array<creator.Element>
217
+ * )=>creator.Element
218
+ * }
219
+ *
220
+ * __CreatorElement(creator elementType string) -> _Element function
221
+ *
222
+ * _Element(
223
+ * propsOrTextOrChildren object|string|Array,
224
+ * textOrChildren string|Array,
225
+ * ) -> creator.Element
226
+ * ```
227
+ */
228
+ function __CreatorElement(creator, elementType) {
229
+ return function _CreatorElement(propsOrTextOrChildren, textOrChildren) {
230
+ return CreatorElement(
231
+ creator,
232
+ elementType,
233
+ propsOrTextOrChildren,
234
+ textOrChildren
235
+ )
236
+ }
237
+ }
238
+
239
+ /**
240
+ * @name _CreatorElement
241
+ *
242
+ * @synopsis
243
+ * ```coffeescript [specscript]
244
+ * args Array
245
+ *
246
+ * creator {
247
+ * createElement: (
248
+ * elementType string,
249
+ * props? object,
250
+ * children? Array<creator.Element>
251
+ * )=>creator.Element
252
+ * }
253
+ *
254
+ * _CreatorElement(creator) -> _Element (...args)=>creator.Element
255
+ * ```
256
+ */
257
+ function _CreatorElement(creator) {
258
+ return function _Element(...args) {
259
+ if (args.length == 1) {
260
+ return __CreatorElement(creator, args[0])
261
+ }
262
+ return CreatorElement(creator, ...args)
263
+ }
264
+ }
265
+
266
+ /**
267
+ * @name $__StyledCreatorElement
268
+ *
269
+ * @synopsis
270
+ * ```coffeescript [specscript]
271
+ * creator {
272
+ * createElement: (
273
+ * elementType string,
274
+ * props? object,
275
+ * children? Array<creator.Element>
276
+ * )=>creator.Element
277
+ * }
278
+ *
279
+ * $__StyledCreatorElement(
280
+ * creator,
281
+ * options {
282
+ * styled Styled,
283
+ * styledMemoizationCap number,
284
+ * }
285
+ * ) -> _StyledCreatorElement function
286
+ *
287
+ * _StyledCreatorElement(elementType string) -> _StyledElement function
288
+ *
289
+ * _StyledElement(
290
+ * propsOrTextOrChildren object|string|Array,
291
+ * textOrChildren string|Array,
292
+ * ) -> creator.Element
293
+ * ```
294
+ */
295
+ function $__StyledCreatorElement(creator, options) {
296
+ const { styled, styledMemoizationCap } = options
297
+ return function _StyledCreatorElement(elementType) {
298
+ const styledComponent = memoizeCappedWithResolver(
299
+ styled[elementType],
300
+ styledMemoizationCap,
301
+ array => array[0],
302
+ )
303
+
304
+ return function _StyledElement(propsOrTextOrChildren, textOrChildren) {
305
+ if (isArray(propsOrTextOrChildren)) {
306
+ return creatorCreateElement(
307
+ creator,
308
+ elementType,
309
+ {},
310
+ propsOrTextOrChildren
311
+ )
312
+ }
313
+
314
+ if (typeof propsOrTextOrChildren == 'string') {
315
+ return creatorCreateElement(
316
+ creator,
317
+ elementType,
318
+ {},
319
+ [propsOrTextOrChildren]
320
+ )
321
+ }
322
+
323
+ if (isArray(textOrChildren)) {
324
+ if (propsOrTextOrChildren == null || propsOrTextOrChildren.css == null) {
325
+ return creatorCreateElement(
326
+ creator,
327
+ elementType,
328
+ propsOrTextOrChildren,
329
+ textOrChildren
330
+ )
331
+ }
332
+ const { css, ...props } = propsOrTextOrChildren
333
+ return creatorCreateElement(
334
+ creator,
335
+ styledComponent([css]),
336
+ props,
337
+ textOrChildren
338
+ )
339
+ }
340
+
341
+ if (textOrChildren == null) {
342
+ if (propsOrTextOrChildren == null || propsOrTextOrChildren.css == null) {
343
+ return creatorCreateElement(
344
+ creator,
345
+ elementType,
346
+ propsOrTextOrChildren,
347
+ []
348
+ )
349
+ }
350
+ const { css, ...props } = propsOrTextOrChildren
351
+ return creatorCreateElement(creator, styledComponent([css]), props, [])
352
+ }
353
+
354
+ if (propsOrTextOrChildren == null || propsOrTextOrChildren.css == null) {
355
+ return creatorCreateElement(
356
+ creator,
357
+ elementType,
358
+ propsOrTextOrChildren,
359
+ [textOrChildren]
360
+ )
361
+ }
362
+
363
+ const { css, ...props } = propsOrTextOrChildren
364
+ return creatorCreateElement(
365
+ creator,
366
+ styledComponent([css]),
367
+ props,
368
+ [textOrChildren]
369
+ )
370
+ }
371
+ }
372
+ }
373
+
374
+ /**
375
+ * @name __assignElementNames
376
+ *
377
+ * @synopsis
378
+ * ```coffeescript [specscript]
379
+ * creator {
380
+ * createElement: (
381
+ * elementType string,
382
+ * props? object,
383
+ * children? Array<creator.Element>
384
+ * )=>creator.Element
385
+ * }
386
+ *
387
+ * __assignElementNames(CreatorElement creator.Element) -> ()
388
+ * ```
389
+ */
390
+ function __assignElementNames(CreatorElement) {
391
+ CreatorElement.A = CreatorElement('a')
392
+ CreatorElement.P = CreatorElement('p')
393
+ CreatorElement.B = CreatorElement('b')
394
+ CreatorElement.Q = CreatorElement('q')
395
+ CreatorElement.I = CreatorElement('i')
396
+ CreatorElement.Ul = CreatorElement('ul')
397
+ CreatorElement.Ol = CreatorElement('ol')
398
+ CreatorElement.Li = CreatorElement('li')
399
+
400
+ CreatorElement.H1 = CreatorElement('h1')
401
+ CreatorElement.H2 = CreatorElement('h2')
402
+ CreatorElement.H3 = CreatorElement('h3')
403
+ CreatorElement.H4 = CreatorElement('h4')
404
+ CreatorElement.H5 = CreatorElement('h5')
405
+ CreatorElement.H6 = CreatorElement('h6')
406
+ CreatorElement.Hr = CreatorElement('hr')
407
+ CreatorElement.Br = CreatorElement('br')
408
+
409
+ CreatorElement.Script = CreatorElement('script')
410
+ CreatorElement.Html = CreatorElement('html')
411
+ CreatorElement.Body = CreatorElement('body')
412
+ CreatorElement.Nav = CreatorElement('nav')
413
+ CreatorElement.Section = CreatorElement('section')
414
+ CreatorElement.Article = CreatorElement('article')
415
+ CreatorElement.Footer = CreatorElement('footer')
416
+ CreatorElement.Span = CreatorElement('span')
417
+ CreatorElement.Div = CreatorElement('div')
418
+ CreatorElement.Img = CreatorElement('img')
419
+ CreatorElement.Video = CreatorElement('video')
420
+
421
+ CreatorElement.Form = CreatorElement('form')
422
+ CreatorElement.Fieldset = CreatorElement('fieldset')
423
+ CreatorElement.Input = CreatorElement('input')
424
+ CreatorElement.Label = CreatorElement('label')
425
+ CreatorElement.Textarea = CreatorElement('textarea')
426
+ CreatorElement.Select = CreatorElement('select')
427
+ CreatorElement.Option = CreatorElement('option')
428
+
429
+ CreatorElement.Button = CreatorElement('button')
430
+ CreatorElement.Iframe = CreatorElement('iframe')
431
+ CreatorElement.Blockquote = CreatorElement('blockquote')
432
+ CreatorElement.Code = CreatorElement('code')
433
+ CreatorElement.Pre = CreatorElement('pre')
119
434
  }
120
435
 
121
436
  /**
@@ -125,43 +440,45 @@ const creatorCreateElement = function (creator, type, props, children) {
125
440
  * ```coffeescript [specscript]
126
441
  * Element = Object
127
442
  *
128
- * var type string|function,
129
- * props Object,
130
- * children string|Object|Array<string|Object>,
131
- * element Element,
132
- * creator { createElement: (type, props?, children?)=>element },
133
- * rootElement type=>((props, children?)|children)=>element {
134
- * Script: ((props, children?)|children)=>element,
135
- * Html: ((props, children?)|children)=>element,
136
- * Body: (props, children?)|children)=>element,
137
- * Section: (props, children?)|children)=>element,
138
- * Article: (props, children?)|children)=>element,
139
- * Span: (props, children?)|children)=>element,
140
- * Div: (props, children?)|children)=>element,
141
- * Img: (props, children?)|children)=>element,
142
- * H1: (props, children?)|children)=>element,
143
- * H2: (props, children?)|children)=>element,
144
- * H3: (props, children?)|children)=>element,
145
- * H4: (props, children?)|children)=>element,
146
- * H5: (props, children?)|children)=>element,
147
- * H6: (props, children?)|children)=>element,
148
- *
149
- * A: (props, children?)|children)=>element,
150
- * P: (props, children?)|children)=>element,
151
- * B: (props, children?)|children)=>element,
152
- * Q: (props, children?)|children)=>element,
153
- * I: (props, children?)|children)=>element,
154
- * Ul: (props, children?)|children)=>element,
155
- * Ol: (props, children?)|children)=>element,
156
- * Li: (props, children?)|children)=>element,
157
- * Textarea: (props, children?)|children)=>element,
158
- * Button: (props, children?)|children)=>element,
159
- * Iframe: (props, children?)|children)=>element,
160
- * Blockquote: (props, children?)|children)=>element,
161
- * Br: (props, children?)|children)=>element,
162
- * Code: (props, children?)|children)=>element,
163
- * Pre: (props, children?)|children)=>element,
164
- * }
443
+ * type string|function
444
+ * props Object
445
+ * children string|Object|Array<string|Object>
446
+ * element Element
447
+ * creator {
448
+ * createElement: (type, props?, children?)=>element,
449
+ * }
450
+ * rootElement type=>((props, children?)|children)=>element {
451
+ * Script: ((props, children?)|children)=>element,
452
+ * Html: ((props, children?)|children)=>element,
453
+ * Body: (props, children?)|children)=>element,
454
+ * Section: (props, children?)|children)=>element,
455
+ * Article: (props, children?)|children)=>element,
456
+ * Span: (props, children?)|children)=>element,
457
+ * Div: (props, children?)|children)=>element,
458
+ * Img: (props, children?)|children)=>element,
459
+ * H1: (props, children?)|children)=>element,
460
+ * H2: (props, children?)|children)=>element,
461
+ * H3: (props, children?)|children)=>element,
462
+ * H4: (props, children?)|children)=>element,
463
+ * H5: (props, children?)|children)=>element,
464
+ * H6: (props, children?)|children)=>element,
465
+ *
466
+ * A: (props, children?)|children)=>element,
467
+ * P: (props, children?)|children)=>element,
468
+ * B: (props, children?)|children)=>element,
469
+ * Q: (props, children?)|children)=>element,
470
+ * I: (props, children?)|children)=>element,
471
+ * Ul: (props, children?)|children)=>element,
472
+ * Ol: (props, children?)|children)=>element,
473
+ * Li: (props, children?)|children)=>element,
474
+ * Textarea: (props, children?)|children)=>element,
475
+ * Button: (props, children?)|children)=>element,
476
+ * Iframe: (props, children?)|children)=>element,
477
+ * Blockquote: (props, children?)|children)=>element,
478
+ * Br: (props, children?)|children)=>element,
479
+ * Code: (props, children?)|children)=>element,
480
+ * Pre: (props, children?)|children)=>element,
481
+ * }
165
482
  *
166
483
  * Arche(creator, options {
167
484
  * styled?: Styled,
@@ -250,121 +567,25 @@ const Arche = function (creator, options = {}) {
250
567
  styledMemoizationCap = 1000,
251
568
  } = options
252
569
 
253
- const originalRootElement = type => function creatingElement(arg0, arg1) {
254
- if (isArray(arg0)) {
255
- return creatorCreateElement(creator, type, {}, arg0)
256
- }
257
-
258
- if (typeof arg0 == 'string') {
259
- return creatorCreateElement(creator, type, {}, [arg0])
260
- }
261
-
262
- if (isArray(arg1)) {
263
- return creatorCreateElement(creator, type, arg0, arg1)
264
- }
265
-
266
- if (arg1 == null) {
267
- return creatorCreateElement(creator, type, arg0, [])
268
- }
269
-
270
- return creatorCreateElement(creator, type, arg0, [arg1])
271
- }
272
-
273
- const styledRootElement = type => {
274
- const styledComponent = memoizeCappedWithResolver(
275
- styled[type],
276
- styledMemoizationCap,
277
- array => array[0],
278
- )
570
+ const OriginalCreatorElement = _CreatorElement(creator)
279
571
 
280
- return function creatingStyledElement(arg0, arg1) {
281
- if (isArray(arg0)) {
282
- return creatorCreateElement(creator, type, {}, arg0)
283
- }
284
-
285
- if (typeof arg0 == 'string') {
286
- return creatorCreateElement(creator, type, {}, [arg0])
287
- }
288
-
289
- if (isArray(arg1)) {
290
- if (arg0 == null || arg0.css == null) {
291
- return creatorCreateElement(creator, type, arg0, arg1)
292
- }
293
- const { css, ...props } = arg0
294
- return creatorCreateElement(creator, styledComponent([css]), props, arg1)
295
- }
296
-
297
- if (arg1 == null) {
298
- if (arg0 == null || arg0.css == null) {
299
- return creatorCreateElement(creator, type, arg0, [])
300
- }
301
- const { css, ...props } = arg0
302
- return creatorCreateElement(creator, styledComponent([css]), props, [])
303
- }
304
-
305
- if (arg0 == null || arg0.css == null) {
306
- return creatorCreateElement(creator, type, arg0, [arg1])
307
- }
308
- const { css, ...props } = arg0
309
- return creatorCreateElement(creator, styledComponent([css]), props, [arg1])
310
- }
311
- }
572
+ const StyledCreatorElement = $__StyledCreatorElement(creator, {
573
+ styled,
574
+ styledMemoizationCap,
575
+ })
312
576
 
313
- const rootElement = (
577
+ const CreatorElement = (
314
578
  styled == null
315
- ? originalRootElement
579
+ ? OriginalCreatorElement
316
580
  : type => typeof type == 'string'
317
- ? styledRootElement(type)
318
- : originalRootElement(type)
581
+ ? StyledCreatorElement(type)
582
+ : OriginalCreatorElement(type)
319
583
  )
320
584
 
321
- rootElement.creator = creator
322
-
323
- rootElement.A = rootElement('a')
324
- rootElement.P = rootElement('p')
325
- rootElement.B = rootElement('b')
326
- rootElement.Q = rootElement('q')
327
- rootElement.I = rootElement('i')
328
- rootElement.Ul = rootElement('ul')
329
- rootElement.Ol = rootElement('ol')
330
- rootElement.Li = rootElement('li')
331
-
332
- rootElement.H1 = rootElement('h1')
333
- rootElement.H2 = rootElement('h2')
334
- rootElement.H3 = rootElement('h3')
335
- rootElement.H4 = rootElement('h4')
336
- rootElement.H5 = rootElement('h5')
337
- rootElement.H6 = rootElement('h6')
338
- rootElement.Hr = rootElement('hr')
339
- rootElement.Br = rootElement('br')
340
-
341
- rootElement.Script = rootElement('script')
342
- rootElement.Html = rootElement('html')
343
- rootElement.Body = rootElement('body')
344
- rootElement.Nav = rootElement('nav')
345
- rootElement.Section = rootElement('section')
346
- rootElement.Article = rootElement('article')
347
- rootElement.Footer = rootElement('footer')
348
- rootElement.Span = rootElement('span')
349
- rootElement.Div = rootElement('div')
350
- rootElement.Img = rootElement('img')
351
- rootElement.Video = rootElement('video')
352
-
353
- rootElement.Form = rootElement('form')
354
- rootElement.Fieldset = rootElement('fieldset')
355
- rootElement.Input = rootElement('input')
356
- rootElement.Label = rootElement('label')
357
- rootElement.Textarea = rootElement('textarea')
358
- rootElement.Select = rootElement('select')
359
- rootElement.Option = rootElement('option')
360
-
361
- rootElement.Button = rootElement('button')
362
- rootElement.Iframe = rootElement('iframe')
363
- rootElement.Blockquote = rootElement('blockquote')
364
- rootElement.Code = rootElement('code')
365
- rootElement.Pre = rootElement('pre')
366
-
367
- return rootElement
585
+ CreatorElement.creator = creator
586
+ __assignElementNames(CreatorElement)
587
+
588
+ return CreatorElement
368
589
  }
369
590
 
370
591
  export default Arche