@tinyvolt/pie 0.6.0 → 0.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyvolt/pie",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A Markdown-based SSG for interactive science explainers. Create a site with: npx @tinyvolt/pie init my-site",
5
5
  "type": "module",
6
6
  "repository": {
@@ -40,11 +40,17 @@ function argType(arg: Arg, store: NodeStore): string {
40
40
  }
41
41
 
42
42
  /** The item type and shape of an argument that is an array, before any node is created for it. */
43
- function arrayOf(arg: Arg, store: NodeStore): { itemType: NodeType; shape: number[] } {
43
+ function arrayOf(arg: Arg, store: NodeStore): { itemType: NodeType | 'Mixed'; shape: number[] } {
44
44
  if (Array.isArray(arg)) return { itemType: listItemType(arg, item => argType(item, store)), shape: listShape(arg) };
45
45
  return store.getNodeValue((arg as { id: string }).id) as ArrayNode;
46
46
  }
47
47
 
48
+ /** The types of the items of an argument that is an array, without repeats. */
49
+ function itemTypesOf(arg: Arg, store: NodeStore): string[] {
50
+ if (Array.isArray(arg)) return [...new Set(leaves(arg).map(item => argType(item, store)))];
51
+ return [...new Set((store.getNodeValue((arg as { id: string }).id) as ArrayNode).items.map(item => item.type))];
52
+ }
53
+
48
54
  /**
49
55
  * A list gets one hidden Array node, reused by every call that passes the same list.
50
56
  * Its items follow the nodes they name.
@@ -130,8 +136,10 @@ export async function execute(store: NodeStore, keyword: string, args: Arg[], {
130
136
  if (func.broadcastable === false) throw new Error(`${keyword} does not take arrays`);
131
137
  const array = arrayOf(arg, store);
132
138
  broadcastInputShapes.push(array.shape);
133
- got = `Array of ${array.itemType}`;
134
- if (types.includes(array.itemType)) return;
139
+ // A Mixed array passes when every one of its item types does.
140
+ const itemTypes = array.itemType === 'Mixed' ? itemTypesOf(arg, store) : [array.itemType];
141
+ got = `Array of ${itemTypes.join(', ')}`;
142
+ if (itemTypes.every(type => types.includes(type as NodeType))) return;
135
143
  }
136
144
  if (!types.includes(got as NodeType)) {
137
145
  throw new Error(`${keyword}: expected ${types.join(' or ')} but got ${got} for argument '${param.name}'`);
@@ -141,7 +141,8 @@ const PointGradDefinition: NodeDefinition<PointGradNode> = { type: 'PointGrad',
141
141
  /** `items` in row-major order: shape [2, 3] holds row 0's three items, then row 1's. */
142
142
  export interface ArrayNode extends Node {
143
143
  type: 'Array';
144
- itemType: NodeType;
144
+ /** 'Mixed' when the items are not all one type. */
145
+ itemType: NodeType | 'Mixed';
145
146
  shape: number[];
146
147
  items: AnyNode[];
147
148
  }
@@ -31,12 +31,10 @@ export function getItem(store: NodeStore, arrayId: string, position: number): Ex
31
31
  return { id: item.id, type: item.type };
32
32
  }
33
33
 
34
- /** The one type all items of a nested list share. `typeOf` gives an item's type. */
35
- export function listItemType(list: Arg[], typeOf: (item: LeafArg) => string): NodeType {
34
+ /** The one type all items of a nested list share, or 'Mixed' when they differ. `typeOf` gives an item's type. */
35
+ export function listItemType(list: Arg[], typeOf: (item: LeafArg) => string): NodeType | 'Mixed' {
36
36
  listShape(list);
37
37
  const types = new Set(leaves(list).map(typeOf));
38
- if (types.size > 1) {
39
- throw new Error(`all items of a list must have the same type, got ${[...types].join(', ')}`);
40
- }
38
+ if (types.size > 1) return 'Mixed';
41
39
  return [...types][0] as NodeType;
42
40
  }
@@ -61,6 +61,7 @@ function inputValue(node: AnyNode | undefined, id: string): InputValue {
61
61
  if (node.type === 'Point') return [num(node.x), num(node.y)];
62
62
  if (node.type === 'Text') return node.value;
63
63
  if (node.type === 'Array') {
64
+ if (node.itemType === 'Mixed') throw new Error(`'${id}' is an Array of mixed types; an Array input must hold one type`);
64
65
  const values = node.items.flatMap(item => inputValue(item, item.id)) as number[];
65
66
  return { shape: node.itemType === 'Point' ? [...node.shape, 2] : node.shape, values };
66
67
  }