@chronocide/hyper 0.5.0 → 0.6.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/README.md CHANGED
@@ -41,25 +41,25 @@ document.body.appendChild(img);
41
41
 
42
42
  ### List
43
43
 
44
- List items can be cached using `list`, only updated if the data is changed; order does not matter. Data does not need to be unique, as duplicate nodes are cloned.
45
-
46
- **Note**, `list` only supports `string` and `number` type.
44
+ Keeping track of children within a list can be tedious, especially if using immutable data. `list` caches elements based on keys and compares each data entry, only updating the child i the data has changed. Keys do not need to be unique, duplicate elements are cloned.
47
45
 
48
46
  ```ts
49
47
  import h, { list } from '@chronocide/hyper';
50
48
 
51
49
  type Planet = { id: string; name: string };
52
50
 
53
- const planets = new Map<string, Planet>();
54
- planets.add('jupiter', { id: 'jupiter', name: 'Jupiter' });
55
- planets.add('mars', { id: 'mars', name: 'Mars' });
56
- planets.add('pluto', { id: 'pluto', name: 'Pluto' });
57
-
58
51
  const ul = h('ul')()(); // <ul></ul>
59
- const render = (id: string) => h('li')()(planets.get(id)?.name ?? '-');
52
+ const render = (planet: Planet) => h('li')({ id: planet.id })(planet.name);
53
+ const key = (planet: Planet) => planet.id;
54
+ const update = list<Planet>(render)(key)(ul);
55
+
56
+ const planets: Planet[] = [
57
+ { id: 'jupiter', name: 'Jupiter' },
58
+ { id: 'mars', name: 'Mars' },
59
+ { id: 'pluto', name: 'Pluto' }
60
+ ];
60
61
 
61
- const update = list(render)(ul);
62
- update(planets); // <ul><li>Jupiter</li><li>Mars</li><li>Pluto</li></ul>
62
+ update(planets); // <ul><li id="jupiter">Jupiter</li><li id="mars">Mars</li><li id="pluto">Pluto</li></ul>
63
63
  ```
64
64
 
65
65
  ## Testing
package/dist/hyper.d.ts CHANGED
@@ -15,7 +15,7 @@ declare const _default: <T extends keyof HTMLElementTagNameMap>(tag: T) => <P ex
15
15
  declare const svg: <T extends keyof SVGElementTagNameMap>(tag: T) => <P extends Attributes>(attributes?: P | undefined) => (...children: Child[]) => SVGElementTagNameMap[T];
16
16
  declare const mathml: <T extends keyof MathMLElementEventMap>(tag: T) => <P extends Attributes>(attributes?: P | undefined) => (...children: Child[]) => MathMLElement;
17
17
  declare const xml: (tag: string) => <P extends Attributes>(attributes?: P) => (...children: Child[]) => HTMLElement;
18
- declare const list: <T extends string | number>(render: (data: T, i: number) => Element) => (root: Element) => (next: T[]) => void;
18
+ declare const list: <T>(render: (x: T, i: number, arr: T[]) => Element) => (key: (x: T) => string) => (root: Element) => (next: T[]) => void;
19
19
 
20
20
  export { _default as default, env, list, mathml, svg, xml };
21
21
  export type { Attributes, Child, HTMLVoidElementTagName };
package/dist/hyper.js CHANGED
@@ -17,18 +17,19 @@ const html = (document) => (tag) => (attributes) => (...children) => create(docu
17
17
  const svg$1 = (document) => (tag) => (attributes) => (...children) => create(document.createElementNS("http://www.w3.org/2000/svg", tag))(attributes)(children);
18
18
  const mathml$1 = (document) => (tag) => (attributes) => (...children) => create(document.createElementNS("http://www.w3.org/1998/Math/MathML", tag))(attributes)(children);
19
19
  const xml$1 = (document) => (tag) => (attributes) => (...children) => create(document.createElementNS("http://www.w3.org/1999/xhtml", tag))(attributes)(children);
20
- const list$1 = (render) => (root) => {
20
+ const list$1 = (render) => (key) => (root) => {
21
21
  const cache = /* @__PURE__ */ new Map();
22
22
  return (next) => {
23
23
  const refs = /* @__PURE__ */ new WeakSet();
24
24
  while (root.children.length > next.length) root.lastChild?.remove();
25
- next.forEach((data, i) => {
25
+ next.forEach((x, i) => {
26
+ const k = key(x);
26
27
  const child = root.children.item(i);
27
- let element = cache.get(data);
28
+ let element = cache.get(k);
28
29
  if (element === child) return;
29
30
  if (!element) {
30
- element = render(data, i);
31
- cache.set(data, element);
31
+ element = render(x, i, next);
32
+ cache.set(k, element);
32
33
  }
33
34
  if (refs.has(element)) {
34
35
  element = element.cloneNode(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chronocide/hyper",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "types": "dist/hyper.d.ts",
6
6
  "exports": {