hypel 0.2.1

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 (3) hide show
  1. package/README.md +89 -0
  2. package/hypel.js +130 -0
  3. package/package.json +43 -0
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ hypel
2
+ =====
3
+
4
+ `hypel` creates dom elements with vanilla javascript
5
+ ``` javascript
6
+ div('#app', [
7
+ h1('hello everybody'),
8
+ ul('#bestest-menu', items.map(item => (
9
+ li('#item-'+item.id, attrs(item.id), item.title))))
10
+ ])
11
+ ```
12
+
13
+ `hypel` is used with vdom implementations such as `react` or `inferno`. A compact example,
14
+ ``` javascript
15
+ import hypel from 'hypel'
16
+ import { h } from 'inferno-hyperscript'
17
+ import { render } from 'inferno'
18
+
19
+ const { div, span, a } = hypel(h)
20
+
21
+ render(
22
+ div('#container.two.classes', {
23
+ // note: syntax for classNames, attributes and other
24
+ // details differ depending upon virtual-dom library
25
+ on: { click: () => console.log('go') }
26
+ }, [
27
+ span('.bold', 'This is bold'), ' and this is just normal text ',
28
+ a('.link', { props: { href: "/foo" } }, 'now go places!')
29
+ ]), document.getElementById('container'))
30
+ // <div class="two classes" id="container">
31
+ // <span class="bold">This is bold</span> and this is just
32
+ // normal text <a class="link">now go places!</a>
33
+ // </div>
34
+ ```
35
+
36
+ `hypel` includes a namespace feature that renders className and id attibutes from namespace values as below. Import `hypelns` or `hypelnssvg` to use this feature,
37
+ ``` javascript
38
+ import { hypelns } from 'hypel'
39
+ import { h } from 'inferno-hyperscript'
40
+
41
+ const { div, h1, ul, li } = hypelns(h)
42
+ const ns = { uid: '123' } // use any key namea
43
+ const items = [
44
+ { id: 'item1', title: 'item 1!'},
45
+ { id: 'item2', title: 'item 2!'}]
46
+
47
+ div(ns, '#:uid-app', [
48
+ h1(ns, 'hello everybody'),
49
+ ul(ns, '#:uid-bestest-menu', items.map(item => (
50
+ li(ns, '#:uid-item-'+item.id, item.title))))
51
+ ])
52
+ )
53
+ // <div id="123-app">
54
+ // <h1>hello everybody</h1>
55
+ // <ul id="123-bestest-menu">
56
+ // <li id="123-item-item1">item 1!</li>
57
+ // <li id="123-item-item2">item 2!</li>
58
+ // </ul>
59
+ // </div>
60
+ ```
61
+
62
+ --------------------------------------------
63
+ ### Credit
64
+
65
+ Credit to [Ossi Hanhinen](https://github.com/ohanhi) and his [hyperscript-helpers package.](https://github.com/ohanhi/hyperscript-helpers) This package was created from hyperscrpt-helpers when that package was no longer maintaind and incompatible with the esm-bundling strategy I used.
66
+
67
+ This package adds to the original hyperscript-helpers in these ways,
68
+ * exports an esm module and declares "module" type in the package.json,
69
+ * exports a single file,
70
+ * uses node-native test-runner,
71
+ * examples demonstrate more vdom packages; snabbdom, inferno and react,
72
+ * adds a namespacing feature,
73
+ * adds github ci for tests,
74
+ * smaller package size
75
+
76
+
77
+ [0]: http://www.bumblehead.com "bumblehead"
78
+
79
+ ![scrounge](https://github.com/iambumblehead/scroungejs/raw/main/img/hand.png)
80
+
81
+ (The MIT License)
82
+
83
+ Copyright (c) [Bumblehead][0] <chris@bumblehead.com>
84
+
85
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
86
+
87
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/hypel.js ADDED
@@ -0,0 +1,130 @@
1
+ const isSelectorRe = /^[.#]/
2
+ const nsSepPlainRe = /\//g
3
+ const nsSepEncodeRe = /:/g
4
+ const nsKeyRe = /:[^: -.#]*/g
5
+ const node = h => tagName => (first, ...rest) => {
6
+ if (isSelectorRe.test(first)) {
7
+ return h(tagName + first, ...rest)
8
+ } else if (typeof first === 'undefined') {
9
+ return h(tagName)
10
+ } else {
11
+ return h(tagName, first, ...rest)
12
+ }
13
+ }
14
+
15
+ // The tag names are verified against html-tag-names in the tests
16
+ // See https://github.com/ohanhi/hyperscript-helpers/issues/34 for the reason
17
+ // why the tags aren't simply required from html-tag-names
18
+ const TAG_NAMES = [
19
+ 'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside',
20
+ 'audio', 'b', 'base', 'basefont', 'bdi', 'bdo', 'bgsound', 'big', 'blink',
21
+ 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite',
22
+ 'code', 'col', 'colgroup', 'command', 'content', 'data', 'datalist', 'dd',
23
+ 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element', 'em',
24
+ 'embed', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form',
25
+ 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header',
26
+ 'hgroup', 'hr', 'html', 'i', 'iframe', 'image', 'img', 'input', 'ins',
27
+ 'isindex', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'listing',
28
+ 'main', 'map', 'mark', 'marquee', 'math', 'menu', 'menuitem', 'meta',
29
+ 'meter', 'multicol', 'nav', 'nextid', 'nobr', 'noembed', 'noframes',
30
+ 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
31
+ 'picture', 'plaintext', 'pre', 'progress', 'q', 'rb', 'rbc', 'rp', 'rt',
32
+ 'rtc', 'ruby', 's', 'samp', 'script', 'search', 'section', 'select', 'shadow',
33
+ 'slot', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style',
34
+ 'sub', 'summary', 'sup', 'svg', 'table', 'tbody', 'td', 'template',
35
+ 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u',
36
+ 'ul', 'var', 'video', 'wbr', 'xmp'
37
+ ]
38
+
39
+ // https://www.w3.org/TR/SVG/eltindex.html
40
+ // The tag names are verified against svg-tag-names in the tests
41
+ // See https://github.com/ohanhi/hyperscript-helpers/issues/34 for the reason
42
+ // why the tags aren't simply required from svg-tag-names
43
+ const TAG_NAMESSVG = [
44
+ 'a', 'altGlyph', 'altGlyphDef', 'altGlyphItem', 'animate', 'animateColor',
45
+ 'animateMotion', 'animateTransform', 'animation', 'audio', 'canvas',
46
+ 'circle', 'clipPath', 'color-profile', 'cursor', 'defs', 'desc', 'discard',
47
+ 'ellipse', 'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite',
48
+ 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap',
49
+ 'feDistantLight', 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG',
50
+ 'feFuncR', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode',
51
+ 'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting',
52
+ 'feSpotLight', 'feTile', 'feTurbulence', 'filter', 'font', 'font-face',
53
+ 'font-face-format', 'font-face-name', 'font-face-src', 'font-face-uri',
54
+ 'foreignObject', 'g', 'glyph', 'glyphRef', 'handler', 'hatch', 'hatchpath',
55
+ 'hkern', 'iframe', 'image', 'line', 'linearGradient', 'listener', 'marker',
56
+ 'mask', 'mesh', 'meshgradient', 'meshpatch', 'meshrow', 'metadata',
57
+ 'missing-glyph', 'mpath', 'path', 'pattern', 'polygon', 'polyline',
58
+ 'prefetch', 'radialGradient', 'rect', 'script', 'set', 'solidColor',
59
+ 'solidcolor', 'stop', 'style', 'svg', 'switch', 'symbol', 'tbreak', 'text',
60
+ 'textArea', 'textPath', 'title', 'tref', 'tspan', 'unknown', 'use', 'video',
61
+ 'view', 'vkern'
62
+ ]
63
+
64
+ const hypel = h => {
65
+ const createTag = node(h)
66
+
67
+ return TAG_NAMES.reduce((accum, tag) => (
68
+ accum[tag] = accum[
69
+ tag.charAt(0).toUpperCase() + tag.slice(1)] = createTag(tag),
70
+ accum
71
+ ), { isSelector: n => isSelectorRe.test(n), createTag, TAG_NAMES })
72
+ }
73
+
74
+ const hypelsvg = h => {
75
+ const createTag = hypel(h).createTag
76
+
77
+ return TAG_NAMESSVG.reduce((accum, tag) => (
78
+ accum[tag] = createTag(tag),
79
+ accum
80
+ ), {})
81
+ }
82
+
83
+ // var div = h('div.hello/world#hello/world');
84
+ // div.properties.className; // helloa
85
+ // div.properties.id; // hello
86
+ const encodeid = idstr => idstr.replace(nsSepPlainRe, ':')
87
+ const decodeid = idstr => idstr.replace(nsSepEncodeRe, '/')
88
+ const getoptsclassidstr = (opts, classidstr) => (
89
+ encodeid(classidstr.replace(nsKeyRe, m => (
90
+ m = m.slice(1),
91
+ m = m in opts ? String(opts[m]) : m,
92
+ m
93
+ ))))
94
+
95
+ const buildoptfns = helperfns => helperfns.TAG_NAMES.reduce((hhh, cur) => (
96
+ hhh[cur] = (...args) => {
97
+ const newargs = typeof args[1] === 'string'
98
+ ? [ getoptsclassidstr(args[0], args[1]), ...args.slice(2) ]
99
+ : args.slice(1)
100
+
101
+ return helperfns[cur](...newargs)
102
+ },
103
+ hhh
104
+ ), {})
105
+
106
+ const buildhelper = helpers => h => {
107
+ const helperobj = helpers(h)
108
+ const namespace = buildoptfns(helperobj)
109
+ const hhopts = opts => helperobj.TAG_NAMES.reduce((hhopts, cur) => (
110
+ hhopts[cur] = (...args) => namespace[cur](opts, ...args),
111
+ hhopts
112
+ ), {})
113
+
114
+ hhopts.encodeid = encodeid
115
+ hhopts.decodeid = decodeid
116
+
117
+ return helperobj.TAG_NAMES.reduce((hhoptsfn, tagname) => (
118
+ hhoptsfn[tagname] = namespace[tagname],
119
+ hhoptsfn
120
+ ), hhopts)
121
+ }
122
+
123
+ const hypelns = buildhelper(hypel)
124
+
125
+ export {
126
+ hypel as default,
127
+ hypel,
128
+ hypelsvg,
129
+ hypelns
130
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "hypel",
3
+ "type": "module",
4
+ "version": "0.2.1",
5
+ "license": "MIT",
6
+ "readmeFilename": "README.md",
7
+ "description": "Terse syntax for hyperscript",
8
+ "author": "Chris <chris@bumblehead.com>",
9
+ "main": "hypel.js",
10
+ "exports": {
11
+ "import": "./hypel.js"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/iambumblehead/hypel.git"
16
+ },
17
+ "files": [
18
+ "./hypel.js"
19
+ ],
20
+ "keywords": [
21
+ "hyperscript-helpers",
22
+ "virtual-hyperscript",
23
+ "virtual-dom",
24
+ "hyperscript",
25
+ "dynamic id",
26
+ "dynamic class"
27
+ ],
28
+ "devDependencies": {
29
+ "inferno-hyperscript": "8.2.2",
30
+ "inferno-server": "8.2.2",
31
+ "html-tag-names": "^2.1.0",
32
+ "hyperscript": "^2.0.2",
33
+ "snabbdom": "3.5.1",
34
+ "eslint": "^8.51.0",
35
+ "jsdom": "^22.1.0",
36
+ "react": "^18.2.0"
37
+ },
38
+ "scripts" : {
39
+ "lint": "eslint .",
40
+ "test" : "node --test",
41
+ "prepublishOnly": "npm run lint && npm test"
42
+ }
43
+ }