arche 0.2.3 → 0.2.5

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.
Files changed (2) hide show
  1. package/index.mjs +251 -0
  2. package/package.json +1 -1
package/index.mjs ADDED
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Arche v0.2.0
3
+ * https://github.com/richytong/arche
4
+ * (c) 2020-2021 Richard Tong
5
+ * Arche may be freely distributed under the MIT license.
6
+ */
7
+
8
+ const isArray = Array.isArray
9
+
10
+ /**
11
+ * @name elementSetAttribute
12
+ *
13
+ * @synopsis
14
+ * ```coffeescript [specscript]
15
+ * Element = Object
16
+ *
17
+ * var element Element,
18
+ * key string,
19
+ * value string|number|Object,
20
+ *
21
+ * elementSetAttribute(element, key, value) -> element
22
+ * ```
23
+ */
24
+ const elementSetAttribute = function (element, key, value) {
25
+ if (value != null && value.constructor == Object) { // style
26
+ for (const subKey in value) {
27
+ element[key][subKey] = value[subKey]
28
+ }
29
+ } else {
30
+ element.setAttribute(key, value)
31
+ }
32
+ return element
33
+ }
34
+
35
+ /**
36
+ * @name creatorCreateElement
37
+ *
38
+ * @synopsis
39
+ * ```coffeescript [specscript]
40
+ * Element = Object
41
+ *
42
+ * var type string|function,
43
+ * props Object,
44
+ * children string|Object|Array<string|Object>,
45
+ * element Element,
46
+ * creator { createElement: (type, props?, children?)=>element },
47
+ *
48
+ * creatorCreateElement(creator, type, props, children) -> element
49
+ * ```
50
+ */
51
+ const creatorCreateElement = function (creator, type, props, children) {
52
+ if (creator.createElement.length == 1) {
53
+ const element = creator.createElement(type) // document.createElement
54
+ for (const key in props) {
55
+ elementSetAttribute(element, key, props[key])
56
+ }
57
+ const childrenLength = children.length
58
+ let childrenIndex = -1
59
+ while (++childrenIndex < childrenLength) {
60
+ const child = children[childrenIndex]
61
+ if (typeof child == 'string') {
62
+ element.appendChild(creator.createTextNode(child))
63
+ } else {
64
+ element.appendChild(child)
65
+ }
66
+ }
67
+ return element
68
+ }
69
+ return creator.createElement(type, props, ...children) // React.createElement
70
+ }
71
+
72
+ /**
73
+ * @name Arche
74
+ *
75
+ * @synopsis
76
+ * ```coffeescript [specscript]
77
+ * Element = Object
78
+ *
79
+ * var type string|function,
80
+ * props Object,
81
+ * children string|Object|Array<string|Object>,
82
+ * element Element,
83
+ * creator { createElement: (type, props?, children?)=>element },
84
+ * rootElement type=>((props, children?)|children)=>element {
85
+ * Script: ((props, children?)|children)=>element,
86
+ * Html: ((props, children?)|children)=>element,
87
+ * Body: (props, children?)|children)=>element,
88
+ * Section: (props, children?)|children)=>element,
89
+ * Article: (props, children?)|children)=>element,
90
+ * Span: (props, children?)|children)=>element,
91
+ * Div: (props, children?)|children)=>element,
92
+ * Img: (props, children?)|children)=>element,
93
+ * H1: (props, children?)|children)=>element,
94
+ * H2: (props, children?)|children)=>element,
95
+ * H3: (props, children?)|children)=>element,
96
+ * H4: (props, children?)|children)=>element,
97
+ * H5: (props, children?)|children)=>element,
98
+ * H6: (props, children?)|children)=>element,
99
+ *
100
+ * A: (props, children?)|children)=>element,
101
+ * P: (props, children?)|children)=>element,
102
+ * B: (props, children?)|children)=>element,
103
+ * Q: (props, children?)|children)=>element,
104
+ * I: (props, children?)|children)=>element,
105
+ * Ul: (props, children?)|children)=>element,
106
+ * Ol: (props, children?)|children)=>element,
107
+ * Li: (props, children?)|children)=>element,
108
+ * Textarea: (props, children?)|children)=>element,
109
+ * Button: (props, children?)|children)=>element,
110
+ * Iframe: (props, children?)|children)=>element,
111
+ * Blockquote: (props, children?)|children)=>element,
112
+ * Br: (props, children?)|children)=>element,
113
+ * Code: (props, children?)|children)=>element,
114
+ * Pre: (props, children?)|children)=>element,
115
+ * }
116
+ *
117
+ * Arche(creator) -> rootElement
118
+ * ```
119
+ *
120
+ * @description
121
+ * > 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))
122
+ *
123
+ * HTML as JavaScript.
124
+ *
125
+ * ```javascript [playground]
126
+ * const ReactElement = Arche(React)
127
+ * // supply the React library
128
+ *
129
+ * const { Div, H1, P } = ReactElement
130
+ * // some common building blocks are provided on ReactElement
131
+ * // as property functions.
132
+ *
133
+ * const myElement = Div([
134
+ * H1('I am a heading'),
135
+ * P('heyo'),
136
+ * P('lorem ipsum'),
137
+ * ])
138
+ *
139
+ * console.log(myElement)
140
+ * // {
141
+ * // $$typeof: Symbol(react.element),
142
+ * // type: 'div',
143
+ * // props: {
144
+ * // children: [
145
+ * // { $$typeof: Symbol(react.element), type: 'h1', props: { children: 'I am a heading' } },
146
+ * // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
147
+ * // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
148
+ * // ],
149
+ * // },
150
+ * // }
151
+ * ```
152
+ *
153
+ * Create dynamic components with props:
154
+ * ```javascript [playground]
155
+ * const ReactElement = Arche(React)
156
+ * const { Div, P, Button } = ReactElement
157
+ *
158
+ * const UserCard = ReactElement(({
159
+ * firstName, lastName, age,
160
+ * }) => Div([
161
+ * H1(`${firstName} ${lastName}`),
162
+ * P({ style: { color: 'lightgrey' } }, [age]),
163
+ * ]))
164
+ * ```
165
+ *
166
+ * Complete interop with React hooks (converted from [this example](https://reactjs.org/docs/hooks-intro.html)):
167
+ * ```javascript [playground]
168
+ * const ReactElement = Arche(React)
169
+ * const { Div, P, Button } = ReactElement
170
+ * const { useState } = React
171
+ *
172
+ * const Example = ReactElement(() => {
173
+ * const [count, setCount] = useState(0)
174
+ *
175
+ * return Div([
176
+ * P(`You clicked ${count} times`),
177
+ * Button({
178
+ * onClick() {
179
+ * setCount(count + 1)
180
+ * },
181
+ * }, 'Click me'),
182
+ * ])
183
+ * })
184
+ * ```
185
+ */
186
+
187
+ const Arche = function (creator) {
188
+ const rootElement = type => function creatingElement(arg0, arg1) {
189
+ if (isArray(arg0)) {
190
+ return creatorCreateElement(creator, type, {}, arg0)
191
+ }
192
+ if (typeof arg0 == 'string') {
193
+ return creatorCreateElement(creator, type, {}, [arg0])
194
+ }
195
+ if (isArray(arg1)) {
196
+ return creatorCreateElement(creator, type, arg0, arg1)
197
+ }
198
+ if (arg1 == null) {
199
+ return creatorCreateElement(creator, type, arg0, [])
200
+ }
201
+ return creatorCreateElement(creator, type, arg0, [arg1])
202
+ }
203
+
204
+ rootElement.A = rootElement('a')
205
+ rootElement.P = rootElement('p')
206
+ rootElement.B = rootElement('b')
207
+ rootElement.Q = rootElement('q')
208
+ rootElement.I = rootElement('i')
209
+ rootElement.Ul = rootElement('ul')
210
+ rootElement.Ol = rootElement('ol')
211
+ rootElement.Li = rootElement('li')
212
+
213
+ rootElement.H1 = rootElement('h1')
214
+ rootElement.H2 = rootElement('h2')
215
+ rootElement.H3 = rootElement('h3')
216
+ rootElement.H4 = rootElement('h4')
217
+ rootElement.H5 = rootElement('h5')
218
+ rootElement.H6 = rootElement('h6')
219
+ rootElement.Hr = rootElement('hr')
220
+ rootElement.Br = rootElement('br')
221
+
222
+ rootElement.Script = rootElement('script')
223
+ rootElement.Html = rootElement('html')
224
+ rootElement.Body = rootElement('body')
225
+ rootElement.Nav = rootElement('nav')
226
+ rootElement.Section = rootElement('section')
227
+ rootElement.Article = rootElement('article')
228
+ rootElement.Footer = rootElement('footer')
229
+ rootElement.Span = rootElement('span')
230
+ rootElement.Div = rootElement('div')
231
+ rootElement.Img = rootElement('img')
232
+ rootElement.Video = rootElement('video')
233
+
234
+ rootElement.Form = rootElement('form')
235
+ rootElement.Fieldset = rootElement('fieldset')
236
+ rootElement.Input = rootElement('input')
237
+ rootElement.Label = rootElement('label')
238
+ rootElement.Textarea = rootElement('textarea')
239
+ rootElement.Select = rootElement('select')
240
+ rootElement.Option = rootElement('option')
241
+
242
+ rootElement.Button = rootElement('button')
243
+ rootElement.Iframe = rootElement('iframe')
244
+ rootElement.Blockquote = rootElement('blockquote')
245
+ rootElement.Code = rootElement('code')
246
+ rootElement.Pre = rootElement('pre')
247
+
248
+ return rootElement
249
+ }
250
+
251
+ export default Arche
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arche",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "HTML as JavaScript",
5
5
  "author": "Richard Tong",
6
6
  "license": "MIT",