ajo 0.0.9 → 0.0.10
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/index.min.js +1 -0
- package/package.json +6 -3
- package/index.js +0 -198
- package/index.test.js +0 -63
package/index.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ajo=(()=>{var d=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var I=(e,n)=>{for(var t in n)d(e,t,{get:n[t],enumerable:!0})},K=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of O(n))!W.call(e,i)&&i!==t&&d(e,i,{get:()=>n[i],enumerable:!(r=F(n,i))||r.enumerable});return e};var L=e=>K(d({},"__esModule",{value:!0}),e);var re={};I(re,{Fragment:()=>Z,cleanup:()=>J,clx:()=>P,component:()=>q,consume:()=>G,h:()=>A,intercept:()=>H,keb:()=>R,propagate:()=>E,provide:()=>D,refresh:()=>j,render:()=>c,stx:()=>Q});const Z=({children:e})=>e,A=(e,n,...t)=>(t=t.length==0?null:t.length==1?t[0]:t,{children:t,...n,nodeName:e}),q=e=>({is:n,key:t,host:r,ref:i,...l})=>A(n??e.is??"div",{key:t,...e.host,...r,skip:!0,ref:o=>(j(o,l,e),f(i)&&i(o))}),c=(e,n)=>{let t=n.firstChild,r,i=x.get(n);for(e of m(e,n)){if(typeof e=="string"){for(r=t;r&&r.nodeType!=3;r=r.nextSibling);r?r.data!==e&&(r.data=e):r=document.createTextNode(e)}else if(e instanceof Node)r=e;else{const{key:l,nodeName:o,skip:M,block:p,children:z,ref:b,...B}=e;if(!(l!=null&&(r=i?.get(l))))for(r=t;r&&r.localName!=o;r=r.nextSibling);r||=document.createElement(o),l!=null&&(i||=x.set(n,new Map)).set(l,r),ne(B,r),!(M||p!=null&&U(S.get(r),S.set(r,p)))&&c(z,r),f(b)&&b(r)}r===t?t=t.nextSibling:te(n,t,r)}for(;t;){const l=t.nextSibling;n.removeChild(t).nodeType==1&&C(t),t=l}},j=(e,n,t)=>{try{t&&!f(t)&&s("Setup",t,u),n=n?v.set(e,{...t?.props,...n}):v.get(e)??{},c((N.get(e)??N.set(e,t(n,e)))(n,e),e)}catch(r){E(e,r)}},D=(e,n,t)=>(g.get(e)??g.set(e,new Map)).set(n,t),G=(e,n,t)=>{let r;for(;e;){if((r=g.get(e))&&r.has(n))return r.get(n);e=e.parentNode}return t},H=(e,n)=>{f(n)||s("Interceptor",n,u),$.set(e,n)},E=(e,n)=>{for(let t;e;e=e.parentNode)if(t=$.get(e))return c(e,t(n));throw n},J=(e,n)=>{f(n)||s("Cleaner",n,u),(y.get(e)??y.set(e,new Set)).add(n)},P=e=>T(e).filter(n=>e[n]).join(" ")||null,Q=e=>Y(e).map(n=>n.join(":")).join(";")||null,R=e=>T(e).reduce((n,t)=>(n[t.replace(h,ee).toLowerCase()]=e[t],n),{}),a=()=>{const e=new WeakMap,{set:n}=e;return e.set=(t,r)=>(n.call(e,t,r),r),e},s=(e,n,t)=>{throw new TypeError(`Expected ${e} to be of type ${t}, got ${typeof n} instead`)},U=(e,n)=>e===n||w(e)&&w(n)&&e.length==n.length&&e.every((t,r)=>t===n[r]),V=(e,{name:n,value:t})=>(e[n]=t,e),X=e=>_(e).reduce(V,{}),f=e=>typeof e==u,{keys:T,entries:Y}=Object,{isArray:w,from:_}=Array,u="function",h=/([a-z0-9])([A-Z])/g,ee="$1-$2",x=a(),S=a(),v=a(),N=a(),g=a(),$=a(),y=a(),k=a(),m=function*(e,n){let t,r="";for(e of f(e?.[Symbol.iterator])?e:[e])if(!(e==null||(t=typeof e)=="boolean")){if(t=="string"||t=="number"){r+=e;continue}if("nodeName"in Object(e)){if(f(e.nodeName)){yield*m(e.nodeName(e,n),n);continue}r&&(yield r,r=""),yield e;continue}f(e[Symbol.iterator])?yield*m(e,n):r+=e}r&&(yield r)},ne=(e,n)=>{const t=k.get(n)??(n.hasAttributes()?X(n.attributes):{});for(const r in{...t,...e}){let i=e[r];if(i!==t[r]){if(r.startsWith("set:")){n[r.slice(4)]=i;continue}if(i===!0)i="";else if(i==null||i===!1){n.removeAttribute(r);continue}n.setAttribute(r,i)}}k.set(n,e)},te=(e,n,t)=>{if(t.contains?.(document.activeElement)){const r=t.nextSibling;for(;n&&n!==t;){const i=n.nextSibling;e.insertBefore(n,r),n=i}}else e.insertBefore(t,n)},C=e=>{for(const n of e.children)C(n);for(const n of y.get(e)??[])n(e)};return L(re);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ajo",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "ajo is a JavaScript view library for building user interfaces",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "index.js",
|
|
@@ -12,12 +12,15 @@
|
|
|
12
12
|
"browser": "./index.min.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.min.js"
|
|
17
|
+
],
|
|
15
18
|
"scripts": {
|
|
16
19
|
"build": "npm run build:require && npm run build:browser",
|
|
17
20
|
"build:require": "esbuild --format=cjs --out-extension:.js=.cjs --outdir=. index.js",
|
|
18
21
|
"build:browser": "esbuild --format=iife --out-extension:.js=.min.js --outdir=. --global-name=ajo --minify index.js",
|
|
19
|
-
"release": "npm
|
|
20
|
-
"test": "uvu"
|
|
22
|
+
"release": "npm test && git commit -am \"$npm_package_version\" && git tag $npm_package_version && git push && git push --tags && npm publish",
|
|
23
|
+
"test": "npm run build && uvu"
|
|
21
24
|
},
|
|
22
25
|
"repository": "cristianfalcone/ajo",
|
|
23
26
|
"author": "Cristian Falcone",
|
package/index.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
export const
|
|
2
|
-
Fragment = ({ children }) => children,
|
|
3
|
-
|
|
4
|
-
h = (nodeName, props, ...children) => {
|
|
5
|
-
children = children.length == 0 ? null : children.length == 1 ? children[0] : children
|
|
6
|
-
return { children, ...props, nodeName }
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
component = setup => ({ is, key, host, ref, ...props }) => h(is ?? setup.is ?? 'div', {
|
|
10
|
-
key, ...setup.host, ...host, skip: true, ref: host => (refresh(host, props, setup), isFn(ref) && ref(host))
|
|
11
|
-
}),
|
|
12
|
-
|
|
13
|
-
render = (h, host) => {
|
|
14
|
-
|
|
15
|
-
let child = host.firstChild, node, byKey = keyed.get(host)
|
|
16
|
-
|
|
17
|
-
for (h of normalize(h, host)) {
|
|
18
|
-
|
|
19
|
-
if (typeof h == 'string') {
|
|
20
|
-
|
|
21
|
-
for (node = child; node; node = node.nextSibling) if (node.nodeType == 3) break
|
|
22
|
-
node ? node.data !== h && (node.data = h) : node = document.createTextNode(h)
|
|
23
|
-
|
|
24
|
-
} else if (h instanceof Node) {
|
|
25
|
-
|
|
26
|
-
node = h
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
|
|
30
|
-
const { key, nodeName, skip, block, children, ref, ...props } = h
|
|
31
|
-
|
|
32
|
-
if (key != null && (node = byKey?.get(key)));
|
|
33
|
-
else for (node = child; node; node = node.nextSibling) if (node.localName == nodeName) break
|
|
34
|
-
|
|
35
|
-
node ||= document.createElement(nodeName)
|
|
36
|
-
|
|
37
|
-
key != null && (byKey ||= keyed.set(host, new Map)).set(key, node)
|
|
38
|
-
|
|
39
|
-
update(props, node)
|
|
40
|
-
|
|
41
|
-
!(skip || block != null && every(deps.get(node), deps.set(node, block))) && render(children, node)
|
|
42
|
-
|
|
43
|
-
isFn(ref) && ref(node)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
node === child ? child = child.nextSibling : before(host, child, node)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
while (child) {
|
|
50
|
-
const next = child.nextSibling
|
|
51
|
-
host.removeChild(child).nodeType == 1 && dispose(child)
|
|
52
|
-
child = next
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
refresh = (host, props, setup) => {
|
|
57
|
-
try {
|
|
58
|
-
if (setup && !isFn(setup)) throwTypeError('Setup', setup, fn)
|
|
59
|
-
props = props ? memo.set(host, { ...setup?.props, ...props }) : memo.get(host) ?? {}
|
|
60
|
-
render((renders.get(host) ?? renders.set(host, setup(props, host)))(props, host), host)
|
|
61
|
-
} catch (error) {
|
|
62
|
-
propagate(host, error)
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
provide = (host, key, value) => (provisions.get(host) ?? provisions.set(host, new Map)).set(key, value),
|
|
67
|
-
|
|
68
|
-
consume = (host, key, fallback) => {
|
|
69
|
-
let map
|
|
70
|
-
while (host) {
|
|
71
|
-
if ((map = provisions.get(host)) && map.has(key)) return map.get(key)
|
|
72
|
-
host = host.parentNode
|
|
73
|
-
}
|
|
74
|
-
return fallback
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
intercept = (host, interceptor) => {
|
|
78
|
-
if (!isFn(interceptor)) throwTypeError('Interceptor', interceptor, fn)
|
|
79
|
-
interceptors.set(host, interceptor)
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
propagate = (host, error) => {
|
|
83
|
-
for (let interceptor; host; host = host.parentNode)
|
|
84
|
-
if (interceptor = interceptors.get(host)) return render(host, interceptor(error))
|
|
85
|
-
throw error
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
cleanup = (host, cleaner) => {
|
|
89
|
-
if (!isFn(cleaner)) throwTypeError('Cleaner', cleaner, fn);
|
|
90
|
-
(cleaners.get(host) ?? cleaners.set(host, new Set)).add(cleaner)
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
clx = o => keys(o).filter(k => o[k]).join(' ') || null,
|
|
94
|
-
stx = o => entries(o).map(t => t.join(':')).join(';') || null,
|
|
95
|
-
keb = o => keys(o).reduce((r, k) => ((r[k.replace(search, replace).toLowerCase()] = o[k]), r), {})
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
wm = () => {
|
|
99
|
-
const instance = new WeakMap, { set } = instance
|
|
100
|
-
instance.set = (key, value) => (set.call(instance, key, value), value)
|
|
101
|
-
return instance
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
throwTypeError = (name, value, expected) => {
|
|
105
|
-
throw new TypeError(`Expected ${name} to be of type ${expected}, got ${typeof value} instead`)
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
every = (a, b) => a === b || isArray(a) && isArray(b) && a.length == b.length && a.every((v, i) => v === b[i]),
|
|
109
|
-
apply = (o, { name, value }) => ((o[name] = value), o),
|
|
110
|
-
reduce = list => from(list).reduce(apply, {}),
|
|
111
|
-
isFn = v => typeof v == fn,
|
|
112
|
-
|
|
113
|
-
{ keys, entries } = Object, { isArray, from } = Array,
|
|
114
|
-
|
|
115
|
-
fn = 'function', search = /([a-z0-9])([A-Z])/g, replace = '$1-$2',
|
|
116
|
-
|
|
117
|
-
keyed = wm(), deps = wm(), memo = wm(), renders = wm(), provisions = wm(), interceptors = wm(), cleaners = wm(), cache = wm(),
|
|
118
|
-
|
|
119
|
-
normalize = function* (h, host) {
|
|
120
|
-
|
|
121
|
-
let type, buffer = ''
|
|
122
|
-
|
|
123
|
-
for (h of isFn(h?.[Symbol.iterator]) ? h : [h]) {
|
|
124
|
-
|
|
125
|
-
if (h == null || (type = typeof h) == 'boolean') continue
|
|
126
|
-
|
|
127
|
-
if (type == 'string' || type == 'number') {
|
|
128
|
-
buffer += h
|
|
129
|
-
continue
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if ('nodeName' in Object(h)) {
|
|
133
|
-
|
|
134
|
-
if (isFn(h.nodeName)) {
|
|
135
|
-
yield* normalize(h.nodeName(h, host), host)
|
|
136
|
-
continue
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (buffer) {
|
|
140
|
-
yield buffer
|
|
141
|
-
buffer = ''
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
yield h
|
|
145
|
-
continue
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
isFn(h[Symbol.iterator]) ? yield* normalize(h, host) : buffer += h
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (buffer) yield buffer
|
|
152
|
-
},
|
|
153
|
-
|
|
154
|
-
update = (props, host) => {
|
|
155
|
-
|
|
156
|
-
const prev = cache.get(host) ?? (host.hasAttributes() ? reduce(host.attributes) : {})
|
|
157
|
-
|
|
158
|
-
for (const name in { ...prev, ...props }) {
|
|
159
|
-
|
|
160
|
-
let value = props[name]
|
|
161
|
-
|
|
162
|
-
if (value === prev[name]) continue
|
|
163
|
-
|
|
164
|
-
if (name.startsWith('set:')) {
|
|
165
|
-
host[name.slice(4)] = value
|
|
166
|
-
continue
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (value === true) value = ''
|
|
170
|
-
else if (value == null || value === false) {
|
|
171
|
-
host.removeAttribute(name)
|
|
172
|
-
continue
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
host.setAttribute(name, value)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
cache.set(host, props)
|
|
179
|
-
},
|
|
180
|
-
|
|
181
|
-
before = (host, child, node) => {
|
|
182
|
-
if (node.contains?.(document.activeElement)) {
|
|
183
|
-
|
|
184
|
-
const ref = node.nextSibling
|
|
185
|
-
|
|
186
|
-
while (child && child !== node) {
|
|
187
|
-
const next = child.nextSibling
|
|
188
|
-
host.insertBefore(child, ref)
|
|
189
|
-
child = next
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
} else host.insertBefore(node, child)
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
dispose = host => {
|
|
196
|
-
for (const child of host.children) dispose(child)
|
|
197
|
-
for (const cleaner of cleaners.get(host) ?? []) cleaner(host)
|
|
198
|
-
}
|
package/index.test.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import 'backdom/register'
|
|
2
|
-
import { suite } from 'uvu'
|
|
3
|
-
import * as assert from 'uvu/assert'
|
|
4
|
-
import { component, h, render } from './index.js'
|
|
5
|
-
|
|
6
|
-
let it
|
|
7
|
-
|
|
8
|
-
// ----------------------------------------------------------------------------
|
|
9
|
-
|
|
10
|
-
it = suite('h')
|
|
11
|
-
|
|
12
|
-
it('should create empty vnode', () => {
|
|
13
|
-
const vnode = h('div')
|
|
14
|
-
assert.equal(vnode, { nodeName: 'div', children: null })
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should create vnode with props', () => {
|
|
18
|
-
assert.equal(h('div', { id: 'app' }), { nodeName: 'div', children: null, id: 'app' })
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('should create vnode with one string child', () => {
|
|
22
|
-
assert.equal(h('div', null, 'foo'), { nodeName: 'div', children: 'foo' })
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should create vnode with one vnode child', () => {
|
|
26
|
-
const child = h('span')
|
|
27
|
-
const vnode = h('div', null, child)
|
|
28
|
-
assert.equal(vnode.children, { nodeName: 'span', children: null })
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it('should allow children as prop', () => {
|
|
32
|
-
const child = h('span')
|
|
33
|
-
const vnode = h('div', { children: child })
|
|
34
|
-
assert.equal(vnode.children, { nodeName: 'span', children: null })
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it.run()
|
|
38
|
-
|
|
39
|
-
// ----------------------------------------------------------------------------
|
|
40
|
-
|
|
41
|
-
it = suite('render')
|
|
42
|
-
|
|
43
|
-
it('should render a vnode', () => {
|
|
44
|
-
const host = document.createElement('div')
|
|
45
|
-
render(h('div', { foo: 'bar' }, 'foobar'), host)
|
|
46
|
-
assert.snapshot(host.innerHTML, '<div foo="bar">foobar</div>')
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('should render a stateless component', () => {
|
|
50
|
-
const host = document.createElement('div')
|
|
51
|
-
const Foo = ({ foo, children }) => h('div', { foo }, children)
|
|
52
|
-
render(h(Foo, { foo: 'bar' }, h('span', null, 'foobar')), host)
|
|
53
|
-
assert.snapshot(host.innerHTML, '<div foo="bar"><span>foobar</span></div>')
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('should render a stateful component', () => {
|
|
57
|
-
const host = document.createElement('div')
|
|
58
|
-
const Foo = component(() => ({ foo, children }) => h('div', { foo }, children))
|
|
59
|
-
render(h(Foo, { is: 'foo-component', foo: 'bar' }, h('span', null, 'foobar')), host)
|
|
60
|
-
assert.snapshot(host.innerHTML, '<foo-component><div foo="bar"><span>foobar</span></div></foo-component>')
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it.run()
|