@chronocide/hyper 0.3.2 → 0.5.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
@@ -43,23 +43,22 @@ document.body.appendChild(img);
43
43
 
44
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
45
 
46
- ```ts
47
- import type { Component } from '@chronocide/hyper';
46
+ **Note**, `list` only supports `string` and `number` type.
48
47
 
48
+ ```ts
49
49
  import h, { list } from '@chronocide/hyper';
50
50
 
51
51
  type Planet = { id: string; name: string };
52
52
 
53
- const planets: Planet[] = [
54
- { id: 'jupiter', name: 'Jupiter' },
55
- { id: 'mars', name: 'Mars' },
56
- { id: 'pluto', name: 'Pluto' }
57
- ];
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' });
58
57
 
59
58
  const ul = h('ul')()(); // <ul></ul>
60
- const component: Component<Planet> = planet => h('li')()(planet.name);
59
+ const render = (id: string) => h('li')()(planets.get(id)?.name ?? '-');
61
60
 
62
- const update = list<Planet>(component)(ul);
61
+ const update = list(render)(ul);
63
62
  update(planets); // <ul><li>Jupiter</li><li>Mars</li><li>Pluto</li></ul>
64
63
  ```
65
64
 
package/dist/hyper.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  type Attributes = Record<string, unknown>;
2
2
  type Child = Node | string;
3
3
  type HTMLVoidElementTagName = 'area' | 'base' | 'br' | 'col' | 'embed' | 'hr' | 'img' | 'input' | 'link' | 'meta' | 'source' | 'track' | 'wbr';
4
- type Component<T> = (data: T) => Element;
5
4
 
6
5
  declare class Env {
7
6
  private _document;
@@ -16,7 +15,7 @@ declare const _default: <T extends keyof HTMLElementTagNameMap>(tag: T) => <P ex
16
15
  declare const svg: <T extends keyof SVGElementTagNameMap>(tag: T) => <P extends Attributes>(attributes?: P | undefined) => (...children: Child[]) => SVGElementTagNameMap[T];
17
16
  declare const mathml: <T extends keyof MathMLElementEventMap>(tag: T) => <P extends Attributes>(attributes?: P | undefined) => (...children: Child[]) => MathMLElement;
18
17
  declare const xml: (tag: string) => <P extends Attributes>(attributes?: P) => (...children: Child[]) => HTMLElement;
19
- declare const list: <T>(component: Component<T>) => (root: Element) => (next: T[]) => void;
18
+ declare const list: <T extends string | number>(render: (data: T, i: number) => Element) => (root: Element) => (next: T[]) => void;
20
19
 
21
20
  export { _default as default, env, list, mathml, svg, xml };
22
- export type { Attributes, Child, Component, HTMLVoidElementTagName };
21
+ export type { Attributes, Child, HTMLVoidElementTagName };
package/dist/hyper.js CHANGED
@@ -17,7 +17,7 @@ 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 = (component) => (root) => {
20
+ const list$1 = (render) => (root) => {
21
21
  const cache = /* @__PURE__ */ new Map();
22
22
  return (next) => {
23
23
  const refs = /* @__PURE__ */ new WeakSet();
@@ -27,7 +27,7 @@ const list$1 = (component) => (root) => {
27
27
  let element = cache.get(data);
28
28
  if (element === child) return;
29
29
  if (!element) {
30
- element = component(data);
30
+ element = render(data, i);
31
31
  cache.set(data, element);
32
32
  }
33
33
  if (refs.has(element)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chronocide/hyper",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "types": "dist/hyper.d.ts",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "license": "AGPL-3.0-only",
18
18
  "scripts": {
19
19
  "env": "node scripts/env.ts",
20
- "test": "node --env-file=.env --test src/**/*.spec.ts",
20
+ "test": "node --env-file=.env --test src/**/*.spec.ts && tsc --noEmit",
21
21
  "build": "rollup -c",
22
22
  "preversion": "npm run test",
23
23
  "version": "npm run build && git add -A dist",