hypel 0.2.4 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +14 -36
  2. package/hypel.js +32 -62
  3. package/package.json +8 -6
package/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  hypel
2
2
  =====
3
3
 
4
+ [![npm][1]][2] [![install size][3]][4]
5
+
6
+ [1]: https://img.shields.io/npm/v/hypel "hypel npm, badge"
7
+ [2]: https://www.npmjs.com/package/hypel "hypel npm, link"
8
+ [3]: https://packagephobia.now.sh/badge?p=hypel "hypel size, badge"
9
+ [4]: https://packagephobia.now.sh/result?p=hypel "hypel size, link"
10
+
4
11
  `hypel` creates dom elements with vanilla javascript
5
- ``` javascript
12
+ ```javascript
6
13
  div('#app', [
7
14
  h1('hello everybody'),
8
15
  ul('#bestest-menu', items.map(item => (
@@ -33,30 +40,11 @@ render(
33
40
  // </div>
34
41
  ```
35
42
 
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!'}]
43
+ ### hook
46
44
 
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>
45
+ `hypel` includes a hook feature, so that tag function arguments can be transformed through a hook. (try it out to see how it works)
46
+ ```javascript
47
+ const tags = hypel(h, args => transformArgs(args))
60
48
  ```
61
49
 
62
50
  --------------------------------------------
@@ -69,21 +57,11 @@ This package adds to the original hyperscript-helpers in these ways,
69
57
  * exports a single file,
70
58
  * uses node-native test-runner,
71
59
  * examples demonstrate more vdom packages; snabbdom, inferno and react,
72
- * adds a namespacing feature,
60
+ * adds a hook feature,
73
61
  * adds github ci for tests,
74
62
  * smaller package size
75
63
 
76
64
 
77
- [0]: http://www.bumblehead.com "bumblehead"
65
+ [0]: https://bumblehead.com "bumblehead"
78
66
 
79
67
  ![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 CHANGED
@@ -1,16 +1,11 @@
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
- }
1
+ const isSelector = str => (
2
+ (str = String(str).charCodeAt(0)), (str === 46 || str === 35))
3
+
4
+ const node = h => tagName => (first, ...rest) => isSelector(first)
5
+ ? h(tagName + first, ...rest)
6
+ : typeof first === 'undefined'
7
+ ? h(tagName)
8
+ : h(tagName, first, ...rest)
14
9
 
15
10
  // The tag names are verified against html-tag-names in the tests
16
11
  // See https://github.com/ohanhi/hyperscript-helpers/issues/34 for the reason
@@ -61,14 +56,33 @@ const TAG_NAMESSVG = [
61
56
  'view', 'vkern'
62
57
  ]
63
58
 
64
- const hypel = h => {
59
+ const toUpperFirst = str => str.charAt(0).toUpperCase() + str.slice(1)
60
+
61
+ // cryptic-looking recursion here allows a tag hook to return another
62
+ // hook and so on, so that tag calls may accumulate data to resolve
63
+ // final hnode,
64
+ // ```javascript
65
+ // span = tags.span({id: '123'})
66
+ // span(':id', 'hello world')
67
+ // // <span id="123">hello world</span>
68
+ // ```
69
+ const hookedTag = (tag, hook, res) => (...args) => {
70
+ res = hook(args)
71
+
72
+ return typeof res === 'function'
73
+ ? hookedTag(tag, res)
74
+ : tag.apply(0, res)
75
+ }
76
+
77
+ const hypel = (h, hook) => {
65
78
  const createTag = node(h)
66
79
 
67
80
  return TAG_NAMES.reduce((accum, tag) => (
68
- accum[tag] = accum[
69
- tag.charAt(0).toUpperCase() + tag.slice(1)] = createTag(tag),
81
+ accum[tag] = accum[toUpperFirst(tag)] = typeof hook === 'function'
82
+ ? (tag => hookedTag(tag, hook))(createTag(tag))
83
+ : createTag(tag),
70
84
  accum
71
- ), { isSelector: n => isSelectorRe.test(n), createTag, TAG_NAMES })
85
+ ), { isSelector, createTag, TAG_NAMES })
72
86
  }
73
87
 
74
88
  const hypelsvg = h => {
@@ -80,52 +94,8 @@ const hypelsvg = h => {
80
94
  ), {})
81
95
  }
82
96
 
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 tryprefix = (opts, classstr, prop) => (
89
- prop = opts[prop + 'prefix'],
90
- prop && prop !== classstr ? prop + '.' + classstr : classstr)
91
- const getoptsclassidstr = (opts, classidstr, s) => (
92
- s = encodeid(classidstr.replace(nsKeyRe, m => (
93
- m = m.slice(1),
94
- // classidstr.charCodeAt(offset + m.length + 1) === charCodeHyphen
95
- tryprefix(opts, m in opts ? String(opts[m]) : m, m)
96
- ))),
97
- s)
98
-
99
- const buildoptfns = helperfns => helperfns.TAG_NAMES.reduce((hhh, cur) => (
100
- hhh[cur] = (...args) => {
101
- const newargs = typeof args[1] === 'string'
102
- ? [ getoptsclassidstr(args[0], args[1]), ...args.slice(2) ]
103
- : args.slice(1)
104
-
105
- return helperfns[cur](...newargs)
106
- },
107
- hhh
108
- ), {})
109
-
110
- const buildhelper = helpers => h => {
111
- const helperobj = helpers(h)
112
- const namespace = buildoptfns(helperobj)
113
- const hhopts = opts => helperobj.TAG_NAMES.reduce((hhopts, cur) => (
114
- hhopts[cur] = (...args) => namespace[cur](opts, ...args),
115
- hhopts
116
- ), { encodeid, decodeid })
117
-
118
- return helperobj.TAG_NAMES.reduce((hhoptsfn, tagname) => (
119
- hhoptsfn[tagname] = namespace[tagname],
120
- hhoptsfn
121
- ), hhopts)
122
- }
123
-
124
- const hypelns = buildhelper(hypel)
125
-
126
97
  export {
127
98
  hypel as default,
128
99
  hypel,
129
- hypelsvg,
130
- hypelns
100
+ hypelsvg
131
101
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hypel",
3
3
  "type": "module",
4
- "version": "0.2.4",
4
+ "version": "0.3.0",
5
5
  "license": "MIT",
6
6
  "readmeFilename": "README.md",
7
7
  "description": "Terse syntax for hyperscript",
@@ -23,16 +23,18 @@
23
23
  "virtual-dom",
24
24
  "hyperscript",
25
25
  "dynamic id",
26
- "dynamic class"
26
+ "dynamic class",
27
+ "inferno",
28
+ "react"
27
29
  ],
28
30
  "devDependencies": {
29
- "inferno-hyperscript": "9.0.3",
30
- "inferno-server": "9.0.3",
31
+ "inferno-hyperscript": "9.0.10",
32
+ "inferno-server": "9.0.10",
31
33
  "html-tag-names": "^2.1.0",
32
34
  "hyperscript": "^2.0.2",
33
- "snabbdom": "3.6.2",
35
+ "snabbdom": "3.6.3",
34
36
  "eslint": "^9.32.0",
35
- "jsdom": "^26.1.0",
37
+ "jsdom": "^27.4.0",
36
38
  "react": "^19.1.1"
37
39
  },
38
40
  "scripts" : {