mathjson-tree-builder 0.1.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.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The construct registry — the single source of
3
+ * truth for "how does construct X render and how many/which arguments
4
+ * does it take." Layers the generated construct list (kept in sync
5
+ * with mathjson-solver's own source, not hand-transcribed) with the small
6
+ * hand-authored `args` and `notation` overlays.
7
+ */
8
+ import type { ConstructDescriptor, MathJsonExpression } from "../types.js";
9
+ import { CATEGORY_LABELS } from "./constructs.generated.js";
10
+ import { isComparisonLike, operatorGlyph, swapNeedsArgReversal, swapTargets, type NotationSpec } from "./notation.js";
11
+ export { CATEGORY_LABELS, isComparisonLike, operatorGlyph, swapNeedsArgReversal, swapTargets };
12
+ export type { NotationSpec };
13
+ /** Every construct mathjson-solver implements, name + category only. */
14
+ export declare function allConstructs(): readonly ConstructDescriptor[];
15
+ /** Look up one construct's registry entry, if any. */
16
+ export declare function getConstruct(name: string): ConstructDescriptor | undefined;
17
+ /** The curated notation renderer for `name`, if it's in the small curated set. */
18
+ export declare function getNotation(name: string): NotationSpec | undefined;
19
+ export declare function categoryLabel(slug: string): string;
20
+ /**
21
+ * A human-readable name for construct `name` — e.g. "Division" for
22
+ * `Divide`. Falls back to the raw construct name when no curated entry
23
+ * exists (see `display-names.ts`).
24
+ */
25
+ export declare function displayName(name: string): string;
26
+ /**
27
+ * The minimum and maximum number of arguments construct `name` accepts, if
28
+ * it has curated `args` metadata to derive that from — `undefined` when it
29
+ * doesn't (the vast majority of the registry), in which case the generic
30
+ * renderer stays fully permissive rather than guess at an unverified
31
+ * arity ("prefer erring toward permissive" over guessing wrong). A trailing
32
+ * `variadic` entry means unbounded (`max: Infinity`, `min` counting that
33
+ * slot as needing at least one instance); otherwise `max` is the curated
34
+ * entry count and `min` subtracts however many are marked `optional`
35
+ * (assumed trailing, e.g. `Round`'s `digits`).
36
+ */
37
+ export declare function argBounds(name: string): {
38
+ min: number;
39
+ max: number;
40
+ } | undefined;
41
+ /**
42
+ * Search the palette by name/category, honoring the host's `blacklist` (mirrors
43
+ * mathjson-solver's own `create_solver(blacklist=...)`).
44
+ */
45
+ export declare function searchConstructs(query: string, blacklist?: readonly string[]): ConstructDescriptor[];
46
+ /**
47
+ * A reasonable, best-effort set of default arguments for freshly inserting
48
+ * construct `name` (via the function picker or the wrap-in-parent action).
49
+ * Notation shape takes priority where present, since those
50
+ * constructs have arity/shape requirements verified against
51
+ * mathjson-solver's own source (e.g. `Product` needs a single `Array` arg).
52
+ * Falls back to hand-authored `args` defaults, then to a single `0`.
53
+ */
54
+ export declare function defaultArgsFor(name: string): MathJsonExpression[];
@@ -0,0 +1 @@
1
+ export declare const styles: import("lit").CSSResult;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Core types for mathjson-tree-builder.
3
+ */
4
+ /**
5
+ * A MathJSON expression, restricted to the "array form" this editor
6
+ * operates on directly (no LaTeX, no boxed/canonical forms):
7
+ *
8
+ * - a number literal
9
+ * - a boolean literal
10
+ * - a string — either a literal string *value*, or a bare symbol
11
+ * reference (e.g. a named survey question). Disambiguating the two is
12
+ * the host application's job via the symbol resolver, not this
13
+ * editor's — see `resolveSymbol` in `data-model/symbols.ts`.
14
+ * - a compound expression `[constructName, ...args]`
15
+ */
16
+ export type MathJsonExpression = number | boolean | string | MathJsonExpression[];
17
+ /** True for a compound expression `[constructName, ...args]`. */
18
+ export declare function isCompound(expr: MathJsonExpression): expr is MathJsonExpression[];
19
+ /**
20
+ * Type-guard for arbitrary parsed JSON (e.g. from the raw-JSON escape
21
+ * hatch): true for a number, boolean, string, or an array recursively made
22
+ * of the same — false for `null`, a plain object, or `undefined`. This is
23
+ * deliberately just a shape check, not semantic validation (a compound
24
+ * expression's first element isn't required to be a known construct name)
25
+ * — the raw-JSON view's job is catching malformed JSON, not building a
26
+ * MathJSON linter.
27
+ */
28
+ export declare function isValidMathJson(value: unknown): value is MathJsonExpression;
29
+ /**
30
+ * A path from the root to a node: an array of indices. `[]` addresses the
31
+ * root itself. `[i]` addresses element `i` of the root (index `0` is the
32
+ * construct name for a compound root). Nested paths descend the same way.
33
+ */
34
+ export type Path = readonly number[];
35
+ /** Metadata for one argument of a construct, for nicer generic-mode labels. */
36
+ export interface ArgSpec {
37
+ label: string;
38
+ /** True on the last entry of a genuinely unbounded (variadic) construct. */
39
+ variadic?: boolean;
40
+ /**
41
+ * True for a trailing argument that may be omitted (e.g. `Round`'s
42
+ * `digits`) — only meaningful on the last entry/entries; arity bounds
43
+ * (`argBounds` in the registry) assume optional args are trailing.
44
+ */
45
+ optional?: boolean;
46
+ defaultValue?: MathJsonExpression;
47
+ }
48
+ /**
49
+ * Registry entry for one MathJSON construct. `args` and `notation` are
50
+ * optional, best-effort metadata — a construct with neither still renders
51
+ * correctly through the fully generic fallback.
52
+ */
53
+ export interface ConstructDescriptor {
54
+ name: string;
55
+ category: string;
56
+ args?: ArgSpec[];
57
+ }
58
+ /**
59
+ * A host-supplied named reference the user can insert into an expression
60
+ * (e.g. a survey question, a spreadsheet cell, a form field) — see
61
+ * `resolveSymbol` in `data-model/symbols.ts`.
62
+ * Inserted verbatim as a bare MathJSON string (`id`).
63
+ */
64
+ export interface SymbolReference {
65
+ id: string;
66
+ label: string;
67
+ type?: string;
68
+ group?: string;
69
+ }
70
+ /** Result of an optional host-wired evaluator. */
71
+ export type EvalResult = {
72
+ result: MathJsonExpression;
73
+ } | {
74
+ error: string;
75
+ path?: Path;
76
+ };
77
+ export type Evaluator = (expr: MathJsonExpression, parameters: Record<string, MathJsonExpression>) => Promise<EvalResult>;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "mathjson-tree-builder",
3
+ "version": "0.1.0",
4
+ "description": "A framework-agnostic Custom Element for visually building MathJSON expressions, tuned for the constructs mathjson-solver implements.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Mārtiņš Mednis",
8
+ "homepage": "https://mrtmednis.gitlab.io/mathjson-tree-builder/",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git@gitlab.com:mrtmednis/mathjson-tree-builder.git"
12
+ },
13
+ "bugs": "https://gitlab.com/mrtmednis/mathjson-tree-builder/-/issues",
14
+ "keywords": [
15
+ "mathjson",
16
+ "custom-element",
17
+ "web-component",
18
+ "formula-builder",
19
+ "mathjson-solver"
20
+ ],
21
+ "main": "./dist/mathjson-tree-builder.js",
22
+ "module": "./dist/mathjson-tree-builder.js",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/mathjson-tree-builder.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "dev": "vite",
35
+ "build": "vite build && tsc -p tsconfig.build.json",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest",
38
+ "typecheck": "tsc --noEmit",
39
+ "registry:extract": "python3 scripts/extract-registry.py",
40
+ "prepublishOnly": "npm run typecheck && npm test && npm run build"
41
+ },
42
+ "devDependencies": {
43
+ "happy-dom": "^20.14.5",
44
+ "lit": "^3.2.1",
45
+ "typescript": "^5.6.3",
46
+ "vite": "^8.3.0",
47
+ "vitest": "^5.0.0"
48
+ }
49
+ }